{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Safe #-}
{-# OPTIONS_HADDOCK show-extensions #-}
module Clash.Class.Num
(
ExtendingNum (..)
, SaturationMode (..)
, SaturatingNum (..)
, boundedAdd
, boundedSub
, boundedMul
)
where
class ExtendingNum a b where
type AResult a b
add :: a -> b -> AResult a b
sub :: a -> b -> AResult a b
type MResult a b
mul :: a -> b -> MResult a b
infixl 7 `mul`
infixl 6 `add`, `sub`
data SaturationMode
= SatWrap
| SatBound
| SatZero
| SatSymmetric
| SatError
deriving (Int -> SaturationMode -> ShowS
[SaturationMode] -> ShowS
SaturationMode -> String
(Int -> SaturationMode -> ShowS)
-> (SaturationMode -> String)
-> ([SaturationMode] -> ShowS)
-> Show SaturationMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SaturationMode] -> ShowS
$cshowList :: [SaturationMode] -> ShowS
show :: SaturationMode -> String
$cshow :: SaturationMode -> String
showsPrec :: Int -> SaturationMode -> ShowS
$cshowsPrec :: Int -> SaturationMode -> ShowS
Show, SaturationMode -> SaturationMode -> Bool
(SaturationMode -> SaturationMode -> Bool)
-> (SaturationMode -> SaturationMode -> Bool) -> Eq SaturationMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SaturationMode -> SaturationMode -> Bool
$c/= :: SaturationMode -> SaturationMode -> Bool
== :: SaturationMode -> SaturationMode -> Bool
$c== :: SaturationMode -> SaturationMode -> Bool
Eq, Int -> SaturationMode
SaturationMode -> Int
SaturationMode -> [SaturationMode]
SaturationMode -> SaturationMode
SaturationMode -> SaturationMode -> [SaturationMode]
SaturationMode
-> SaturationMode -> SaturationMode -> [SaturationMode]
(SaturationMode -> SaturationMode)
-> (SaturationMode -> SaturationMode)
-> (Int -> SaturationMode)
-> (SaturationMode -> Int)
-> (SaturationMode -> [SaturationMode])
-> (SaturationMode -> SaturationMode -> [SaturationMode])
-> (SaturationMode -> SaturationMode -> [SaturationMode])
-> (SaturationMode
-> SaturationMode -> SaturationMode -> [SaturationMode])
-> Enum SaturationMode
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: SaturationMode
-> SaturationMode -> SaturationMode -> [SaturationMode]
$cenumFromThenTo :: SaturationMode
-> SaturationMode -> SaturationMode -> [SaturationMode]
enumFromTo :: SaturationMode -> SaturationMode -> [SaturationMode]
$cenumFromTo :: SaturationMode -> SaturationMode -> [SaturationMode]
enumFromThen :: SaturationMode -> SaturationMode -> [SaturationMode]
$cenumFromThen :: SaturationMode -> SaturationMode -> [SaturationMode]
enumFrom :: SaturationMode -> [SaturationMode]
$cenumFrom :: SaturationMode -> [SaturationMode]
fromEnum :: SaturationMode -> Int
$cfromEnum :: SaturationMode -> Int
toEnum :: Int -> SaturationMode
$ctoEnum :: Int -> SaturationMode
pred :: SaturationMode -> SaturationMode
$cpred :: SaturationMode -> SaturationMode
succ :: SaturationMode -> SaturationMode
$csucc :: SaturationMode -> SaturationMode
Enum, SaturationMode
SaturationMode -> SaturationMode -> Bounded SaturationMode
forall a. a -> a -> Bounded a
maxBound :: SaturationMode
$cmaxBound :: SaturationMode
minBound :: SaturationMode
$cminBound :: SaturationMode
Bounded)
class (Bounded a, Num a) => SaturatingNum a where
satAdd :: SaturationMode -> a -> a -> a
satSub :: SaturationMode -> a -> a -> a
satMul :: SaturationMode -> a -> a -> a
satSucc :: SaturationMode -> a -> a
satSucc SaturationMode
s a
n = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satAdd SaturationMode
s a
n a
1
{-# INLINE satSucc #-}
satPred :: SaturationMode -> a -> a
satPred SaturationMode
s a
n = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satSub SaturationMode
s a
n a
1
{-# INLINE satPred #-}
infixl 7 `satMul`
infixl 6 `satAdd`, `satSub`
boundedAdd :: SaturatingNum a => a -> a -> a
boundedAdd :: a -> a -> a
boundedAdd = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satAdd SaturationMode
SatBound
{-# INLINE boundedAdd #-}
infixl 6 `boundedAdd`
boundedSub :: SaturatingNum a => a -> a -> a
boundedSub :: a -> a -> a
boundedSub = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satSub SaturationMode
SatBound
{-# INLINE boundedSub #-}
infixl 6 `boundedSub`
boundedMul :: SaturatingNum a => a -> a -> a
boundedMul :: a -> a -> a
boundedMul = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satMul SaturationMode
SatBound
{-# INLINE boundedMul #-}
infixl 7 `boundedMul`