Skip to content

Document that the result of shifting out of bounds varies by platform - #283

Merged
Shnatsel merged 4 commits into
linebender:mainfrom
Shnatsel:document-platform-variation
Jul 27, 2026
Merged

Document that the result of shifting out of bounds varies by platform#283
Shnatsel merged 4 commits into
linebender:mainfrom
Shnatsel:document-platform-variation

Conversation

@Shnatsel

Copy link
Copy Markdown
Contributor

Now that I've researched it for #279 (comment), might as well document it.

I also had Codex write tests to probe for other platform divergences and it didn't find anything that isn't already documented, other than differences in how the signalling bit in NaNs is propagated or not in some ops. But the standard library notes that you "very likely" don't care if a NaN is signalling or not, so I didn't bother documenting that.

Comment on lines +1026 to +1027
For unsigned integers, zeros are shifted in on the left. For signed integers, the sign bit is replicated.\n\n\
When shifting out of bounds (e.g. shifting a 32-bit value by 32 or more), the result is implementation-defined and may vary by platform.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you mentioned that it might panic in debug mode for fallback, maybe expicitly adding "or panic" might be a good idea?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just make it consistently panic in debug mode, then?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the other backends? I don't think we have to, just would be good to clarify that "implementation-defined" can also include panicking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it might be good idea to panic in debug mode here, since doing out-of-bounds shifts and relying on the result is a footgun. But std has explicit wrapping shifts while we don't, so it's probably best not to force panics on people.

We could make fallback wrap instead of panicking; I think that's the best approach, considering the above.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap sounds good to me.

@Shnatsel
Shnatsel enabled auto-merge July 27, 2026 17:03
@Shnatsel
Shnatsel added this pull request to the merge queue Jul 27, 2026
Merged via the queue into linebender:main with commit f1767c3 Jul 27, 2026
22 checks passed
@Shnatsel
Shnatsel deleted the document-platform-variation branch July 27, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants