nim-stint/stint/uintops.nim

273 lines
7.0 KiB
Nim
Raw Normal View History

# Stint
# Copyright 2018-Present Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
# Internal
./private/datatypes,
./private/uint_bitwise,
2020-06-13 14:44:13 +00:00
./private/uint_shift,
./private/uint_addsub,
2022-01-12 17:25:55 +00:00
./private/uint_mul,
./private/uint_div,
2023-06-13 12:51:49 +00:00
./private/primitives/addcarry_subborrow,
stew/bitops2
export StUint
# Initialization
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
func setZero*(a: var StUint) =
## Set ``a`` to 0
for i in 0 ..< a.limbs.len:
a.limbs[i] = 0
func setSmallInt(a: var StUint, k: Word) =
## Set ``a`` to k
a.limbs[0] = k
for i in 1 ..< a.limbs.len:
a.limbs[i] = 0
func setOne*(a: var StUint) =
setSmallInt(a, 1)
2023-06-12 14:07:15 +00:00
func zero*[bits: static[int]](T: typedesc[StUint[bits]]): T {.inline.} =
## Returns the zero of the input type
2023-06-14 06:53:42 +00:00
result.setZero
2023-06-12 14:07:15 +00:00
func one*[bits: static[int]](T: typedesc[StUint[bits]]): T {.inline.} =
## Returns the one of the input type
result.setOne()
2023-06-12 14:07:15 +00:00
func high*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
for i in 0 ..< result.limbs.len:
result[i] = high(Word)
2023-06-12 14:07:15 +00:00
func low*[bits](_: typedesc[StUint[bits]]): StUint[bits] {.inline.} =
2023-06-14 06:53:42 +00:00
result.setZero
{.pop.}
# Comparisons
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func isZero*(a: StUint): bool =
for i in 0 ..< a.limbs.len:
if a[i] != 0:
return false
return true
func isOne*(a: StUint): bool =
if a.limbs[0] != 1:
return false
for i in 1 ..< a.limbs.len:
if a.limbs[i] != 0:
return false
return true
2023-06-12 14:07:15 +00:00
func `==`*(a, b: StUint): bool {.inline.} =
## Unsigned `equal` comparison
for i in 0 ..< a.limbs.len:
if a[i] != b[i]:
return false
return true
2023-06-12 14:07:15 +00:00
func `<`*(a, b: StUint): bool {.inline.} =
## Unsigned `less than` comparison
var diff: Word
var borrow: Borrow
for i in 0 ..< a.limbs.len:
subB(borrow, diff, a[i], b[i], borrow)
return bool(borrow)
2023-06-12 14:07:15 +00:00
func `<=`*(a, b: StUint): bool {.inline.} =
## Unsigned `less or equal` comparison
2020-06-13 15:10:26 +00:00
not(b < a)
2023-06-12 14:07:15 +00:00
func isOdd*(a: StUint): bool {.inline.} =
## Returns true if input is off
## false otherwise
bool(a[0] and 1)
2023-06-12 14:07:15 +00:00
func isEven*(a: StUint): bool {.inline.} =
## Returns true if input is zero
## false otherwise
not a.isOdd()
{.pop.}
# Bitwise operations
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func `not`*(a: StUint): StUint =
## Bitwise complement of unsigned integer a
## i.e. flips all bits of the input
result.bitnot(a)
2023-06-12 14:07:15 +00:00
func `or`*(a, b: StUint): StUint =
## `Bitwise or` of numbers a and b
result.bitor(a, b)
2023-06-12 14:07:15 +00:00
func `and`*(a, b: StUint): StUint =
## `Bitwise and` of numbers a and b
result.bitand(a, b)
2023-06-12 14:07:15 +00:00
func `xor`*(a, b: StUint): StUint =
## `Bitwise xor` of numbers x and y
result.bitxor(a, b)
2020-09-06 13:54:57 +00:00
{.pop.} # End noInit
export
countOnes,
parity,
leadingZeros,
trailingZeros,
firstOne
{.push raises: [], inline, gcsafe.}
2023-06-14 00:38:23 +00:00
func `shr`*(a: StUint, k: Natural): StUint =
2020-06-13 14:44:13 +00:00
## Shift right by k bits
result.shiftRight(a, k)
2020-06-13 14:44:13 +00:00
2023-06-14 00:38:23 +00:00
func `shl`*(a: StUint, k: Natural): StUint =
2020-06-13 14:44:13 +00:00
## Shift left by k bits
result.shiftLeft(a, k)
2020-06-13 14:44:13 +00:00
2023-06-14 00:38:23 +00:00
func setBit*(a: var StUint, k: Natural) =
2023-06-14 01:06:44 +00:00
## set bit at position `k`
## k = 0..a.bits-1
2023-06-13 12:51:49 +00:00
let limbIndex = k div WordBitWidth
let bitIndex = k mod WordBitWidth
setBit(a.limbs[limbIndex], bitIndex)
2023-06-14 00:38:23 +00:00
func clearBit*(a: var StUint, k: Natural) =
2023-06-14 01:06:44 +00:00
## set bit at position `k`
## k = 0..a.bits-1
2023-06-13 12:51:49 +00:00
let limbIndex = k div WordBitWidth
let bitIndex = k mod WordBitWidth
clearBit(a.limbs[limbIndex], bitIndex)
2023-06-14 00:38:23 +00:00
func getBit*(a: StUint, k: Natural): bool =
2023-06-14 01:06:44 +00:00
## set bit at position `k`
## k = 0..a.bits-1
2023-06-14 00:38:23 +00:00
let limbIndex = k div WordBitWidth
let bitIndex = k mod WordBitWidth
getBit(a.limbs[limbIndex], bitIndex)
2023-06-13 12:51:49 +00:00
{.pop.}
2020-06-13 14:44:13 +00:00
# Addsub
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func `+`*(a, b: StUint): StUint =
2020-09-06 13:54:57 +00:00
## Addition for multi-precision unsigned int
result.sum(a, b)
export `+=`
2023-06-12 14:07:15 +00:00
func `-`*(a, b: StUint): StUint =
2020-09-06 13:54:57 +00:00
## Substraction for multi-precision unsigned int
result.diff(a, b)
export `-=`
export inc
2023-06-12 14:07:15 +00:00
func `+`*(a: StUint, b: SomeUnsignedInt): StUint =
2020-09-06 13:54:57 +00:00
## Addition for multi-precision unsigned int
## with an unsigned integer
result.sum(a, Word(b))
2020-09-06 13:54:57 +00:00
export `+=`
2020-09-06 13:54:57 +00:00
{.pop.}
# Multiplication
# --------------------------------------------------------
# Multiplication is implemented in a separate file at the limb-level
# - It's too big to be inlined (especially with unrolled loops)
# - It's implemented at the limb-level so that
# in the future Stuint[254] and Stuint256] share a common codepath
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func `*`*(a, b: StUint): StUint =
## Integer multiplication
result.limbs.prod(a.limbs, b.limbs)
result.clearExtraBitsOverMSB()
{.pop.}
# Exponentiation
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func pow*(a: StUint, e: Natural): StUint =
## Compute ``a`` to the power of ``e``,
## ``e`` must be non-negative
# Implementation uses exponentiation by squaring
# See Nim math module: https://github.com/nim-lang/Nim/blob/4ed24aa3eb78ba4ff55aac3008ec3c2427776e50/lib/pure/math.nim#L429
# And Eli Bendersky's blog: https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms
var (a, e) = (a, e)
result.setOne()
while true:
if bool(e and 1): # if y is odd
result = result * a
e = e shr 1
if e == 0:
break
a = a * a
2023-06-12 14:07:15 +00:00
func pow*[aBits, eBits](a: StUint[aBits], e: StUint[eBits]): StUint[aBits] =
## Compute ``x`` to the power of ``y``,
## ``x`` must be non-negative
# Implementation uses exponentiation by squaring
# See Nim math module: https://github.com/nim-lang/Nim/blob/4ed24aa3eb78ba4ff55aac3008ec3c2427776e50/lib/pure/math.nim#L429
# And Eli Bendersky's blog: https://eli.thegreenplace.net/2009/03/21/efficient-integer-exponentiation-algorithms
var (a, e) = (a, e)
result.setOne()
while true:
if e.isOdd:
result = result * a
e = e shr 1
if e.isZero:
break
a = a * a
{.pop.}
# Division & Modulo
# --------------------------------------------------------
2023-06-12 14:07:15 +00:00
{.push raises: [], inline, noinit, gcsafe.}
2023-06-12 14:07:15 +00:00
func `div`*(x, y: StUint): StUint =
## Division operation for multi-precision unsigned uint
2023-06-12 14:07:15 +00:00
var tmp{.noinit.}: StUint
divRem(result.limbs, tmp.limbs, x.limbs, y.limbs)
2023-06-12 14:07:15 +00:00
func `mod`*(x, y: StUint): StUint =
## Remainder operation for multi-precision unsigned uint
2023-06-12 14:07:15 +00:00
var tmp{.noinit.}: StUint
divRem(tmp.limbs, result.limbs, x.limbs, y.limbs)
2023-06-12 14:07:15 +00:00
func divmod*(x, y: StUint): tuple[quot, rem: StUint] =
## Division and remainder operations for multi-precision unsigned uint
divRem(result.quot.limbs, result.rem.limbs, x.limbs, y.limbs)
2022-01-12 17:25:55 +00:00
{.pop.}