diff options
| author | Caleb Zulawski <caleb.zulawski@gmail.com> | 2024-03-03 10:06:20 -0500 |
|---|---|---|
| committer | Caleb Zulawski <caleb.zulawski@gmail.com> | 2024-03-03 10:06:20 -0500 |
| commit | 5b5b259bf3488a07501e638b0af1d00af0c3c1c0 (patch) | |
| tree | e9ec4d84271e2fbe2681d349745370406f69526e | |
| parent | eea6f7799cef4cdccbcb6c379d37758fc5e4b2ac (diff) | |
| download | rust-5b5b259bf3488a07501e638b0af1d00af0c3c1c0.tar.gz rust-5b5b259bf3488a07501e638b0af1d00af0c3c1c0.zip | |
Test std_float
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | crates/std_float/Cargo.toml | 3 | ||||
| -rw-r--r-- | crates/std_float/src/lib.rs | 61 | ||||
| -rw-r--r-- | crates/std_float/tests/float.rs | 64 |
4 files changed, 78 insertions, 51 deletions
diff --git a/Cargo.lock b/Cargo.lock index 46312c09657..1ede15ff002 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,7 @@ name = "std_float" version = "0.1.0" dependencies = [ "core_simd", + "test_helpers", ] [[package]] diff --git a/crates/std_float/Cargo.toml b/crates/std_float/Cargo.toml index 84c69774cbd..8842b226104 100644 --- a/crates/std_float/Cargo.toml +++ b/crates/std_float/Cargo.toml @@ -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 = [] diff --git a/crates/std_float/src/lib.rs b/crates/std_float/src/lib.rs index 98f10e94a30..5237f7cce15 100644 --- a/crates/std_float/src/lib.rs +++ b/crates/std_float/src/lib.rs @@ -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(); - } -} diff --git a/crates/std_float/tests/float.rs b/crates/std_float/tests/float.rs new file mode 100644 index 00000000000..60bdf00fba8 --- /dev/null +++ b/crates/std_float/tests/float.rs @@ -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 } |
