From: Jubilee Young Date: Sun, 25 Apr 2021 22:01:05 +0000 (-0700) Subject: Give rounding intrinsics their own modules X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=b4fda6ef06be190aa655bb23a9c66e46589daff4;p=rust.git Give rounding intrinsics their own modules --- diff --git a/crates/core_simd/src/intrinsics.rs b/crates/core_simd/src/intrinsics.rs index 57e666873c1..e8bcca22f68 100644 --- a/crates/core_simd/src/intrinsics.rs +++ b/crates/core_simd/src/intrinsics.rs @@ -43,14 +43,6 @@ /// neg/fneg pub(crate) fn simd_neg(x: T) -> T; - // floor - #[cfg(feature = "std")] - pub(crate) fn simd_floor(x: T) -> T; - - // ceil - #[cfg(feature = "std")] - pub(crate) fn simd_ceil(x: T) -> T; - /// fabs pub(crate) fn simd_fabs(x: T) -> T; @@ -85,3 +77,17 @@ pub(crate) fn simd_reduce_or(x: T) -> U; pub(crate) fn simd_reduce_xor(x: T) -> U; } + +#[cfg(feature = "std")] +mod std { + extern "platform-intrinsic" { + // ceil + pub(crate) fn simd_ceil(x: T) -> T; + + // floor + pub(crate) fn simd_floor(x: T) -> T; + } +} + +#[cfg(feature = "std")] +pub(crate) use crate::intrinsics::std::*; diff --git a/crates/core_simd/src/round.rs b/crates/core_simd/src/round.rs index ccad1aad9c4..1855c1480d2 100644 --- a/crates/core_simd/src/round.rs +++ b/crates/core_simd/src/round.rs @@ -2,12 +2,12 @@ macro_rules! implement { { $type:ident, $int_type:ident } => { + #[cfg(feature = "std")] impl crate::$type where Self: crate::LanesAtMost32, { /// Returns the largest integer less than or equal to each lane. - #[cfg(feature = "std")] #[must_use = "method returns a new vector and does not mutate the original value"] #[inline] pub fn floor(self) -> Self { @@ -15,7 +15,6 @@ pub fn floor(self) -> Self { } /// Returns the smallest integer greater than or equal to each lane. - #[cfg(feature = "std")] #[must_use = "method returns a new vector and does not mutate the original value"] #[inline] pub fn ceil(self) -> Self { diff --git a/crates/core_simd/tests/ops_macros.rs b/crates/core_simd/tests/ops_macros.rs index 37f3b49a330..9f999225877 100644 --- a/crates/core_simd/tests/ops_macros.rs +++ b/crates/core_simd/tests/ops_macros.rs @@ -353,7 +353,6 @@ macro_rules! impl_float_tests { mod $scalar { type Vector = core_simd::$vector; type Scalar = $scalar; - type IntScalar = $int_scalar; impl_unary_op_test!(Vector, Scalar, Neg::neg); impl_binary_op_test!(Vector, Scalar, Add::add, AddAssign::add_assign); @@ -362,25 +361,6 @@ mod $scalar { impl_binary_op_test!(Vector, Scalar, Div::div, DivAssign::div_assign); impl_binary_op_test!(Vector, Scalar, Rem::rem, RemAssign::rem_assign); - #[cfg(feature = "std")] - test_helpers::test_lanes! { - fn ceil() { - test_helpers::test_unary_elementwise( - &Vector::::ceil, - &Scalar::ceil, - &|_| true, - ) - } - - fn floor() { - test_helpers::test_unary_elementwise( - &Vector::::floor, - &Scalar::floor, - &|_| true, - ) - } - } - test_helpers::test_lanes! { fn is_sign_positive() { test_helpers::test_unary_mask_elementwise( @@ -446,39 +426,6 @@ fn abs() { ) } - fn round_from_int() { - test_helpers::test_unary_elementwise( - &Vector::::round_from_int, - &|x| x as Scalar, - &|_| true, - ) - } - - fn to_int_unchecked() { - // The maximum integer that can be represented by the equivalently sized float has - // all of the mantissa digits set to 1, pushed up to the MSB. - const ALL_MANTISSA_BITS: IntScalar = ((1 << ::MANTISSA_DIGITS) - 1); - const MAX_REPRESENTABLE_VALUE: Scalar = - (ALL_MANTISSA_BITS << (core::mem::size_of::() * 8 - ::MANTISSA_DIGITS as usize - 1)) as Scalar; - - let mut runner = proptest::test_runner::TestRunner::default(); - runner.run( - &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE), - |x| { - let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() }; - let result_2 = { - let mut result = [0; LANES]; - for (i, o) in x.iter().zip(result.iter_mut()) { - *o = unsafe { i.to_int_unchecked() }; - } - result - }; - test_helpers::prop_assert_biteq!(result_1, result_2); - Ok(()) - }, - ).unwrap(); - } - fn horizontal_sum() { test_helpers::test_1(&|x| { test_helpers::prop_assert_biteq! ( diff --git a/crates/core_simd/tests/round.rs b/crates/core_simd/tests/round.rs new file mode 100644 index 00000000000..dc9c8ad4ad2 --- /dev/null +++ b/crates/core_simd/tests/round.rs @@ -0,0 +1,66 @@ +macro_rules! float_rounding_test { + { $vector:ident, $scalar:tt, $int_scalar:tt } => { + mod $scalar { + type Vector = core_simd::$vector; + type Scalar = $scalar; + type IntScalar = $int_scalar; + + #[cfg(feature = "std")] + test_helpers::test_lanes! { + fn ceil() { + test_helpers::test_unary_elementwise( + &Vector::::ceil, + &Scalar::ceil, + &|_| true, + ) + } + + fn floor() { + test_helpers::test_unary_elementwise( + &Vector::::floor, + &Scalar::floor, + &|_| true, + ) + } + } + + test_helpers::test_lanes! { + fn from_int() { + test_helpers::test_unary_elementwise( + &Vector::::round_from_int, + &|x| x as Scalar, + &|_| true, + ) + } + + fn to_int_unchecked() { + // The maximum integer that can be represented by the equivalently sized float has + // all of the mantissa digits set to 1, pushed up to the MSB. + const ALL_MANTISSA_BITS: IntScalar = ((1 << ::MANTISSA_DIGITS) - 1); + const MAX_REPRESENTABLE_VALUE: Scalar = + (ALL_MANTISSA_BITS << (core::mem::size_of::() * 8 - ::MANTISSA_DIGITS as usize - 1)) as Scalar; + + let mut runner = proptest::test_runner::TestRunner::default(); + runner.run( + &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE), + |x| { + let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() }; + let result_2 = { + let mut result = [0; LANES]; + for (i, o) in x.iter().zip(result.iter_mut()) { + *o = unsafe { i.to_int_unchecked() }; + } + result + }; + test_helpers::prop_assert_biteq!(result_1, result_2); + Ok(()) + }, + ).unwrap(); + } + } + } + } +} + +float_rounding_test! { SimdF32, f32, i32 } +float_rounding_test! { SimdF64, f64, i64 }