-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] Simplify Int.__floordiv__
and friends
#3497
base: nightly
Are you sure you want to change the base?
Conversation
ce14b14
to
8ac9226
Compare
stdlib/src/builtin/int.mojo
Outdated
var mod = self - div * rhs | ||
var divMod = select(((rhs < 0) ^ (self < 0)) & mod, div - 1, div) | ||
div = select(self > 0 & rhs > 0, div, divMod) |
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.
The first branch was never taken anyways, since
self > 0 & rhs > 0
=
self > (0 & rhs) > 0
=
self > 0 > 0
=
False
Int.__floordiv__
and friendsInt.__floordiv__
and friends
!sync |
if ((rhs < 0) ^ (self < 0)) & mod: | ||
return div - 1, mod + rhs | ||
return div, mod | ||
return self // rhs, self % rhs |
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.
Question By recomputing things, won't this regress performance a bit?
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.
Both //
and %
are marked as always_inline
and they share most of the code; I just assume that the compiler can see through the defs and do CSE well. I can inline and do a manual dedup if you so prefer.
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.
As a ref point, UInt
also does this.
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.
I checked and they generates identical assembly.
10a8a71
to
933cc33
Compare
278467d
to
d64f143
Compare
This comment was marked as outdated.
This comment was marked as outdated.
d64f143
to
c7b4819
Compare
c7b4819
to
2fda091
Compare
2dbaaea
to
8ad2a38
Compare
Signed-off-by: Yiwu Chen <210at85@gmail.com>
8ad2a38
to
86f9501
Compare
No description provided.