From: Jacob Lifshay Date: Mon, 4 Jul 2022 10:42:27 +0000 (-0700) Subject: add FPFormat.get_exponent_value to get an unbiased exponent corrected for subnormals X-Git-Url: https://git.libre-soc.org/?p=ieee754fpu.git;a=commitdiff_plain;h=da903f9bc46de51cae1f4a8fd9e9ea2d83bd572a add FPFormat.get_exponent_value to get an unbiased exponent corrected for subnormals --- diff --git a/src/ieee754/fpcommon/fpbase.py b/src/ieee754/fpcommon/fpbase.py index 9f537e02..84768edc 100644 --- a/src/ieee754/fpcommon/fpbase.py +++ b/src/ieee754/fpcommon/fpbase.py @@ -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 """