Skip to content

Commit 7348d2d

Browse files
Merge pull request #393 from rust-lang/assume-masks-are-correct
Assume masks are correct
2 parents 061d5ac + aebf6f1 commit 7348d2d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

crates/core_simd/src/masks.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)]
1313
mod mask_impl;
1414

15-
use crate::simd::{cmp::SimdPartialEq, LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};
15+
use crate::simd::{LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};
1616
use core::cmp::Ordering;
1717
use core::{fmt, mem};
1818

@@ -58,7 +58,16 @@ macro_rules! impl_element {
5858
where
5959
LaneCount<N>: SupportedLaneCount,
6060
{
61-
(value.simd_eq(Simd::splat(0 as _)) | value.simd_eq(Simd::splat(-1 as _))).all()
61+
// We can't use `Simd` directly, because `Simd`'s functions call this function and
62+
// we will end up with an infinite loop.
63+
// Safety: `value` is an integer vector
64+
unsafe {
65+
use core::intrinsics::simd;
66+
let falses: Simd<Self, N> = simd::simd_eq(value, Simd::splat(0 as _));
67+
let trues: Simd<Self, N> = simd::simd_eq(value, Simd::splat(-1 as _));
68+
let valid: Simd<Self, N> = simd::simd_or(falses, trues);
69+
simd::simd_reduce_all(valid)
70+
}
6271
}
6372

6473
#[inline]
@@ -174,7 +183,10 @@ where
174183
#[must_use = "method returns a new mask and does not mutate the original value"]
175184
pub unsafe fn from_int_unchecked(value: Simd<T, N>) -> Self {
176185
// Safety: the caller must confirm this invariant
177-
unsafe { Self(mask_impl::Mask::from_int_unchecked(value)) }
186+
unsafe {
187+
core::intrinsics::assume(<T as Sealed>::valid(value));
188+
Self(mask_impl::Mask::from_int_unchecked(value))
189+
}
178190
}
179191

180192
/// Converts a vector of integers to a mask, where 0 represents `false` and -1

0 commit comments

Comments
 (0)