Skip to content

Commit

Permalink
Merge pull request #365 from rust-lang/modules
Browse files Browse the repository at this point in the history
Add num, ptr, and cmp modules
  • Loading branch information
calebzulawski authored Oct 21, 2023
2 parents 3e4e13c + 596aabe commit 21fa6af
Show file tree
Hide file tree
Showing 27 changed files with 104 additions and 65 deletions.
2 changes: 1 addition & 1 deletion crates/core_simd/examples/dot_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![feature(slice_as_chunks)]
// Add these imports to use the stdsimd library
#![feature(portable_simd)]
use core_simd::simd::*;
use core_simd::simd::prelude::*;

// This is your barebones dot product implementation:
// Take 2 vectors, multiply them element wise and *then*
Expand Down
6 changes: 4 additions & 2 deletions crates/core_simd/examples/matrix_inversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Code ported from the `packed_simd` crate
// Run this code with `cargo test --example matrix_inversion`
#![feature(array_chunks, portable_simd)]
use core_simd::simd::*;
use Which::*;
use core_simd::simd::{
prelude::*,
Which::{self, *},
};

// Gotta define our own 4x4 matrix since Rust doesn't ship multidim arrays yet :^)
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/examples/nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate std_float;
/// Taken from the `packed_simd` crate
/// Run this benchmark with `cargo test --example nbody`
mod nbody {
use core_simd::simd::*;
use core_simd::simd::prelude::*;
#[allow(unused)] // False positive?
use std_float::StdFloat;

Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/examples/spectral_norm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(portable_simd)]

use core_simd::simd::*;
use core_simd::simd::prelude::*;

