Skip to content

Conversation

madhav-madhusoodanan
Copy link
Contributor

@madhav-madhusoodanan madhav-madhusoodanan commented Oct 2, 2025

Summary

The _kshiftri_mask32, _kshiftri_mask64, _kshiftli_mask32 and _kshiftli_mask64 intrinsics in core_arch::x86 module do not handle a critical edge case when the shift amount exceeds the bit-length of the input argument.

EDIT: updated the 8-bit and 16-bit versions of the same.

Current behaviour

For _kshiftri_mask32 and _kshiftli_mask32 intrinsics, when the shift amount (passed as a const-generic argument) exceeds 32, the shift amount applied to the argument becomes shift % 32.

Similar is the case with the 8-bit, 16-bit and 64-bit variants of the same.

Godbolt link for the 32-bit variant with minimal replication.
Godbolt link for the 64-bit variant with minimal replication.

Expected behaviour

When the shift amount exceeds 32 (for the 32-bit versions) or 64 (for the 64-bit versions) the result becomes zero, in line with Intel's documentation on the same.

Similar is the case with 8-bit and 16-bit versions.

Fix

Use unbounded_shr() and unbounded_shl() functions instead of >> and << operations.

Godbolt link that shows the corrected version of the 32-bit implementation.
Godbolt link that shows the corrected version of the 64-bit implementation

r? @sayantn
cc: @folkertdev

@sayantn
Copy link
Contributor

sayantn commented Oct 2, 2025

There are 8- and 16-bit versions too in avx512dq, check them too pls

_kshiftli_mask32 and _kshiftli_mask64 to zero out when the amount of
shift exceeds the bit length of the input argument.
@madhav-madhusoodanan madhav-madhusoodanan force-pushed the x86_fix_kshift_instructions branch from 4be142a to 6e263ec Compare October 2, 2025 20:51
zero out when the amount of shift exceeds the bit length of the input
argument.
to zero out when the amount of shift exceeds 16.
@madhav-madhusoodanan
Copy link
Contributor Author

Done @sayantn

@sayantn
Copy link
Contributor

sayantn commented Oct 2, 2025

Thanks, I have opened #1931 to fix the other shift-related bugs

@sayantn sayantn added this pull request to the merge queue Oct 2, 2025
Merged via the queue into rust-lang:master with commit 03ad8a7 Oct 2, 2025
63 checks passed
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