Maintainer | Iago Abal <iago.abal@gmail.com> |
---|---|
Safe Haskell | Safe-Inferred |
Implementation of bit-vectors as wrappers over Integer
.
- Bit-vectors are interpreted as unsigned integers (i.e. natural numbers) except for some very specific cases.
- Bit-vectors are size-polymorphic insofar as most operations treat a bit-vector of size k as of size n for n >= k if required.
For documentation purposes we will write [n]k
to denote a bit-vector
of size n
representing the natural number k
.
- type BitVector = BV
- data BV
- size :: BV -> Int
- width :: BV -> Int
- nat :: BV -> Integer
- uint :: BV -> Integer
- int :: BV -> Integer
- bitVec :: Integral a => Int -> a -> BV
- ones :: Int -> BV
- zeros :: Int -> BV
- (==.) :: BV -> BV -> Bool
- (/=.) :: BV -> BV -> Bool
- (@.) :: BV -> Int -> Bool
- (@@) :: BV -> (Int, Int) -> BV
- (!.) :: BV -> Int -> Bool
- least :: Int -> BV -> BV
- most :: Int -> BV -> BV
- msb :: BV -> Bool
- lsb :: BV -> Bool
- msb1 :: BV -> Int
- sdiv :: BV -> BV -> BV
- srem :: BV -> BV -> BV
- smod :: BV -> BV -> BV
- lg2 :: BV -> BV
- (#) :: BV -> BV -> BV
- zeroExtend :: Int -> BV -> BV
- signExtend :: Int -> BV -> BV
- foldl_ :: (a -> Bool -> a) -> a -> BV -> a
- foldr_ :: (Bool -> a -> a) -> a -> BV -> a
- reverse_ :: BV -> BV
- replicate_ :: Int -> BV -> BV
- module Data.Bits
- not_ :: BV -> BV
- nand :: BV -> BV -> BV
- nor :: BV -> BV -> BV
- xnor :: BV -> BV -> BV
- (<<.) :: BV -> BV -> BV
- (>>.) :: BV -> BV -> BV
- ashr :: BV -> BV -> BV
- (<<<.) :: BV -> BV -> BV
- (>>>.) :: BV -> BV -> BV
- fromBits :: [Bool] -> BV
- toBits :: BV -> [Bool]
- maxNat :: Integral a => Int -> a
- integerWidth :: Integer -> Int
Bit-vectors
Big-endian pseudo size-polymorphic bit-vectors.
Creation
bitVec :: Integral a => Int -> a -> BVSource
Create a bit-vector given a size and an integer value.
>>>
bitVec 4 3
[4]3
This function also handles negative values.
>>>
bitVec 4 (-1)
[4]15
Comparison
(==.) :: BV -> BV -> BoolSource
Fixed-size equality.
In contrast with ==
, which is size-polymorphic, this equality
requires both bit-vectors to be of equal size.
>>>
[n]k ==. [m]k
False
>>>
[n]k == [n]k
True
Indexing
(@.) :: BV -> Int -> BoolSource
Bit indexing.
u @. i
stands for the i-th bit of u.
>>>
[4]2 @. 0
False
>>>
[4]2 @. 1
True
(@@) :: BV -> (Int, Int) -> BVSource
Bit-string extraction.
u @@ (j,i) == fromBits (map (u @.) [j,j-1..i])
>>>
[4]7 @@ (3,1)
[3]3
(!.) :: BV -> Int -> BoolSource
Reverse bit-indexing.
Index from the end of the sequenc
u !. i == u @. (size u - i - 1)
>>>
[3]3 !. 0
False
Most significant 1-bit.
Pre: input must be non-zero.
>>>
msb1 [4]2
1
>>>
msb1 [4]4
2
Arithmetic
List-like operations
zeroExtend :: Int -> BV -> BVSource
Logical extension.
>>>
zeroExtend 3 [1]1
[4]1
signExtend :: Int -> BV -> BVSource
Arithmetic extension.
>>>
signExtend 2 [2]1
[4]1
>>>
signExtend 2 [2]3
[4]15
foldl_ :: (a -> Bool -> a) -> a -> BV -> aSource
foldl_ f z (fromBits [un, ..., u1, u0]) == ((((z `f` un) `f` ...) `f` u1) `f` u0)
foldl_ f e = fromBits . foldl f e . toBits
foldr_ :: (Bool -> a -> a) -> a -> BV -> aSource
foldr_ f z (fromBits [un, ..., u1, u0]) == unf
(...f
(u1 `f` (u0 `f` z)))
foldr_ f e = fromBits . foldr f e . toBits
replicate_ :: Int -> BV -> BVSource
Pre: if replicate_ n u
then n > 0
must hold.
replicate_ n == fromBits . concat . replicate n . toBits
Bitwise operations
module Data.Bits
An alias for complement
.
Conversion
fromBits :: [Bool] -> BVSource
Create a bit-vector from a big-endian list of bits.
>>>
fromBits [False, False, True]
[3]1
Create a big-endian list of bits from a bit-vector.
>>>
toBits [4]11
[True, False, True, True]
Utilities
integerWidth :: Integer -> IntSource
Minimum width of a bit-vector to represent a given integer number.
>>>
integerWith 4
3
>>>
integerWith (-4)
4