Skip to content

Commit

Permalink
Merge pull request rust-lang#386 from rust-lang/core-intrinsics
Browse files Browse the repository at this point in the history
Use core::intrinsics
  • Loading branch information
calebzulawski authored Jan 5, 2024
2 parents 1273da6 + e7130ec commit 97007cc
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 286 deletions.
170 changes: 0 additions & 170 deletions crates/core_simd/src/intrinsics.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![no_std]
#![feature(
core_intrinsics,
const_refs_to_cell,
const_maybe_uninit_as_mut_ptr,
const_mut_refs,
convert_float_to_int,
decl_macro,
inline_const,
intra_doc_pointers,
platform_intrinsics,
repr_simd,
simd_ffi,
staged_api,
Expand Down
19 changes: 10 additions & 9 deletions crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
)]
mod mask_impl;

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

Expand Down Expand Up @@ -141,8 +139,9 @@ where
// but these are "dependently-sized" types, so copy elision it is!
unsafe {
let bytes: [u8; N] = mem::transmute_copy(&array);
let bools: Simd<i8, N> = intrinsics::simd_ne(Simd::from_array(bytes), Simd::splat(0u8));
Mask::from_int_unchecked(intrinsics::simd_cast(bools))
let bools: Simd<i8, N> =
core::intrinsics::simd::simd_ne(Simd::from_array(bytes), Simd::splat(0u8));
Mask::from_int_unchecked(core::intrinsics::simd::simd_cast(bools))
}
}

Expand All @@ -160,7 +159,7 @@ where
// This would be hypothetically valid as an "in-place" transmute,
// but these are "dependently-sized" types, so copy elision it is!
unsafe {
let mut bytes: Simd<i8, N> = intrinsics::simd_cast(self.to_int());
let mut bytes: Simd<i8, N> = core::intrinsics::simd::simd_cast(self.to_int());
bytes &= Simd::splat(1i8);
mem::transmute_copy(&bytes)
}
Expand Down Expand Up @@ -374,15 +373,17 @@ where
);

// Safety: the input and output are integer vectors
let index: Simd<T, N> = unsafe { intrinsics::simd_cast(index) };
let index: Simd<T, N> = unsafe { core::intrinsics::simd::simd_cast(index) };

let masked_index = self.select(index, Self::splat(true).to_int());

// Safety: the input and output are integer vectors
let masked_index: Simd<T::Unsigned, N> = unsafe { intrinsics::simd_cast(masked_index) };
let masked_index: Simd<T::Unsigned, N> =
unsafe { core::intrinsics::simd::simd_cast(masked_index) };

// Safety: the input is an integer vector
let min_index: T::Unsigned = unsafe { intrinsics::simd_reduce_min(masked_index) };
let min_index: T::Unsigned =
unsafe { core::intrinsics::simd::simd_reduce_min(masked_index) };

// Safety: the return value is the unsigned version of T
let min_index: T = unsafe { core::mem::transmute_copy(&min_index) };
Expand Down
9 changes: 6 additions & 3 deletions crates/core_simd/src/masks/bitmask.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unused_imports)]
use super::MaskElement;
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
use core::marker::PhantomData;

Expand Down Expand Up @@ -109,14 +108,18 @@ where
#[must_use = "method returns a new vector and does not mutate the original value"]
pub fn to_int(self) -> Simd<T, N> {
unsafe {
intrinsics::simd_select_bitmask(self.0, Simd::splat(T::TRUE), Simd::splat(T::FALSE))
core::intrinsics::simd::simd_select_bitmask(
self.0,
Simd::splat(T::TRUE),
Simd::splat(T::FALSE),
)
}
}

#[inline]
#[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 {
unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) }
unsafe { Self(core::intrinsics::simd::simd_bitmask(value), PhantomData) }
}

#[inline]
Expand Down
21 changes: 10 additions & 11 deletions crates/core_simd/src/masks/full_masks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Masks that take up full SIMD vector registers.
use crate::simd::intrinsics;
use crate::simd::{LaneCount, MaskElement, Simd, SupportedLaneCount};

