From 5788c8f343eaff67269cd5813d2a73f2d710a91f Mon Sep 17 00:00:00 2001 From: Mateusz Marszalek Date: Mon, 9 Dec 2024 20:03:01 +0100 Subject: [PATCH] Changes to test_lza.py --- coreblocks/func_blocks/fu/fpu/lza.py | 12 +--- test/func_blocks/fu/{ => fpu}/test_lza.py | 84 ++++++----------------- 2 files changed, 24 insertions(+), 72 deletions(-) rename test/func_blocks/fu/{ => fpu}/test_lza.py (55%) diff --git a/coreblocks/func_blocks/fu/fpu/lza.py b/coreblocks/func_blocks/fu/fpu/lza.py index 275c1bd3a..7d05850f7 100644 --- a/coreblocks/func_blocks/fu/fpu/lza.py +++ b/coreblocks/func_blocks/fu/fpu/lza.py @@ -1,18 +1,10 @@ from amaranth import * +from amaranth.utils import ceil_log2 from transactron import TModule, Method, def_method from coreblocks.func_blocks.fu.fpu.fpu_common import FPUParams -from math import log2 from transactron.utils.amaranth_ext import count_leading_zeros -def nearestpow2(n): - a = int(log2(n)) - if 2**a == n: - return n - else: - return 2 ** (a + 1) - - class LZAMethodLayout: """LZA module layouts for methods @@ -84,7 +76,7 @@ def elaborate(self, platform): @def_method(m, self.predict_request) def _(sig_a, sig_b, carry): - f_size = nearestpow2(self.lza_params.sig_width) + f_size = 2 ** ceil_log2(self.lza_params.sig_width) filler_size = f_size - self.lza_params.sig_width lower_ones = Const((2**filler_size) - 1, f_size) diff --git a/test/func_blocks/fu/test_lza.py b/test/func_blocks/fu/fpu/test_lza.py similarity index 55% rename from test/func_blocks/fu/test_lza.py rename to test/func_blocks/fu/fpu/test_lza.py index 9c5f0cb97..890bdc379 100644 --- a/test/func_blocks/fu/test_lza.py +++ b/test/func_blocks/fu/fpu/test_lza.py @@ -30,24 +30,8 @@ def elaborate(self, platform): m.submodules.predict = self.predict_request_adapter = TestbenchIO(AdapterTrans(lza.predict_request)) return m - class HelpValues: - def __init__(self, params: FPUParams): - self.test_val_sig_a_1 = 16368512 - self.test_val_sig_b_1 = 409600 - self.test_val_sig_a_2 = 0 - self.test_val_sig_b_2 = (2**24) - 1 - self.test_val_sig_a_3 = (2**24) // 2 - self.test_val_sig_b_3 = (2**24) // 2 - self.test_val_sig_a_4 = 12582912 - self.test_val_sig_b_4 = 12550144 - self.test_val_sig_a_5 = 16744448 - self.test_val_sig_b_5 = 12615680 - self.test_val_sig_a_6 = 8421376 - self.test_val_sig_b_6 = 8421376 - def test_manual(self): params = FPUParams(sig_width=24, exp_width=8) - help_values = TestLZA.HelpValues(params) lza = TestLZA.LZAModuleTest(params) async def random_test(sim: TestbenchContext, seed: int, iters: int): @@ -65,64 +49,34 @@ async def random_test(sim: TestbenchContext, seed: int, iters: int): async def lza_test(sim: TestbenchContext): test_cases = [ { - "sig_a": help_values.test_val_sig_a_1, - "sig_b": help_values.test_val_sig_b_1, + "sig_a": 16368512, + "sig_b": 409600, "carry": 0, }, { - "sig_a": help_values.test_val_sig_a_1, - "sig_b": help_values.test_val_sig_b_1, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_2, - "sig_b": help_values.test_val_sig_b_2, + "sig_a": 0, + "sig_b": (2**24) - 1, "carry": 0, }, { - "sig_a": help_values.test_val_sig_a_2, - "sig_b": help_values.test_val_sig_b_2, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_3, - "sig_b": help_values.test_val_sig_b_3, + "sig_a": (2**24) // 2, + "sig_b": (2**24) // 2, "carry": 0, }, { - "sig_a": help_values.test_val_sig_a_3, - "sig_b": help_values.test_val_sig_b_3, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_4, - "sig_b": help_values.test_val_sig_b_4, + "sig_a": 12582912, + "sig_b": 12550144, "carry": 0, }, { - "sig_a": help_values.test_val_sig_a_4, - "sig_b": help_values.test_val_sig_b_4, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_5, - "sig_b": help_values.test_val_sig_b_5, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_5, - "sig_b": help_values.test_val_sig_b_5, - "carry": 1, - }, - { - "sig_a": help_values.test_val_sig_a_6, - "sig_b": help_values.test_val_sig_b_6, + "sig_a": 16744448, + "sig_b": 12615680, "carry": 0, }, { - "sig_a": help_values.test_val_sig_a_6, - "sig_b": help_values.test_val_sig_b_6, - "carry": 1, + "sig_a": 8421376, + "sig_b": 8421376, + "carry": 0, }, ] expected_results = [ @@ -139,10 +93,16 @@ async def lza_test(sim: TestbenchContext): {"shift_amount": 7, "is_zero": 0}, {"shift_amount": 7, "is_zero": 0}, ] - for i in range(len(test_cases)): + for i in range(len(test_cases) // 2): + + resp = await lza.predict_request_adapter.call(sim, test_cases[i]) + assert resp["shift_amount"] == expected_results[2 * i]["shift_amount"] + assert resp["is_zero"] == expected_results[2 * i]["is_zero"] + + test_cases[i]["carry"] = 1 resp = await lza.predict_request_adapter.call(sim, test_cases[i]) - assert resp["shift_amount"] == expected_results[i]["shift_amount"] - assert resp["is_zero"] == expected_results[i]["is_zero"] + assert resp["shift_amount"] == expected_results[2 * i + 1]["shift_amount"] + assert resp["is_zero"] == expected_results[2 * i + 1]["is_zero"] async def test_process(sim: TestbenchContext): await lza_test(sim)