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 Signed (n :: Nat) Source #
Arbitrary-width signed integer represented by n
bits, including the sign
bit
Uses standard 2-complements representation. Meaning that, given n
bits,
a Signed
n
number has a range of: [-(2^(n
-1)) .. 2^(n
-1)-1] for
n > 0
. When n = 0
, both the min and max bound are 0.
- 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 :: Signed 3
3>>>
minBound :: Signed 3
-4>>>
read (show (minBound :: Signed 3)) :: Signed 3
-4>>>
1 + 2 :: Signed 3
3>>>
2 + 3 :: Signed 3
-3>>>
(-2) + (-3) :: Signed 3
3>>>
2 * 3 :: Signed 4
6>>>
2 * 4 :: Signed 4
-8>>>
(2 :: Signed 3) `mul` (4 :: Signed 4) :: Signed 7
8>>>
(2 :: Signed 3) `add` (3 :: Signed 3) :: Signed 4
5>>>
(-2 :: Signed 3) `add` (-3 :: Signed 3) :: Signed 4
-5>>>
satAdd SatSymmetric 2 3 :: Signed 3
3>>>
satAdd SatSymmetric (-2) (-3) :: Signed 3
-3
Signed has the type role
>>>
:i Signed
type role Signed nominal ...
as it is not safe to coerce between different width Signed. To change the
width, use the functions in the Resize
class.