Skip to content

Commit

Permalink
Fix BitVector shifts (#2732)
Browse files Browse the repository at this point in the history
(cherry picked from commit f96795e)

Co-authored-by: Felix Klein <[email protected]>
  • Loading branch information
mergify[bot] and kleinreact authored Jun 2, 2024
1 parent c613630 commit 6efdf62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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

0 comments on commit 6efdf62

Please sign in to comment.