]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/tests/round.rs
Auto merge of #89167 - workingjubilee:use-simd, r=MarkSimulacrum
[rust.git] / library / portable-simd / crates / core_simd / tests / round.rs
1 #![feature(portable_simd)]
2
3 macro_rules! float_rounding_test {
4     { $scalar:tt, $int_scalar:tt } => {
5         mod $scalar {
6             type Vector<const LANES: usize> = core_simd::Simd<$scalar, LANES>;
7             type Scalar = $scalar;
8             type IntScalar = $int_scalar;
9
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,
15                         &Scalar::ceil,
16                         &|_| true,
17                     )
18                 }
19
20                 fn floor<const LANES: usize>() {
21                     test_helpers::test_unary_elementwise(
22                         &Vector::<LANES>::floor,
23                         &Scalar::floor,
24                         &|_| true,
25                     )
26                 }
27
28                 fn round<const LANES: usize>() {
29                     test_helpers::test_unary_elementwise(
30                         &Vector::<LANES>::round,
31                         &Scalar::round,
32                         &|_| true,
33                     )
34                 }
35
36                 fn trunc<const LANES: usize>() {
37                     test_helpers::test_unary_elementwise(
38                         &Vector::<LANES>::trunc,
39                         &Scalar::trunc,
40                         &|_| true,
41                     )
42                 }
43
44                 fn fract<const LANES: usize>() {
45                     test_helpers::test_unary_elementwise(
46                         &Vector::<LANES>::fract,
47                         &Scalar::fract,
48                         &|_| true,
49                     )
50                 }
51             }
52
53             test_helpers::test_lanes! {
54                 fn from_int<const LANES: usize>() {
55                     test_helpers::test_unary_elementwise(
56                         &Vector::<LANES>::round_from_int,
57                         &|x| x as Scalar,
58                         &|_| true,
59                     )
60                 }
61
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;
68
69                     let mut runner = proptest::test_runner::TestRunner::default();
70                     runner.run(
71                         &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
72                         |x| {
73                             let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() };
74                             let result_2 = {
75                                 let mut result = [0; LANES];
76                                 for (i, o) in x.iter().zip(result.iter_mut()) {
77                                     *o = unsafe { i.to_int_unchecked() };
78                                 }
79                                 result
80                             };
81                             test_helpers::prop_assert_biteq!(result_1, result_2);
82                             Ok(())
83                         },
84                     ).unwrap();
85                 }
86             }
87         }
88     }
89 }
90
91 float_rounding_test! { f32, i32 }
92 float_rounding_test! { f64, i64 }