Pass extended precision bitops2 tests
This commit is contained in:
parent
59bca47012
commit
254d4da649
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
import ./int_public, ./uint_public, macros
|
import ./int_public, ./uint_public, macros
|
||||||
|
|
||||||
|
# TODO: deprecate
|
||||||
|
|
||||||
type Signedness = enum
|
type Signedness = enum
|
||||||
BothSigned, IntOnly, UintOnly
|
BothSigned, IntOnly, UintOnly
|
||||||
|
|
||||||
|
|
|
@ -140,19 +140,31 @@ func parity*(a: Stuint): int =
|
||||||
|
|
||||||
func leadingZeros*(a: Stuint): int =
|
func leadingZeros*(a: Stuint): int =
|
||||||
result = 0
|
result = 0
|
||||||
|
|
||||||
|
# Adjust when we use only part of the word size
|
||||||
|
var extraBits = WordBitWidth * a.limbs.len - a.bits
|
||||||
|
|
||||||
for word in mostToLeastSig(a):
|
for word in mostToLeastSig(a):
|
||||||
let zeroCount = word.leadingZeros()
|
let zeroCount = word.leadingZeros()
|
||||||
|
if extraBits > 0:
|
||||||
|
result += zeroCount - min(extraBits, WordBitWidth)
|
||||||
|
extraBits -= WordBitWidth
|
||||||
|
else:
|
||||||
result += zeroCount
|
result += zeroCount
|
||||||
if zeroCount != WordBitWidth:
|
if zeroCount != WordBitWidth:
|
||||||
return
|
break
|
||||||
|
|
||||||
func trailingZeros*(a: Stuint): int =
|
func trailingZeros*(a: Stuint): int =
|
||||||
result = 0
|
result = 0
|
||||||
for word in leastToMostSig(a):
|
for word in leastToMostSig(a):
|
||||||
let zeroCount = word.leadingZeros()
|
let zeroCount = word.trailingZeros()
|
||||||
result += zeroCount
|
result += zeroCount
|
||||||
if zeroCount != WordBitWidth:
|
if zeroCount != WordBitWidth:
|
||||||
return
|
break
|
||||||
|
|
||||||
|
when a.limbs.len * WordBitWidth != a.bits:
|
||||||
|
if result > a.bits:
|
||||||
|
result = a.bits
|
||||||
|
|
||||||
func firstOne*(a: Stuint): int =
|
func firstOne*(a: Stuint): int =
|
||||||
result = trailingZeros(a)
|
result = trailingZeros(a)
|
||||||
|
@ -204,7 +216,7 @@ func `shl`*(a: Stuint, k: SomeInteger): Stuint =
|
||||||
{.push raises: [], inline, noInit, gcsafe.}
|
{.push raises: [], inline, noInit, gcsafe.}
|
||||||
|
|
||||||
func `+`*(a, b: Stuint): Stuint =
|
func `+`*(a, b: Stuint): Stuint =
|
||||||
# Addition for multi-precision unsigned int
|
## Addition for multi-precision unsigned int
|
||||||
var carry = Carry(0)
|
var carry = Carry(0)
|
||||||
for wr, wa, wb in leastToMostSig(result, a, b):
|
for wr, wa, wb in leastToMostSig(result, a, b):
|
||||||
addC(carry, wr, wa, wb, carry)
|
addC(carry, wr, wa, wb, carry)
|
||||||
|
@ -218,7 +230,7 @@ func `+=`*(a: var Stuint, b: Stuint) =
|
||||||
a.clearExtraBits()
|
a.clearExtraBits()
|
||||||
|
|
||||||
func `-`*(a, b: Stuint): Stuint =
|
func `-`*(a, b: Stuint): Stuint =
|
||||||
# Substraction for multi-precision unsigned int
|
## Substraction for multi-precision unsigned int
|
||||||
var borrow = Borrow(0)
|
var borrow = Borrow(0)
|
||||||
for wr, wa, wb in leastToMostSig(result, a, b):
|
for wr, wa, wb in leastToMostSig(result, a, b):
|
||||||
subB(borrow, wr, wa, wb, borrow)
|
subB(borrow, wr, wa, wb, borrow)
|
||||||
|
@ -234,11 +246,22 @@ func `-=`*(a: var Stuint, b: Stuint) =
|
||||||
func inc*(a: var Stuint, w: Word = 1) =
|
func inc*(a: var Stuint, w: Word = 1) =
|
||||||
var carry = Carry(0)
|
var carry = Carry(0)
|
||||||
when cpuEndian == littleEndian:
|
when cpuEndian == littleEndian:
|
||||||
addC(carry, x.limbs[0], x.limbs[0], w, carry)
|
addC(carry, a.limbs[0], a.limbs[0], w, carry)
|
||||||
for i in 1 ..< x.len:
|
for i in 1 ..< a.limbs.len:
|
||||||
addC(carry, x.limbs[i], x.limbs[i], 0, carry)
|
addC(carry, a.limbs[i], a.limbs[i], 0, carry)
|
||||||
a.clearExtraBits()
|
a.clearExtraBits()
|
||||||
|
|
||||||
|
func `+`*(a: Stuint, b: SomeUnsignedInt): Stuint =
|
||||||
|
## Addition for multi-precision unsigned int
|
||||||
|
## with an unsigned integer
|
||||||
|
result = a
|
||||||
|
result.inc(Word(b))
|
||||||
|
|
||||||
|
func `+=`*(a: var Stuint, b: SomeUnsignedInt) =
|
||||||
|
## In-place addition for multi-precision unsigned int
|
||||||
|
## with an unsigned integer
|
||||||
|
a.inc(Word(b))
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
# Multiplication
|
# Multiplication
|
||||||
# --------------------------------------------------------
|
# --------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue