move mask to common

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-18 13:01:14 +01:00
parent ef5dd8345b
commit 236047767f
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
3 changed files with 8 additions and 5 deletions

View File

@ -31,12 +31,15 @@ const
Zero* = Word(0)
One* = Word(1)
MaxWord* = (not Zero) shr 1
MaxWord* = (not Zero) shr (WordPhysBitSize - WordBitSize)
## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF
## also 0b0111...1111
## This biggest representable number in our limbs.
## i.e. The most significant bit is never set at the end of each function
template mask*(w: Word): Word =
w and MaxWord
# ############################################################
#
# Instrumentation

View File

@ -51,7 +51,7 @@ func fromRawUintLE(
# if full, dump
if acc_len >= WordBitSize:
dst.limbs[dst_idx] = acc and MaxWord
dst.limbs[dst_idx] = mask(acc)
inc dst_idx
acc_len -= WordBitSize
acc = src_byte shr (8 - acc_len)
@ -88,7 +88,7 @@ func fromRawUintBE(
# if full, dump
if acc_len >= WordBitSize:
dst.limbs[dst_idx] = acc and MaxWord
dst.limbs[dst_idx] = mask(acc)
inc dst_idx
acc_len -= WordBitSize
acc = src_byte shr (8 - acc_len)

View File

@ -39,14 +39,14 @@ func double(a: var BigInt): bool =
for i in 0 ..< a.limbs.len:
var z = BaseType(a.limbs[i]) * 2 + BaseType(result)
result = z.isMsbSet()
a.limbs[i] = Word(z) and MaxWord
a.limbs[i] = mask(Word(z))
func sub(a: var BigInt, b: BigInt, ctl: bool): bool =
## In-place optional substraction
for i in 0 ..< a.limbs.len:
let new_a = BaseType(a.limbs[i]) - BaseType(b.limbs[i]) - BaseType(result)
result = new_a.isMsbSet()
a.limbs[i] = if ctl: new_a.Word and MaxWord
a.limbs[i] = if ctl: new_a.Word.mask()
else: a.limbs[i]
func doubleMod(a: var BigInt, M: BigInt) =