stint/private/datatypes

Source   Edit  

Types

Limbs[N] = array[N, Word]
Limbs type. Source   Edit  
SignedWord = int64
Source   Edit  
SomeBigInteger[bits] = StUint[bits] | StInt[bits]
Source   Edit  
StInt[bits] = object
  impl*: StUint[bits]

Stack-based integer.

Signed, in two complement format

Source   Edit  
StUint[bits] = object
  limbs*: array[bits.wordsRequired, Word]

Stack-based integer.

Unsigned.

Source   Edit  
uint128 {.importc: "unsigned __int128", nodecl.} = object
Source   Edit  
Word = uint64
Source   Edit  

Consts

GCC_Compatible = true
Source   Edit  
WordBitWidth = 64
Source   Edit  
X86 = true
Source   Edit  

Procs

func copyWords(a: var openArray[Word]; startA: int; b: openArray[Word];
               startB: int; numWords: int) {....raises: [], inline, noinit, ...gcsafe,
    tags: [], forbids: [].}
Copy a slice of B into A. This properly deals with overlaps when A and B are slices of the same buffer. Source   Edit  
func usedBitsAndWords(a: openArray[Word]): tuple[bits, words: int] {....raises: [],
    inline, noinit, ...gcsafe, tags: [], forbids: [].}

Returns the number of used words and bits in a bigInt.

Returns (0, 0) for all-zeros array (even if technically you need 1 bit and 1 word to encode zero).

Source   Edit  
func wordsRequired(bits: int): int {.compileTime, ...raises: [], tags: [],
                                     forbids: [].}
Compute the number of limbs required for the announced bit length. Source   Edit  

Macros

macro staticFor(idx: untyped{nkIdent}; start, stopEx: static int; body: untyped): untyped {.
    ...deprecated: "stew/staticfor".}
Deprecated: stew/staticfor
staticFor [min inclusive, max exclusive). Source   Edit  

Templates

template `[]`(a: StInt; i: SomeInteger or BackwardsIndex): Word
Source   Edit  
template `[]`(a: StUint; i: SomeInteger or BackwardsIndex): Word
Source   Edit  
template `[]=`(a: var StInt; i: SomeInteger or BackwardsIndex; val: Word)
Source   Edit  
template `[]=`(a: var StUint; i: SomeInteger or BackwardsIndex; val: Word)
Source   Edit  
template clearExtraBitsOverMSB(a: var StUint | StInt) {..}

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.

Source   Edit  
template len(a: StInt): int
Source   Edit  
template len(a: StUint): int
Source   Edit  
template limbs(a: StInt): untyped
Source   Edit