Rewrite CompactValue to avoid undefined behavior from the use of a union for type-punning #1154

Closed
htpiv wants to merge 4 commits from htpiv/use-bit-cast into main

4 Commits

Author SHA1 Message Date
Harold Pratt (HAL)
68b780fa37 Two small fixes:
1. memcpy is declared in <cstring>; make sure to include it if needed
2. asFloat's __cpp_lib_bit_cast was using the wrong name for its
   parameter (the param was renamed after testing and I never rebuilt)
   with `__cpp_lib_bit_cast` on. Oops :-(
2022-07-25 10:34:20 -07:00
Harold Pratt (HAL)
047d159996 Merge branch 'main' into htpiv/use-bit-cast 2022-07-25 10:26:31 -07:00
Harold Pratt
2292c73091 Merge branch 'facebook:main' into htpiv/use-bit-cast 2022-07-18 16:31:28 -07:00
Harold Pratt
223e11a52e Rewrite CompactValue to avoid undefined C++ behavior.
C++ does not, pedantically, allow the use of unions for type-punning in the way that C does. Most compilers, in practice, do support it; however, recent versions of MSVC appear to have a bug that cause bad code to be generated due to this U.B. (see: https://developercommunity.visualstudio.com/t/Bad-code-generated-for-std::isnan-compil/10082631). This led to a series of issues in the react-native-windows project, see:
* https://github.com/microsoft/react-native-windows/issues/4122
* https://github.com/microsoft/react-native-windows/issues/8675

In C++20, the `<bit>` header and `bit_cast` function provide a pleasant API for type-punning. Since C++20 is not universally available, if the feature-test macro for `bit_cast` is not defined, memcpy is used instead.
2022-07-18 16:25:35 -07:00