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

Fix BitVector shifts #2731

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2024-05-31T20_14_29+02_00_fix_bitvector_shifts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIXED: (+>>.) and (.<<+) such that they are compliant with (+>>) and (<<+) for vectors of zero length in the sense that the input vector is kept unchanged.
13 changes: 9 additions & 4 deletions clash-prelude/src/Clash/Sized/BitVector.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{-|
Copyright : (C) 2013-2016, University of Twente
2022 , Google Inc.
2022-2024, Google Inc.
License : BSD2 (see the file LICENSE)
Maintainer : Christiaan Baaij <[email protected]>
-}

{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}

{-# OPTIONS_HADDOCK show-extensions #-}

Expand Down Expand Up @@ -37,7 +38,7 @@ module Clash.Sized.BitVector
where

import Clash.Sized.Internal.BitVector
import Clash.Promoted.Nat (natToNum)
import Clash.Promoted.Nat (SNat(..), SNatLE(..), compareSNat, natToNum)
import Data.Bits (shiftL, shiftR)
import GHC.TypeNats (KnownNat)

Expand All @@ -55,7 +56,9 @@ infixr 4 +>>.
-- 0b0111_1000
--
(+>>.) :: forall n. KnownNat n => Bit -> BitVector n -> BitVector n
b +>>. bv = replaceBit# (shiftR bv 1) (natToNum @n - 1) b
b +>>. bv = case compareSNat (SNat @n) (SNat @0) of
SNatGT -> replaceBit# (shiftR bv 1) (natToNum @n - 1) b
SNatLE -> bv

infixr 4 .<<+
-- | Shift in a bit from the LSB side of a 'BitVector'. Equal to left shifting
Expand All @@ -67,4 +70,6 @@ infixr 4 .<<+
-- 0b1110_0001
--
(.<<+) :: forall n. KnownNat n => BitVector n -> Bit -> BitVector n
bv .<<+ b = replaceBit# (shiftL bv 1) 0 b
bv .<<+ b = case compareSNat (SNat @n) (SNat @0) of
SNatGT -> replaceBit# (shiftL bv 1) 0 b
SNatLE -> bv