Skip to content

Restructure crate as core module #160

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

Merged
merged 1 commit into from
Sep 19, 2021
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
15 changes: 8 additions & 7 deletions crates/core_simd/src/comparisons.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{LaneCount, Mask, Simd, SimdElement, SupportedLaneCount};
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Mask, Simd, SimdElement, SupportedLaneCount};

impl<T, const LANES: usize> Simd<T, LANES>
where
Expand All @@ -8,13 +9,13 @@ where
/// Test if each lane is equal to the corresponding lane in `other`.
#[inline]
pub fn lanes_eq(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_eq(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_eq(self, other)) }
}

/// Test if each lane is not equal to the corresponding lane in `other`.
#[inline]
pub fn lanes_ne(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ne(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_ne(self, other)) }
}
}

Expand All @@ -26,24 +27,24 @@ where
/// Test if each lane is less than the corresponding lane in `other`.
#[inline]
pub fn lanes_lt(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_lt(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_lt(self, other)) }
}

/// Test if each lane is greater than the corresponding lane in `other`.
#[inline]
pub fn lanes_gt(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_gt(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_gt(self, other)) }
}

/// Test if each lane is less than or equal to the corresponding lane in `other`.
#[inline]
pub fn lanes_le(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_le(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_le(self, other)) }
}

/// Test if each lane is greater than or equal to the corresponding lane in `other`.
#[inline]
pub fn lanes_ge(self, other: Self) -> Mask<T::Mask, LANES> {
unsafe { Mask::from_int_unchecked(crate::intrinsics::simd_ge(self, other)) }
unsafe { Mask::from_int_unchecked(intrinsics::simd_ge(self, other)) }
}
}
4 changes: 4 additions & 0 deletions crates/core_simd/src/core_simd_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Portable SIMD module.

This module offers a portable abstraction for SIMD operations
that is not bound to any particular hardware architecture.
17 changes: 10 additions & 7 deletions crates/core_simd/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
use core::fmt;

macro_rules! impl_fmt_trait {
{ $($trait:ident,)* } => {
$(
impl<T, const LANES: usize> core::fmt::$trait for crate::Simd<T, LANES>
impl<T, const LANES: usize> fmt::$trait for Simd<T, LANES>
where
crate::LaneCount<LANES>: crate::SupportedLaneCount,
T: crate::SimdElement + core::fmt::$trait,
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement + fmt::$trait,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[repr(transparent)]
struct Wrapper<'a, T: core::fmt::$trait>(&'a T);
struct Wrapper<'a, T: fmt::$trait>(&'a T);

impl<T: core::fmt::$trait> core::fmt::Debug for Wrapper<'_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
impl<T: fmt::$trait> fmt::Debug for Wrapper<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/core_simd/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ extern "platform-intrinsic" {
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;

// select
pub(crate) fn simd_select<T, U>(m: T, a: U, b: U) -> U;
pub(crate) fn simd_select<M, T>(m: M, a: T, b: T) -> T;
#[allow(unused)]
pub(crate) fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
pub(crate) fn simd_select_bitmask<M, T>(m: M, a: T, b: T) -> T;
}

#[cfg(feature = "std")]
Expand All @@ -114,4 +114,4 @@ mod std {
}

#[cfg(feature = "std")]
pub(crate) use crate::intrinsics::std::*;
pub(crate) use crate::simd::intrinsics::std::*;
4 changes: 2 additions & 2 deletions crates/core_simd/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{LaneCount, Simd, SupportedLaneCount};
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
use core::{
iter::{Product, Sum},
ops::{Add, Mul},
Expand All @@ -15,7 +15,7 @@ macro_rules! impl_traits {
}
}

impl<const LANES: usize> core::iter::Product<Self> for Simd<$type, LANES>
impl<const LANES: usize> Product<Self> for Simd<$type, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
Expand Down
31 changes: 3 additions & 28 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,6 @@
#![unstable(feature = "portable_simd", issue = "86656")]
//! Portable SIMD module.

#[macro_use]
mod permute;
#[macro_use]
mod reduction;

mod select;
pub use select::Select;

#[cfg(feature = "generic_const_exprs")]
mod to_bytes;

mod comparisons;
mod fmt;
mod intrinsics;
mod iter;
mod math;
mod ops;
mod round;
mod vendor;

mod lane_count;
pub use lane_count::*;

mod masks;
pub use masks::*;

mod vector;
pub use vector::*;
#[path = "mod.rs"]
mod core_simd;
pub use self::core_simd::simd::*;
12 changes: 7 additions & 5 deletions crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
)]
mod mask_impl;

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

/// Marker trait for types that may be used as SIMD mask elements.
pub unsafe trait MaskElement: SimdElement {
Expand Down Expand Up @@ -251,17 +253,17 @@ where
LaneCount<LANES>: SupportedLaneCount,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}

impl<T, const LANES: usize> core::fmt::Debug for Mask<T, LANES>
impl<T, const LANES: usize> fmt::Debug for Mask<T, LANES>
where
T: MaskElement + core::fmt::Debug,
T: MaskElement + fmt::Debug,
LaneCount<LANES>: SupportedLaneCount,
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries((0..LANES).map(|lane| self.test(lane)))
.finish()
Expand Down
11 changes: 4 additions & 7 deletions crates/core_simd/src/masks/bitmask.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{LaneCount, MaskElement, Simd, SupportedLaneCount};
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
use core::marker::PhantomData;

