Skip to content

Conversation

@GrigoryEvko
Copy link

Description:

Partially addresses #481 by adding unchecked shift operations to SimdInt and SimdUint traits.

Changes

Adds unchecked_shl and unchecked_shr methods that skip the shift amount masking performed by regular shift
operators. 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 masking

Scope

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:

  • Valid shift amounts
  • Zero shift
  • Shift by 1

Closes #481
Related: rust-lang/libs-team#662

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.
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.

Add unchecked_* methods for integers

1 participant