module CLaSH.Sized.Internal.BitVector
(
BitVector (..)
, Bit
, size#
, maxIndex#
, high
, low
, bLit
, (++#)
, reduceAnd#
, reduceOr#
, reduceXor#
, index#
, replaceBit#
, setSlice#
, slice#
, split#
, msb#
, lsb#
, eq#
, neq#
, lt#
, ge#
, gt#
, le#
, enumFrom#
, enumFromThen#
, enumFromTo#
, enumFromThenTo#
, minBound#
, maxBound#
, (+#)
, (-#)
, (*#)
, negate#
, fromInteger#
, plus#
, minus#
, times#
, quot#
, rem#
, toInteger#
, and#
, or#
, xor#
, complement#
, shiftL#
, shiftR#
, rotateL#
, rotateR#
, popCountBV
, resize#
)
where
import Control.Lens (Index, Ixed (..), IxValue)
import Data.Bits (Bits (..), FiniteBits (..))
import Data.Char (digitToInt)
import Data.Data (Data)
import Data.Default (Default (..))
import Data.Maybe (listToMaybe)
import GHC.Integer (smallInteger)
import GHC.Prim (dataToTag#)
import GHC.TypeLits (KnownNat, Nat, type (+), type (), natVal)
import Language.Haskell.TH (Q, TExp, TypeQ, appT, conT, litT, numTyLit, sigE)
import Language.Haskell.TH.Syntax (Lift(..))
import Numeric (readInt)
import Test.QuickCheck.Arbitrary (Arbitrary (..), CoArbitrary (..),
arbitrarySizedBoundedIntegral,
coarbitraryIntegral, shrinkIntegral)
import CLaSH.Class.Num (ExtendingNum (..), SaturatingNum (..),
SaturationMode (..))
import CLaSH.Class.Resize (Resize (..))
import CLaSH.Promoted.Nat (SNat, snatToInteger)
import CLaSH.Promoted.Ord (Max)
import qualified CLaSH.Sized.Vector as V
import qualified CLaSH.Sized.Internal.Index as I
newtype BitVector (n :: Nat) =
BV { unsafeToInteger :: Integer}
deriving Data
type Bit = BitVector 1
instance KnownNat n => Show (BitVector n) where
show bv@(BV i) = reverse . underScore . reverse $ showBV (natVal bv) i []
where
showBV 0 _ s = s
showBV n v s = let (a,b) = divMod v 2
in case b of
1 -> showBV (n 1) a ('1':s)
_ -> showBV (n 1) a ('0':s)
underScore xs = case splitAt 5 xs of
([a,b,c,d,e],rest) -> [a,b,c,d,'_'] ++ underScore (e:rest)
(rest,_) -> rest
bLit :: KnownNat n => String -> Q (TExp (BitVector n))
bLit s = [|| fromInteger# i' ||]
where
i :: Maybe Integer
i = fmap fst . listToMaybe . (readInt 2 (`elem` "01") digitToInt) $ filter (/= '_') s
i' :: Integer
i' = case i of
Just j -> j
_ -> error "Failed to parse: " s
instance Eq (BitVector n) where
(==) = eq#
(/=) = neq#
eq# :: BitVector n -> BitVector n -> Bool
eq# (BV v1) (BV v2) = v1 == v2
neq# :: BitVector n -> BitVector n -> Bool
neq# (BV v1) (BV v2) = v1 /= v2
instance Ord (BitVector n) where
(<) = lt#
(>=) = ge#
(>) = gt#
(<=) = le#
lt#,ge#,gt#,le# :: BitVector n -> BitVector n -> Bool
lt# (BV n) (BV m) = n < m
ge# (BV n) (BV m) = n >= m
gt# (BV n) (BV m) = n > m
le# (BV n) (BV m) = n <= m
instance KnownNat n => Enum (BitVector n) where
succ = (+# fromInteger# 1)
pred = (-# fromInteger# 1)
toEnum = fromInteger# . toInteger
fromEnum = fromEnum . toInteger#
enumFrom = enumFrom#
enumFromThen = enumFromThen#
enumFromTo = enumFromTo#
enumFromThenTo = enumFromThenTo#
enumFrom# :: KnownNat n => BitVector n -> [BitVector n]
enumFromThen# :: KnownNat n => BitVector n -> BitVector n -> [BitVector n]
enumFromTo# :: KnownNat n => BitVector n -> BitVector n -> [BitVector n]
enumFromThenTo# :: KnownNat n => BitVector n -> BitVector n -> BitVector n
-> [BitVector n]
enumFrom# x = map toEnum [fromEnum x ..]
enumFromThen# x y = map toEnum [fromEnum x, fromEnum y ..]
enumFromTo# x y = map toEnum [fromEnum x .. fromEnum y]
enumFromThenTo# x1 x2 y = map toEnum [fromEnum x1, fromEnum x2 .. fromEnum y]
instance KnownNat n => Bounded (BitVector n) where
minBound = minBound#
maxBound = maxBound#
minBound# :: BitVector n
minBound# = BV 0
maxBound# :: KnownNat n => BitVector n
maxBound# = let res = BV ((2 ^ natVal res) 1) in res
instance KnownNat n => Num (BitVector n) where
(+) = (+#)
() = (-#)
(*) = (*#)
negate = negate#
abs = id
signum bv = resize# (reduceOr# bv)
fromInteger = fromInteger#
(+#),(-#),(*#) :: KnownNat n => BitVector n -> BitVector n -> BitVector n
(+#) (BV i) (BV j) = fromInteger_INLINE (i + j)
(-#) (BV i) (BV j) = fromInteger_INLINE (i j)
(*#) (BV i) (BV j) = fromInteger_INLINE (i * j)
negate# :: KnownNat n => BitVector n -> BitVector n
negate# bv@(BV i) = BV (sz i)
where
sz = 2 ^ natVal bv
fromInteger# :: KnownNat n => Integer -> BitVector n
fromInteger# = fromInteger_INLINE
fromInteger_INLINE :: KnownNat n => Integer -> BitVector n
fromInteger_INLINE i = let res = BV (i `mod` (2 ^ natVal res)) in res
instance (KnownNat (Max m n + 1), KnownNat (m + n)) =>
ExtendingNum (BitVector m) (BitVector n) where
type AResult (BitVector m) (BitVector n) = BitVector (Max m n + 1)
plus = plus#
minus = minus#
type MResult (BitVector m) (BitVector n) = BitVector (m + n)
times = times#
plus#, minus# :: KnownNat (Max m n + 1) => BitVector m -> BitVector n
-> BitVector (Max m n + 1)
plus# (BV a) (BV b) = fromInteger_INLINE (a + b)
minus# (BV a) (BV b) = fromInteger_INLINE (a b)
times# :: KnownNat (m + n) => BitVector m -> BitVector n -> BitVector (m + n)
times# (BV a) (BV b) = fromInteger_INLINE (a * b)
instance KnownNat n => Real (BitVector n) where
toRational = toRational . toInteger#
instance KnownNat n => Integral (BitVector n) where
quot = quot#
rem = rem#
div = quot#
mod = rem#
quotRem n d = (n `quot#` d,n `rem#` d)
divMod n d = (n `quot#` d,n `rem#` d)
toInteger = toInteger#
quot#,rem# :: BitVector n -> BitVector n -> BitVector n
quot# (BV i) (BV j) = BV (i `quot` j)
rem# (BV i) (BV j) = BV (i `rem` j)
toInteger# :: BitVector n -> Integer
toInteger# (BV i) = i
instance (KnownNat n, KnownNat (n+1), KnownNat (n+2)) => Bits (BitVector n) where
(.&.) = and#
(.|.) = or#
xor = xor#
complement = complement#
zeroBits = 0
bit i = replaceBit# 0 i high
setBit v i = replaceBit# v i high
clearBit v i = replaceBit# v i low
complementBit v i = replaceBit# v i (complement# (index# v i))
testBit v i = eq# (index# v i) high
bitSizeMaybe v = Just (size# v)
bitSize = size#
isSigned _ = False
shiftL v i = shiftL# v i
shiftR v i = shiftR# v i
rotateL v i = rotateL# v i
rotateR v i = rotateR# v i
popCount bv = fromEnum (popCountBV (bv ++# (0 :: Bit)))
instance (KnownNat n, KnownNat (n+1), KnownNat (n+2)) => FiniteBits (BitVector n) where
finiteBitSize = size#
reduceAnd# :: (KnownNat n) => BitVector n -> BitVector 1
reduceAnd# bv@(BV i) = BV (smallInteger (dataToTag# check))
where
check = i == maxI
sz = natVal bv
maxI = (2 ^ sz) 1
reduceOr# :: BitVector n -> BitVector 1
reduceOr# (BV i) = BV (smallInteger (dataToTag# check))
where
check = i /= 0
reduceXor# :: BitVector n -> BitVector 1
reduceXor# (BV i) = BV (toInteger (popCount i `mod` 2))
instance Default (BitVector n) where
def = minBound#
size# :: KnownNat n => BitVector n -> Int
size# bv = fromInteger (natVal bv)
maxIndex# :: KnownNat n => BitVector n -> Int
maxIndex# bv = fromInteger (natVal bv) 1
index# :: KnownNat n => BitVector n -> Int -> Bit
index# bv@(BV v) i
| i >= 0 && i < sz = BV (smallInteger
(dataToTag#
(testBit v i)))
| otherwise = err
where
sz = fromInteger (natVal bv)
err = error $ concat [ "(!): "
, show i
, " is out of range ["
, show (sz 1)
, "..0]"
]
msb# :: KnownNat n => BitVector n -> Bit
msb# bv@(BV v) = BV (smallInteger (dataToTag# (testBit v i)))
where
i = fromInteger (natVal bv 1)
lsb# :: BitVector n -> Bit
lsb# (BV v) = BV (smallInteger (dataToTag# (testBit v 0)))
slice# :: BitVector (m + 1 + i) -> SNat m -> SNat n -> BitVector (m + 1 n)
slice# (BV i) m n = BV (shiftR (i .&. mask) n')
where
m' = snatToInteger m
n' = fromInteger (snatToInteger n)
mask = 2 ^ (m' + 1) 1
high :: Bit
high = BV 1
low :: Bit
low = BV 0
(++#) :: KnownNat m => BitVector n -> BitVector m -> BitVector (n + m)
(BV v1) ++# bv2@(BV v2) = BV (v1' + v2)
where
v1' = shiftL v1 (fromInteger (natVal bv2))
replaceBit# :: KnownNat n => BitVector n -> Int -> Bit -> BitVector n
replaceBit# bv@(BV v) i (BV b)
| i >= 0 && i < sz = BV (if b == 1 then setBit v i else clearBit v i)
| otherwise = err
where
sz = fromInteger (natVal bv)
err = error $ concat [ "replaceBit: "
, show i
, " is out of range ["
, show (sz 1)
, "..0]"
]
setSlice# :: BitVector (m + 1 + i) -> SNat m -> SNat n -> BitVector (m + 1 n)
-> BitVector (m + 1 + i)
setSlice# (BV i) m n (BV j) = BV ((i .&. mask) .|. j')
where
m' = snatToInteger m
n' = snatToInteger n
j' = shiftL j (fromInteger n')
mask = complement ((2 ^ (m' + 1) 1) `xor` (2 ^ n' 1))
split# :: KnownNat n => BitVector (m + n) -> (BitVector m, BitVector n)
split# (BV i) = (l,r)
where
n = fromInteger (natVal r)
mask = (2 ^ n) 1
r = BV (i .&. mask)
l = BV (i `shiftR` n)
and#, or#, xor# :: BitVector n -> BitVector n -> BitVector n
and# (BV v1) (BV v2) = BV (v1 .&. v2)
or# (BV v1) (BV v2) = BV (v1 .|. v2)
xor# (BV v1) (BV v2) = BV (v1 `xor` v2)
complement# :: KnownNat n => BitVector n -> BitVector n
complement# (BV v1) = fromInteger_INLINE (complement v1)
shiftL#, shiftR#, rotateL#, rotateR# :: KnownNat n => BitVector n -> Int
-> BitVector n
shiftL# (BV v) i
| i < 0 = error
$ "'shiftL undefined for negative number: " ++ show i
| otherwise = fromInteger_INLINE (shiftL v i)
shiftR# (BV v) i
| i < 0 = error
$ "'shiftR undefined for negative number: " ++ show i
| otherwise = fromInteger_INLINE (shiftR v i)
rotateL# _ b | b < 0 = error "'shiftL undefined for negative numbers"
rotateL# bv@(BV n) b = fromInteger_INLINE (l .|. r)
where
l = shiftL n b'
r = shiftR n b''
b' = b `mod` sz
b'' = sz b'
sz = fromInteger (natVal bv)
rotateR# _ b | b < 0 = error "'shiftR undefined for negative numbers"
rotateR# bv@(BV n) b = fromInteger_INLINE (l .|. r)
where
l = shiftR n b'
r = shiftL n b''
b' = b `mod` sz
b'' = sz b'
sz = fromInteger (natVal bv)
popCountBV :: (KnownNat (n+1), KnownNat (n + 2))
=> BitVector (n+1)
-> I.Index (n+2)
popCountBV bv = sum (V.map fromIntegral v)
where
v = V.bv2v bv
instance Resize BitVector where
resize = resize#
zeroExtend = resize#
signExtend = resize#
truncateB = resize#
resize# :: KnownNat m => BitVector n -> BitVector m
resize# (BV n) = fromInteger_INLINE n
instance KnownNat n => Lift (BitVector n) where
lift bv@(BV i) = sigE [| fromInteger# i |] (decBitVector (natVal bv))
decBitVector :: Integer -> TypeQ
decBitVector n = appT (conT ''BitVector) (litT $ numTyLit n)
instance (KnownNat n, KnownNat (n + 1), KnownNat (n + n)) =>
SaturatingNum (BitVector n) where
satPlus SatWrap a b = a +# b
satPlus w a b = case msb# r of
0 -> resize# r
_ -> case w of
SatZero -> minBound#
_ -> maxBound#
where
r = plus# a b
satMin SatWrap a b = a -# b
satMin _ a b = case msb# r of
0 -> resize# r
_ -> minBound#
where
r = minus# a b
satMult SatWrap a b = a *# b
satMult w a b = case rL of
0 -> rR
_ -> case w of
SatZero -> minBound#
_ -> maxBound#
where
r = times# a b
(rL,rR) = split# r
instance KnownNat n => Arbitrary (BitVector n) where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance KnownNat n => CoArbitrary (BitVector n) where
coarbitrary = coarbitraryIntegral
type instance Index (BitVector n) = Int
type instance IxValue (BitVector n) = Bit
instance KnownNat n => Ixed (BitVector n) where
ix i f bv = replaceBit# bv i <$> f (index# bv i)