Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UB in _mm_movemask_ps, _mm_movemask_pd, _mm256_movemask_ps and _mm256_movemask_pd #1482

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/core_arch/src/x86/avx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,10 @@ pub unsafe fn _mm_testnzc_ps(a: __m128, b: __m128) -> i32 {
#[cfg_attr(test, assert_instr(vmovmskpd))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm256_movemask_pd(a: __m256d) -> i32 {
simd_bitmask::<u64x4, u8>(transmute(a)).into()
// Propagate the highest bit to the rest, because simd_bitmask
// requires all-1 or all-0.
let mask: i64x4 = simd_lt(transmute(a), i64x4::splat(0));
simd_bitmask::<i64x4, u8>(mask).into()
}

/// Sets each bit of the returned mask based on the most significant bit of the
Expand All @@ -2079,7 +2082,10 @@ pub unsafe fn _mm256_movemask_pd(a: __m256d) -> i32 {
#[cfg_attr(test, assert_instr(vmovmskps))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm256_movemask_ps(a: __m256) -> i32 {
simd_bitmask::<u32x8, u8>(transmute(a)).into()
// Propagate the highest bit to the rest, because simd_bitmask
// requires all-1 or all-0.
let mask: i32x8 = simd_lt(transmute(a), i32x8::splat(0));
simd_bitmask::<i32x8, u8>(mask).into()
}

/// Returns vector of type __m256d with all elements set to zero.
Expand Down
5 changes: 4 additions & 1 deletion crates/core_arch/src/x86/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,10 @@ pub unsafe fn _mm_movelh_ps(a: __m128, b: __m128) -> __m128 {
#[cfg_attr(test, assert_instr(movmskps))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_movemask_ps(a: __m128) -> i32 {
simd_bitmask::<u32x4, u8>(transmute(a)).into()
// Propagate the highest bit to the rest, because simd_bitmask
// requires all-1 or all-0.
let mask: i32x4 = simd_lt(transmute(a), i32x4::splat(0));
simd_bitmask::<i32x4, u8>(mask).into()
}

/// Construct a `__m128` with the lowest element read from `p` and the other
Expand Down
5 changes: 4 additions & 1 deletion crates/core_arch/src/x86/sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,10 @@ pub unsafe fn _mm_setzero_pd() -> __m128d {
#[cfg_attr(test, assert_instr(movmskpd))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_movemask_pd(a: __m128d) -> i32 {
simd_bitmask::<u64x2, u8>(transmute(a)).into()
// Propagate the highest bit to the rest, because simd_bitmask
// requires all-1 or all-0.
let mask: i64x2 = simd_lt(transmute(a), i64x2::splat(0));
simd_bitmask::<i64x2, u8>(mask).into()
}

/// Loads 128-bits (composed of 2 packed double-precision (64-bit)
Expand Down
Loading