Skip to content

Commit

Permalink
Merge pull request #393 from rust-lang/assume-masks-are-correct
Browse files Browse the repository at this point in the history
Assume masks are correct
  • Loading branch information
calebzulawski authored Feb 17, 2024
2 parents 061d5ac + aebf6f1 commit 7348d2d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)]
mod mask_impl;

use crate::simd::{cmp::SimdPartialEq, LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};
use crate::simd::{LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};
use core::cmp::Ordering;
use core::{fmt, mem};

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

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

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

0 comments on commit 7348d2d

Please sign in to comment.