commit af405307c1a52c233e87c5b73773fbca0ae04146
parent 87c28f17c8d5ca7e73f44b4f47de2f10c92d6562
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 30 Nov 2025 13:10:45 +0400
wide: add boxed add_c
Diffstat:
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/lib/Data/Word/Wide.hs b/lib/Data/Word/Wide.hs
@@ -21,8 +21,12 @@ module Data.Word.Wide (
, xor
, not
+ -- * Comparison
+ , eq_vartime
+
-- * Arithmetic
, add
+ , add_c
, sub
, mul
@@ -91,6 +95,12 @@ from (Wide (# Limb a, Limb b #)) =
fi (W# b) .<<. (B.finiteBitSize (0 :: Word))
.|. fi (W# a)
+-- comparison -----------------------------------------------------------------
+
+eq_vartime :: Wide -> Wide -> Bool
+eq_vartime (Wide (# Limb a0, Limb b0 #)) (Wide (# Limb a1, Limb b1 #)) =
+ isTrue# (andI# (eqWord# a0 a1) (eqWord# b0 b1))
+
-- bits -----------------------------------------------------------------------
or_w# :: (# Limb, Limb #) -> (# Limb, Limb #) -> (# Limb, Limb #)
@@ -129,13 +139,23 @@ not (Wide w) = Wide (not_w# w)
add_c#
:: (# Limb, Limb #) -- ^ augend
-> (# Limb, Limb #) -- ^ addend
- -> (# (# Limb, Limb #), Limb #) -- ^ (# sum, carry bit #)
+ -> (# (# Limb, Limb #), Limb #) -- ^ (# sum, carry bit #)
add_c# (# a0, a1 #) (# b0, b1 #) =
let !(# s0, c0 #) = L.add_o# a0 b0
!(# s1, c1 #) = L.add_c# a1 b1 c0
in (# (# s0, s1 #), c1 #)
{-# INLINE add_c# #-}
+-- | Overflowing addition on 'Wide' words, computing 'a + b', returning
+-- the sum and carry.
+add_c
+ :: Wide -- ^ augend
+ -> Wide -- ^ addend
+ -> (Wide, Word) -- ^ (sum, carry)
+add_c (Wide a) (Wide b) =
+ let !(# s, Limb c #) = add_c# a b
+ in (Wide s, W# c)
+
-- | Wrapping addition, computing 'a + b'.
add_w#
:: (# Limb, Limb #) -- ^ augend