diff options
author | Ujp8LfXBJ6wCPR <github@lillecarl.com> | 2020-02-16 13:22:36 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-12-21 16:36:07 +0100 |
commit | 6229a6ddc11be7409646a8ac99a5ecaf1b8d37d9 (patch) | |
tree | e4131d2349b67bd66b536ad6046738663512a3f2 /src/common/Collision/BoundingIntervalHierarchy.h | |
parent | d4db721b7f4c75766b7a51b43934aba0605c55b9 (diff) |
Remove dependency on undefined behaviour (#24159)
See https://stackoverflow.com/a/4328396
Previous method works but is technically illegal.
(cherry picked from commit b089ed3b7578029ec1d35537372a2b8b923c7ec6)
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 2f73967f09d..e15162dcd61 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 |