Skip to content

Commit 90a368b

Browse files
committed
Add (<<%) and (%>>)
1 parent 6bebeae commit 90a368b

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

clash-prelude/clash-prelude.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Library
178178
Clash.Class.BitPack
179179
Clash.Class.BitPack.BitIndex
180180
Clash.Class.BitPack.BitReduction
181+
Clash.Class.BitPack.BitShift
181182
Clash.Class.BitPack.Internal
182183
Clash.Class.BitPack.Internal.TH
183184
Clash.Class.Counter

clash-prelude/src/Clash/Class/BitPack.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{-|
33
Copyright : (C) 2013-2016, University of Twente
44
2016-2017, Myrtle Software Ltd
5-
2021, QBayLogic B.V.
5+
2021-2025, QBayLogic B.V.
66
License : BSD2 (see the file LICENSE)
77
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
88
-}
@@ -33,9 +33,14 @@ module Clash.Class.BitPack
3333
, reduceAnd
3434
, reduceOr
3535
, reduceXor
36+
37+
-- * Bit Shifts
38+
, (<<%)
39+
, (%>>)
3640
)
3741
where
3842

3943
import Clash.Class.BitPack.Internal
4044
import Clash.Class.BitPack.BitIndex
4145
import Clash.Class.BitPack.BitReduction
46+
import Clash.Class.BitPack.BitShift
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{-|
2+
Copyright : (C) 2025, QBayLogic B.V.
3+
License : BSD2 (see the file LICENSE)
4+
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
5+
-}
6+
7+
{-# LANGUAGE Safe #-}
8+
9+
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}
10+
11+
module Clash.Class.BitPack.BitShift where
12+
13+
import Clash.Class.BitPack.Internal (BitPack (..), bitCoerce)
14+
import Clash.Promoted.Nat (SNat (..))
15+
import Clash.Sized.BitVector (BitVector)
16+
17+
{- $setup
18+
>>> import Clash.Prelude
19+
-}
20+
21+
{-# INLINE (<<%) #-}
22+
-- | Left shift the bits by the given amount.
23+
--
24+
-- >>> (1 :: Unsigned 3) <<% d0
25+
-- 1
26+
-- >>> (1 :: Unsigned 3) <<% d2
27+
-- 4
28+
-- >>> (1 :: Unsigned 3) <<% d4
29+
-- 0
30+
-- >>> pack (-1 :: Signed 6)
31+
-- 0b11_1111
32+
-- >>> reduceAnd (-1 :: Signed 6)
33+
infixl 7 <<%
34+
(<<%) :: forall a n. BitPack a => a -> SNat n -> a
35+
x <<% SNat =
36+
unpack $ snd @(BitVector n) @_ $ bitCoerce (pack x, 0 :: BitVector n)
37+
38+
{-# INLINE (%>>) #-}
39+
-- | Right shift the bits by the given amount.
40+
--
41+
-- >>> d0 %>> (4 :: Unsigned 3)
42+
-- 4
43+
-- >>> d1 %>> (4 :: Unsigned 3)
44+
-- 2
45+
-- >>> d5 %>> (4 :: Unsigned 3)
46+
-- 0
47+
infixl 7 %>>
48+
(%>>) :: forall a n. BitPack a => SNat n -> a -> a
49+
SNat %>> x =
50+
unpack $ fst @_ @(BitVector n) $ bitCoerce (0 :: BitVector n, pack x)

0 commit comments

Comments
 (0)