Skip to content
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

Counter for Vectors #2787

Closed
gergoerdi opened this issue Aug 17, 2024 · 3 comments · Fixed by #2788
Closed

Counter for Vectors #2787

gergoerdi opened this issue Aug 17, 2024 · 3 comments · Fixed by #2788

Comments

@gergoerdi
Copy link
Contributor

gergoerdi commented Aug 17, 2024

How about something like this?

data Endian = BigEndian | LittleEndian

newtype Counters (endianness :: Endian) n a = Counters{ getCounters :: Vec n a }
    deriving (Generic, NFDataX, Eq, Show)

ripple :: ((Bool -> a -> (Bool, a)) -> Bool -> Vec n a -> (Bool, Vec n a)) -> (a -> (Bool, a)) -> Vec n a -> (Bool, Vec n a)
ripple mapAccum f = mapAccum step True
  where
    step carry x = if carry then f x else (False, x)

instance (Counter a, KnownNat n, 1 <= n) => Counter (Counters BigEndian n a) where
    countMin = Counters $ repeat countMin
    countMax = Counters $ repeat countMax

    countSuccOverflow = fmap Counters . ripple mapAccumR countSuccOverflow . getCounters
    countPredOverflow = fmap Counters . ripple mapAccumR countPredOverflow . getCounters

instance (Counter a, KnownNat n, 1 <= n) => Counter (Counters LittleEndian n a) where
    countMin = Counters $ repeat countMin
    countMax = Counters $ repeat countMax

    countSuccOverflow = fmap Counters . ripple mapAccumL countSuccOverflow . getCounters
    countPredOverflow = fmap Counters . ripple mapAccumL countPredOverflow . getCounters

Mostly I just want to be able to use a Counter BigEndian n (Index 10) as the world's least-effort BCD counter...

@martijnbastiaan
Copy link
Member

Is there a reason we can't build Counter a => Counter (Vec n a)? Or do you want to introduce Counters as a means to introduce endianness?

@gergoerdi
Copy link
Contributor Author

Just neither direction seemed more natural to me than the other, so I went with both.

@martijnbastiaan
Copy link
Member

Yeah, I've had similar thoughts when writing instances for tuples. I see you made #2788 marking this issue as fixed; feel free to open an additional issue proposing to extend Counter with endianness if that's important to you.

gergoerdi added a commit to gergoerdi/clash-compiler that referenced this issue Aug 26, 2024
gergoerdi added a commit to gergoerdi/clash-compiler that referenced this issue Aug 27, 2024
gergoerdi added a commit to gergoerdi/clash-compiler that referenced this issue Aug 27, 2024
gergoerdi added a commit to gergoerdi/clash-compiler that referenced this issue Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants