add FPFormat.get_exponent_value to get an unbiased exponent corrected for subnormals
authorJacob Lifshay <programmerjake@gmail.com>
Mon, 4 Jul 2022 10:42:27 +0000 (03:42 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Mon, 4 Jul 2022 10:42:27 +0000 (03:42 -0700)
src/ieee754/fpcommon/fpbase.py

index 9f537e024b0b1af8645d05e0d638364bfc9483d4..84768edc158e2a15323f2baef6984ca2f6f874e6 100644 (file)
@@ -320,6 +320,17 @@ class FPFormat:
             x |= Const(0, signed(1))
         return x - self.exponent_bias
 
+    def get_exponent_value(self, x):
+        """ returns the exponent of its input number, x, adjusted for the
+        mathematically correct subnormal exponent.
+        """
+        x = self.get_exponent_field(x)
+        if isinstance(x, Value) and not x.shape().signed:
+            # convert x to signed without changing its value,
+            # since exponents can be negative
+            x |= Const(0, signed(1))
+        return x + (x == self.exponent_denormal_zero) - self.exponent_bias
+
     def get_mantissa_field(self, x):
         """ returns the mantissa of its input number, x
         """