From: Jacob Lifshay Date: Mon, 4 Jul 2022 06:18:20 +0000 (-0700) Subject: add tests for non-power-of-2 shifts for MultiShift* classes X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3478f44084197401c8a17f7f01d5393ff86db52e;p=ieee754fpu.git add tests for non-power-of-2 shifts for MultiShift* classes --- diff --git a/src/ieee754/add/test_multishift.py b/src/ieee754/add/test_multishift.py index 86483453..1859b8a7 100644 --- a/src/ieee754/add/test_multishift.py +++ b/src/ieee754/add/test_multishift.py @@ -1,10 +1,11 @@ -from random import randint -from nmigen import Module, Signal -from nmigen.compat.sim import run_simulation - +from nmigen import Module, Signal, Elaboratable +from nmutil.formaltest import FHDLTestCase +from nmutil.sim_util import do_sim, hash_256 +import unittest from ieee754.fpcommon.fpbase import MultiShift, MultiShiftR, MultiShiftRMerge -class MultiShiftModL: + +class MultiShiftModL(Elaboratable): def __init__(self, width): self.ms = MultiShift(width) self.a = Signal(width) @@ -18,7 +19,8 @@ class MultiShiftModL: return m -class MultiShiftModR: + +class MultiShiftModR(Elaboratable): def __init__(self, width): self.ms = MultiShift(width) self.a = Signal(width) @@ -32,7 +34,8 @@ class MultiShiftModR: return m -class MultiShiftModRMod: + +class MultiShiftModRMod(Elaboratable): def __init__(self, width): self.ms = MultiShiftR(width) self.a = Signal(width) @@ -49,7 +52,8 @@ class MultiShiftModRMod: return m -class MultiShiftRMergeMod: + +class MultiShiftRMergeMod(Elaboratable): def __init__(self, width): self.ms = MultiShiftRMerge(width) self.a = Signal(width) @@ -67,68 +71,123 @@ class MultiShiftRMergeMod: return m -def check_case(dut, width, a, b): - yield dut.a.eq(a) - yield dut.b.eq(b) - yield +class TestMultiShift(FHDLTestCase): + def check_case(self, dut, width, a, b): + yield dut.a.eq(a) + yield dut.b.eq(b) + yield - x = (a << b) & ((1<> b) & ((1<> b) & ((1 << width) - 1) - out_x = yield dut.x - assert out_x == x, "Output x 0x%x not equal to expected 0x%x" % (out_x, x) + out_x = yield dut.x + self.assertEqual( + out_x, x, "Output x 0x%x not equal to expected 0x%x" % (out_x, x)) + def check_case_merge(self, dut, width, a, b): + yield dut.a.eq(a) + yield dut.b.eq(b) + yield -def check_case_merge(dut, width, a, b): - yield dut.a.eq(a) - yield dut.b.eq(b) - yield + x = (a >> b) & ((1 << width) - 1) # actual shift + if (a & ((2 << b) - 1)) != 0: # mask for sticky bit + x |= 1 # set LSB - x = (a >> b) & ((1<