fn a(i: usize, j: usize) -> f64 {
((i + j) * (i + j + 1) / 2 + i + 1) as f64
Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/src/core_simd_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Instead, they map to a reasonable implementation of the operation for the target

Consistency between targets is not compromised to use faster or fewer instructions.
In some cases, `std::arch` will provide a faster function that has slightly different behavior than the `std::simd` equivalent.
For example, [`_mm_min_ps`](`core::arch::x86_64::_mm_min_ps`)[^1] can be slightly faster than [`SimdFloat::simd_min`], but does not conform to the IEEE standard also used by [`f32::min`].
For example, [`_mm_min_ps`](`core::arch::x86_64::_mm_min_ps`)[^1] can be slightly faster than [`SimdFloat::simd_min`](`num::SimdFloat::simd_min`), but does not conform to the IEEE standard also used by [`f32::min`].
When necessary, [`Simd<T, N>`] can be converted to the types provided by `std::arch` to make use of target-specific functions.

Many targets simply don't have SIMD, or don't support SIMD for a particular element type.
Expand Down
4 changes: 3 additions & 1 deletion crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ mod mask_impl;
mod to_bitmask;
pub use to_bitmask::{ToBitMask, ToBitMaskArray};

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

Expand Down
12 changes: 6 additions & 6 deletions crates/core_simd/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ pub(crate) mod intrinsics;

mod alias;
mod cast;
mod elements;
mod eq;
mod fmt;
mod iter;
mod lane_count;
mod masks;
mod ops;
mod ord;
mod select;
mod swizzle_dyn;
mod to_bytes;
Expand All @@ -24,15 +21,18 @@ pub mod simd {

pub mod prelude;

pub mod num;

pub mod ptr;

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::elements::*;
pub use crate::core_simd::eq::*;
pub use crate::core_simd::lane_count::{LaneCount, SupportedLaneCount};
pub use crate::core_simd::masks::*;
pub use crate::core_simd::ord::*;
pub use crate::core_simd::swizzle::*;
pub use crate::core_simd::swizzle_dyn::*;
pub use crate::core_simd::to_bytes::ToBytes;
Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::simd::{LaneCount, Simd, SimdElement, SimdPartialEq, SupportedLaneCount};
use crate::simd::{cmp::SimdPartialEq, LaneCount, Simd, SimdElement, SupportedLaneCount};
use core::ops::{Add, Mul};
use core::ops::{BitAnd, BitOr, BitXor};
use core::ops::{Div, Rem, Sub};
Expand Down
7 changes: 7 additions & 0 deletions crates/core_simd/src/simd/cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Traits for comparing and ordering vectors.
mod eq;
mod ord;

pub use eq::*;
pub use ord::*;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::simd::{
intrinsics, LaneCount, Mask, Simd, SimdConstPtr, SimdElement, SimdMutPtr, SupportedLaneCount,
intrinsics,
ptr::{SimdConstPtr, SimdMutPtr},
LaneCount, Mask, Simd, SimdElement, SupportedLaneCount,
};

/// Parallel `PartialEq`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::simd::{
intrinsics, LaneCount, Mask, Simd, SimdConstPtr, SimdMutPtr, SimdPartialEq, SupportedLaneCount,
cmp::SimdPartialEq,
intrinsics,
ptr::{SimdConstPtr, SimdMutPtr},
LaneCount, Mask, Simd, SupportedLaneCount,
};

/// Parallel `PartialOrd`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
mod const_ptr;
//! Traits for vectors with numeric elements.
mod float;
mod int;
mod mut_ptr;
mod uint;

mod sealed {
pub trait Sealed {}
}

pub use const_ptr::*;
pub use float::*;
pub use int::*;
pub use mut_ptr::*;
pub use uint::*;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{
intrinsics, LaneCount, Mask, Simd, SimdCast, SimdElement, SimdPartialEq, SimdPartialOrd,
SupportedLaneCount,
cmp::{SimdPartialEq, SimdPartialOrd},
intrinsics, LaneCount, Mask, Simd, SimdCast, SimdElement, SupportedLaneCount,
};

/// Operations on SIMD vectors of floats.
Expand All @@ -28,7 +28,7 @@ pub trait SimdFloat: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{SimdFloat, SimdInt, Simd};
/// # use simd::prelude::*;
/// let floats: Simd<f32, 4> = Simd::from_array([1.9, -4.5, f32::INFINITY, f32::NAN]);
/// let ints = floats.cast::<i32>();
/// assert_eq!(ints, Simd::from_array([1, -4, i32::MAX, 0]));
Expand Down Expand Up @@ -162,7 +162,7 @@ pub trait SimdFloat: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{f32x2, SimdFloat};
/// # use simd::prelude::*;
/// let v = f32x2::from_array([1., 2.]);
/// assert_eq!(v.reduce_sum(), 3.);
/// ```
Expand All @@ -176,7 +176,7 @@ pub trait SimdFloat: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{f32x2, SimdFloat};
/// # use simd::prelude::*;
/// let v = f32x2::from_array([3., 4.]);
/// assert_eq!(v.reduce_product(), 12.);
/// ```
Expand All @@ -195,7 +195,7 @@ pub trait SimdFloat: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{f32x2, SimdFloat};
/// # use simd::prelude::*;
/// let v = f32x2::from_array([1., 2.]);
/// assert_eq!(v.reduce_max(), 2.);
///
Expand All @@ -222,7 +222,7 @@ pub trait SimdFloat: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{f32x2, SimdFloat};
/// # use simd::prelude::*;
/// let v = f32x2::from_array([3., 7.]);
/// assert_eq!(v.reduce_min(), 3.);
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::sealed::Sealed;
use crate::simd::{
intrinsics, LaneCount, Mask, Simd, SimdCast, SimdElement, SimdPartialOrd, SimdUint,
cmp::SimdPartialOrd, intrinsics, num::SimdUint, LaneCount, Mask, Simd, SimdCast, SimdElement,
SupportedLaneCount,
};

Expand Down Expand Up @@ -32,7 +32,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdInt};
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let x = Simd::from_array([MIN, 0, 1, MAX]);
/// let max = Simd::splat(MAX);
Expand All @@ -50,7 +50,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdInt};
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let x = Simd::from_array([MIN, -2, -1, MAX]);
/// let max = Simd::splat(MAX);
Expand All @@ -68,7 +68,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdInt};
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let xs = Simd::from_array([MIN, MIN +1, -5, 0]);
/// assert_eq!(xs.abs(), Simd::from_array([MIN, MAX, 5, 0]));
Expand All @@ -83,7 +83,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdInt};
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let xs = Simd::from_array([MIN, -2, 0, 3]);
/// let unsat = xs.abs();
Expand All @@ -101,7 +101,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdInt};
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let x = Simd::from_array([MIN, -2, 3, MAX]);
/// let unsat = -x;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{i32x4, SimdInt};
/// # use simd::prelude::*;
/// let v = i32x4::from_array([1, 2, 3, 4]);
/// assert_eq!(v.reduce_sum(), 10);
///
Expand All @@ -149,7 +149,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{i32x4, SimdInt};
/// # use simd::prelude::*;
/// let v = i32x4::from_array([1, 2, 3, 4]);
/// assert_eq!(v.reduce_product(), 24);
///
Expand All @@ -167,7 +167,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{i32x4, SimdInt};
/// # use simd::prelude::*;
/// let v = i32x4::from_array([1, 2, 3, 4]);
/// assert_eq!(v.reduce_max(), 4);
/// ```
Expand All @@ -181,7 +181,7 @@ pub trait SimdInt: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{i32x4, SimdInt};
/// # use simd::prelude::*;
/// let v = i32x4::from_array([1, 2, 3, 4]);
/// assert_eq!(v.reduce_min(), 1);
/// ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait SimdUint: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdUint};
/// # use simd::prelude::*;
/// use core::u32::MAX;
/// let x = Simd::from_array([2, 1, 0, MAX]);
/// let max = Simd::splat(MAX);
Expand All @@ -47,7 +47,7 @@ pub trait SimdUint: Copy + Sealed {
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::{Simd, SimdUint};
/// # use simd::prelude::*;
/// use core::u32::MAX;
/// let x = Simd::from_array([2, 1, 0, MAX]);
/// let max = Simd::splat(MAX);
Expand Down Expand Up @@ -122,7 +122,7 @@ macro_rules! impl_trait {

#[inline]
fn wrapping_neg(self) -> Self {
use crate::simd::SimdInt;
use crate::simd::num::SimdInt;
(-self.cast::<$signed>()).cast()
}

Expand Down
6 changes: 4 additions & 2 deletions crates/core_simd/src/simd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#[doc(no_inline)]
pub use super::{
simd_swizzle, Mask, Simd, SimdConstPtr, SimdFloat, SimdInt, SimdMutPtr, SimdOrd, SimdPartialEq,
SimdPartialOrd, SimdUint,
cmp::{SimdOrd, SimdPartialEq, SimdPartialOrd},
num::{SimdFloat, SimdInt, SimdUint},
ptr::{SimdConstPtr, SimdMutPtr},
simd_swizzle, Mask, Simd,
};

#[rustfmt::skip]
Expand Down
11 changes: 11 additions & 0 deletions crates/core_simd/src/simd/ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Traits for vectors of pointers.
mod const_ptr;
mod mut_ptr;

mod sealed {
pub trait Sealed {}
}

pub use const_ptr::*;
pub use mut_ptr::*;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{intrinsics, LaneCount, Mask, Simd, SimdPartialEq, SimdUint, SupportedLaneCount};
use crate::simd::{
cmp::SimdPartialEq, intrinsics, num::SimdUint, LaneCount, Mask, Simd, SupportedLaneCount,
};

/// Operations on SIMD vectors of constant pointers.
pub trait SimdConstPtr: Copy + Sealed {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{intrinsics, LaneCount, Mask, Simd, SimdPartialEq, SimdUint, SupportedLaneCount};
use crate::simd::{
cmp::SimdPartialEq, intrinsics, num::SimdUint, LaneCount, Mask, Simd, SupportedLaneCount,
};

/// Operations on SIMD vectors of mutable pointers.
pub trait SimdMutPtr: Copy + Sealed {
Expand Down
4 changes: 2 additions & 2 deletions crates/core_simd/src/swizzle_dyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
#[inline]
#[allow(clippy::let_and_return)]
unsafe fn avx2_pshufb(bytes: Simd<u8, 32>, idxs: Simd<u8, 32>) -> Simd<u8, 32> {
use crate::simd::SimdPartialOrd;
use crate::simd::cmp::SimdPartialOrd;
#[cfg(target_arch = "x86")]
use core::arch::x86;
#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -149,7 +149,7 @@ where
// On x86, make sure the top bit is set.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
let idxs = {
use crate::simd::SimdPartialOrd;
use crate::simd::cmp::SimdPartialOrd;
idxs.simd_lt(Simd::splat(N as u8))
.select(idxs, Simd::splat(u8::MAX))
};
Expand Down
5 changes: 4 additions & 1 deletion crates/core_simd/src/to_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::simd::{LaneCount, Simd, SimdElement, SimdFloat, SimdInt, SimdUint, SupportedLaneCount};
use crate::simd::{
num::{SimdFloat, SimdInt, SimdUint},
LaneCount, Simd, SimdElement, SupportedLaneCount,
};

mod sealed {
use super::*;
Expand Down
Loading

0 comments on commit 21fa6af

Please sign in to comment.