Debian Codegraph @

debian/missing-sources/wide/tests/all_tests/main.rs

raw
1 #![allow(clippy::approx_constant)]
2 #![allow(clippy::unnecessary_cast)]
3 #![allow(clippy::assertions_on_constants)]
4 #![allow(clippy::needless_range_loop)]
5 #![allow(clippy::nonminimal_bool)]
6 #![allow(unused_imports)]
7 #![allow(clippy::precedence)]
8 #![allow(clippy::eq_op)]
9 #![allow(clippy::identity_op)]
10
11 use core::fmt;
12 use std::{num::Wrapping, ops::ShlAssign};
13
14 mod t_f32x4;
15 mod t_f32x8;
16 mod t_f64x2;
17 mod t_f64x4;
18 mod t_i16x16;
19 mod t_i16x8;
20 mod t_i32x4;
21 mod t_i32x8;
22 mod t_i64x2;
23 mod t_i64x4;
24 mod t_i8x16;
25 mod t_i8x32;
26 mod t_u16x16;
27 mod t_u16x8;
28 mod t_u32x4;
29 mod t_u32x8;
30 mod t_u64x2;
31 mod t_u64x4;
32 mod t_u8x16;
33 mod t_u8x32;
34 mod t_usefulness;
35
36 /// Generates the next pseudo-random number.
37 /// Definitely non-cryptographic, just used for generating random test values.
38 fn next_rand_u64(state: &mut u64) -> u64 {
39 // Constants for the LCG
40 const A: u64 = 6364136223846793005;
41 const C: u64 = 1442695040888963407;
42
43 // Update the state and calculate the next number (rotate to avoid lack of
44 // randomness in low bits)
45 *state = state.wrapping_mul(A).wrapping_add(C).rotate_left(31);
46
47 *state
48 }
49
50 const RNG_SEED: u64 = 0x123456789abcdef0;
51
52 /// Generate a pseudo-random value for a type that implements GenSample.
53 fn gen_random<T: GenSample>(rng: &mut u64) -> T {
54 let r = next_rand_u64(rng);
55
56 // generate special values more often than random chance to test edge cases
57 let next = match r & 0xf {
58 0 => 0,
59 1 => 1,
60 2 => u64::MAX,
61 _ => next_rand_u64(rng),
62 };
63
64 T::get_sample(next)
65 }
66
67 /// Test a vector operation against a pure scalar implementation for random
68 /// values to make sure that the behavior is the same. This allows for easier
69 /// for correctness for various values of the vector.
70 fn test_random_vector_vs_scalar<
71 V,
72 VR,
73 T,
74 TR,
75 FnVec: Fn(V, V) -> VR,
76 FnScalar: Fn(T, T) -> TR,
77 const N: usize,
78 >(
79 vector_fn: FnVec,
80 scalar_fn: FnScalar,
81 ) where
82 V: Copy + From<[T; N]>,
83 T: Copy + Default + std::fmt::Debug + GenSample,
84 TR: Copy + PartialEq + std::fmt::Debug + Default + GenSample,
85 VR: Copy + Into<[TR; N]>,
86 {
87 let mut a_arr = [T::default(); N];
88 let mut b_arr: [T; N] = [T::default(); N];
89
90 // use a fixed seed for reproducibility
91 let mut rng = RNG_SEED;
92
93 // do 100 iterations
94 for _i in 0..100 {
95 for i in 0..N {
96 a_arr[i] = gen_random(&mut rng);
97 b_arr[i] = gen_random(&mut rng);
98 }
99
100 let mut expected_arr: [TR; N] = [TR::default(); N];
101 for i in 0..N {
102 expected_arr[i] = scalar_fn(a_arr[i], b_arr[i]);
103 }
104
105 let expected_vec_arr: [TR; N] =
106 vector_fn(V::from(a_arr), V::from(b_arr)).into();
107
108 for i in 0..N {
109 assert!(
110 expected_arr[i].binary_eq(expected_vec_arr[i]),
111 "scalar = {:?}\nvec = {:?}\na = {:?}\nb = {:?}",
112 expected_arr,
113 expected_vec_arr,
114 a_arr,
115 b_arr
116 );
117 }
118 }
119 }
120
121 /// Test a vector reduce operations that generate a scalar from a vector
122 /// against a pure scalar implementation for random values to make
123 /// sure that the behavior is the same. This allows for easier for correctness
124 /// for various values of the vector.
125 ///
126 /// The scalar operation uses the same construction as the Rust fold function
127 /// which takes an accumulator and returns the accumulator after applying the
128 /// operation.
129 fn test_random_vector_vs_scalar_reduce<
130 V,
131 T,
132 TR,
133 FnVec: Fn(V) -> TR,
134 FnScalar: Fn(TR, T, usize) -> TR,
135 const N: usize,
136 >(
137 vector_fn: FnVec,
138 acc: TR,
139 scalar_fn: FnScalar,
140 ) where
141 V: From<[T; N]> + Into<[T; N]> + Copy + std::fmt::Debug,
142 T: Copy + PartialEq + std::fmt::Debug + Default + GenSample,
143 TR: Copy + PartialEq + std::fmt::Debug + Default,
144 {
145 let mut a_arr = [T::default(); N];
146
147 // use a fixed seed for reproducibility
148 let mut rng = RNG_SEED;
149
150 // do 100 iterations
151 for _i in 0..100 {
152 for i in 0..N {
153 a_arr[i] = gen_random(&mut rng);
154 }
155
156 let mut expected_scalar = acc;
157 for i in 0..N {
158 expected_scalar = scalar_fn(expected_scalar, a_arr[i], i);
159 }
160
161 let expected_vec = vector_fn(V::from(a_arr));
162 assert_eq!(
163 expected_scalar, expected_vec,
164 "scalar = {:?} vec = {:?} source = {:?}",
165 expected_scalar, expected_vec, a_arr
166 );
167 }
168 }
169
170 fn test_basic_traits<V, T, const N: usize>()
171 where
172 V: Copy
173 + From<[T; N]>
174 + Into<[T; N]>
175 + std::ops::Add<Output = V>
176 + std::ops::Sub<Output = V>
177 + std::ops::BitXor<Output = V>
178 + std::ops::BitOr<Output = V>
179 + std::ops::BitAnd<Output = V>
180 + std::ops::Not<Output = V>
181 + std::ops::Neg<Output = V>
182 + wide::CmpEq<Output = V>
183 + PartialEq
184 + Eq
185 + fmt::Debug,
186 T: Copy
187 + Default
188 + std::fmt::Debug
189 + GenSample
190 + PartialEq
191 + Eq
192 + std::ops::BitXor<Output = T>
193 + std::ops::BitOr<Output = T>
194 + std::ops::BitAnd<Output = T>
195 + std::ops::Not<Output = T>,
196 Wrapping<T>:
197 std::ops::Add<Output = Wrapping<T>> + std::ops::Sub<Output = Wrapping<T>>,
198 {
199 // test add
200 test_random_vector_vs_scalar(
201 |a: V, b| a + b,
202 |a, b| (Wrapping::<T>(a) + Wrapping::<T>(b)).0,
203 );
204
205 // test sub
206 test_random_vector_vs_scalar(
207 |a: V, b| a - b,
208 |a, b| (Wrapping::<T>(a) - Wrapping::<T>(b)).0,
209 );
210
211 // test neg
212 test_random_vector_vs_scalar(
213 |a: V, b| a - (-b),
214 |a, b| (Wrapping::<T>(a) + Wrapping::<T>(b)).0,
215 );
216
217 test_random_vector_vs_scalar(|a: V, b| a ^ b, |a, b| a ^ b);
218
219 test_random_vector_vs_scalar(|a: V, b| a & b, |a, b| a & b);
220
221 test_random_vector_vs_scalar(|a: V, b| a | b, |a, b| a | b);
222
223 test_random_vector_vs_scalar(
224 |a: V, b| a.cmp_eq(b),
225 |a, b| if a == b { !T::default() } else { T::default() },
226 );
227
228 let a = V::from([T::default(); N]);
229 let b = V::from([!T::default(); N]);
230
231 assert!(a != b);
232 assert!(a == a);
233 assert!(b == a.not());
234 }
235
236 /// trait to reduce a 64 bit pseudo-random number to a random sample value
237 trait GenSample
238 where
239 Self: PartialEq + Copy,
240 {
241 fn get_sample(v: u64) -> Self;
242 fn binary_eq(self, b: Self) -> bool {
243 self == b
244 }
245 }
246
247 impl GenSample for u64 {
248 fn get_sample(v: u64) -> Self {
249 v
250 }
251 }
252
253 impl GenSample for u32 {
254 fn get_sample(v: u64) -> Self {
255 v as u32
256 }
257 }
258
259 impl GenSample for u16 {
260 fn get_sample(v: u64) -> Self {
261 v as u16
262 }
263 }
264
265 impl GenSample for u8 {
266 fn get_sample(v: u64) -> Self {
267 v as u8
268 }
269 }
270
271 impl GenSample for i64 {
272 fn get_sample(v: u64) -> Self {
273 v as i64
274 }
275 }
276
277 impl GenSample for i32 {
278 fn get_sample(v: u64) -> Self {
279 v as i32
280 }
281 }
282
283 impl GenSample for i16 {
284 fn get_sample(v: u64) -> Self {
285 v as i16
286 }
287 }
288
289 impl GenSample for i8 {
290 fn get_sample(v: u64) -> Self {
291 v as i8
292 }
293 }
294
295 impl GenSample for f32 {
296 fn get_sample(v: u64) -> Self {
297 // generate special float values more often than random
298 // chance to test edge cases
299 let m = (v >> 8) & 15;
300
301 match m {
302 1 => f32::NAN,
303 2 => f32::INFINITY,
304 3 => f32::NEG_INFINITY,
305 _ => ((v as i64) as f32) / 7.0,
306 }
307 }
308
309 /// floating points Nan always fails equality so we need to special case it
310 fn binary_eq(self, b: Self) -> bool {
311 if self.is_nan() {
312 b.is_nan()
313 } else if self.is_infinite() {
314 b.is_infinite() && self.is_sign_positive() == b.is_sign_positive()
315 } else {
316 (self - b).abs() < 0.000001
317 }
318 }
319 }
320
321 impl GenSample for f64 {
322 // generate special float values more often than random
323 // chance to test edge cases
324 fn get_sample(v: u64) -> Self {
325 let m = (v >> 8) & 15;
326
327 match m {
328 1 => f64::NAN,
329 2 => f64::INFINITY,
330 3 => f64::NEG_INFINITY,
331 _ => ((v as i64) as f64) / 7.0,
332 }
333 }
334
335 /// floating points Nan always fails equality so we need to special case it
336 fn binary_eq(self, b: Self) -> bool {
337 if self.is_nan() {
338 b.is_nan()
339 } else if self.is_infinite() {
340 b.is_infinite() && self.is_sign_positive() == b.is_sign_positive()
341 } else {
342 (self - b).abs() < 0.000001
343 }
344 }
345 }