Skip to content

Commit 4f77fac

Browse files
Allow rotate{L,R} on zero-width BitVectors (#3138)
Fixes #2980
1 parent 6fa2d7a commit 4f77fac

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: `rotateL` and `rotateR` no longer error when used on `BitVector 0` [#2980](https://github.com/clash-lang/clash-compiler/issues/2980)

clash-prelude/src/Clash/Sized/Internal/BitVector.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,9 @@ shiftR# (BV m v) i
10901090
{-# ANN rotateL# hasBlackBox #-}
10911091
rotateL# =
10921092
\(BV msk v) b ->
1093-
if b >= 0 then
1093+
if sz == 0 then
1094+
BV msk v
1095+
else if b >= 0 then
10941096
let vl = naturalShiftL v b'
10951097
vr = naturalShiftR v b''
10961098

@@ -1110,7 +1112,9 @@ rotateL# =
11101112
{-# ANN rotateR# hasBlackBox #-}
11111113
rotateR# =
11121114
\(BV msk v) b ->
1113-
if b >= 0 then
1115+
if sz == 0 then
1116+
BV msk v
1117+
else if b >= 0 then
11141118
let vl = naturalShiftR v b'
11151119
vr = naturalShiftL v b''
11161120
ml = naturalShiftR msk b'

clash-prelude/tests/Clash/Tests/BitVector.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import qualified Test.Tasty.Hedgehog.Extra as H
2424
import qualified Test.Tasty.QuickCheck as Q
2525

2626
import Clash.Prelude
27-
(Bit, high, low, bitPattern, type (<=), type (-), natToInteger, msb, bLit, hLit, oLit)
27+
(Bit, high, low, bitPattern, type (<=), type (-), natToInteger, msb, bLit, hLit, oLit, rotateL, rotateR)
2828
import Clash.Sized.Internal.BitVector (BitVector (..))
2929

3030
import Clash.Tests.SizedNum
@@ -97,6 +97,10 @@ tests = localOption (Q.QuickCheckMaxRatio 2) $ testGroup "All"
9797
[ testCase "maxBound :: BitVector 0" $ maxBound @(BitVector 0) @?= 0
9898
, testCase "minBound :: BitVector 0" $ minBound @(BitVector 0) @?= 0
9999
]
100+
, testGroup "Rotate"
101+
[ testCase "rotateL BitVector 0" $ rotateL @(BitVector 0) 0 2 @?= 0
102+
, testCase "rotateR BitVector 0" $ rotateR @(BitVector 0) 0 2 @?= 0
103+
]
100104
, testGroup "MSB"
101105
[ H.testPropertyXXX "msb @(BitVector 1)" (msbTest @1)
102106
, H.testPropertyXXX "msb @(BitVector 2)" (msbTest @2)

0 commit comments

Comments
 (0)