Skip to content

Commit

Permalink
fix for signed comparison bug
Browse files Browse the repository at this point in the history
  • Loading branch information
alanvgreen committed Feb 4, 2022
1 parent d0fd66c commit d58a45d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions proj/hps_accel/gateware/gen2/hps_cfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ def max_(word0, word1):
bytes0 = [word0[i:i + 8] for i in range(0, 32, 8)]
bytes1 = [word1[i:i + 8] for i in range(0, 32, 8)]
for r, b0, b1 in zip(result, bytes0, bytes1):
sb0 = Signal(signed(8))
m.d.comb += sb0.eq(b0)
sb1 = Signal(signed(8))
m.d.comb += sb1.eq(b1)
m.d.comb += r.eq(Mux(sb1 > sb0, b1, b0))
# the bytes represent signed values, but signed comparison
# appears to trigger a bug
# https://github.com/google/CFU-Playground/issues/451
# To avoid that bug, we flip the sign bits (i.e add 128)
# and do an unsigned comparison instead.
m.d.comb += r.eq(Mux((b1 ^ 0x80) > (b0 ^ 0x80), b1, b0))
return Cat(*result)

last2 = Signal(32)
Expand All @@ -225,6 +226,7 @@ def max_(word0, word1):
class HpsCfu(Cfu):
"""Gen2 accelerator CFU.
"""

def __init__(self, specialize_nx=False):
"""Constructor
Expand Down
2 changes: 2 additions & 0 deletions proj/hps_accel/gateware/gen2/test_hps_cfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def test_pool(self):
((Constants.INS_POOL, 0x80808080, 0x80808080), 0x80808080),
((Constants.INS_POOL, 0x08030601, 0x04070205), 0x08070605),
((Constants.INS_POOL, 0x00660000, 0x00007700), 0x08667705),
((Constants.INS_POOL, 0x12808080, 0x88808080), None),
((Constants.INS_POOL, 0x1a808080, 0x81808080), 0x1a808080),
]

self.run_ops(OPS, False)

0 comments on commit d58a45d

Please sign in to comment.