ppad-fixed-0.1.3: Large fixed-width words and constant-time arithmetic.
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Data.Word.Limb

Description

The primitive Limb type, as well as operations on it.

All operations run in constant time with respect to inputs, unless specifically indicated otherwise.

Synopsis

Limb

newtype Limb :: TYPE 'WordRep Source #

A Limb is the smallest component of a wider word.

Constructors

Limb Word# 

render :: Limb -> String Source #

Return a Limb value as a String.

Bit manipulation and representation

and# Source #

Arguments

:: Limb

a

-> Limb

b

-> Limb

a & b

Bitwise and.

or# Source #

Arguments

:: Limb

a

-> Limb

b

-> Limb

a | b

Bitwise or.

not# Source #

Arguments

:: Limb

a

-> Limb

not a

Bitwise not.

xor# Source #

Arguments

:: Limb

a

-> Limb

b

-> Limb

a ^ b

Bitwise exclusive or.

bits# Source #

Arguments

:: Limb

limb

-> Int

bits required to represent limb

Number of bits required to represent this limb.

shl# Source #

Arguments

:: Limb

limb

-> Int#

shift amount

-> Limb

result

Unchecked bit-shift left.

shl1# :: Limb -> (# Limb, Limb #) Source #

Unchecked bit-shift left by 1, returning the result and carry.

shr# Source #

Arguments

:: Limb

limb

-> Int#

shift amount

-> Limb

result

Unchecked logical bit-shift right.

shr1# :: Limb -> (# Limb, Limb #) Source #

Unchecked logical bit-shift right by 1, returning the result and carry.

Comparison

eq# :: Limb -> Limb -> Choice Source #

Constant-time equality comparison.

ne# :: Limb -> Limb -> Choice Source #

Constant-time inequality comparison.

eq_vartime# :: Limb -> Limb -> Bool Source #

Variable-time equality comparison.

ne_vartime# :: Limb -> Limb -> Bool Source #

Variable-time inequality comparison.

nonzero# :: Limb -> Choice Source #

Constant-time comparison to zero.

lt# :: Limb -> Limb -> Choice Source #

Constant-time less than comparison.

gt# :: Limb -> Limb -> Choice Source #

Constant-time greater than comparison.

Selection

select# Source #

Arguments

:: Limb

a

-> Limb

b

-> Choice

c

-> Limb

result

Return a if c is truthy, otherwise return b.

cswap# Source #

Arguments

:: Limb

a

-> Limb

b

-> Choice

c

