Add unchecked_shl and unchecked_shr for SIMD integers #488
+156
−0
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