Skip to content

Commit 93ceb2e

Browse files
Add SaturatingNum and Bounded for Signal dom a (#3137)
Closes #2931 Co-authored-by: Martijn Bastiaan <martijn@qbaylogic.com>
1 parent 4f77fac commit 93ceb2e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADDED: `SaturatingNum` and `Bounded` instances for `Signal dom a`. The `sat` family (`satAdd`, `satSucc`, ...) can now be used on `Signal`s and `{min,max}Bound` can be used to construct them [#2931](github.com/clash-lang/clash-compiler/issues/2931)

clash-prelude/src/Clash/Signal/Internal.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ import System.IO.Unsafe (unsafeInterleaveIO, unsafePerformIO)
211211
import Test.QuickCheck (Arbitrary (..), CoArbitrary(..), Property,
212212
property)
213213

214+
import Clash.Class.Num (SaturatingNum(..))
214215
import Clash.CPP (fStrictMapSignal)
215216
import Clash.NamedTypes
216217
import Clash.Promoted.Nat (SNat (..), snatToNum, snatToNatural)
@@ -910,6 +911,17 @@ instance Num a => Num (Signal dom a) where
910911
signum = fmap signum
911912
fromInteger = signal# . fromInteger
912913

914+
instance Bounded a => Bounded (Signal dom a) where
915+
minBound = pure minBound
916+
maxBound = pure maxBound
917+
918+
instance SaturatingNum a => SaturatingNum (Signal dom a) where
919+
satAdd mode = liftA2 (satAdd mode)
920+
satSub mode = liftA2 (satSub mode)
921+
satMul mode = liftA2 (satMul mode)
922+
satSucc mode = fmap (satSucc mode)
923+
satPred mode = fmap (satPred mode)
924+
913925
-- | __NB__: Not synthesizable
914926
--
915927
-- __NB__: In \"@'foldr' f z s@\":

clash-prelude/tests/Clash/Tests/Signal.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ tests =
6161
in
6262
evaluate a >> pure ()
6363
]
64+
, testGroup "SaturatingNum"
65+
[ testCase "satSucc SatWrap" case_satSuccSatWrap
66+
, testCase "satSucc SatBound" case_satSuccSatBound
67+
, testCase "satPred SatWrap" case_satPredSatWrap
68+
, testCase "satPred SatBound" case_satPredSatBound
69+
]
6470
, testGroup "unsafeSynchronizer"
6571
[ testCase "case_dynamicStaticEq" case_dynamicStaticEq
6672
, testCase "case_dynamicHasEffect" case_dynamicHasEffect
@@ -180,3 +186,31 @@ case_F6_F1 = do
180186
, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
181187
, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6
182188
]
189+
190+
-- | Test that satSucc SatWrap works on signals
191+
case_satSuccSatWrap :: Assertion
192+
case_satSuccSatWrap = do
193+
let s = fromList [255, 254] :: Signal System (Unsigned 8)
194+
result = satSucc SatWrap s
195+
sampleN @System 2 result @?= [0, 255]
196+
197+
-- | Test that satSucc SatBound works on signals
198+
case_satSuccSatBound :: Assertion
199+
case_satSuccSatBound = do
200+
let s = fromList [255, 254] :: Signal System (Unsigned 8)
201+
result = satSucc SatBound s
202+
sampleN @System 2 result @?= [255, 255]
203+
204+
-- | Test that satPred SatWrap works on signals
205+
case_satPredSatWrap :: Assertion
206+
case_satPredSatWrap = do
207+
let s = fromList [0, 1] :: Signal System (Unsigned 8)
208+
result = satPred SatWrap s
209+
sampleN @System 2 result @?= [255, 0]
210+
211+
-- | Test that satPred SatBound works on signals
212+
case_satPredSatBound :: Assertion
213+
case_satPredSatBound = do
214+
let s = fromList [0, 1] :: Signal System (Unsigned 8)
215+
result = satPred SatBound s
216+
sampleN @System 2 result @?= [0, 0]

0 commit comments

Comments
 (0)