commit a9d4855bedf548913fcfe1e4eaf6e5dca540f524
parent 65cbd0bdb805a024e6d2eed0abd5e8ace308c774
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 20 Dec 2025 18:49:34 -0330
lib: return choice instead of bool
Diffstat:
2 files changed, 12 insertions(+), 27 deletions(-)
diff --git a/lib/Data/Word/Wider.hs b/lib/Data/Word/Wider.hs
@@ -284,17 +284,12 @@ shr1_c# (# w0, w1, w2, w3 #) =
in (# (# r0, r1, r2, r3 #), C.from_word# w #)
{-# INLINE shr1_c# #-}
--- | Constant-time 1-bit shift-right with carry, indicating whether the
--- lowest bit was set.
---
--- >>> shr1_c 2
--- (1,False)
--- >>> shr1_c 1
--- (0,True)
-shr1_c :: Wider -> (Wider, Bool)
+-- | Constant-time 1-bit shift-right with carry, with a 'Choice'
+-- indicating whether the lowest bit was set.
+shr1_c :: Wider -> (# Wider, C.Choice #)
shr1_c (Wider w) =
let !(# r, c #) = shr1_c# w
- in (Wider r, C.decide c)
+ in (# Wider r, c #)
-- | Constant-time 1-bit shift-right.
--
@@ -324,17 +319,12 @@ shl1_c# (# w0, w1, w2, w3 #) =
in (# (# r0, r1, r2, r3 #), C.from_word# w #)
{-# INLINE shl1_c# #-}
--- | Constant-time 1-bit shift-left with carry, indicating whether the
--- highest bit was set.
---
--- >>> shl1_c 1
--- (2,False)
--- >>> shl1_c (2 ^ (255 :: Word))
--- (0,True)
-shl1_c :: Wider -> (Wider, Bool)
+-- | Constant-time 1-bit shift-left with carry, with a 'Choice' indicating
+-- whether the highest bit was set.
+shl1_c :: Wider -> (# Wider, C.Choice #)
shl1_c (Wider w) =
let !(# r, c #) = shl1_c# w
- in (Wider r, C.decide c)
+ in (# Wider r, c #)
-- | Constant-time 1-bit shift-left.
--
@@ -742,14 +732,9 @@ odd# :: (# Limb, Limb, Limb, Limb #) -> C.Choice
odd# (# Limb w, _, _, _ #) = C.from_word# (Exts.and# w 1##)
{-# INLINE odd# #-}
--- | Check if a 'Wider' is odd.
---
--- >>> odd 1
--- True
--- >>> odd 2
--- False
+-- | Check if a 'Wider' is odd, returning a 'Choice'.
odd
:: Wider
- -> Bool
-odd (Wider w) = C.decide (odd# w)
+ -> C.Choice
+odd (Wider w) = odd# w
diff --git a/test/Wider.hs b/test/Wider.hs
@@ -147,7 +147,7 @@ instance Q.Arbitrary W.Wider where
arbitrary = fmap W.to Q.arbitrary
odd_correct :: W.Wider -> Bool
-odd_correct w = W.odd w == I.integerTestBit (W.from w) 0
+odd_correct w = C.decide (W.odd w) == I.integerTestBit (W.from w) 0
tests :: TestTree
tests = testGroup "wider tests" [