add very quick float convert to SelectableInt
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 14 May 2021 17:28:31 +0000 (18:28 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 14 May 2021 17:28:31 +0000 (18:28 +0100)
src/openpower/decoder/selectable_int.py

index 1481f289d9958a00f6dc6e9cfb9c0bd324663c87..c997acd520d796b00feed91a2db0314dec9494ee 100644 (file)
@@ -1,4 +1,5 @@
 import unittest
+import struct
 from copy import copy
 from openpower.decoder.power_fields import BitRange
 from operator import (add, sub, mul, floordiv, truediv, mod, or_, and_, xor,
@@ -416,6 +417,14 @@ class SelectableInt:
     def asint(self):
         return self.value
 
+    def __float__(self):
+        """convert to double-precision float.  TODO, properly convert
+        rather than a hack-job: must actually support Power IEEE754 FP
+        """
+        assert self.bits == 64 # must be 64-bit
+        data = self.value.to_bytes(8, byteorder='little')
+        return struct.unpack('<d', data)[0]
+
 
 def onebit(bit):
     return SelectableInt(1 if bit else 0, 1)
@@ -561,6 +570,18 @@ class SelectableIntTestCase(unittest.TestCase):
         result = a + b
         self.assertTrue(result.value == 0xffffffffffffffff)
 
+    def test_double_1(self):
+        """use http://weitz.de/ieee/,
+        """
+        for asint, asfloat in [(0x4000000000000000, 2.0),
+                               (0x4056C00000000000, 91.0),
+                               (0xff80000000000000, -1.4044477616111843e+306),
+                              ]:
+            a = SelectableInt(asint, bits=64)
+            convert = float(a)
+            print ("test_double_1", asint, asfloat, convert)
+            self.assertTrue(asfloat == convert)
+
 
 if __name__ == "__main__":
     unittest.main()