]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/tests/round.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[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             use std_float::StdFloat;
7
8             type Vector<const LANES: usize> = core_simd::Simd<$scalar, LANES>;
9             type Scalar = $scalar;
10             type IntScalar = $int_scalar;
11
12             test_helpers::test_lanes! {
13                 fn ceil<const LANES: usize>() {
14                     test_helpers::test_unary_elementwise(
15                         &Vector::<LANES>::ceil,
16                         &Scalar::ceil,
17                         &|_| true,
18                     )
19                 }
20
21                 fn floor<const LANES: usize>() {
22                     test_helpers::test_unary_elementwise(
23                         &Vector::<LANES>::floor,
24                         &Scalar::floor,
25                         &|_| true,
26                     )
27                 }
28
29                 fn round<const LANES: usize>() {
30                     test_helpers::test_unary_elementwise(
31                         &Vector::<LANES>::round,
32                         &Scalar::round,
33                         &|_| true,
34                     )
35                 }
36
37                 fn trunc<const LANES: usize>() {
38                     test_helpers::test_unary_elementwise(
39                         &Vector::<LANES>::trunc,
40                         &Scalar::trunc,
41                         &|_| true,
42                     )
43                 }
44
45                 fn fract<const LANES: usize>() {
46                     test_helpers::test_unary_elementwise(
47                         &Vector::<LANES>::fract,
48                         &Scalar::fract,
49                         &|_| true,
50                     )
51                 }
52             }
53
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;
61
62                     let mut runner = test_helpers::make_runner();
63                     runner.run(
64                         &test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE),
65                         |x| {
66                             let result_1 = unsafe { Vector::from_array(x).to_int_unchecked::<IntScalar>().to_array() };
67                             let result_2 = {
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>() };
71                                 }
72                                 result
73                             };
74                             test_helpers::prop_assert_biteq!(result_1, result_2);
75                             Ok(())
76                         },
77                     ).unwrap();
78                 }
79             }
80         }
81     }
82 }
83
84 float_rounding_test! { f32, i32 }
85 float_rounding_test! { f64, i64 }