From 63a48c7d871668c968b0558dd7cdf114a7d2f81a Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sat, 27 Jul 2019 16:02:50 +0100 Subject: [PATCH] add testing functions to FPFormat --- src/ieee754/fclass/pipeline.py | 2 +- src/ieee754/fpcommon/fpbase.py | 149 ++++++++++++++++++ src/ieee754/fpcommon/test/unit_test_double.py | 8 +- 3 files changed, 154 insertions(+), 5 deletions(-) diff --git a/src/ieee754/fclass/pipeline.py b/src/ieee754/fclass/pipeline.py index 6b489b28..89cdcd39 100644 --- a/src/ieee754/fclass/pipeline.py +++ b/src/ieee754/fclass/pipeline.py @@ -93,7 +93,7 @@ class FPClassMod(Elaboratable): m.d.comb += self.o.z.eq(1<<9) # | a quiet NaN # subnormal - with m.Elif(a1.exp_n126): + with m.Elif(a1.exp_n126 & ~a1.m_zero): with m.If(a1.s): m.d.comb += self.o.z.eq(1<<2) # | a negative subnormal number. with m.Else(): diff --git a/src/ieee754/fpcommon/fpbase.py b/src/ieee754/fpcommon/fpbase.py index 5f7df717..2413a365 100644 --- a/src/ieee754/fpcommon/fpbase.py +++ b/src/ieee754/fpcommon/fpbase.py @@ -9,6 +9,7 @@ from functools import reduce from nmutil.singlepipe import PrevControl, NextControl from nmutil.pipeline import ObjectProxy +import unittest import math @@ -81,16 +82,78 @@ class FPFormat: retval += f", {self.has_sign}" return retval + ")" + def get_sign(self, x): + """ returns the sign of its input number, x (assumes number is signed) + """ + return x >> (self.e_width + self.m_width) + + def get_exponent(self, x): + """ returns the exponent of its input number, x + """ + x = ((x >> self.m_width) & self.exponent_inf_nan) + return x - self.exponent_bias + + def get_mantissa(self, x): + """ returns the mantissa of its input number, x + """ + return x & self.mantissa_mask + + def is_zero(self, x): + """ returns true if x is subnormal (exp at minimum + """ + e_sub = self.exponent_denormal_zero - self.exponent_bias + print ("e_sub", e_sub) + return self.get_exponent(x) == e_sub and self.get_mantissa(x) == 0 + + def is_subnormal(self, x): + """ returns true if x is subnormal (exp at minimum + """ + e_sub = self.exponent_denormal_zero - self.exponent_bias + print ("e_sub", e_sub) + return self.get_exponent(x) == e_sub and self.get_mantissa(x) != 0 + + def is_inf(self, x): + """ returns true if x is infinite + """ + return (self.get_exponent(x) == self.emax and + self.get_mantissa(x) == 0) + + def is_nan(self, x): + """ returns true if x is nan + """ + highbit = 1<> 52) - 1023 + return ((x & 0x7ff0000000000000) >> 52) - (max_e-1) def set_exponent(x, e): - return (x & ~0x7ff0000000000000) | ((e+1023) << 52) + return (x & ~0x7ff0000000000000) | ((e+(max_e-1)) << 52) def get_sign(x): return ((x & 0x8000000000000000) >> 63) def is_nan(x): - return get_exponent(x) == 1024 and get_mantissa(x) != 0 + return get_exponent(x) == max_e and get_mantissa(x) != 0 def is_inf(x): - return get_exponent(x) == 1024 and get_mantissa(x) == 0 + return get_exponent(x) == max_e and get_mantissa(x) == 0 def is_pos_inf(x): return is_inf(x) and not get_sign(x) -- 2.30.2