Skip to content

Commit

Permalink
Merge pull request #13 from guzba/ryan
Browse files Browse the repository at this point in the history
faster for refc
  • Loading branch information
guzba authored Jan 26, 2023
2 parents 5b14d59 + 2b56309 commit a282a89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crunchy.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.1.5"
version = "0.1.6"
author = "Ryan Oldenburg"
description = "SIMD-optimized hashing, checksums and CRCs"
license = "MIT"
Expand Down
17 changes: 13 additions & 4 deletions src/crunchy/bigints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import std/[algorithm, bitops, math, options]

{.push stackTrace:off.}

type
BigInt* = object
## An arbitrary precision integer.
Expand Down Expand Up @@ -1280,14 +1282,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:
Expand All @@ -1300,7 +1305,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 =
Expand Down Expand Up @@ -1348,3 +1354,6 @@ func powmod*(base, exponent, modulus: BigInt): BigInt =
result = mymod((result * basePow), modulus, memoized)
basePow = mymod((basePow * basePow), modulus, memoized)
exponent = exponent shr 1

{.pop.}
{.pop.}

0 comments on commit a282a89

Please sign in to comment.