-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29882: Fix portability bug introduced in GH-30774 #30794
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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.
Why removing this constant? I added it to not have to use the macro to get an uint32_t literal number. UINT32_C() if I recall correctly.
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.
@vstinner This is already explained in the long comment that you didn't review. :-)
The problem here isn't the constant; it's the type declaration.
For the multiplication in the last line of the function to be portable, we need at least one of the unsigned operands in that multiplication to not be promoted to
int
. An inline constant0x01010101U
satisfies that criterion: by C99 §6.4.4.1, together with C's guarantees about the minimum precision oflong
, it has type eitherunsigned int
orunsigned long
. Auint32_t
constant with the same value does not satisfy that criterion, for reasons already explained.And there isn't a type declaration that works here. I just explained why
uint32_t
won't work. If we declareSUM
asunsigned int
instead ofuint32_t
, we have portability issues on machines whereint
isn't large enough to represent the value0x01010101
. If we declare it asunsigned long
, we're back to doing a 64-bit-by-64-bit multiply on almost all current Linux and macOS boxes.If you really want to keep the constant, another option is to leave this line exactly as-is and change the multiplication in the last line to
x * (SUM + 0U)
; that+ 0U
effectively forces the second multiplicand to have type with rank greater than or equal to that ofint
, making it immune to further integer promotion.But as Tim observed in the #30774 discussion, none of this prevents us from potentially doing a 512-bit-by-512-bit multiply on a box that has 512-bit integers. But short of relying on compiler-specific intrinsics, that's inescapable anyway: standard C simply isn't capable of doing arithmetic on anything smaller than an
int
.