-
Notifications
You must be signed in to change notification settings - Fork 45
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
Handle nominator smaller than divisor in udivrem() #111
Conversation
It has nothing to do with the argument aliasiing. Here's a repro:
I think it's related to the early return here: https://github.com/holiman/uint256/pull/111/files#diff-af4a4c8b4e176a360fd1472a72691c2ffab2426e94f4e69829a48bd12f01a908R512 |
Shouldn't it rather be something like this:
|
in case
thanks @holiman |
I have tested and all tests got |
@@ -508,6 +508,10 @@ func udivrem(quot, u []uint64, d *Int) (rem Int) { | |||
} | |||
} | |||
|
|||
if uLen < dLen { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from #111 (comment) ,so it should be
if uLen < dLen {
copy(rem[:], u[:])
return rem
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I think the point has already been made :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I just wanted to formal mark this point 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except we need to handle u[:]
more carefully here because it may be bigger than Int
. Is rem Int
all zeros at function start?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, rem
is initialized blank.
Should be good now. Do you think we should benchmark this? |
Codecov Report
@@ Coverage Diff @@
## master #111 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 1385 1388 +3
=========================================
+ Hits 1385 1388 +3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I mean, doesn't hurt, but not a blocker for me |
It makes it faster (probably by accident).
|
It looks that with additional check and "copy/return" block the total number of instructions is almost exactly the same as before so maybe this saves us some additional bounds checks down the line. https://godbolt.org/z/jq5KesEen |
I wanted to make
udivrem()
more robust by making it handle the case where nominator is smaller than divisor (currently it will panic weirdly in this case).I'm testing the change though
TestRandomDiv
andTestRandomMod
wheredivraw
andmodraw
wrappers are provided.The
modraw
fails a test when a param and result are aliased. I have not been able to figure out the cause of the problem so far.