Skip to content

Commit

Permalink
uint256: optimize div-related functions by reducing bounds check (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronChen0 committed May 16, 2024
1 parent 66a4528 commit 11a325c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,21 @@ func (z *Int) isBitSet(n uint) bool {
}

// addTo computes x += y.
// Requires len(x) >= len(y).
// Requires len(x) >= len(y) > 0.
func addTo(x, y []uint64) uint64 {
var carry uint64
_ = x[len(y)-1] // bounds check hint to compiler; see golang.org/issue/14808
for i := 0; i < len(y); i++ {
x[i], carry = bits.Add64(x[i], y[i], carry)
}
return carry
}

// subMulTo computes x -= y * multiplier.
// Requires len(x) >= len(y).
// Requires len(x) >= len(y) > 0.
func subMulTo(x, y []uint64, multiplier uint64) uint64 {

var borrow uint64
_ = x[len(y)-1] // bounds check hint to compiler; see golang.org/issue/14808
for i := 0; i < len(y); i++ {
s, carry1 := bits.Sub64(x[i], borrow, 0)
ph, pl := bits.Mul64(y[i], multiplier)
Expand Down

0 comments on commit 11a325c

Please sign in to comment.