Skip to content

Commit

Permalink
lza and test_lza refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Durchbruchswagen committed Jan 26, 2025
1 parent 7085ce9 commit f920d2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
10 changes: 2 additions & 8 deletions coreblocks/func_blocks/fu/fpu/lza.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 transactron.utils.amaranth_ext import count_leading_zeros
Expand Down Expand Up @@ -76,14 +75,11 @@ def elaborate(self, platform):

@def_method(m, self.predict_request)
def _(sig_a, sig_b, carry):
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)

t = Signal(self.lza_params.sig_width + 1)
g = Signal(self.lza_params.sig_width + 1)
z = Signal(self.lza_params.sig_width + 1)
f = Signal(f_size)
f = Signal(self.lza_params.sig_width)
shift_amount = Signal(range(self.lza_params.sig_width))
is_zero = Signal(1)

Expand All @@ -95,10 +91,8 @@ def _(sig_a, sig_b, carry):
m.d.av_comb += z[0].eq(1)

for i in reversed(range(1, self.lza_params.sig_width + 1)):
m.d.av_comb += f[i + filler_size - 1].eq((t[i] ^ z[i - 1]))
m.d.av_comb += f[i - 1].eq((t[i] ^ z[i - 1]))

m.d.av_comb += shift_amount.eq(0)
m.d.av_comp += f.eq(f | lower_ones)
m.d.av_comb += shift_amount.eq(count_leading_zeros(f))

m.d.av_comb += is_zero.eq((carry & t[1 : self.lza_params.sig_width].all()))
Expand Down
56 changes: 16 additions & 40 deletions test/func_blocks/fu/fpu/test_lza.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,66 +46,42 @@ async def random_test(sim: TestbenchContext, seed: int, iters: int):
true_lz = clz(sig_a, sig_b, 0, params.sig_width)
assert pred_lz == true_lz or (pred_lz + 1) == true_lz

async def lza_test(sim: TestbenchContext):
async def lza_edge_cases_test(sim: TestbenchContext):
test_cases = [
{
"sig_a": 16368512,
"sig_b": 409600,
"sig_a": 0b111110001010110011001111,
"sig_b": 0b000001110101001100110000,
"carry": 0,
},
{
"sig_a": 0,
"sig_b": (2**24) - 1,
"carry": 0,
},
{
"sig_a": (2**24) // 2,
"sig_b": (2**24) // 2,
"carry": 0,
"sig_a": 0b111110001010110011001111,
"sig_b": 0b000001110101001100110000,
"carry": 1,
},
{
"sig_a": 12582912,
"sig_b": 12550144,
"sig_a": 0b111111111111111111111111,
"sig_b": 0b000000000000000000000001,
"carry": 0,
},
{
"sig_a": 16744448,
"sig_b": 12615680,
"carry": 0,
},
{
"sig_a": 8421376,
"sig_b": 8421376,
"carry": 0,
"sig_a": 0b101110111101111011011101,
"sig_b": 0b001000000000000000000001,
"carry": 1,
},
]
expected_results = [
{"shift_amount": 13, "is_zero": 0},
{"shift_amount": 13, "is_zero": 0},
{"shift_amount": 23, "is_zero": 0},
{"shift_amount": 0, "is_zero": 1},
{"shift_amount": 24, "is_zero": 1},
{"shift_amount": 24, "is_zero": 0},
{"shift_amount": 0, "is_zero": 0},
{"shift_amount": 23, "is_zero": 0},
{"shift_amount": 0, "is_zero": 0},
{"shift_amount": 0, "is_zero": 0},
{"shift_amount": 0, "is_zero": 0},
{"shift_amount": 0, "is_zero": 0},
{"shift_amount": 7, "is_zero": 0},
{"shift_amount": 7, "is_zero": 0},
]
for i in range(len(test_cases)):

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[2 * i + 1]["shift_amount"]
assert resp["is_zero"] == expected_results[2 * i + 1]["is_zero"]
assert resp["shift_amount"] == expected_results[i]["shift_amount"]
assert resp["is_zero"] == expected_results[i]["is_zero"]

async def test_process(sim: TestbenchContext):
await lza_test(sim)
await lza_edge_cases_test(sim)
await random_test(sim, 2024, 20)

with self.run_simulation(lza) as sim:
Expand Down

0 comments on commit f920d2f

Please sign in to comment.