-> (# Limb, Limb #)

result

Return (# b, a #) if c is truthy, otherwise return (# a, b #).

Negation

neg# :: Limb -> Limb Source #

Wrapping (two's complement) negation.

Arithmetic

add_o# Source #

Arguments

:: Limb

augend

-> Limb

addend

-> (# Limb, Limb #)

(# sum, carry #)

Overflowing addition, computing augend + addend, returning the sum and carry.

add_c# Source #

Arguments

:: Limb

augend

-> Limb

addend

-> Limb

carry

-> (# Limb, Limb #)

(# sum, new carry #)

Carrying addition, computing augend + addend + carry, returning the sum and new carry.

add_w# Source #

Arguments

:: Limb

augend

-> Limb

addend

-> Limb

sum

Wrapping addition, computing augend + addend, returning the sum (discarding overflow).

add_s# :: Limb -> Limb -> Limb Source #

Saturating addition, computing augend + addend, returning the sum (clamping to the maximum representable value in the case of overflow).

sub_b# Source #

Arguments

:: Limb

minuend

-> Limb

subtrahend

-> Limb

borrow

-> (# Limb, Limb #)

(# difference, new borrow #)

Borrowing subtraction, computing minuend - (subtrahend + borrow), returning the difference and new borrow mask.

sub_w# Source #

Arguments

:: Limb

minuend

-> Limb

subtrahend

-> Limb

difference

Wrapping subtraction, computing minuend - subtrahend, returning the difference (and discarding underflow).

sub_s# Source #

Arguments

:: Limb

minuend

-> Limb

subtrahend

-> Limb

difference

Saturating subtraction, computing minuend - subtrahend, returning the difference (and clamping to zero in the case of underflow).

mul_c# Source #

Arguments

:: Limb

multiplicand

-> Limb

multiplier

-> (# Limb, Limb #)

(# low, high #) product

Widening multiplication, returning low and high words of the product.

mul_w# Source #

Arguments

:: Limb

multiplicand

-> Limb

multiplier

-> Limb

low word of product

Wrapping multiplication, returning only the low word of the product.

mul_s# Source #

Arguments

:: Limb

multiplicand

-> Limb

multiplier

-> Limb

clamped low word of product

Saturating multiplication, returning only the low word of the product, and clamping to the maximum value in the case of overflow.

mac# Source #

Arguments

:: Limb

a (multiplicand)

-> Limb

b (multiplier)

-> Limb

m (addend)

-> Limb

c (carry)

-> (# Limb, Limb #)

a * b + m + c

Multiply-add-carry, computing a * b + m + c, returning the result along with the new carry.

Re-exported

data Word #

A Word is an unsigned integral type, with the same size as Int.

Constructors

W# Word# 

Instances

Instances details
Data Word

Since: base-4.0.0.0

Instance details

Defined in Data.Data

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Word -> c Word #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Word #

toConstr :: Word -> Constr #

dataTypeOf :: Word -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Word) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Word) #

gmapT :: (forall b. Data b => b -> b) -> Word -> Word #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Word -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Word -> r #

gmapQ :: (forall d. Data d => d -> u) -> Word -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Word -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Word -> m Word #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Word -> m Word #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Word -> m Word #

Storable Word

Since: base-2.1

Instance details

Defined in Foreign.Storable

Methods

sizeOf :: Word -> Int #

alignment :: Word -> Int #

peekElemOff :: Ptr Word -> Int -> IO Word #

pokeElemOff :: Ptr Word -> Int -> Word -> IO () #

peekByteOff :: Ptr b -> Int -> IO Word #

pokeByteOff :: Ptr b -> Int -> Word -> IO () #

peek :: Ptr Word -> IO Word #

poke :: Ptr Word -> Word -> IO () #

Bits Word

Since: base-2.1

Instance details

Defined in GHC.Bits

FiniteBits Word

Since: base-4.6.0.0

Instance details

Defined in GHC.Bits

Bounded Word

Since: base-2.1

Instance details

Defined in GHC.Enum

Enum Word

Since: base-2.1

Instance details

Defined in GHC.Enum

Methods

succ :: Word -> Word #

pred :: Word -> Word #

toEnum :: Int -> Word #

fromEnum :: Word -> Int #

enumFrom :: Word -> [Word] #

enumFromThen :: Word -> Word -> [Word] #

enumFromTo :: Word -> Word -> [Word] #

enumFromThenTo :: Word -> Word -> Word -> [Word] #

Ix Word

Since: base-4.6.0.0

Instance details

Defined in GHC.Ix

Methods

range :: (Word, Word) -> [Word] #

index :: (Word, Word) -> Word -> Int #

unsafeIndex :: (Word, Word) -> Word -> Int #

inRange :: (Word, Word) -> Word -> Bool #

rangeSize :: (Word, Word) -> Int #

unsafeRangeSize :: (Word, Word) -> Int #

Num Word

Since: base-2.1

Instance details

Defined in GHC.Num

Methods

(+) :: Word -> Word -> Word #

(-) :: Word -> Word -> Word #

(*) :: Word -> Word -> Word #

negate :: Word -> Word #

abs :: Word -> Word #

signum :: Word -> Word #

fromInteger :: Integer -> Word #

Read Word

Since: base-4.5.0.0

Instance details

Defined in GHC.Read

Integral Word

Since: base-2.1

Instance details

Defined in GHC.Real

Methods

quot :: Word -> Word -> Word #

rem :: Word -> Word -> Word #

div :: Word -> Word -> Word #

mod :: Word -> Word -> Word #

quotRem :: Word -> Word -> (Word, Word) #

divMod :: Word -> Word -> (Word, Word) #

toInteger :: Word -> Integer #

Real Word

Since: base-2.1

Instance details

Defined in GHC.Real

Methods

toRational :: Word -> Rational #

Show Word

Since: base-2.1

Instance details

Defined in GHC.Show

Methods

showsPrec :: Int -> Word -> ShowS #

show :: Word -> String #

showList :: [Word] -> ShowS #

PrintfArg Word

Since: base-2.1

Instance details

Defined in Text.Printf

NFData Word 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word -> () #

Eq Word 
Instance details

Defined in GHC.Classes

Methods

(==) :: Word -> Word -> Bool #

(/=) :: Word -> Word -> Bool #

Ord Word 
Instance details

Defined in GHC.Classes

Methods

compare :: Word -> Word -> Ordering #

(<) :: Word -> Word -> Bool #

(<=) :: Word -> Word -> Bool #

(>) :: Word -> Word -> Bool #

(>=) :: Word -> Word -> Bool #

max :: Word -> Word -> Word #

min :: Word -> Word -> Word #

Generic1 (URec Word :: k -> Type) 
Instance details

Defined in GHC.Generics

Associated Types

type Rep1 (URec Word :: k -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

type Rep1 (URec Word :: k -> Type) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UWord" 'PrefixI 'True) (S1 ('MetaSel ('Just "uWord#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UWord :: k -> Type)))

Methods

from1 :: forall (a :: k). URec Word a -> Rep1 (URec Word :: k -> Type) a #

to1 :: forall (a :: k). Rep1 (URec Word :: k -> Type) a -> URec Word a #

Foldable (UWord :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in Data.Foldable

Methods

fold :: Monoid m => UWord m -> m #

foldMap :: Monoid m => (a -> m) -> UWord a -> m #

foldMap' :: Monoid m => (a -> m) -> UWord a -> m #

foldr :: (a -> b -> b) -> b -> UWord a -> b #

foldr' :: (a -> b -> b) -> b -> UWord a -> b #

foldl :: (b -> a -> b) -> b -> UWord a -> b #

foldl' :: (b -> a -> b) -> b -> UWord a -> b #

foldr1 :: (a -> a -> a) -> UWord a -> a #

foldl1 :: (a -> a -> a) -> UWord a -> a #

toList :: UWord a -> [a] #

null :: UWord a -> Bool #

length :: UWord a -> Int #

elem :: Eq a => a -> UWord a -> Bool #

maximum :: Ord a => UWord a -> a #

minimum :: Ord a => UWord a -> a #

sum :: Num a => UWord a -> a #

product :: Num a => UWord a -> a #

Traversable (UWord :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in Data.Traversable

Methods

traverse :: Applicative f => (a -> f b) -> UWord a -> f (UWord b) #

sequenceA :: Applicative f => UWord (f a) -> f (UWord a) #

mapM :: Monad m => (a -> m b) -> UWord a -> m (UWord b) #

sequence :: Monad m => UWord (m a) -> m (UWord a) #

Functor (URec Word :: Type -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

fmap :: (a -> b) -> URec Word a -> URec Word b #

(<$) :: a -> URec Word b -> URec Word a #

Generic (URec Word p) 
Instance details

Defined in GHC.Generics

Associated Types

type Rep (URec Word p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

type Rep (URec Word p) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UWord" 'PrefixI 'True) (S1 ('MetaSel ('Just "uWord#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UWord :: Type -> Type)))

Methods

from :: URec Word p -> Rep (URec Word p) x #

to :: Rep (URec Word p) x -> URec Word p #

Show (URec Word p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

showsPrec :: Int -> URec Word p -> ShowS #

show :: URec Word p -> String #

showList :: [URec Word p] -> ShowS #

Eq (URec Word p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

(==) :: URec Word p -> URec Word p -> Bool #

(/=) :: URec Word p -> URec Word p -> Bool #

Ord (URec Word p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

Methods

compare :: URec Word p -> URec Word p -> Ordering #

(<) :: URec Word p -> URec Word p -> Bool #

(<=) :: URec Word p -> URec Word p -> Bool #

(>) :: URec Word p -> URec Word p -> Bool #

(>=) :: URec Word p -> URec Word p -> Bool #

max :: URec Word p -> URec Word p -> URec Word p #

min :: URec Word p -> URec Word p -> URec Word p #

data URec Word (p :: k)

Used for marking occurrences of Word#

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

data URec Word (p :: k) = UWord {}
type Rep1 (URec Word :: k -> Type)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

type Rep1 (URec Word :: k -> Type) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UWord" 'PrefixI 'True) (S1 ('MetaSel ('Just "uWord#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UWord :: k -> Type)))
type Rep (URec Word p)

Since: base-4.9.0.0

Instance details

Defined in GHC.Generics

type Rep (URec Word p) = D1 ('MetaData "URec" "GHC.Generics" "base" 'False) (C1 ('MetaCons "UWord" 'PrefixI 'True) (S1 ('MetaSel ('Just "uWord#") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (UWord :: Type -> Type)))