1 #![feature(portable_simd)]
3 macro_rules! float_rounding_test {
4 { $scalar:tt, $int_scalar:tt } => {
6 use std_float::StdFloat;
8 type Vector<const LANES: usize> = core_simd::Simd<$scalar, LANES>;
10 type IntScalar = $int_scalar;
12 test_helpers::test_lanes! {
13 fn ceil<const LANES: usize>() {
14 test_helpers::test_unary_elementwise(
15 &Vector::<LANES>::ceil,
21 fn floor<const LANES: usize>() {
22 test_helpers::test_unary_elementwise(
23 &Vector::<LANES>::floor,
29 fn round<const LANES: usize>() {
30 test_helpers::test_unary_elementwise(
31 &Vector::<LANES>::round,
37 fn trunc<const LANES: usize>() {
38 test_helpers::test_unary_elementwise(
39 &Vector::<LANES>::trunc,
45 fn fract<const LANES: usize>() {
46 test_helpers::test_unary_elementwise(
47 &Vector::<LANES>::fract,
54 test_helpers::test_lanes! {
55 fn to_int_unchecked<const LANES: usize>() {
56 // The maximum integer that can be represented by the equivalently sized float has
57 // all of the mantissa digits set to 1, pushed up to the MSB.
58 const ALL_MANTISSA_BITS: IntScalar = ((1 << <Scalar>::MANTISSA_DIGITS) - 1);
59 const MAX_REPRESENTABLE_VALUE: Scalar =
60 (ALL_MANTISSA_BITS << (core::mem::size_of::<Scalar>() * 8 - <Scalar>::MANTISSA_DIGITS as usize - 1)) as Scalar;
62 let mut runner = test_helpers::make_runner();
64 &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
66 let result_1 = unsafe { Vector::from_array(x).to_int_unchecked::<IntScalar>().to_array() };
68 let mut result: [IntScalar; LANES] = [0; LANES];
69 for (i, o) in x.iter().zip(result.iter_mut()) {
70 *o = unsafe { i.to_int_unchecked::<IntScalar>() };
74 test_helpers::prop_assert_biteq!(result_1, result_2);
84 float_rounding_test! { f32, i32 }
85 float_rounding_test! { f64, i64 }