Skip to content

Commit a5d1105

Browse files
committed
Added a value range check
1 parent 36e7884 commit a5d1105

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

veriloggen/stream/stypes.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,25 +2171,29 @@ def SraRound(left, right):
21712171
msb = left[-1]
21722172
msb.latency = 0
21732173

2174-
if isinstance(right, int):
2175-
rounder = Sll(Int(1), right - 1)
2174+
if isinstance(right, int) and right <= 0:
2175+
rounder = 0
2176+
elif isinstance(right, int):
2177+
rounder = 1 << (right - 1)
21762178
else:
2177-
right_slice = right[0:int(log(left.width, 2))]
2178-
right_slice.latency = 0
2179-
right_slice = right_slice - 1
2180-
right_slice.latency = 0
2181-
rounder = Sll(Int(1), right_slice)
2179+
right_slice = right
2180+
right_width = int(ceil(log(left.width, 2)))
2181+
if right_width < right.width:
2182+
right_slice = right[0:right_width]
2183+
right_slice.latency = 0
2184+
2185+
right_minus_one = right_slice - 1
2186+
right_minus_one.latency = 0
2187+
rounder = Sll(Int(1), right_minus_one)
2188+
rounder.latency = 0
21822189

2183-
rounder.latency = 0
21842190
rounder_sign = Mux(msb, Int(-1), Int(0))
21852191
rounder_sign.latency = 0
21862192

2187-
# if left.width < right
2188-
# raise ValueError("Shift amount of SraRound operator must be less than val bit width")
2189-
21902193
pre_round = left + rounder
21912194
pre_round.width = left.width + 1
21922195
pre_round.latency = 0
2196+
21932197
pre_round = pre_round + rounder_sign
21942198
pre_round.latency = 0
21952199

@@ -2199,6 +2203,7 @@ def SraRound(left, right):
21992203

22002204
return Mux(right == Int(0), left, shifted)
22012205

2206+
22022207
class _Constant(_Numeric):
22032208

22042209
def __init__(self, value):

0 commit comments

Comments
 (0)