#![cfg_attr(feature = "as_crate", no_std)] #![cfg_attr(feature = "as_crate", feature(platform_intrinsics), feature(portable_simd))]
#[cfg(not(feature = "as_crate"))]
use core::simd;
#[cfg(feature = "as_crate")]
use core_simd::simd;
use simd::{LaneCount, Simd, SupportedLaneCount};
#[cfg(feature = "as_crate")]
mod experimental {
pub trait Sealed {}
}
#[cfg(feature = "as_crate")]
use experimental as sealed;
use crate::sealed::Sealed;
extern "platform-intrinsic" {
fn simd_ceil<T>(x: T) -> T;
fn simd_floor<T>(x: T) -> T;
fn simd_round<T>(x: T) -> T;
fn simd_trunc<T>(x: T) -> T;
fn simd_fsqrt<T>(x: T) -> T;
fn simd_fma<T>(x: T, y: T, z: T) -> T;
}
pub trait StdFloat: Sealed + Sized {
#[inline]
#[must_use = "method returns a new vector and does not mutate the original value"]
fn mul_add(self, a: Self, b: Self) -> Self {
unsafe { simd_fma(self, a, b) }
}
#[inline]
#[must_use = "method returns a new vector and does not mutate the original value"]
fn sqrt(self) -> Self {
unsafe { simd_fsqrt(self) }
}
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn ceil(self) -> Self {
unsafe { simd_ceil(self) }
}
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn floor(self) -> Self {
unsafe { simd_floor(self) }
}
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn round(self) -> Self {
unsafe { simd_round(self) }
}
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn trunc(self) -> Self {
unsafe { simd_trunc(self) }
}
#[must_use = "method returns a new vector and does not mutate the original value"]
fn fract(self) -> Self;
}
impl<const N: usize> Sealed for Simd<f32, N> where LaneCount<N>: SupportedLaneCount {}
impl<const N: usize> Sealed for Simd<f64, N> where LaneCount<N>: SupportedLaneCount {}
impl<const N: usize> StdFloat for Simd<f32, N>
where
LaneCount<N>: SupportedLaneCount,
{
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn fract(self) -> Self {
self - self.trunc()
}
}
impl<const N: usize> StdFloat for Simd<f64, N>
where
LaneCount<N>: SupportedLaneCount,
{
#[must_use = "method returns a new vector and does not mutate the original value"]
#[inline]
fn fract(self) -> Self {
self - self.trunc()
}
}
#[cfg(test)]
mod tests {
use super::*;
use simd::*;
#[test]
fn everything_works() {
let x = f32x4::from_array([0.1, 0.5, 0.6, -1.5]);
let x2 = x + x;
let _xc = x.ceil();
let _xf = x.floor();
let _xr = x.round();
let _xt = x.trunc();
let _xfma = x.mul_add(x, x);
let _xsqrt = x.sqrt();
let _ = x2.abs() * x2;
}
}