From 6229a6ddc11be7409646a8ac99a5ecaf1b8d37d9 Mon Sep 17 00:00:00 2001 From: Ujp8LfXBJ6wCPR Date: Sun, 16 Feb 2020 13:22:36 +0100 Subject: Remove dependency on undefined behaviour (#24159) See https://stackoverflow.com/a/4328396 Previous method works but is technically illegal. (cherry picked from commit b089ed3b7578029ec1d35537372a2b8b923c7ec6) --- src/common/Collision/BoundingIntervalHierarchy.h | 25 +++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src') 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 #include #include +#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 -- cgit v1.2.3