Skip to content

Add reverseBV to clash prelude #2725

Open
@rowanG077

Description

@rowanG077

Slow in simulation implementation:

-- | Reverse bitvector
reverseBV :: KnownNat n => BitVector n -> BitVector n
reverseBV = v2bv . reverse . bv2v

Originally mentioned by @christiaanb in #2694 (comment)

  1. add this function to clash-prelude and
  2. make something with better time complexity such as https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel

Originally posted by @DigitalBrains1 in #2694 (comment)

Hah I had noted this too, but didn't comment on it. I think I didn't even write it down for myself, which was dumb. Anyway...... the current implementation is fine in hardware, and a quick implementation would probably need a primitive. Unless you use clashSimulation from Clash.Magic to use the quick implementation in simulation but use v2bv . reverse . bv2v when generating HDL. That will write your primitive for you (but like a usual primitive, it's still up to you to make sure simulation and HDL match in behaviour).

[edit]
The quick implementation should probably deconstruct the bitvector and construct it again at the end because logical operations on bitvector are needlessly expensive for this application, in this PR I achieved a roughly threefold speedup of logical operations that way.
[/edit]

[edit 2]
I read this wrong: I read this as that Rowan was going to do the implementation. Once the issue is raised, I'll copy-paste the essence of the above to that issue.
[/edit 2]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions