-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uint256: optimize Sqrt #174
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #174 +/- ##
===========================================
- Coverage 100.00% 99.69% -0.31%
===========================================
Files 5 5
Lines 1632 1639 +7
===========================================
+ Hits 1632 1634 +2
- Misses 0 5 +5 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #174 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 5
Lines 1632 1640 +8
=========================================
+ Hits 1632 1640 +8 |
Follow your suggestion. |
We can simplify even more, by moving the division to the end of the loop, and lose the flag. diff --git a/uint256.go b/uint256.go
index 493f44a..7b93ee9 100644
--- a/uint256.go
+++ b/uint256.go
@@ -369,9 +369,9 @@ func umul(x, y *Int, res *[8]uint64) {
func (z *Int) Mul(x, y *Int) *Int {
var (
carry0, carry1, carry2 uint64
- res1, res2 uint64
- x0, x1, x2, x3 = x[0], x[1], x[2], x[3]
- y0, y1, y2, y3 = y[0], y[1], y[2], y[3]
+ res1, res2 uint64
+ x0, x1, x2, x3 = x[0], x[1], x[2], x[3]
+ y0, y1, y2, y3 = y[0], y[1], y[2], y[3]
)
carry0, z[0] = bits.Mul64(x0, y0)
@@ -1283,14 +1283,14 @@ func (z *Int) Sqrt(x *Int) *Int {
if x.IsUint64() {
var (
x0 uint64 = x.Uint64()
- z1 uint64 = 1 << ((bits.Len64(x0)+1)/2)
+ z1 uint64 = 1 << ((bits.Len64(x0) + 1) / 2)
z2 uint64
)
if x0 < 2 {
return z.SetUint64(x0)
}
for {
- z2 = (z1 + x0 / z1) >> 1
+ z2 = (z1 + x0/z1) >> 1
if z2 >= z1 {
return z.SetUint64(z1)
}
@@ -1306,27 +1306,24 @@ func (z *Int) Sqrt(x *Int) *Int {
// We can do the first division outside the loop
z2.Rsh(x, uint(x.BitLen()+1)/2) // The first div is equal to a right shift
- first := true
for {
- // z2.Div(x, z1) -- x > MaxUint64, x > z1 > 0
- if !first { // first division was done outside the loop
- z2.Clear()
- udivrem(z2[:], x[:], z1, nil)
- }
- first = false
z2.Add(z2, z1)
-
+
// z2 = z2.Rsh(z2, 1) -- the code below does a 1-bit rsh faster
- z2[0] = (z2[0] >> 1) | z2[1] << 63
- z2[1] = (z2[1] >> 1) | z2[2] << 63
- z2[2] = (z2[2] >> 1) | z2[3] << 63
+ z2[0] = (z2[0] >> 1) | z2[1]<<63
+ z2[1] = (z2[1] >> 1) | z2[2]<<63
+ z2[2] = (z2[2] >> 1) | z2[3]<<63
z2[3] >>= 1
if !z2.Lt(z1) {
return z.Set(z1)
}
z1.Set(z2)
+ // Next iteration of the loop
+ // z2.Div(x, z1) -- x > MaxUint64, x > z1 > 0
+ z2.Clear()
+ udivrem(z2[:], x[:], z1, nil)
}
}
|
Good observation! |
👍 |
x.IsUint64()
a special case.x.IsUint64()
isfalse
, thenis equal to
Lt
is faster thanCmp
.Rsh
is 10x cheaper thanDiv
.Benchmark