From d1a1c0a011a16fbb65461b732a3a2f8655ef5cec Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 27 Dec 2017 22:53:47 -0500 Subject: [PATCH] picasso_frontend: Get rid of undefined behavior Type-punning via a union is well-defined in C (specifically C99 and onwards), but not C++ --- source/picasso_frontend.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/source/picasso_frontend.cpp b/source/picasso_frontend.cpp index 490db1e..0cdedd3 100644 --- a/source/picasso_frontend.cpp +++ b/source/picasso_frontend.cpp @@ -1,19 +1,13 @@ #include "picasso.h" -static inline uint32_t floatrawbits(float f) -{ - union { float f; uint32_t i; } s; - s.f = f; - return s.i; -} - // f24 has: // - 1 sign bit // - 7 exponent bits // - 16 mantissa bits uint32_t f32tof24(float f) { - uint32_t i = floatrawbits(f); + uint32_t i; + std::memcpy(&i, &f, sizeof(f)); uint32_t mantissa = (i << 9) >> 9; int32_t exponent = (i << 1) >> 24;