File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 22{-|
33Copyright : (C) 2013-2016, University of Twente
44 2016-2017, Myrtle Software Ltd
5- 2021, QBayLogic B.V.
5+ 2021-2025 , QBayLogic B.V.
66License : BSD2 (see the file LICENSE)
77Maintainer : 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 )
3741where
3842
3943import Clash.Class.BitPack.Internal
4044import Clash.Class.BitPack.BitIndex
4145import Clash.Class.BitPack.BitReduction
46+ import Clash.Class.BitPack.BitShift
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments