commit 1e28378cf2fe8b311e334df04276d843719d4a15
parent c739c2264ee45658bf329aed9a8a3dcb0b8ca2ab
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 7 Dec 2025 07:09:22 +0400
lib: add neg to wide
Diffstat:
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/lib/Data/Word/Wide.hs b/lib/Data/Word/Wide.hs
@@ -25,9 +25,13 @@ module Data.Word.Wide (
-- * Bit Manipulation
, or
+ , or#
, and
+ , and#
, xor
+ , xor#
, not
+ , not#
-- * Comparison
, eq_vartime
@@ -37,6 +41,7 @@ module Data.Word.Wide (
, add_o
, sub
, mul
+ , neg
-- * Unboxed Arithmetic
, add_o#
@@ -44,6 +49,7 @@ module Data.Word.Wide (
, sub_b#
, sub_w#
, mul_w#
+ , neg#
) where
import Control.DeepSeq
@@ -68,15 +74,13 @@ data Wide = Wide !(# Limb, Limb #)
instance Show Wide where
show = show . from
--- XX add neg
-
instance Num Wide where
(+) = add
(-) = sub
(*) = mul
abs = id
fromInteger = to
- negate w = add (not w) (Wide (# Limb 1##, Limb 0## #))
+ negate = neg
signum a = case a of
Wide (# Limb 0##, Limb 0## #) -> 0
_ -> 1
@@ -143,6 +147,19 @@ not :: Wide -> Wide
not (Wide w) = Wide (not_w# w)
{-# INLINE not #-}
+-- negation -------------------------------------------------------------------
+
+neg#
+ :: (# Limb, Limb #) -- ^ argument
+ -> (# Limb, Limb #) -- ^ (wrapping) additive inverse
+neg# w = add_w# (not_w# w) (# Limb 1##, Limb 0## #)
+{-# INLINE neg# #-}
+
+neg
+ :: Wide -- ^ argument
+ -> Wide -- ^ (wrapping) additive inverse
+neg (Wide w) = Wide (neg# w)
+
-- addition, subtraction ------------------------------------------------------
-- | Overflowing addition, computing 'a + b', returning the sum and a