Fix left shift operator undefined behavior in integer_ops#2627
Open
joselopeqti wants to merge 2 commits intoKhronosGroup:mainfrom
Open
Fix left shift operator undefined behavior in integer_ops#2627joselopeqti wants to merge 2 commits intoKhronosGroup:mainfrom
joselopeqti wants to merge 2 commits intoKhronosGroup:mainfrom
Conversation
svenvh
approved these changes
Mar 5, 2026
bashbaug
reviewed
Mar 9, 2026
Comment on lines
+38
to
+43
| cl_ulong mask = 0; | ||
| if (offset < 64) | ||
| mask = (count < 64) ? ((1ULL << count) - 1) << offset : ~0ULL; | ||
| cl_ulong shifted = 0; | ||
| if (offset < 64) shifted = (insert << offset); | ||
| cl_ulong result = (shifted & mask) | (base & ~mask); |
Contributor
There was a problem hiding this comment.
Could we do something like this? A bit more compact and easier to understand (IMHO):
Suggested change
| cl_ulong mask = 0; | |
| if (offset < 64) | |
| mask = (count < 64) ? ((1ULL << count) - 1) << offset : ~0ULL; | |
| cl_ulong shifted = 0; | |
| if (offset < 64) shifted = (insert << offset); | |
| cl_ulong result = (shifted & mask) | (base & ~mask); | |
| cl_ulong result = base; | |
| if (offset < 64) | |
| { | |
| cl_ulong mask = (count < 64) ? ((1ULL << count) - 1) << offset : ~0ULL; | |
| result = ((insert << offset) & mask) | (base & ~mask); | |
| } |
I also considered this version with just the ternary operator also, but I think the version above with the single if statement is easier to understand.
cl_ulong mask =
(count < 64) && (offset < 64) ? ((1ULL << count) - 1) << offset : ~0ULL;
cl_ulong result =
(offset < 64) ? ((insert << offset) & mask) | (base & ~mask) : base;
Member
On current |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
As per the C++ Programming Reference page n0: 151, section: 7.6.7 Shift operators
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/n5001.pdf