Skip to content

Commit 7e4bdca

Browse files
Merge portable-simd#210 - ./std-float
This introduces an extension trait to allow use of floating point methods that need runtime support. It is *excessively* documented because its mere existence is quite vexing, as the entire thing constitutes a leakage of implementation details into user observable space. Eventually the entire thing will ideally be folded into core and restructured to match the rest of the library, whatever that structure might look like at the time. This is preferred in lieu of the "lang item" path because any energy the lang items require (and it will be not-insignificant, by Simulacrum's estimation) is better spent on implementing our libmvec.
2 parents 4bbef26 + af26e3b commit 7e4bdca

File tree

10 files changed

+191
-95
lines changed

10 files changed

+191
-95
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
members = [
44
"crates/core_simd",
5+
"crates/std_float",
56
"crates/test_helpers",
67
]

crates/core_simd/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ features = ["alloc"]
2626

2727
[dev-dependencies.test_helpers]
2828
path = "../test_helpers"
29+
30+
[dev-dependencies]
31+
std_float = { path = "../std_float/", features = ["as_crate"] }

crates/core_simd/examples/nbody.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
#![cfg_attr(feature = "std", feature(portable_simd))]
1+
#![feature(portable_simd)]
2+
extern crate std_float;
23

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

1012
use std::f64::consts::PI;
1113
const SOLAR_MASS: f64 = 4.0 * PI * PI;
@@ -167,7 +169,6 @@ mod nbody {
167169
}
168170
}
169171

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

