-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Integer library based on GMP -- -- This package provides the low-level implementation of the standard -- Integer type based on the GNU Multiple Precision Arithmetic -- Library (GMP). -- -- This package provides access to the internal representation of -- Integer as well as primitive operations with no proper error -- handling, and should only be used directly with the utmost care. @package integer-gmp @version 1.0.3.0 module GHC.Integer.Logarithms -- | Compute base-2 log of Word# -- -- This is internally implemented as count-leading-zeros machine -- instruction. wordLog2# :: Word# -> Int# -- | Calculate the integer base 2 logarithm of an Integer. The -- calculation is more efficient than for the general case, on platforms -- with 32- or 64-bit words much more efficient. -- -- The argument must be strictly positive, that condition is not -- checked. integerLog2# :: Integer -> Int# -- | Calculate the integer logarithm for an arbitrary base. -- -- The base must be greater than 1, the second argument, the -- number whose logarithm is sought, shall be positive, otherwise the -- result is meaningless. -- -- The following property holds -- --
-- base ^ integerLogBase# base m <= m < base ^(integerLogBase# base m + 1) ---- -- for base > 1 and m > 0. -- -- Note: Internally uses integerLog2# for base 2 integerLogBase# :: Integer -> Integer -> Int# -- | Fast Integer logarithms to base 2. integerLog2# and -- wordLog2# are of general usefulness, the others are only needed -- for a fast implementation of fromRational. Since they are -- needed in GHC.Float, we must expose this module, but it should -- not show up in the docs. -- -- See https://gitlab.haskell.org/ghc/ghc/issues/5122 for the -- origin of the code in this module module GHC.Integer.Logarithms.Internals -- | Compute base-2 log of Word# -- -- This is internally implemented as count-leading-zeros machine -- instruction. wordLog2# :: Word# -> Int# -- | Extended version of integerLog2# -- -- Assumption: Integer is strictly positive -- -- First component of result is log2 n, second is 0# -- iff n is a power of two. integerLog2IsPowerOf2# :: Integer -> (# Int#, Int# #) -- | Calculate the integer base 2 logarithm of an Integer. The -- calculation is more efficient than for the general case, on platforms -- with 32- or 64-bit words much more efficient. -- -- The argument must be strictly positive, that condition is not -- checked. integerLog2# :: Integer -> Int# roundingMode# :: Integer -> Int# -> Int# -- | The Integer type. -- -- This module exposes the portable Integer API. See -- GHC.Integer.GMP.Internals for the integer-gmp-specific -- internal representation of Integer as well as optimized -- GMP-specific operations. module GHC.Integer -- | Arbitrary precision integers. In contrast with fixed-size integral -- types such as Int, the Integer type represents the -- entire infinite range of integers. -- -- For more information about this type's representation, see the -- comments in its implementation. data Integer -- | Construct Integer value from list of Ints. -- -- This function is used by GHC for constructing Integer literals. mkInteger :: Bool -> [Int] -> Integer -- | Should rather be called intToInteger smallInteger :: Int# -> Integer wordToInteger :: Word# -> Integer integerToWord :: Integer -> Word# -- | Truncates Integer to least-significant Int# integerToInt :: Integer -> Int# encodeFloatInteger :: Integer -> Int# -> Float# floatFromInteger :: Integer -> Float# encodeDoubleInteger :: Integer -> Int# -> Double# decodeDoubleInteger :: Double# -> (# Integer, Int# #) doubleFromInteger :: Integer -> Double# -- | Add two Integers plusInteger :: Integer -> Integer -> Integer -- | Subtract one Integer from another. minusInteger :: Integer -> Integer -> Integer -- | Multiply two Integers timesInteger :: Integer -> Integer -> Integer -- | Negate Integer negateInteger :: Integer -> Integer -- | Compute absolute value of an Integer absInteger :: Integer -> Integer -- | Return -1, 0, and 1 depending on whether -- argument is negative, zero, or positive, respectively signumInteger :: Integer -> Integer -- | Simultaneous divInteger and modInteger. -- -- Divisor must be non-zero otherwise the GHC runtime will terminate with -- a division-by-zero fault. divModInteger :: Integer -> Integer -> (# Integer, Integer #) divInteger :: Integer -> Integer -> Integer modInteger :: Integer -> Integer -> Integer -- | Simultaneous quotInteger and remInteger. -- -- Divisor must be non-zero otherwise the GHC runtime will terminate with -- a division-by-zero fault. quotRemInteger :: Integer -> Integer -> (# Integer, Integer #) quotInteger :: Integer -> Integer -> Integer remInteger :: Integer -> Integer -> Integer eqInteger :: Integer -> Integer -> Bool -- | Not-equal predicate. neqInteger :: Integer -> Integer -> Bool leInteger :: Integer -> Integer -> Bool gtInteger :: Integer -> Integer -> Bool ltInteger :: Integer -> Integer -> Bool geInteger :: Integer -> Integer -> Bool compareInteger :: Integer -> Integer -> Ordering eqInteger# :: Integer -> Integer -> Int# neqInteger# :: Integer -> Integer -> Int# leInteger# :: Integer -> Integer -> Int# gtInteger# :: Integer -> Integer -> Int# ltInteger# :: Integer -> Integer -> Int# geInteger# :: Integer -> Integer -> Int# -- | Bitwise AND operation andInteger :: Integer -> Integer -> Integer -- | Bitwise OR operation orInteger :: Integer -> Integer -> Integer -- | Bitwise XOR operation xorInteger :: Integer -> Integer -> Integer -- | Bitwise NOT operation complementInteger :: Integer -> Integer -- | Shift-left operation -- -- Even though the shift-amount is expressed as Int#, the result -- is undefined for negative shift-amounts. shiftLInteger :: Integer -> Int# -> Integer -- | Arithmetic shift-right operation -- -- Even though the shift-amount is expressed as Int#, the result -- is undefined for negative shift-amounts. shiftRInteger :: Integer -> Int# -> Integer -- | Test if n-th bit is set. testBitInteger :: Integer -> Int# -> Bool -- | Count number of set bits. For negative arguments returns negative -- population count of negated argument. popCountInteger :: Integer -> Int# -- | Integer for which only n-th bit is set. Undefined -- behaviour for negative n values. bitInteger :: Int# -> Integer hashInteger :: Integer -> Int# -- | This modules provides access to the Integer constructors and -- exposes some highly optimized GMP-operations. -- -- Note that since integer-gmp does not depend on base, -- error reporting via exceptions, error, or undefined -- is not available. Instead, the low-level functions will crash the -- runtime if called with invalid arguments. -- -- See also GHC Commentary: Libraries/Integer. module GHC.Integer.GMP.Internals -- | Arbitrary precision integers. In contrast with fixed-size integral -- types such as Int, the Integer type represents the -- entire infinite range of integers. -- -- For more information about this type's representation, see the -- comments in its implementation. data Integer -- | iff value in [minBound::Int, maxBound::Int] -- range S# :: !Int# -> Integer -- | iff value in ]maxBound::Int, +inf[ range Jp# :: {-# UNPACK #-} !BigNat -> Integer -- | iff value in ]-inf, minBound::Int[ range Jn# :: {-# UNPACK #-} !BigNat -> Integer -- | Test whether all internal invariants are satisfied by Integer -- value -- -- Returns 1# if valid, 0# otherwise. -- -- This operation is mostly useful for test-suites and/or code which -- constructs Integer values directly. isValidInteger# :: Integer -> Int# -- | Compute greatest common divisor. gcdInteger :: Integer -> Integer -> Integer -- | Extended euclidean algorithm. -- -- For a and b, compute their greatest -- common divisor g and the coefficient s -- satisfying as + bt = g. gcdExtInteger :: Integer -> Integer -> (# Integer, Integer #) -- | Compute least common multiple. lcmInteger :: Integer -> Integer -> Integer -- | Square Integer sqrInteger :: Integer -> Integer -- | "powModInteger b e m" computes -- base b raised to exponent e modulo -- abs(m). -- -- Negative exponents are supported if an inverse modulo -- m exists. -- -- Warning: It's advised to avoid calling this primitive with -- negative exponents unless it is guaranteed the inverse exists, as -- failure to do so will likely cause program abortion due to a -- divide-by-zero fault. See also recipModInteger. -- -- Future versions of integer_gmp may not support negative -- e values anymore. powModInteger :: Integer -> Integer -> Integer -> Integer -- | "powModSecInteger b e m" computes -- base b raised to exponent e modulo -- m. It is required that e >= 0 and -- m is odd. -- -- This is a "secure" variant of powModInteger using the -- mpz_powm_sec() function which is designed to be resilient to -- side channel attacks and is therefore intended for cryptographic -- applications. -- -- This primitive is only available when the underlying GMP library -- supports it (GMP >= 5). Otherwise, it internally falls back to -- powModInteger, and a warning will be emitted when -- used. powModSecInteger :: Integer -> Integer -> Integer -> Integer -- | "recipModInteger x m" computes the -- inverse of x modulo m. If the inverse -- exists, the return value y will satisfy 0 < -- y < abs(m), otherwise the result is 0. recipModInteger :: Integer -> Integer -> Integer wordToNegInteger :: Word# -> Integer bigNatToInteger :: BigNat -> Integer bigNatToNegInteger :: BigNat -> Integer -- | Type representing raw arbitrary-precision Naturals -- -- This is common type used by Natural and Integer. As -- this type consists of a single constructor wrapping a -- ByteArray# it can be unpacked. -- -- Essential invariants: -- --
-- bitBigNat = shiftLBigNat (wordToBigNat 1##) ---- -- avoiding a few redundant allocations bitBigNat :: Int# -> BigNat -- | Test if BigNat value is equal to zero. isZeroBigNat :: BigNat -> Bool -- | Test for special 0-sized BigNat representing underflows. isNullBigNat# :: BigNat -> Int# compareBigNatWord :: BigNat -> GmpLimb# -> Ordering compareBigNat :: BigNat -> BigNat -> Ordering eqBigNatWord :: BigNat -> GmpLimb# -> Bool eqBigNatWord# :: BigNat -> GmpLimb# -> Int# eqBigNat :: BigNat -> BigNat -> Bool eqBigNat# :: BigNat -> BigNat -> Int# gtBigNatWord# :: BigNat -> GmpLimb# -> Int# -- | Compute greatest common divisor. -- -- Warning: result may become negative if (at least) one argument -- is minBound gcdInt :: Int# -> Int# -> Int# -- | Compute greatest common divisor. gcdWord :: Word# -> Word# -> Word# -- | Version of powModInteger operating on Word#s powModWord :: GmpLimb# -> GmpLimb# -> GmpLimb# -> GmpLimb# -- | Version of recipModInteger operating on Word#s recipModWord :: GmpLimb# -> GmpLimb# -> GmpLimb# -- | Probalistic Miller-Rabin primality test. -- -- "testPrimeInteger n k" determines -- whether n is prime and returns one of the following -- results: -- --
-- exportIntegerToAddr i addr e ---- -- See description of exportIntegerToMutableByteArray for more -- details. exportIntegerToAddr :: Integer -> Addr# -> Int# -> IO Word -- | Version of exportIntegerToAddr operating on Words. exportWordToAddr :: Word -> Addr# -> Int# -> IO Word -- | Version of exportIntegerToMutableByteArray operating on -- BigNats. exportBigNatToMutableByteArray :: BigNat -> MutableByteArray# RealWorld -> Word# -> Int# -> IO Word -- | Dump Integer (without sign) to mutable byte-array in base-256 -- representation. -- -- The call -- --
-- exportIntegerToMutableByteArray i mba offset msbf ---- -- writes -- --
-- importIntegerFromAddr addr size msbf ---- -- See description of importIntegerFromByteArray for more details. importIntegerFromAddr :: Addr# -> Word# -> Int# -> IO Integer -- | Version of importIntegerFromByteArray constructing a -- BigNat importBigNatFromByteArray :: ByteArray# -> Word# -> Word# -> Int# -> BigNat -- | Read Integer (without sign) from byte-array in base-256 -- representation. -- -- The call -- --
-- importIntegerFromByteArray ba offset size msbf ---- -- reads -- --