{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Safe #-}
{-# OPTIONS_HADDOCK show-extensions #-}
module Clash.Class.Num
(
ExtendingNum (..)
, SaturationMode (..)
, SaturatingNum (..)
, boundedAdd
, boundedSub
, boundedMul
, satSucc
, satPred
)
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
data SaturationMode
= SatWrap
| SatBound
| SatZero
| SatSymmetric
deriving 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
class (Bounded a, Num a) => SaturatingNum a where
satAdd :: SaturationMode -> a -> a -> a
satSub :: SaturationMode -> a -> a -> a
satMul :: SaturationMode -> a -> a -> a
satSucc :: SaturatingNum a => SaturationMode -> a -> a
satSucc :: SaturationMode -> a -> a
satSucc s :: SaturationMode
s n :: a
n = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satAdd SaturationMode
s a
n 1
{-# INLINE satSucc #-}
satPred :: SaturatingNum a => SaturationMode -> a -> a
satPred :: SaturationMode -> a -> a
satPred s :: SaturationMode
s n :: a
n = SaturationMode -> a -> a -> a
forall a. SaturatingNum a => SaturationMode -> a -> a -> a
satSub SaturationMode
s a
n 1
{-# INLINE satPred #-}
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 #-}
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 #-}
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 #-}