]> gitweb.factorcode.org Git - factor.git/commitdiff
float_bits: cleaner way to cast between bits and float/double
authorDoug Coleman <doug.coleman@gmail.com>
Sun, 13 Feb 2022 17:59:57 +0000 (11:59 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 13 Feb 2022 18:13:01 +0000 (12:13 -0600)
vm/float_bits.hpp

index 3a2e1474c4db98ec22e922f6c8f6775129dd4422..2db5b70fbadf08e75fd0c178769cd10403221d0a 100644 (file)
@@ -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<uint64_t*>(&x));
 }
 
 inline static double bits_double(uint64_t y) {
-  double_bits_pun b;
-  b.y = y;
-  return b.x;
+  return *(reinterpret_cast<double*>(&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<uint32_t*>(&x));
 }
 
 inline static float bits_float(uint32_t y) {
-  float_bits_pun b;
-  b.y = y;
-  return b.x;
+  return *(reinterpret_cast<float*>(&y));
 }
 
 }