diff options
| author | Ujp8LfXBJ6wCPR <github@lillecarl.com> | 2020-02-16 13:22:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-16 13:22:36 +0100 |
| commit | b089ed3b7578029ec1d35537372a2b8b923c7ec6 (patch) | |
| tree | c337971246d19250648c97cc5fbd15fafccb7f0c /src/common/Collision/BoundingIntervalHierarchy.h | |
| parent | 54fb7a25ed60f0c083f2c3efae45cbf853bed0d6 (diff) | |
Remove dependency on undefined behaviour (#24159)
See https://stackoverflow.com/a/4328396
Previous method works but is technically illegal.
Diffstat (limited to 'src/common/Collision/BoundingIntervalHierarchy.h')
| -rw-r--r-- | src/common/Collision/BoundingIntervalHierarchy.h | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/common/Collision/BoundingIntervalHierarchy.h b/src/common/Collision/BoundingIntervalHierarchy.h index 75a21c8d833..382d8abdf75 100644 --- a/src/common/Collision/BoundingIntervalHierarchy.h +++ b/src/common/Collision/BoundingIntervalHierarchy.h @@ -29,29 +29,26 @@ #include <algorithm> #include <limits> #include <cmath> +#include "string.h" #define MAX_STACK_SIZE 64 +// https://stackoverflow.com/a/4328396 + static inline uint32 floatToRawIntBits(float f) { - union - { - uint32 ival; - float fval; - } temp; - temp.fval=f; - return temp.ival; + static_assert(sizeof(float) == sizeof(uint32), "Size of uint32 and float must be equal for this to work"); + uint32 ret; + memcpy(&ret, &f, sizeof(float)); + return ret; } static inline float intBitsToFloat(uint32 i) { - union - { - uint32 ival; - float fval; - } temp; - temp.ival=i; - return temp.fval; + static_assert(sizeof(float) == sizeof(uint32), "Size of uint32 and float must be equal for this to work"); + float ret; + memcpy(&ret, &i, sizeof(uint32)); + return ret; } struct AABound |