186187
fn main() {
187-
#[cfg(feature = "std")]
188188
{
189189
let (energy_before, energy_after) = nbody::run(1000);
190190
println!("Energy before: {}", energy_before);

crates/core_simd/src/intrinsics.rs

-26
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,3 @@ extern "platform-intrinsic" {
8787
#[allow(unused)]
8888
pub(crate) fn simd_select_bitmask<M, T>(m: M, a: T, b: T) -> T;
8989
}
90-
91-
#[cfg(feature = "std")]
92-
mod std {
93-
extern "platform-intrinsic" {
94-
// ceil
95-
pub(crate) fn simd_ceil<T>(x: T) -> T;
96-
97-
// floor
98-
pub(crate) fn simd_floor<T>(x: T) -> T;
99-
100-
// round
101-
pub(crate) fn simd_round<T>(x: T) -> T;
102-
103-
// trunc
104-
pub(crate) fn simd_trunc<T>(x: T) -> T;
105-
106-
// fsqrt
107-
pub(crate) fn simd_fsqrt<T>(x: T) -> T;
108-
109-
// fma
110-
pub(crate) fn simd_fma<T>(x: T, y: T, z: T) -> T;
111-
}
112-
}
113-
114-
#[cfg(feature = "std")]
115-
pub(crate) use crate::simd::intrinsics::std::*;

crates/core_simd/src/round.rs

-41
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,6 @@ macro_rules! implement {
55
{
66
$type:ty, $int_type:ty
77
} => {
8-
#[cfg(feature = "std")]
9-
impl<const LANES: usize> Simd<$type, LANES>
10-
where
11-
LaneCount<LANES>: SupportedLaneCount,
12-
{
13-
/// Returns the smallest integer greater than or equal to each lane.
14-
#[must_use = "method returns a new vector and does not mutate the original value"]
15-
#[inline]
16-
pub fn ceil(self) -> Self {
17-
unsafe { intrinsics::simd_ceil(self) }
18-
}
19-
20-
/// Returns the largest integer value less than or equal to each lane.
21-
#[must_use = "method returns a new vector and does not mutate the original value"]
22-
#[inline]
23-
pub fn floor(self) -> Self {
24-
unsafe { intrinsics::simd_floor(self) }
25-
}
26-
27-
/// Rounds to the nearest integer value. Ties round toward zero.
28-
#[must_use = "method returns a new vector and does not mutate the original value"]
29-
#[inline]
30-
pub fn round(self) -> Self {
31-
unsafe { intrinsics::simd_round(self) }
32-
}
33-
34-
/// Returns the floating point's integer value, with its fractional part removed.
35-
#[must_use = "method returns a new vector and does not mutate the original value"]
36-
#[inline]
37-
pub fn trunc(self) -> Self {
38-
unsafe { intrinsics::simd_trunc(self) }
39-
}
40-
41-
/// Returns the floating point's fractional value, with its integer part removed.
42-
#[must_use = "method returns a new vector and does not mutate the original value"]
43-
#[inline]
44-
pub fn fract(self) -> Self {
45-
self - self.trunc()
46-
}
47-
}
48-
498
impl<const LANES: usize> Simd<$type, LANES>
509
where
5110
LaneCount<LANES>: SupportedLaneCount,

crates/core_simd/src/vector/float.rs

-23
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,6 @@ macro_rules! impl_float_vector {
3838
unsafe { intrinsics::simd_fabs(self) }
3939
}
4040

41-
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error,
42-
/// yielding a more accurate result than an unfused multiply-add.
43-
///
44-
/// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
45-
/// architecture has a dedicated `fma` CPU instruction. However, this is not always
46-
/// true, and will be heavily dependent on designing algorithms with specific target
47-
/// hardware in mind.
48-
#[cfg(feature = "std")]
49-
#[inline]
50-
#[must_use = "method returns a new vector and does not mutate the original value"]
51-
pub fn mul_add(self, a: Self, b: Self) -> Self {
52-
unsafe { intrinsics::simd_fma(self, a, b) }
53-
}
54-
55-
/// Produces a vector where every lane has the square root value
56-
/// of the equivalently-indexed lane in `self`
57-
#[inline]
58-
#[must_use = "method returns a new vector and does not mutate the original value"]
59-
#[cfg(feature = "std")]
60-
pub fn sqrt(self) -> Self {
61-
unsafe { intrinsics::simd_fsqrt(self) }
62-
}
63-
6441
/// Takes the reciprocal (inverse) of each lane, `1/x`.
6542
#[inline]
6643
#[must_use = "method returns a new vector and does not mutate the original value"]

crates/core_simd/tests/ops_macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ macro_rules! impl_float_tests {
546546

547547
#[cfg(feature = "std")]
548548
mod std {
549+
use std_float::StdFloat;
550+
549551
use super::*;
550552
test_helpers::test_lanes! {
551553
fn sqrt<const LANES: usize>() {

crates/core_simd/tests/round.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
macro_rules! float_rounding_test {
44
{ $scalar:tt, $int_scalar:tt } => {
55
mod $scalar {
6+
use std_float::StdFloat;
7+
68
type Vector<const LANES: usize> = core_simd::Simd<$scalar, LANES>;
79
type Scalar = $scalar;
810
type IntScalar = $int_scalar;

crates/std_float/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "std_float"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
core_simd = { path = "../core_simd" }
10+
11+
[features]
12+
default = ["as_crate"]
13+
as_crate = []

crates/std_float/src/lib.rs

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#![cfg_attr(feature = "as_crate", no_std)] // We are std!
2+
#![cfg_attr(
3+
feature = "as_crate",
4+
feature(platform_intrinsics),
5+
feature(portable_simd)
6+
)]
7+
#[cfg(not(feature = "as_crate"))]
8+
use core::simd;
9+
#[cfg(feature = "as_crate")]
10+
use core_simd::simd;
11+
12+
use simd::{LaneCount, Simd, SupportedLaneCount};
13+
14+
#[cfg(feature = "as_crate")]
15+
mod experimental {
16+
pub trait Sealed {}
17+
}
18+
19+
#[cfg(feature = "as_crate")]
20+
use experimental as sealed;
21+
22+
use crate::sealed::Sealed;
23+
24+
// "platform intrinsics" are essentially "codegen intrinsics"
25+
// each of these may be scalarized and lowered to a libm call
26+
extern "platform-intrinsic" {
27+
// ceil
28+
fn simd_ceil<T>(x: T) -> T;
29+
30+
// floor
31+
fn simd_floor<T>(x: T) -> T;
32+
33+
// round
34+
fn simd_round<T>(x: T) -> T;
35+
36+
// trunc
37+
fn simd_trunc<T>(x: T) -> T;
38+
39+
// fsqrt
40+
fn simd_fsqrt<T>(x: T) -> T;
41+
42+
// fma
43+
fn simd_fma<T>(x: T, y: T, z: T) -> T;
44+
}
45+
46+
/// This trait provides a possibly-temporary implementation of float functions
47+
/// that may, in the absence of hardware support, canonicalize to calling an
48+
/// operating system's `math.h` dynamically-loaded library (also known as a
49+
/// shared object). As these conditionally require runtime support, they
50+
/// should only appear in binaries built assuming OS support: `std`.
51+
///
52+
/// However, there is no reason SIMD types, in general, need OS support,
53+
/// as for many architectures an embedded binary may simply configure that
54+
/// support itself. This means these types must be visible in `core`
55+
/// but have these functions available in `std`.
56+
///
57+
/// [`f32`] and [`f64`] achieve a similar trick by using "lang items", but
58+
/// due to compiler limitations, it is harder to implement this approach for
59+
/// abstract data types like [`Simd`]. From that need, this trait is born.
60+
///
61+
/// It is possible this trait will be replaced in some manner in the future,
62+
/// when either the compiler or its supporting runtime functions are improved.
63+
/// For now this trait is available to permit experimentation with SIMD float
64+
/// operations that may lack hardware support, such as `mul_add`.
65+
pub trait StdFloat: Sealed + Sized {
66+
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error,
67+
/// yielding a more accurate result than an unfused multiply-add.
68+
///
69+
/// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
70+
/// architecture has a dedicated `fma` CPU instruction. However, this is not always
71+
/// true, and will be heavily dependent on designing algorithms with specific target
72+
/// hardware in mind.
73+
#[inline]
74+
#[must_use = "method returns a new vector and does not mutate the original value"]
75+
fn mul_add(self, a: Self, b: Self) -> Self {
76+
unsafe { simd_fma(self, a, b) }
77+
}
78+
79+
/// Produces a vector where every lane has the square root value
80+
/// of the equivalently-indexed lane in `self`
81+
#[inline]
82+
#[must_use = "method returns a new vector and does not mutate the original value"]
83+
fn sqrt(self) -> Self {
84+
unsafe { simd_fsqrt(self) }
85+
}
86+
87+
/// Returns the smallest integer greater than or equal to each lane.
88+
#[must_use = "method returns a new vector and does not mutate the original value"]
89+
#[inline]
90+
fn ceil(self) -> Self {
91+
unsafe { simd_ceil(self) }
92+
}
93+
94+
/// Returns the largest integer value less than or equal to each lane.
95+
#[must_use = "method returns a new vector and does not mutate the original value"]
96+
#[inline]
97+
fn floor(self) -> Self {
98+
unsafe { simd_floor(self) }
99+
}
100+
101+
/// Rounds to the nearest integer value. Ties round toward zero.
102+
#[must_use = "method returns a new vector and does not mutate the original value"]
103+
#[inline]
104+
fn round(self) -> Self {
105+
unsafe { simd_round(self) }
106+
}
107+
108+
/// Returns the floating point's integer value, with its fractional part removed.
109+
#[must_use = "method returns a new vector and does not mutate the original value"]
110+
#[inline]
111+
fn trunc(self) -> Self {
112+
unsafe { simd_trunc(self) }
113+
}
114+
115+
/// Returns the floating point's fractional value, with its integer part removed.
116+
#[must_use = "method returns a new vector and does not mutate the original value"]
117+
fn fract(self) -> Self;
118+
}
119+
120+
impl<const N: usize> Sealed for Simd<f32, N> where LaneCount<N>: SupportedLaneCount {}
121+
impl<const N: usize> Sealed for Simd<f64, N> where LaneCount<N>: SupportedLaneCount {}
122+
123+
// We can safely just use all the defaults.
124+
impl<const N: usize> StdFloat for Simd<f32, N>
125+
where
126+
LaneCount<N>: SupportedLaneCount,
127+
{
128+
/// Returns the floating point's fractional value, with its integer part removed.
129+
#[must_use = "method returns a new vector and does not mutate the original value"]
130+
#[inline]
131+
fn fract(self) -> Self {
132+
self - self.trunc()
133+
}
134+
}
135+
136+
impl<const N: usize> StdFloat for Simd<f64, N>
137+
where
138+
LaneCount<N>: SupportedLaneCount,
139+
{
140+
/// Returns the floating point's fractional value, with its integer part removed.
141+
#[must_use = "method returns a new vector and does not mutate the original value"]
142+
#[inline]
143+
fn fract(self) -> Self {
144+
self - self.trunc()
145+
}
146+
}
147+
148+
#[cfg(test)]
149+
mod tests {
150+
use super::*;
151+
use simd::*;
152+
153+
#[test]
154+
fn everything_works() {
155+
let x = f32x4::from_array([0.1, 0.5, 0.6, -1.5]);
156+
let x2 = x + x;
157+
let _xc = x.ceil();
158+
let _xf = x.floor();
159+
let _xr = x.round();
160+
let _xt = x.trunc();
161+
let _xfma = x.mul_add(x, x);
162+
let _xsqrt = x.sqrt();
163+
let _ = x2.abs() * x2;
164+
}
165+
}

0 commit comments

Comments
 (0)