Skip to content

Commit

Permalink
Changes to test_lza.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Durchbruchswagen committed Dec 9, 2024
1 parent 958395d commit 5788c8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 72 deletions.
12 changes: 2 additions & 10 deletions coreblocks/func_blocks/fu/fpu/lza.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = [
Expand All @@ -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)
Expand Down

0 comments on commit 5788c8f

Please sign in to comment.