Merged
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 |
holiman
reviewed
May 27, 2024
Contributor
Author
|
Follow your suggestion. |
Owner
|
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)
}
}
|
Contributor
Author
|
Good observation! |
Owner
👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
x.IsUint64()a special case.x.IsUint64()isfalse, thenis equal to
Ltis faster thanCmp.Rshis 10x cheaper thanDiv.Benchmark