Uh oh!
There was an error while loading. Please reload this page.
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 constant0x01010101Usatisfies that criterion: by C99 §6.4.4.1, together with C's guarantees about the minimum precision oflong, it has type eitherunsigned intorunsigned long. Auint32_tconstant 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_twon't work. If we declareSUMasunsigned intinstead ofuint32_t, we have portability issues on machines whereintisn'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+ 0Ueffectively 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.