forked from M-Labs/artiq
dsp: add limits support to SatAddMixin
This commit is contained in:
parent
97a54046e8
commit
e53d0bcd5b
|
@ -29,21 +29,33 @@ def eqh(a, b):
|
||||||
|
|
||||||
|
|
||||||
class SatAddMixin:
|
class SatAddMixin:
|
||||||
def sat_add(self, a):
|
"""Signed saturating addition mixin"""
|
||||||
|
def sat_add(self, *a, limits=None, clipped=None):
|
||||||
a = list(a)
|
a = list(a)
|
||||||
# assert all(value_bits_sign(ai)[1] for ai in a)
|
# assert all(value_bits_sign(ai)[1] for ai in a)
|
||||||
n = max(len(ai) for ai in a)
|
length = max(len(ai) for ai in a)
|
||||||
o = log2_int(len(a), need_pow2=False)
|
carry = log2_int(len(a), need_pow2=False)
|
||||||
s = Signal((n + o, True))
|
full = Signal((length + carry, True))
|
||||||
s0 = Signal((n, True))
|
limited = Signal((length, True))
|
||||||
z = Signal((1, True))
|
clip = Signal(2)
|
||||||
|
if clipped is not None:
|
||||||
|
clipped.eq(clip)
|
||||||
self.comb += [
|
self.comb += [
|
||||||
s.eq(reduce(add, a, z)),
|
full.eq(reduce(add, a)),
|
||||||
s0[-1].eq(s[-1]),
|
]
|
||||||
If(s[-o-1:] == Replicate(s[-1], o + 1),
|
if limits is None:
|
||||||
s0[:-1].eq(s[:n-1]),
|
self.comb += [
|
||||||
|
If(full[-1-carry:] == Replicate(full[-1], carry + 1),
|
||||||
|
limited.eq(full),
|
||||||
|
clip.eq(0),
|
||||||
).Else(
|
).Else(
|
||||||
s0[:-1].eq(Replicate(~s[-1], n - 1)),
|
limited.eq(Cat(Replicate(~full[-1], length - 1), full[-1])),
|
||||||
|
clip.eq(Cat(full[-1], ~full[-1])),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
return s0
|
else:
|
||||||
|
self.comb += [
|
||||||
|
clip.eq(Cat(full < limits[0], full > limits[1])),
|
||||||
|
limited.eq(Array([full, limits[0], limits[1], 0])[clip]),
|
||||||
|
]
|
||||||
|
return limited
|
||||||
|
|
Loading…
Reference in New Issue