2018-04-25 19:21:58 +00:00
|
|
|
# Stint
|
2018-03-26 09:46:24 +00:00
|
|
|
# Copyright 2018 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.
|
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
import
|
|
|
|
# Status lib
|
|
|
|
stew/bitops2
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
when sizeof(int) == 8 and not defined(Stint32):
|
2023-06-09 08:46:21 +00:00
|
|
|
type
|
|
|
|
Word* = uint64
|
|
|
|
SignedWord* = int64
|
2018-03-26 09:46:24 +00:00
|
|
|
else:
|
2023-06-09 08:46:21 +00:00
|
|
|
type
|
|
|
|
Word* = uint32
|
|
|
|
SignedWord* = int32
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2020-06-12 17:23:03 +00:00
|
|
|
const WordBitWidth* = sizeof(Word) * 8
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
func wordsRequired*(bits: int): int {.compileTime.} =
|
|
|
|
## Compute the number of limbs required
|
2022-01-23 21:45:47 +00:00
|
|
|
## for the **announced** bit length
|
2020-06-12 16:37:02 +00:00
|
|
|
(bits + WordBitWidth - 1) div WordBitWidth
|
2018-04-21 10:12:05 +00:00
|
|
|
|
2018-03-26 09:46:24 +00:00
|
|
|
type
|
2020-06-12 17:01:05 +00:00
|
|
|
Limbs*[N: static int] = array[N, Word]
|
2020-06-13 10:29:31 +00:00
|
|
|
## Limbs type
|
2018-03-26 09:46:24 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
StUint*[bits: static[int]] = object
|
2020-06-12 16:37:02 +00:00
|
|
|
## Stack-based integer
|
|
|
|
## Unsigned
|
2020-06-13 10:29:31 +00:00
|
|
|
limbs*: array[bits.wordsRequired, Word]
|
2022-01-23 21:45:47 +00:00
|
|
|
# Limbs-Endianess is little-endian
|
|
|
|
|
2023-06-13 01:14:52 +00:00
|
|
|
StInt*[bits: static[int]] = object
|
|
|
|
## Stack-based integer
|
2023-06-15 07:08:27 +00:00
|
|
|
## Signed, in two complement format
|
2023-06-15 07:31:59 +00:00
|
|
|
impl*: StUint[bits]
|
2023-06-13 01:14:52 +00:00
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
Carry* = uint8 # distinct range[0'u8 .. 1]
|
|
|
|
Borrow* = uint8 # distinct range[0'u8 .. 1]
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
SomeBigInteger*[bits: static[int]] = StUint[bits] | StInt[bits]
|
2020-06-12 21:53:08 +00:00
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
const GCC_Compatible* = defined(gcc) or defined(clang) or defined(llvm_gcc)
|
|
|
|
const X86* = defined(amd64) or defined(i386)
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2020-06-12 16:37:02 +00:00
|
|
|
when sizeof(int) == 8 and GCC_Compatible:
|
|
|
|
type
|
|
|
|
uint128*{.importc: "unsigned __int128".} = object
|
2023-09-25 14:49:04 +00:00
|
|
|
hi, lo: uint64
|
2022-04-07 07:52:36 +00:00
|
|
|
|
2023-06-14 15:26:44 +00:00
|
|
|
# Accessors
|
|
|
|
# --------------------------------------------------------
|
|
|
|
|
|
|
|
template limbs*(a: StInt): untyped =
|
2023-06-15 07:31:59 +00:00
|
|
|
a.impl.limbs
|
2023-06-14 15:26:44 +00:00
|
|
|
|
|
|
|
template `[]`*(a: StInt, i: SomeInteger or BackwardsIndex): Word =
|
2023-06-15 07:31:59 +00:00
|
|
|
a.impl.limbs[i]
|
2023-06-14 15:26:44 +00:00
|
|
|
|
|
|
|
template `[]=`*(a: var StInt, i: SomeInteger or BackwardsIndex, val: Word) =
|
2023-06-15 07:31:59 +00:00
|
|
|
a.impl.limbs[i] = val
|
2023-06-14 15:26:44 +00:00
|
|
|
|
|
|
|
template `[]`*(a: StUint, i: SomeInteger or BackwardsIndex): Word =
|
|
|
|
a.limbs[i]
|
|
|
|
|
|
|
|
template `[]=`*(a: var StUint, i: SomeInteger or BackwardsIndex, val: Word) =
|
|
|
|
a.limbs[i] = val
|
|
|
|
|
2023-06-20 07:59:26 +00:00
|
|
|
template len*(a: StInt): int =
|
|
|
|
a.impl.limbs.len
|
|
|
|
|
|
|
|
template len*(a: StUint): int =
|
|
|
|
a.limbs.len
|
|
|
|
|
2022-01-23 21:45:47 +00:00
|
|
|
# Bithacks
|
2020-06-12 21:53:08 +00:00
|
|
|
# --------------------------------------------------------
|
|
|
|
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2018-10-25 10:58:40 +00:00
|
|
|
|
2022-01-23 21:45:47 +00:00
|
|
|
template clearExtraBitsOverMSB*(a: var StUint) =
|
2021-02-21 19:21:56 +00:00
|
|
|
## A Stuint is stored in an array of 32 of 64-bit word
|
|
|
|
## If we do bit manipulation at the word level,
|
|
|
|
## for example a 8-bit stuint stored in a 64-bit word
|
|
|
|
## we need to clear the upper 56-bit
|
|
|
|
when a.bits != a.limbs.len * WordBitWidth:
|
|
|
|
const posExtraBits = a.bits - (a.limbs.len-1) * WordBitWidth
|
|
|
|
const mask = (Word(1) shl posExtraBits) - 1
|
2022-01-23 21:45:47 +00:00
|
|
|
a[^1] = a[^1] and mask
|
|
|
|
|
|
|
|
func usedBitsAndWords*(a: openArray[Word]): tuple[bits, words: int] =
|
|
|
|
## Returns the number of used words and bits in a bigInt
|
2022-01-25 22:12:52 +00:00
|
|
|
## Returns (0, 0) for all-zeros array (even if technically you need 1 bit and 1 word to encode zero)
|
2022-01-23 21:45:47 +00:00
|
|
|
var clz = 0
|
|
|
|
# Count Leading Zeros
|
|
|
|
for i in countdown(a.len-1, 0):
|
|
|
|
let count = log2trunc(a[i])
|
|
|
|
# debugEcho "count: ", count, ", a[", i, "]: ", a[i].toBin(64)
|
|
|
|
if count == -1:
|
|
|
|
clz += WordBitWidth
|
|
|
|
else:
|
|
|
|
clz += WordBitWidth - count - 1
|
|
|
|
return (a.len*WordBitWidth - clz, i+1)
|
2022-01-25 22:12:52 +00:00
|
|
|
return (0, 0)
|
2021-02-21 19:21:56 +00:00
|
|
|
|
2022-01-23 21:45:47 +00:00
|
|
|
{.pop.}
|
|
|
|
|
|
|
|
# Iterations
|
|
|
|
# --------------------------------------------------------
|
2020-06-12 18:05:40 +00:00
|
|
|
|
|
|
|
import std/macros
|
|
|
|
|
|
|
|
proc replaceNodes(ast: NimNode, what: NimNode, by: NimNode): NimNode =
|
|
|
|
# Replace "what" ident node by "by"
|
|
|
|
proc inspect(node: NimNode): NimNode =
|
|
|
|
case node.kind:
|
|
|
|
of {nnkIdent, nnkSym}:
|
|
|
|
if node.eqIdent(what):
|
|
|
|
return by
|
|
|
|
return node
|
|
|
|
of nnkEmpty:
|
|
|
|
return node
|
|
|
|
of nnkLiterals:
|
|
|
|
return node
|
|
|
|
else:
|
|
|
|
var rTree = node.kind.newTree()
|
|
|
|
for child in node:
|
|
|
|
rTree.add inspect(child)
|
|
|
|
return rTree
|
|
|
|
result = inspect(ast)
|
|
|
|
|
|
|
|
macro staticFor*(idx: untyped{nkIdent}, start, stopEx: static int, body: untyped): untyped =
|
|
|
|
## staticFor [min inclusive, max exclusive)
|
|
|
|
result = newStmtList()
|
|
|
|
for i in start ..< stopEx:
|
|
|
|
result.add nnkBlockStmt.newTree(
|
|
|
|
ident("unrolledIter_" & $idx & $i),
|
|
|
|
body.replaceNodes(idx, newLit i)
|
|
|
|
)
|
2021-08-06 12:44:25 +00:00
|
|
|
|
|
|
|
# Copy
|
|
|
|
# --------------------------------------------------------
|
2023-06-12 14:07:15 +00:00
|
|
|
{.push raises: [], inline, noinit, gcsafe.}
|
2022-01-23 21:45:47 +00:00
|
|
|
|
|
|
|
func copyWords*(
|
|
|
|
a: var openArray[Word], startA: int,
|
|
|
|
b: openArray[Word], startB: int,
|
|
|
|
numWords: int) =
|
|
|
|
## Copy a slice of B into A. This properly deals
|
|
|
|
## with overlaps when A and B are slices of the same buffer
|
|
|
|
for i in countdown(numWords-1, 0):
|
|
|
|
a[startA+i] = b[startB+i]
|
|
|
|
|
2023-06-09 08:46:21 +00:00
|
|
|
{.pop.}
|