Skip to content

Commit

Permalink
Auto merge of rust-lang#93146 - workingjubilee:use-std-simd, r=Mark-S…
Browse files Browse the repository at this point in the history
…imulacrum

pub use std::simd::StdFloat;

Syncs portable-simd up to commit rust-lang/portable-simd@03f6fbb,
Diff: rust-lang/portable-simd@533f0fc...03f6fbb

This sync requires a little bit more legwork because it also introduces a trait into `std::simd`, so that it is no longer simply a reexport of `core::simd`. Out of simple-minded consistency and to allow more options, I replicated the pattern for the way `core::simd` is integrated in the first place, however this is not necessary if it doesn't acquire any interdependencies inside `std`: it could be a simple crate reexport. I just don't know yet if that will happen or not.

To summarize other misc changes:
- Shifts no longer panic, now wrap on too-large shifts (like `Simd` integers usually do!)
- mask16x32 will now be many i16s, not many i32s... 🙃
- `#[must_use]` is spread around generously
- Adjusts division, float min/max, and `Mask::{from,to}_array` internally to be faster
- Adds the much-requested `Simd::cast::<U>` function (equivalent to `simd.to_array().map(|lane| lane as U)`)
  • Loading branch information
bors committed Feb 3, 2022
2 parents 1be5c8f + e96159e commit 796bf14
Show file tree
Hide file tree
Showing 18 changed files with 537 additions and 335 deletions.
1 change: 1 addition & 0 deletions library/portable-simd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

members = [
"crates/core_simd",
"crates/std_float",
"crates/test_helpers",
]
3 changes: 3 additions & 0 deletions library/portable-simd/crates/core_simd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ features = ["alloc"]

[dev-dependencies.test_helpers]
path = "../test_helpers"

[dev-dependencies]
std_float = { path = "../std_float/", features = ["as_crate"] }
10 changes: 5 additions & 5 deletions library/portable-simd/crates/core_simd/examples/nbody.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![cfg_attr(feature = "std", feature(portable_simd))]
#![feature(portable_simd)]
extern crate std_float;

/// Benchmarks game nbody code
/// Taken from the `packed_simd` crate
/// Run this benchmark with `cargo test --example nbody`
#[cfg(feature = "std")]
mod nbody {
use core_simd::*;
use core_simd::simd::*;
#[allow(unused)] // False positive?
use std_float::StdFloat;

use std::f64::consts::PI;
const SOLAR_MASS: f64 = 4.0 * PI * PI;
Expand Down Expand Up @@ -167,7 +169,6 @@ mod nbody {
}
}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
// Good enough for demonstration purposes, not going for strictness here.
Expand All @@ -184,7 +185,6 @@ mod tests {
}

fn main() {
#[cfg(feature = "std")]
{
let (energy_before, energy_after) = nbody::run(1000);
println!("Energy before: {}", energy_before);
Expand Down
34 changes: 8 additions & 26 deletions library/portable-simd/crates/core_simd/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ extern "platform-intrinsic" {

/// fptoui/fptosi/uitofp/sitofp
pub(crate) fn simd_cast<T, U>(x: T) -> U;
/// follows Rust's `T as U` semantics, including saturating float casts
/// which amounts to the same as `simd_cast` for many cases
#[cfg(not(bootstrap))]
pub(crate) fn simd_as<T, U>(x: T) -> U;

/// neg/fneg
pub(crate) fn simd_neg<T>(x: T) -> T;

/// fabs
pub(crate) fn simd_fabs<T>(x: T) -> T;

// minnum/maxnum
pub(crate) fn simd_fmin<T>(x: T, y: T) -> T;
pub(crate) fn simd_fmax<T>(x: T, y: T) -> T;

pub(crate) fn simd_eq<T, U>(x: T, y: T) -> U;
pub(crate) fn simd_ne<T, U>(x: T, y: T) -> U;
pub(crate) fn simd_lt<T, U>(x: T, y: T) -> U;
Expand Down Expand Up @@ -87,29 +95,3 @@ extern "platform-intrinsic" {
#[allow(unused)]
pub(crate) fn simd_select_bitmask<M, T>(m: M, a: T, b: T) -> T;
}

#[cfg(feature = "std")]
mod std {
extern "platform-intrinsic" {
// ceil
pub(crate) fn simd_ceil<T>(x: T) -> T;

// floor
pub(crate) fn simd_floor<T>(x: T) -> T;

// round
pub(crate) fn simd_round<T>(x: T) -> T;

// trunc
pub(crate) fn simd_trunc<T>(x: T) -> T;

// fsqrt
pub(crate) fn simd_fsqrt<T>(x: T) -> T;

// fma
pub(crate) fn simd_fma<T>(x: T, y: T, z: T) -> T;
}
}

#[cfg(feature = "std")]
pub(crate) use crate::simd::intrinsics::std::*;
42 changes: 30 additions & 12 deletions library/portable-simd/crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
)]
mod mask_impl;

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

mod sealed {
use super::*;
Expand Down Expand Up @@ -105,22 +106,39 @@ where
Self(mask_impl::Mask::splat(value))
}

/// Converts an array to a SIMD vector.
/// Converts an array of bools to a SIMD mask.
pub fn from_array(array: [bool; LANES]) -> Self {
let mut vector = Self::splat(false);
for (i, v) in array.iter().enumerate() {
vector.set(i, *v);
// SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
// true: 0b_0000_0001
// false: 0b_0000_0000
// Thus, an array of bools is also a valid array of bytes: [u8; N]
// This would be hypothetically valid as an "in-place" transmute,
// but these are "dependently-sized" types, so copy elision it is!
unsafe {
let bytes: [u8; LANES] = mem::transmute_copy(&array);
let bools: Simd<i8, LANES> =
intrinsics::simd_ne(Simd::from_array(bytes), Simd::splat(0u8));
Mask::from_int_unchecked(intrinsics::simd_cast(bools))
}
vector
}

/// Converts a SIMD vector to an array.
/// Converts a SIMD mask to an array of bools.
pub fn to_array(self) -> [bool; LANES] {
let mut array = [false; LANES];
for (i, v) in array.iter_mut().enumerate() {
*v = self.test(i);
// This follows mostly the same logic as from_array.
// SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
// true: 0b_0000_0001
// false: 0b_0000_0000
// Thus, an array of bools is also a valid array of bytes: [u8; N]
// Since our masks are equal to integers where all bits are set,
// we can simply convert them to i8s, and then bitand them by the
// bitpattern for Rust's "true" bool.
// 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, LANES> = intrinsics::simd_cast(self.to_int());
bytes &= Simd::splat(1i8);
mem::transmute_copy(&bytes)
}
array
}

/// Converts a vector of integers to a mask, where 0 represents `false` and -1
Expand Down Expand Up @@ -516,7 +534,7 @@ pub type mask16x8 = Mask<i16, 8>;
pub type mask16x16 = Mask<i16, 16>;

/// Vector of 32 16-bit masks
pub type mask16x32 = Mask<i32, 32>;
pub type mask16x32 = Mask<i16, 32>;

/// Vector of two 32-bit masks
pub type mask32x2 = Mask<i32, 2>;
Expand Down
Loading

0 comments on commit 796bf14

Please sign in to comment.