Add unchecked_shl and unchecked_shr for SIMD integers#488
Open
GrigoryEvko wants to merge 1 commit intorust-lang:masterfrom
Open
Add unchecked_shl and unchecked_shr for SIMD integers#488GrigoryEvko wants to merge 1 commit intorust-lang:masterfrom
GrigoryEvko wants to merge 1 commit intorust-lang:masterfrom
Conversation
Adds unchecked shift operations to SimdInt and SimdUint traits. These skip the masking that regular shifts perform, providing optimization opportunities when the shift amount is known to be valid. Regular shifts mask: (value << rhs) where rhs &= (BITS-1) Unchecked shifts: direct simd_shl/simd_shr without masking Safety: Caller must ensure shift amount < BITS for all lanes.
| ) | ||
| } | ||
|
|
||
| fn unchecked_shl<const LANES: usize>() { |
Member
There was a problem hiding this comment.
Perhaps use test_helpers::test_binary_elementwise but apply a bitmask/clamp to the rhs argument to ensure it's always less than the number of bits.
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.
Description:
Partially addresses #481 by adding unchecked shift operations to
SimdIntandSimdUinttraits.Changes
Adds
unchecked_shlandunchecked_shrmethods that skip the shift amount masking performed by regular shiftoperators. These provide optimization opportunities when the shift amount is known to be valid.
Regular shifts:
value << rhs // Masks: rhs &= (BITS - 1)Unchecked shifts:
unsafe { value.unchecked_shl(rhs) } // Direct simd_shl without maskingScope
This PR implements only shift operations, not arithmetic operations (add/sub/mul).
Rationale: SIMD arithmetic operations use the same LLVM intrinsics as regular operators (simd_add, simd_sub,
simd_mul) and provide no optimization benefit. LLVM does not currently expose nsw/nuw (no signed/unsigned
wrap) variants for SIMD vectors. In contrast, shift operations can skip the masking instruction, providing
measurable performance improvement.
Safety
Caller must ensure rhs < T::BITS for all lanes. Violating this results in undefined behavior.
Testing
Tests included for:
Closes #481
Related: rust-lang/libs-team#662