-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SaturatingNum Bit #2827
Comments
As also mentioned on #clash, another alternative might be to use the already existing instance for instance SaturatingNum Bit where
satAdd mode a b = bitCoerce @(BitVector 1) @Bit $
satAdd mode
(bitCoerce @Bit @(BitVector 1) a)
(bitCoerce @Bit @(BitVector 1) b)
satSub mode a b = bitCoerce @(BitVector 1) @Bit $
satSub mode
(bitCoerce @Bit @(BitVector 1) a)
(bitCoerce @Bit @(BitVector 1) b)
satMul mode a b = bitCoerce @(BitVector 1) @Bit $
satMul mode
(bitCoerce @Bit @(BitVector 1) a)
(bitCoerce @Bit @(BitVector 1) b) |
should work with |
How about
This should work without any explicit type arguments on |
Aaaaaaaaaaand we have a winner:
|
I'm not a big fan of using the I think the code from the first post is fine (although I'd write |
Ah, but the code in the first post turns an undefined bit into an So yeah, I'm in favour of code that doesn't use [edit] [edit 2] import Clash.Sized.Internal.BitVector (pack#)
import Data.Composition ((.:)) -- from `composition' package
import Data.Function (on)
instance SaturatingNum Bit where
satAdd mode = unpack .: satAdd mode `on` pack#
satSub mode = unpack .: satSub mode `on` pack#
satMul mode = unpack .: satMul mode `on` pack# Personally I'm more inclined to write it without |
The implementation for Also, multiplication for I think we should also discuss (elsewhere) why those >>> xbv = $(bLit ".")
>>> xb = unpack xbv :: Bit
>>> xbv
0b.
>>> xb
.
>>> 1 * xb
.
>>> 0 * xb
0
>>> xb * 0
0
>>> 0 * xbv
*** Exception: X: Clash.Sized.BitVector.* called with (partially) undefined arguments: 0b0 * 0b. [edit] Known least significant bitsI think given the following definitions:
then the following holds:
(thus leading to a trailer of known 0 bits) [edit] |
I have a use case where
Overflowing Bit
, and thus aSaturatingNum Bit
instance, would be useful. I see no reasonBit
couldn't be made intoSaturatingNum
. Sketch(y) implementation:The text was updated successfully, but these errors were encountered: