From d2a0c957b68ef2e3f033be057e11db37d400dbde Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 13 Feb 2022 14:56:04 -0600 Subject: [PATCH] Revert "float_bits: cleaner way to cast between bits and float/double" This reverts commit e030e1a62fbf273e353af1f15866f81e2db1284f. Works but has warnings and is undefined behavior. Also doesn't fix the issue with NAN: 123 setting the qnan bit. Can safely use `bit_cast` with C++20 someday. --- vm/float_bits.hpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/vm/float_bits.hpp b/vm/float_bits.hpp index 2db5b70fba..3a2e1474c4 100644 --- a/vm/float_bits.hpp +++ b/vm/float_bits.hpp @@ -1,19 +1,40 @@ namespace factor { +// Some functions for converting floating point numbers to binary +// representations and vice versa + +union double_bits_pun { + double x; + uint64_t y; +}; + inline static uint64_t double_bits(double x) { - return *(reinterpret_cast(&x)); + double_bits_pun b; + b.x = x; + return b.y; } inline static double bits_double(uint64_t y) { - return *(reinterpret_cast(&y)); + double_bits_pun b; + b.y = y; + return b.x; } +union float_bits_pun { + float x; + uint32_t y; +}; + inline static uint32_t float_bits(float x) { - return *(reinterpret_cast(&x)); + float_bits_pun b; + b.x = x; + return b.y; } inline static float bits_float(uint32_t y) { - return *(reinterpret_cast(&y)); + float_bits_pun b; + b.y = y; + return b.x; } } -- 2.34.1