-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-44946: Streamline operators and creation of ints for common case of single 'digit'. #27832
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
Merged
markshannon
merged 17 commits into
python:main
from
faster-cpython:streamline-medium-ints
Aug 25, 2021
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
da57f0b
Streamline binary operations and creating new int objects for common …
markshannon 0533a9f
Make sure that all ints, even internal, temporary ones, have at least…
markshannon 9349daa
Readability improvements as suggested by Victor Stinner.
markshannon 96496e2
Prefix private function name with _
markshannon 5e4aad5
Reduce the number of casts.
markshannon 59ba476
Avoid casting away top bits.
markshannon 0d3ca1d
Streamline integer negation and invert a bit. Suggested by Mark Dicki…
markshannon c73333b
Clarify comment and internal function name. Remove a bit of redundant…
markshannon 16d3167
Remove two more narrowing casts.
markshannon f20a2a8
Change _PyLong_FromLarge to use correctly sized int.
markshannon ab2b908
Avoid more narrowings.
markshannon e43060a
Revert get_small_int to taking a sdigit. Place narrowing casts in cor…
markshannon ed2a430
Use _PyLong_FromSTwoDigits not PyLong_FromLong in long_add.
markshannon 1f2d47c
Implement PyLong_FromLong separately from _PyLong_FromSTwoDigits to a…
markshannon 649c311
Don't overflow shift in PyLong_FromLong.
markshannon a69f420
Convert IS_MEDIUM_INT macro to inline function.
markshannon 47571ff
Edit comment
markshannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Avoid casting away top bits.
- Loading branch information
commit 59ba47614f136ab0a68660ecd33a3a62c38c44d6
There are no files selected for viewing
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
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.
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.
It would be useful to have a comment clarifying what range of values this macro can safely be used for. I'm assuming it should be enough that it's valid for values in the range
(-PyLong_BASE**2, PyLong_BASE**2)
.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 think I was unclear. The
(twodigits)x
cast potentially loses information ifx
is large enough, leading to the possibility of false positives forIS_MEDIUM_INT
. For example, that will happen on Windows with a largePy_ssize_t
value and 15-bit digits - in that case,Py_ssize_t
is much larger thanunsigned long
.So there's some restriction on the value of
x
for which this test is valid. "Fits instwodigits
" would probably be enough, but I don't think we use this macro for values outside the range(-PyLong_BASE**2, PyLong_BASE**2)
.Uh oh!
There was an error while loading. Please reload this page.
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.
More generally, C's integer-handling rules make this sort of thing horribly messy to reason about: for example in the 15-bit digit case the addition is an addition of an
unsigned long
to a (signed!)int
, since the integer promotions will promote theunsigned short
PyLong_MASK
to anint
(though even that part is not guaranteed by the standard - there's nothing preventingshort
andint
having the same precision, in which casePyLong_MASK
will be promoted tounsigned int
instead ofint
). So now we have to consult the rules for unsigned + signed addition in the "usual arithmetic conversions", which eventually say that becauselong
has greater rank thanint
(even if it has the same precision), both operands will be treated asunsigned long
for the addition.The
2 * PyLong_MASK
is another case that could end up being either signed or unsigned depending on ranks, types, etc; it's probably better spelled as2U * PyLong_MASK
; that way we can at least be sure that it's performed as an unsigned multiplication and that the final comparison is unsigned-to-unsigned.I'd suggest the addition of an extra cast around the result of the addition, just to reduce the number of mental hoops one has to jump through to establish that this really does give the right result: that is,
We should also add extra parentheses around the
x
, in case someone tries to useIS_MEDIUM_INT
on an expression more complicated than a single name.Uh oh!
There was an error while loading. Please reload this page.
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 wholeheartedly agree that C's integer handling is a pain to think about 😞
For clarity I think this is best to use an inline function that makes all casts super explicit.
That way that it makes the cast explicit (if called with something other than
stwodigits
orsdigits
, the caller is responsible.Does that seem sensible?