#[repr(transparent)]
Expand Down Expand Up @@ -138,7 +137,7 @@ where
U: MaskElement,
{
// Safety: masks are simply integer vectors of 0 and -1, and we can cast the element type.
unsafe { Mask(intrinsics::simd_cast(self.0)) }
unsafe { Mask(core::intrinsics::simd::simd_cast(self.0)) }
}

#[inline]
Expand All @@ -150,7 +149,7 @@ where
unsafe {
// Compute the bitmask
let mut bytes: <LaneCount<N> as SupportedLaneCount>::BitMask =
intrinsics::simd_bitmask(self.0);
core::intrinsics::simd::simd_bitmask(self.0);

// LLVM assumes bit order should match endianness
if cfg!(target_endian = "big") {
Expand Down Expand Up @@ -183,7 +182,7 @@ where
}

// Compute the regular mask
Self::from_int_unchecked(intrinsics::simd_select_bitmask(
Self::from_int_unchecked(core::intrinsics::simd::simd_select_bitmask(
bytes,
Self::splat(true).to_int(),
Self::splat(false).to_int(),
Expand All @@ -199,7 +198,7 @@ where
let resized = self.to_int().resize::<M>(T::FALSE);

// Safety: `resized` is an integer vector with length M, which must match T
let bitmask: U = unsafe { intrinsics::simd_bitmask(resized) };
let bitmask: U = unsafe { core::intrinsics::simd::simd_bitmask(resized) };

// LLVM assumes bit order should match endianness
if cfg!(target_endian = "big") {
Expand All @@ -223,7 +222,7 @@ where

// SAFETY: `mask` is the correct bitmask type for a u64 bitmask
let mask: Simd<T, M> = unsafe {
intrinsics::simd_select_bitmask(
core::intrinsics::simd::simd_select_bitmask(
bitmask,
Simd::<T, M>::splat(T::TRUE),
Simd::<T, M>::splat(T::FALSE),
Expand Down Expand Up @@ -274,14 +273,14 @@ where
#[must_use = "method returns a new bool and does not mutate the original value"]
pub fn any(self) -> bool {
// Safety: use `self` as an integer vector
unsafe { intrinsics::simd_reduce_any(self.to_int()) }
unsafe { core::intrinsics::simd::simd_reduce_any(self.to_int()) }
}

#[inline]
#[must_use = "method returns a new vector and does not mutate the original value"]
pub fn all(self) -> bool {
// Safety: use `self` as an integer vector
unsafe { intrinsics::simd_reduce_all(self.to_int()) }
unsafe { core::intrinsics::simd::simd_reduce_all(self.to_int()) }
}
}

Expand All @@ -306,7 +305,7 @@ where
#[must_use = "method returns a new mask and does not mutate the original value"]
fn bitand(self, rhs: Self) -> Self {
// Safety: `self` is an integer vector
unsafe { Self(intrinsics::simd_and(self.0, rhs.0)) }
unsafe { Self(core::intrinsics::simd::simd_and(self.0, rhs.0)) }
}
}

Expand All @@ -320,7 +319,7 @@ where
#[must_use = "method returns a new mask and does not mutate the original value"]
fn bitor(self, rhs: Self) -> Self {
// Safety: `self` is an integer vector
unsafe { Self(intrinsics::simd_or(self.0, rhs.0)) }
unsafe { Self(core::intrinsics::simd::simd_or(self.0, rhs.0)) }
}
}

Expand All @@ -334,7 +333,7 @@ where
#[must_use = "method returns a new mask and does not mutate the original value"]
fn bitxor(self, rhs: Self) -> Self {
// Safety: `self` is an integer vector
unsafe { Self(intrinsics::simd_xor(self.0, rhs.0)) }
unsafe { Self(core::intrinsics::simd::simd_xor(self.0, rhs.0)) }
}
}

Expand Down
4 changes: 0 additions & 4 deletions crates/core_simd/src/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[macro_use]
mod swizzle;

pub(crate) mod intrinsics;

mod alias;
mod cast;
mod fmt;
Expand All @@ -27,8 +25,6 @@ pub mod simd {

pub mod cmp;

pub(crate) use crate::core_simd::intrinsics;

pub use crate::core_simd::alias::*;
pub use crate::core_simd::cast::*;
pub use crate::core_simd::lane_count::{LaneCount, SupportedLaneCount};
Expand Down
Loading

0 comments on commit 97007cc

Please sign in to comment.