Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LZA (Leading zeros anticipation) #741

Merged
merged 12 commits into from
Dec 14, 2024
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why divide by 2? You seem to ignore half of the cases this way. Am I missing something?

(This is one of the reasons why using indexes is discouraged.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it was a mistake on my part. I wanted to remove cases where I explicitly set carry to 1 by performing two tests per test case and manually setting carry to 1 in the second one. For some reason, I decided that dividing the number of test cases by two after I deleted redundant ones is a great idea. But as I said this was a mistake, that is now fixed.


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
Loading