/// A mask where each lane is represented by a single bit.
Expand Down Expand Up @@ -99,11 +100,7 @@ where
unsafe {
let mask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
core::mem::transmute_copy(&self);
crate::intrinsics::simd_select_bitmask(
mask,
Simd::splat(T::TRUE),
Simd::splat(T::FALSE),
)
intrinsics::simd_select_bitmask(mask, Simd::splat(T::TRUE), Simd::splat(T::FALSE))
}
}

Expand All @@ -115,7 +112,7 @@ where
core::mem::size_of::<<LaneCount::<LANES> as SupportedLaneCount>::IntBitMask>(),
);
let mask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
crate::intrinsics::simd_bitmask(value);
intrinsics::simd_bitmask(value);
Self(core::mem::transmute_copy(&mask), PhantomData)
}

Expand Down
19 changes: 10 additions & 9 deletions crates/core_simd/src/masks/full_masks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Masks that take up full SIMD vector registers.

use super::MaskElement;
use crate::{LaneCount, Simd, SupportedLaneCount};
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SupportedLaneCount};

#[repr(transparent)]
pub struct Mask<T, const LANES: usize>(Simd<T, LANES>)
Expand Down Expand Up @@ -98,7 +99,7 @@ where
where
U: MaskElement,
{
unsafe { Mask(crate::intrinsics::simd_cast(self.0)) }
unsafe { Mask(intrinsics::simd_cast(self.0)) }
}

#[cfg(feature = "generic_const_exprs")]
Expand All @@ -111,7 +112,7 @@ where
LaneCount::<LANES>::BITMASK_LEN,
);
let bitmask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
crate::intrinsics::simd_bitmask(self.0);
intrinsics::simd_bitmask(self.0);
let mut bitmask: [u8; LaneCount::<LANES>::BITMASK_LEN] =
core::mem::transmute_copy(&bitmask);

Expand Down Expand Up @@ -149,7 +150,7 @@ where
let bitmask: <LaneCount<LANES> as SupportedLaneCount>::IntBitMask =
core::mem::transmute_copy(&bitmask);

Self::from_int_unchecked(crate::intrinsics::simd_select_bitmask(
Self::from_int_unchecked(intrinsics::simd_select_bitmask(
bitmask,
Self::splat(true).to_int(),
Self::splat(false).to_int(),
Expand All @@ -159,12 +160,12 @@ where

#[inline]
pub fn any(self) -> bool {
unsafe { crate::intrinsics::simd_reduce_any(self.to_int()) }
unsafe { intrinsics::simd_reduce_any(self.to_int()) }
}

#[inline]
pub fn all(self) -> bool {
unsafe { crate::intrinsics::simd_reduce_all(self.to_int()) }
unsafe { intrinsics::simd_reduce_all(self.to_int()) }
}
}

Expand All @@ -186,7 +187,7 @@ where
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
unsafe { Self(crate::intrinsics::simd_and(self.0, rhs.0)) }
unsafe { Self(intrinsics::simd_and(self.0, rhs.0)) }
}
}

Expand All @@ -198,7 +199,7 @@ where
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
unsafe { Self(crate::intrinsics::simd_or(self.0, rhs.0)) }
unsafe { Self(intrinsics::simd_or(self.0, rhs.0)) }
}
}

Expand All @@ -210,7 +211,7 @@ where
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
unsafe { Self(crate::intrinsics::simd_xor(self.0, rhs.0)) }
unsafe { Self(intrinsics::simd_xor(self.0, rhs.0)) }
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/core_simd/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{LaneCount, Simd, SupportedLaneCount};
use crate::simd::intrinsics::{simd_saturating_add, simd_saturating_sub};
use crate::simd::{LaneCount, Simd, SupportedLaneCount};

macro_rules! impl_uint_arith {
($($ty:ty),+) => {
Expand All @@ -20,7 +21,7 @@ macro_rules! impl_uint_arith {
/// ```
#[inline]
pub fn saturating_add(self, second: Self) -> Self {
unsafe { crate::intrinsics::simd_saturating_add(self, second) }
unsafe { simd_saturating_add(self, second) }
}

/// Lanewise saturating subtract.
Expand All @@ -38,7 +39,7 @@ macro_rules! impl_uint_arith {
/// assert_eq!(sat, Simd::splat(0));
#[inline]
pub fn saturating_sub(self, second: Self) -> Self {
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
unsafe { simd_saturating_sub(self, second) }
}
})+
}
Expand All @@ -64,7 +65,7 @@ macro_rules! impl_int_arith {
/// ```
#[inline]
pub fn saturating_add(self, second: Self) -> Self {
unsafe { crate::intrinsics::simd_saturating_add(self, second) }
unsafe { simd_saturating_add(self, second) }
}

/// Lanewise saturating subtract.
Expand All @@ -82,7 +83,7 @@ macro_rules! impl_int_arith {
/// assert_eq!(sat, Simd::from_array([MIN, MIN, MIN, 0]));
#[inline]
pub fn saturating_sub(self, second: Self) -> Self {
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
unsafe { simd_saturating_sub(self, second) }
}

/// Lanewise absolute value, implemented in Rust.
Expand Down
33 changes: 33 additions & 0 deletions crates/core_simd/src/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[macro_use]
mod permute;
#[macro_use]
mod reduction;

mod select;

#[cfg(feature = "generic_const_exprs")]
mod to_bytes;

mod comparisons;
mod fmt;
mod intrinsics;
mod iter;
mod math;
mod ops;
mod round;
mod vendor;

mod lane_count;

mod masks;

mod vector;

#[doc = include_str!("core_simd_docs.md")]
pub mod simd {
pub use crate::core_simd::lane_count::*;
pub use crate::core_simd::masks::*;
pub use crate::core_simd::select::Select;
pub use crate::core_simd::vector::*;
pub(crate) use crate::core_simd::*;
}
Loading