Skip to content

Commit

Permalink
Avoid bang-patterns in Internal.Index
Browse files Browse the repository at this point in the history
It generates core that pattern matches on the constructor of the
Index data type. This can mess up certain parts of the Clash
compiler.
  • Loading branch information
christiaanb committed Nov 3, 2023
1 parent dcca851 commit 10ac261
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions clash-prelude/src/Clash/Sized/Internal/Index.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import {-# SOURCE #-} Clash.Sized.Internal.BitVector (BitVector (BV), high, low,
import qualified Clash.Sized.Internal.BitVector as BV
import Clash.Promoted.Nat (SNat(..), snatToNum, natToInteger, leToPlusKN)
import Clash.XException
(ShowX (..), NFDataX (..), errorX, showsPrecXWith, rwhnfX)
(ShowX (..), NFDataX (..), errorX, showsPrecXWith, rwhnfX, seqX)

{- $setup
>>> import Clash.Sized.Internal.Index
Expand Down Expand Up @@ -379,9 +379,9 @@ times# :: Index m -> Index n -> Index (((m - 1) * (n - 1)) + 1)
times# (I a) (I b) = I (a * b)

instance (KnownNat n, 1 <= n) => SaturatingNum (Index n) where
satAdd SatWrap !a !b =
satAdd SatWrap a b =
case natToInteger @n of
1 -> fromInteger# 0
1 -> a +# b
_ -> leToPlusKN @1 @n $
case plus# a b of
z | let m = fromInteger# (natToInteger @n)
Expand Down Expand Up @@ -419,9 +419,9 @@ instance (KnownNat n, 1 <= n) => SaturatingNum (Index n) where
then fromInteger# 0
else a -# b

satMul SatWrap !a !b =
satMul SatWrap a b =
case natToInteger @n of
1 -> fromInteger# 0
1 -> a *# b
2 -> case a of {0 -> 0; _ -> b}
_ -> leToPlusKN @1 @n $
case times# a b of
Expand All @@ -446,19 +446,19 @@ instance (KnownNat n, 1 <= n) => SaturatingNum (Index n) where
, z > m -> maxBound#
z -> resize# z

satSucc SatError !a =
satSucc SatError a =
case natToInteger @n of
1 -> errorX "Index.satSucc: overflow"
1 -> a `seqX` errorX "Index.satSucc: overflow"
_ -> satAdd SatError a $ fromInteger# 1
satSucc satMode !a =
case natToInteger @n of
1 -> fromInteger# 0
_ -> satAdd satMode a $ fromInteger# 1
{-# INLINE satSucc #-}

satPred SatError !a =
satPred SatError a =
case natToInteger @n of
1 -> errorX "Index.satPred: underflow"
1 -> a `seqX` errorX "Index.satPred: underflow"
_ -> satSub SatError a $ fromInteger# 1
satPred satMode !a =
case natToInteger @n of
Expand Down

0 comments on commit 10ac261

Please sign in to comment.