Test std_float

This commit is contained in:
Caleb Zulawski 2024-03-03 10:06:20 -05:00
parent eea6f7799c
commit 5b5b259bf3
4 changed files with 78 additions and 51 deletions

1
Cargo.lock generated
View File

@ -177,6 +177,7 @@ name = "std_float"
version = "0.1.0"
dependencies = [
"core_simd",
"test_helpers",
]
[[package]]

View File

@ -8,6 +8,9 @@ edition = "2021"
[dependencies]
core_simd = { path = "../core_simd", default-features = false }
[dev-dependencies.test_helpers]
path = "../test_helpers"
[features]
default = ["as_crate"]
as_crate = []

View File

@ -101,10 +101,19 @@ pub trait StdFloat: Sealed + Sized {
/// in the equivalently-indexed lane in `self`.
#[inline]
#[must_use = "method returns a new vector and does not mutate the original value"]
fn log(self) -> Self {
fn ln(self) -> Self {
unsafe { intrinsics::simd_flog(self) }
}
/// Produces a vector where every lane has the logarithm with respect to an arbitrary
/// in the equivalently-indexed lanes in `self` and `base`.
#[inline]
#[must_use = "method returns a new vector and does not mutate the original value"]
fn log(self, base: Self) -> Self {
unsafe { intrinsics::simd_div(self.ln(), base.ln()) }
}
/// Produces a vector where every lane has the base-2 logarithm of the value
/// in the equivalently-indexed lane in `self`.
#[inline]
@ -181,53 +190,3 @@ where
self - self.trunc()
}
}
#[cfg(test)]
mod tests_simd_floats {
use super::*;
use simd::prelude::*;
#[test]
fn everything_works_f32() {
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 _abs_mul = x2.abs() * x2;
let _fexp = x.exp();
let _fexp2 = x.exp2();
let _flog = x.log();
let _flog2 = x.log2();
let _flog10 = x.log10();
let _fsin = x.sin();
let _fcos = x.cos();
}
#[test]
fn everything_works_f64() {
let x = f64x4::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 _abs_mul = x2.abs() * x2;
let _fexp = x.exp();
let _fexp2 = x.exp2();
let _flog = x.log();
let _flog2 = x.log2();
let _flog10 = x.log10();
let _fsin = x.sin();
let _fcos = x.cos();
}
}

View File

@ -0,0 +1,64 @@
#![feature(portable_simd)]
macro_rules! unary_test {
{ $scalar:tt, $($func:tt),+ } => {
test_helpers::test_lanes! {
$(
fn $func<const LANES: usize>() {
test_helpers::test_unary_elementwise(
&core_simd::simd::Simd::<$scalar, LANES>::$func,
&$scalar::$func,
&|_| true,
)
}
)*
}
}
}
macro_rules! binary_test {
{ $scalar:tt, $($func:tt),+ } => {
test_helpers::test_lanes! {
$(
fn $func<const LANES: usize>() {
test_helpers::test_binary_elementwise(
&core_simd::simd::Simd::<$scalar, LANES>::$func,
&$scalar::$func,
&|_, _| true,
)
}
)*
}
}
}
macro_rules! ternary_test {
{ $scalar:tt, $($func:tt),+ } => {
test_helpers::test_lanes! {
$(
fn $func<const LANES: usize>() {
test_helpers::test_ternary_elementwise(
&core_simd::simd::Simd::<$scalar, LANES>::$func,
&$scalar::$func,
&|_, _, _| true,
)
}
)*
}
}
}
macro_rules! impl_tests {
{ $scalar:tt } => {
mod $scalar {
use std_float::StdFloat;
unary_test! { $scalar, sqrt, sin, cos, exp, exp2, ln, log2, log10, ceil, floor, round, trunc, fract }
binary_test! { $scalar, log }
ternary_test! { $scalar, mul_add }
}
}
}
impl_tests! { f32 }
impl_tests! { f64 }