From 1f392530d99730e9d698eed5cdea5b7c20f332aa Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 25 Jan 2023 19:37:10 -0600 Subject: [PATCH 1/3] faster for refc --- src/crunchy/bigints.nim | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/crunchy/bigints.nim b/src/crunchy/bigints.nim index b688ae4..08d4f52 100644 --- a/src/crunchy/bigints.nim +++ b/src/crunchy/bigints.nim @@ -1280,14 +1280,17 @@ func invmod*(a, modulus: BigInt): BigInt = raise newException(ValueError, $a & " has no modular inverse modulo " & $modulus) result = t0.modulo(modulus) -proc inPlaceSubtraction(a: var BigInt, c: BigInt) = +{.push checks:off.} + +template inPlaceSubtraction(a: var BigInt, c: BigInt) = # In-place subtraction # a > c let al = a.limbs.len cl = c.limbs.len - var m = min(al, cl) - a.limbs.setLen(max(al, cl)) + m = min(al, cl) + if al > cl: + a.limbs.setLen(max(al, cl)) var tmp = 0'i64 for i in 0 ..< m: @@ -1300,7 +1303,8 @@ proc inPlaceSubtraction(a: var BigInt, c: BigInt) = tmp = 1 - (tmp shr 32) a.isNegative = false - a.normalize() + if a.limbs[a.limbs.high] == 0'u32: + a.normalize() # assert tmp == 0 proc mymod*(a, b: BigInt, memoized: var seq[BigInt]): BigInt = @@ -1348,3 +1352,5 @@ func powmod*(base, exponent, modulus: BigInt): BigInt = result = mymod((result * basePow), modulus, memoized) basePow = mymod((basePow * basePow), modulus, memoized) exponent = exponent shr 1 + +{.pop.} From b2d9488506b81889a8491def071cd6bbb548bda7 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 25 Jan 2023 19:41:38 -0600 Subject: [PATCH 2/3] faster for refc --- src/crunchy/bigints.nim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/crunchy/bigints.nim b/src/crunchy/bigints.nim index 08d4f52..529e1c9 100644 --- a/src/crunchy/bigints.nim +++ b/src/crunchy/bigints.nim @@ -7,6 +7,8 @@ import std/[algorithm, bitops, math, options] +{.push stackTrace:off.} + type BigInt* = object ## An arbitrary precision integer. @@ -1354,3 +1356,4 @@ func powmod*(base, exponent, modulus: BigInt): BigInt = exponent = exponent shr 1 {.pop.} +{.pop.} From 2b5630960a54b74f0a09d66f7e11c7def84febe3 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 25 Jan 2023 19:50:52 -0600 Subject: [PATCH 3/3] 0.1.6 --- crunchy.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crunchy.nimble b/crunchy.nimble index 1cad9db..e6bae59 100644 --- a/crunchy.nimble +++ b/crunchy.nimble @@ -1,4 +1,4 @@ -version = "0.1.5" +version = "0.1.6" author = "Ryan Oldenburg" description = "SIMD-optimized hashing, checksums and CRCs" license = "MIT"