From e030e1a62fbf273e353af1f15866f81e2db1284f Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 13 Feb 2022 11:59:57 -0600 Subject: [PATCH] float_bits: cleaner way to cast between bits and float/double --- vm/float_bits.hpp | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/vm/float_bits.hpp b/vm/float_bits.hpp index 3a2e1474c4..2db5b70fba 100644 --- a/vm/float_bits.hpp +++ b/vm/float_bits.hpp @@ -1,40 +1,19 @@ 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) { - double_bits_pun b; - b.x = x; - return b.y; + return *(reinterpret_cast(&x)); } inline static double bits_double(uint64_t y) { - double_bits_pun b; - b.y = y; - return b.x; + return *(reinterpret_cast(&y)); } -union float_bits_pun { - float x; - uint32_t y; -}; - inline static uint32_t float_bits(float x) { - float_bits_pun b; - b.x = x; - return b.y; + return *(reinterpret_cast(&x)); } inline static float bits_float(uint32_t y) { - float_bits_pun b; - b.y = y; - return b.x; + return *(reinterpret_cast(&y)); } } -- 2.34.1