From 11a325c8675c252e64928feff2cb8e2908784875 Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Thu, 16 May 2024 17:06:06 +0800 Subject: [PATCH] uint256: optimize div-related functions by reducing bounds check (#168) --- uint256.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/uint256.go b/uint256.go index 91cff61..e5a9123 100644 --- a/uint256.go +++ b/uint256.go @@ -421,9 +421,10 @@ 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) } @@ -431,10 +432,10 @@ func addTo(x, y []uint64) uint64 { } // 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)