clash-compiler
clash-compiler copied to clipboard
SaturatingNum Bit
I have a use case where Overflowing Bit
, and thus a SaturatingNum Bit
instance, would be useful. I see no reason Bit
couldn't be made into SaturatingNum
. Sketch(y) implementation:
instance SaturatingNum Bit where
satAdd = \case
SatWrap -> addOr 0
SatBound -> addOr maxBound
SatZero -> addOr 0
SatSymmetric -> addOr maxBound
SatError -> addOr $ errorX "satAdd"
where
addOr ext = \x y -> if x == 1 && y == 1 then ext else x + y
satSub = \case
SatWrap -> subOr 1
SatBound -> subOr minBound
SatZero -> subOr 0
SatSymmetric -> subOr maxBound
SatError -> subOr $ errorX "satSub"
where
subOr ext = \x y -> if x == 0 && y == 1 then ext else x - y
satMul = \case
_ -> (*)