1 #![feature(portable_simd)]
3 macro_rules! float_rounding_test {
4 { $scalar:tt, $int_scalar:tt } => {
6 type Vector<const LANES: usize> = core_simd::Simd<$scalar, LANES>;
8 type IntScalar = $int_scalar;
10 #[cfg(feature = "std")]
11 test_helpers::test_lanes! {
12 fn ceil<const LANES: usize>() {
13 test_helpers::test_unary_elementwise(
14 &Vector::<LANES>::ceil,
20 fn floor<const LANES: usize>() {
21 test_helpers::test_unary_elementwise(
22 &Vector::<LANES>::floor,
28 fn round<const LANES: usize>() {
29 test_helpers::test_unary_elementwise(
30 &Vector::<LANES>::round,
36 fn trunc<const LANES: usize>() {
37 test_helpers::test_unary_elementwise(
38 &Vector::<LANES>::trunc,
44 fn fract<const LANES: usize>() {
45 test_helpers::test_unary_elementwise(
46 &Vector::<LANES>::fract,
53 test_helpers::test_lanes! {
54 fn from_int<const LANES: usize>() {
55 test_helpers::test_unary_elementwise(
56 &Vector::<LANES>::round_from_int,
62 fn to_int_unchecked<const LANES: usize>() {
63 // The maximum integer that can be represented by the equivalently sized float has
64 // all of the mantissa digits set to 1, pushed up to the MSB.
65 const ALL_MANTISSA_BITS: IntScalar = ((1 << <Scalar>::MANTISSA_DIGITS) - 1);
66 const MAX_REPRESENTABLE_VALUE: Scalar =
67 (ALL_MANTISSA_BITS << (core::mem::size_of::<Scalar>() * 8 - <Scalar>::MANTISSA_DIGITS as usize - 1)) as Scalar;
69 let mut runner = proptest::test_runner::TestRunner::default();
71 &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
73 let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() };
75 let mut result = [0; LANES];
76 for (i, o) in x.iter().zip(result.iter_mut()) {
77 *o = unsafe { i.to_int_unchecked() };
81 test_helpers::prop_assert_biteq!(result_1, result_2);
91 float_rounding_test! { f32, i32 }
92 float_rounding_test! { f64, i64 }