Copyright | (C) 2013-2016 University of Twente |
---|---|
License | BSD2 (see the file LICENSE) |
Maintainer | Christiaan Baaij <christiaan.baaij@gmail.com> |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Documentation
data Unsigned (n :: Nat) Source #
Arbitrary-width unsigned integer represented by n
bits
Given n
bits, an Unsigned
n
number has a range of: [0 .. 2^n
-1]
- NB: The usual Haskell method of converting an integral numeric type to
another,
fromIntegral
, is not well suited for Clash as it will go throughInteger
which is arbitrarily bounded in HDL. Instead usebitCoerce
and theResize
class. - NB: The
Num
operators performwrap-around
on overflow. If you want saturation on overflow, check out theSaturatingNum
class.
>>>
maxBound :: Unsigned 3
7>>>
minBound :: Unsigned 3
0>>>
read (show (maxBound :: Unsigned 3)) :: Unsigned 3
7>>>
1 + 2 :: Unsigned 3
3>>>
2 + 6 :: Unsigned 3
0>>>
1 - 3 :: Unsigned 3
6>>>
2 * 3 :: Unsigned 3
6>>>
2 * 4 :: Unsigned 3
0>>>
(2 :: Unsigned 3) `mul` (4 :: Unsigned 3) :: Unsigned 6
8>>>
(2 :: Unsigned 3) `add` (6 :: Unsigned 3) :: Unsigned 4
8>>>
satAdd SatSymmetric 2 6 :: Unsigned 3
7>>>
satSub SatSymmetric 2 3 :: Unsigned 3
0
Unsigned has the type role
>>>
:i Unsigned
type role Unsigned nominal ...
as it is not safe to coerce between different width Unsigned. To change the
width, use the functions in the Resize
class.