|
| 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