Debian Codegraph @

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

raw
1 use wide::*;
2
3 use bytemuck::*;
4
5 #[test]
6 fn size_align() {
7 assert_eq!(core::mem::size_of::<f32x4>(), 16);
8 assert_eq!(core::mem::align_of::<f32x4>(), 16);
9 }
10
11 #[test]
12 fn impl_debug_for_f32x4() {
13 let expected = "(1.0, 2.0, 3.0, 4.0)";
14 let actual = format!("{:?}", f32x4::from([1.0, 2.0, 3.0, 4.0]));
15 assert_eq!(expected, actual);
16
17 let expected = "(1.000, 2.000, 3.000, 4.000)";
18 let actual = format!("{:.3?}", f32x4::from([1.0, 2.0, 3.0, 4.0]));
19 assert_eq!(expected, actual);
20 }
21
22 #[test]
23 fn impl_add_for_f32x4() {
24 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
25 let b = f32x4::from([5.0, 6.0, 7.0, 8.0]);
26 let expected = f32x4::from([6.0, 8.0, 10.0, 12.0]);
27 let actual = a + b;
28 assert_eq!(expected, actual);
29 }
30
31 #[test]
32 fn impl_add_const_for_f32x4() {
33 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
34 let expected = f32x4::from([6.0, 7.0, 8.0, 9.0]);
35 let actual = a + 5.0;
36 assert_eq!(expected, actual);
37 }
38
39 #[test]
40 fn impl_sub_const_for_f32x4() {
41 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
42 let expected = f32x4::from([-1.0, 0.0, 1.0, 2.0]);
43 let actual = a - 2.0;
44 assert_eq!(expected, actual);
45 }
46
47 #[test]
48 fn impl_mul_const_for_f32x4() {
49 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
50 let expected = f32x4::from([2.0, 4.0, 6.0, 8.0]);
51 let actual = a * 2.0;
52 assert_eq!(expected, actual);
53 }
54
55 #[test]
56 fn impl_div_const_for_f32x4() {
57 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
58 let expected = f32x4::from([0.5, 1.0, 1.5, 2.0]);
59 let actual = a / 2.0;
60 assert_eq!(expected, actual);
61 }
62
63 #[test]
64 fn impl_sub_for_f32x4() {
65 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
66 let b = f32x4::from([5.0, 7.0, 17.0, 1.0]);
67 let expected = f32x4::from([-4.0, -5.0, -14.0, 3.0]);
68 let actual = a - b;
69 assert_eq!(expected, actual);
70 }
71
72 #[test]
73 fn impl_mul_for_f32x4() {
74 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
75 let b = f32x4::from([5.0, 7.0, 17.0, 1.0]);
76 let expected = f32x4::from([5.0, 14.0, 51.0, 4.0]);
77 let actual = a * b;
78 assert_eq!(expected, actual);
79 }
80
81 #[test]
82 fn impl_div_for_f32x4() {
83 let a = f32x4::from([4.0, 9.0, 10.0, 12.0]);
84 let b = f32x4::from([2.0, 2.0, 5.0, -3.0]);
85 let expected = f32x4::from([2.0, 4.5, 2.0, -4.0]);
86 let actual = a / b;
87 assert_eq!(expected, actual);
88 }
89
90 #[test]
91 fn impl_bitand_for_f32x4() {
92 let a = f32x4::from([0.0, 0.0, 1.0, 1.0]);
93 let b = f32x4::from([0.0, 1.0, 0.0, 1.0]);
94 let expected = f32x4::from([0.0, 0.0, 0.0, 1.0]);
95 let actual = a & b;
96 assert_eq!(expected, actual);
97 }
98
99 #[test]
100 fn impl_bitor_for_f32x4() {
101 let a = f32x4::from([0.0, 0.0, 1.0, 1.0]);
102 let b = f32x4::from([0.0, 1.0, 0.0, 1.0]);
103 let expected = f32x4::from([0.0, 1.0, 1.0, 1.0]);
104 let actual = a | b;
105 assert_eq!(expected, actual);
106 }
107
108 #[test]
109 fn impl_bitxor_for_f32x4() {
110 let a = f32x4::from([0.0, 0.0, 1.0, 1.0]);
111 let b = f32x4::from([0.0, 1.0, 0.0, 1.0]);
112 let expected = f32x4::from([0.0, 1.0, 1.0, 0.0]);
113 let actual = a ^ b;
114 assert_eq!(expected, actual);
115 }
116
117 #[test]
118 fn impl_f32x4_cmp_eq() {
119 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
120 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
121 let expected: [i32; 4] = [0, -1, 0, 0];
122 let actual: [i32; 4] = cast(a.cmp_eq(b));
123 assert_eq!(expected, actual);
124 }
125
126 #[test]
127 fn impl_f32x4_cmp_ne() {
128 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
129 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
130 let expected: [i32; 4] = [-1, 0, -1, -1];
131 let actual: [i32; 4] = cast(a.cmp_ne(b));
132 assert_eq!(expected, actual);
133 }
134
135 #[test]
136 fn impl_f32x4_cmp_ge() {
137 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
138 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
139 let expected: [i32; 4] = [0, -1, -1, -1];
140 let actual: [i32; 4] = cast(a.cmp_ge(b));
141 assert_eq!(expected, actual);
142 }
143
144 #[test]
145 fn impl_f32x4_cmp_gt() {
146 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
147 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
148 let expected: [i32; 4] = [0, 0, -1, -1];
149 let actual: [i32; 4] = cast(a.cmp_gt(b));
150 assert_eq!(expected, actual);
151 }
152
153 #[test]
154 fn impl_f32x4_cmp_le() {
155 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
156 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
157 let expected: [i32; 4] = [-1, -1, 0, 0];
158 let actual: [i32; 4] = cast(a.cmp_le(b));
159 assert_eq!(expected, actual);
160 }
161
162 #[test]
163 fn impl_f32x4_cmp_lt() {
164 let a = f32x4::from([1.0, 2.0, 3.0, 4.0]);
165 let b = f32x4::from([2.0, 2.0, 2.0, 2.0]);
166 let expected: [i32; 4] = [-1, 0, 0, 0];
167 let actual: [i32; 4] = cast(a.cmp_lt(b));
168 assert_eq!(expected, actual);
169
170 let expected: [i32; 4] = [0, 0, 0, 0];
171 let actual: [i32; 4] = cast(a.cmp_lt(a));
172 assert_eq!(expected, actual);
173 }
174
175 #[test]
176 fn impl_f32x4_blend() {
177 let use_t: f32 = f32::from_bits(u32::MAX);
178 let t = f32x4::from([1.0, 2.0, 3.0, 4.0]);
179 let f = f32x4::from([5.0, 6.0, 7.0, 8.0]);
180 let mask = f32x4::from([use_t, 0.0, use_t, 0.0]);
181 let expected = f32x4::from([1.0, 6.0, 3.0, 8.0]);
182 let actual = mask.blend(t, f);
183 assert_eq!(expected, actual);
184 }
185
186 #[test]
187 fn impl_f32x4_abs() {
188 let a = f32x4::from([-1.0, 2.0, -3.5, f32::NEG_INFINITY]);
189 let expected = f32x4::from([1.0, 2.0, 3.5, f32::INFINITY]);
190 let actual = a.abs();
191 assert_eq!(expected, actual);
192 }
193
194 #[test]
195 fn impl_f32x4_floor() {
196 let a = f32x4::from([-1.1, 60.9, 1.1, f32::INFINITY]);
197 let expected = f32x4::from([-2.0, 60.0, 1.0, f32::INFINITY]);
198 let actual = a.floor();
199 assert_eq!(expected, actual);
200 }
201
202 #[test]
203 fn impl_f32x4_ceil() {
204 let a = f32x4::from([-1.1, 60.9, 1.1, f32::NEG_INFINITY]);
205 let expected = f32x4::from([-1.0, 61.0, 2.0, f32::NEG_INFINITY]);
206 let actual = a.ceil();
207 assert_eq!(expected, actual);
208 }
209
210 #[test]
211 fn impl_f32x4_fast_max() {
212 let a = f32x4::from([1.0, 5.0, 3.0, -4.0]);
213 let b = f32x4::from([2.0, 3.0, -5.0, -10.0]);
214 let expected = f32x4::from([2.0, 5.0, 3.0, -4.0]);
215 let actual = a.fast_max(b);
216 assert_eq!(expected, actual);
217 }
218
219 #[test]
220 fn impl_f32x4_max() {
221 let a = f32x4::from([1.0, 5.0, -3.0, -0.0]);
222 let b = f32x4::from([2.0, -5.0, -1.0, 0.0]);
223 let expected = f32x4::from([2.0, 5.0, -1.0, 0.0]);
224 let actual = a.max(b);
225 assert_eq!(expected, actual);
226
227 let a = f32x4::from([1.0, 5.0, 3.0, f32::NAN]);
228 let b = f32x4::from([f32::NAN, f32::NEG_INFINITY, f32::INFINITY, 10.0]);
229 let expected = f32x4::from([1.0, 5.0, f32::INFINITY, 10.0]);
230 let actual = a.max(b);
231 assert_eq!(expected, actual);
232 }
233
234 #[test]
235 fn impl_f32x4_fast_min() {
236 let a = f32x4::from([1.0, 5.0, 3.0, -4.0]);
237 let b = f32x4::from([2.0, 3.0, -5.0, -10.0]);
238 let expected = f32x4::from([1.0, 3.0, -5.0, -10.0]);
239 let actual = a.fast_min(b);
240 assert_eq!(expected, actual);
241 }
242
243 #[test]
244 fn impl_f32x4_min() {
245 let a = f32x4::from([1.0, 5.0, -3.0, -0.0]);
246 let b = f32x4::from([2.0, -5.0, -1.0, 0.0]);
247 let expected = f32x4::from([1.0, -5.0, -3.0, -0.0]);
248 let actual = a.min(b);
249 assert_eq!(expected, actual);
250
251 let a = f32x4::from([1.0, 5.0, 3.0, f32::NAN]);
252 let b = f32x4::from([f32::NAN, f32::NEG_INFINITY, f32::INFINITY, 10.0]);
253 let expected = f32x4::from([1.0, f32::NEG_INFINITY, 3.0, 10.0]);
254 let actual = a.min(b);
255 assert_eq!(expected, actual);
256 }
257
258 #[test]
259 fn impl_f32x4_is_nan() {
260 let a = f32x4::from([0.0, f32::NAN, f32::NAN, 0.0]);
261 let expected = [0, u32::MAX, u32::MAX, 0];
262 let actual: [u32; 4] = cast(a.is_nan());
263 assert_eq!(expected, actual);
264 }
265
266 #[test]
267 fn impl_f32x4_is_finite() {
268 let a = f32x4::from([f32::NAN, 1.0, f32::INFINITY, f32::NEG_INFINITY]);
269 let expected = [0, u32::MAX, 0, 0];
270 let actual: [u32; 4] = cast(a.is_finite());
271 assert_eq!(expected, actual);
272 }
273
274 #[test]
275 fn impl_f32x4_is_inf() {
276 let a = f32x4::from([f32::NAN, 1.0, f32::INFINITY, f32::NEG_INFINITY]);
277 let expected = [0, 0, u32::MAX, u32::MAX];
278 let actual: [u32; 4] = cast(a.is_inf());
279 assert_eq!(expected, actual);
280 }
281 #[test]
282 fn impl_f32x4_round() {
283 let a = f32x4::from([1.1, 2.5, 3.7, 4.0]);
284 let expected = f32x4::from([1.0, 2.0, 4.0, 4.0]);
285 let actual = a.round();
286 assert_eq!(expected, actual);
287 //
288 let a = f32x4::from([-1.1, -2.5, -3.7, -4.0]);
289 let expected = f32x4::from([-1.0, -2.0, -4.0, -4.0]);
290 let actual = a.round();
291 assert_eq!(expected, actual);
292 //
293 let a = f32x4::from([f32::INFINITY, f32::NEG_INFINITY, 5.5, 5.0]);
294 let expected = f32x4::from([f32::INFINITY, f32::NEG_INFINITY, 6.0, 5.0]);
295 let actual = a.round();
296 assert_eq!(expected, actual);
297 //
298 let a = f32x4::from(f32::NAN);
299 let expected: [u32; 4] = [u32::MAX; 4];
300 let actual: [u32; 4] = cast(a.round().is_nan());
301 assert_eq!(expected, actual);
302 //
303 let a = f32x4::from(-0.0);
304 let expected = a;
305 let actual = a.round();
306 assert_eq!(expected, actual);
307 }
308
309 #[test]
310 fn impl_f32x4_fast_round_int() {
311 for (f, i) in [(1.0, 1), (1.1, 1), (-2.1, -2), (2.5, 2), (0.0, 0), (-0.0, 0)]
312 .iter()
313 .copied()
314 {
315 let a = f32x4::from(f);
316 let expected = i32x4::from(i);
317 let actual = a.fast_round_int();
318 assert_eq!(expected, actual);
319 }
320 }
321
322 #[test]
323 fn impl_f32x4_round_int() {
324 for (f, i) in [
325 (1.0, 1),
326 (1.1, 1),
327 (-2.1, -2),
328 (2.5, 2),
329 (0.0, 0),
330 (-0.0, 0),
331 (f32::NAN, 0),
332 (f32::INFINITY, i32::MAX),
333 (f32::NEG_INFINITY, i32::MIN),
334 ]
335 .iter()
336 .copied()
337 {
338 let a = f32x4::from(f);
339 let expected = i32x4::from(i);
340 let actual = a.round_int();
341 assert_eq!(expected, actual);
342 }
343 }
344
345 #[test]
346 fn impl_f32x4_fast_trunc_int() {
347 let a = f32x4::from([1.1, 2.5, 3.7, 4.0]);
348 let expected = i32x4::from([1, 2, 3, 4]);
349 let actual = a.fast_trunc_int();
350 assert_eq!(expected, actual);
351 //
352 let a = f32x4::from([-1.1, -2.5, -3.7, -4.0]);
353 let expected = i32x4::from([-1, -2, -3, -4]);
354 let actual = a.fast_trunc_int();
355 assert_eq!(expected, actual);
356 }
357
358 #[test]
359 fn impl_f32x4_trunc_int() {
360 let a = f32x4::from([1.1, 2.5, 3.7, 4.0]);
361 let expected = i32x4::from([1, 2, 3, 4]);
362 let actual = a.trunc_int();
363 assert_eq!(expected, actual);
364 //
365 let a = f32x4::from([-1.1, -2.5, -3.7, -4.0]);
366 let expected = i32x4::from([-1, -2, -3, -4]);
367 let actual = a.trunc_int();
368 assert_eq!(expected, actual);
369 //
370 let a = f32x4::from([f32::NEG_INFINITY, f32::INFINITY, f32::NAN, 0.0]);
371 let expected = i32x4::from([i32::MIN, i32::MAX, 0, 0]);
372 let actual = a.trunc_int();
373 assert_eq!(expected, actual);
374 }
375
376 #[test]
377 fn impl_f32x4_mul_add() {
378 let a = f32x4::from([2.0, 3.0, 4.0, 5.0]);
379 let b = f32x4::from([4.0, 5.0, 6.0, 7.0]);
380 let c = f32x4::from([1.0, 1.0, 1.0, 1.0]);
381 let expected = f32x4::from([9.0, 16.0, 25.0, 36.0]);
382 let actual = a.mul_add(b, c);
383 assert_eq!(expected, actual);
384 }
385
386 #[test]
387 fn impl_f32x4_mul_neg_add() {
388 let a = f32x4::from([2.0, 3.0, 4.0, 5.0]);
389 let b = f32x4::from([4.0, 5.0, 6.0, 7.0]);
390 let c = f32x4::from([1.0, 1.0, 1.0, 1.0]);
391 let expected = f32x4::from([-7.0, -14.0, -23.0, -34.0]);
392 let actual = a.mul_neg_add(b, c);
393 assert_eq!(expected, actual);
394 }
395
396 #[test]
397 fn impl_f32x4_flip_signs() {
398 let a = f32x4::from([1.0, 1.0, -1.0, -1.0]);
399 let b = f32x4::from([2.0, -3.0, 4.0, -5.0]);
400 let expected = f32x4::from([1.0, -1.0, -1.0, 1.0]);
401 let actual = a.flip_signs(b);
402 assert_eq!(expected, actual);
403 }
404
405 #[test]
406 fn impl_f32x4_copysign() {
407 let a = f32x4::from([1.0, 1.0, -1.0, -1.0]);
408 let b = f32x4::from([2.0, -3.0, 4.0, -5.0]);
409 let expected = f32x4::from([1.0, -1.0, 1.0, -1.0]);
410 let actual = a.copysign(b);
411 assert_eq!(expected, actual);
412 }
413
414 #[test]
415 fn impl_f32x4_sin_cos() {
416 for x in -2500..=2500 {
417 let base = (x * 4) as f32;
418 let angles = [base, base + 1.0, base + 2.0, base + 3.0];
419 let (actual_sins, actual_coses) = f32x4::from(angles).sin_cos();
420 for i in 0..4 {
421 let angle = angles[i];
422 let check = |name: &str, vals: f32x4, expected: f32| {
423 let actual_arr: [f32; 4] = cast(vals);
424 let actual = actual_arr[i];
425 assert!(
426 (actual - expected).abs() < 0.0000002,
427 "Wanted {name}({angle}) to be {expected} but got {actual}",
428 name = name,
429 angle = angle,
430 expected = expected,
431 actual = actual
432 );
433 };
434 check("sin", actual_sins, angle.sin());
435 check("cos", actual_coses, angle.cos());
436 }
437 }
438 }
439
440 // NOTE:Disabled for i586
441 #[cfg(target_feature = "sse")]
442 #[test]
443 fn impl_f32x4_asin_acos() {
444 let inc = 1.0 / 2501.0 / 4.0;
445 for x in -2500..=2500 {
446 let base = (x * 4) as f32 * inc;
447 let origs = [base, base + inc, base + 2.0 * inc, base + 3.0 * inc];
448 let (actual_asins, actual_acoses) = f32x4::from(origs).asin_acos();
449 for i in 0..4 {
450 let orig = origs[i];
451 let check = |name: &str, vals: f32x4, expected: f32| {
452 let actual_arr: [f32; 4] = cast(vals);
453 let actual = actual_arr[i];
454 assert!(
455 (actual - expected).abs() < 0.0000006,
456 "Wanted {name}({orig}) to be {expected} but got {actual}",
457 name = name,
458 orig = orig,
459 expected = expected,
460 actual = actual
461 );
462 };
463 check("asin", actual_asins, orig.asin());
464 check("acos", actual_acoses, orig.acos());
465 }
466 }
467 }
468
469 // FIXME: remove cfg requirement once masks as their own types are implemented
470 #[cfg(target_feature = "sse")]
471 #[test]
472 fn impl_f32x4_asin() {
473 let inc = 1.0 / 2501.0 / 4.0;
474 for x in -2500..=2500 {
475 let base = (x * 4) as f32 * inc;
476 let origs = [base, base + inc, base + 2.0 * inc, base + 3.0 * inc];
477 let actual_asins = f32x4::from(origs).asin();
478 for i in 0..4 {
479 let orig = origs[i];
480 let check = |name: &str, vals: f32x4, expected: f32| {
481 let actual_arr: [f32; 4] = cast(vals);
482 let actual = actual_arr[i];
483 assert!(
484 (actual - expected).abs() < 0.0000006,
485 "Wanted {name}({orig}) to be {expected} but got {actual}",
486 name = name,
487 orig = orig,
488 expected = expected,
489 actual = actual
490 );
491 };
492 check("asin", actual_asins, orig.asin());
493 }
494 }
495 }
496
497 // FIXME: remove cfg requirement once masks as their own types are implemented
498 #[cfg(target_feature = "sse")]
499 #[test]
500 fn impl_f32x4_acos() {
501 let inc = 1.0 / 2501.0 / 4.0;
502 for x in -2500..=2500 {
503 let base = (x * 4) as f32 * inc;
504 let origs = [base, base + inc, base + 2.0 * inc, base + 3.0 * inc];
505 let actual_acoses = f32x4::from(origs).acos();
506 for i in 0..4 {
507 let orig = origs[i];
508 let check = |name: &str, vals: f32x4, expected: f32| {
509 let actual_arr: [f32; 4] = cast(vals);
510 let actual = actual_arr[i];
511 assert!(
512 (actual - expected).abs() < 0.0000006,
513 "Wanted {name}({orig}) to be {expected} but got {actual}",
514 name = name,
515 orig = orig,
516 expected = expected,
517 actual = actual
518 );
519 };
520 check("acos", actual_acoses, orig.acos());
521 }
522 }
523 }
524
525 // FIXME: remove cfg requirement once masks as their own types are implemented
526 #[cfg(target_feature = "sse")]
527 #[test]
528 fn impl_f32x4_atan() {
529 let inc = 1.0 / 2501.0 / 4.0;
530 for x in -2500..=2500 {
531 let base = (x * 4) as f32 * inc;
532 let origs = [base, base + inc, base + 2.0 * inc, base + 3.0 * inc];
533 let actual_atans = f32x4::from(origs).atan();
534 for i in 0..4 {
535 let orig = origs[i];
536 let check = |name: &str, vals: f32x4, expected: f32| {
537 let actual_arr: [f32; 4] = cast(vals);
538 let actual = actual_arr[i];
539 assert!(
540 (actual - expected).abs() < 0.0000006,
541 "Wanted {name}({orig}) to be {expected} but got {actual}",
542 name = name,
543 orig = orig,
544 expected = expected,
545 actual = actual
546 );
547 };
548 check("atan", actual_atans, orig.atan());
549 }
550 }
551 }
552
553 // FIXME: remove cfg requirement once masks as their own types are implemented
554 #[cfg(target_feature = "sse")]
555 #[test]
556 fn impl_f32x4_atan2() {
557 let inc_y = 1.0 / 51.0 / 4.0;
558 let inc_x = 1.0 / 2501.0 / 4.0;
559 for y in -50..=50 {
560 let base_y = (y * 4) as f32 * inc_y;
561 let origs_y =
562 [base_y, base_y + inc_y, base_y + 2.0 * inc_y, base_y + 3.0 * inc_y];
563 let actual_y = f32x4::from(origs_y);
564 for x in -2500..=2500 {
565 let base_x = (x * 4) as f32 * inc_x;
566 let origs_x =
567 [base_x, base_x + inc_x, base_x + 2.0 * inc_x, base_x + 3.0 * inc_x];
568 let actual_x = f32x4::from(origs_x);
569 let actual_atan2s = actual_y.atan2(actual_x);
570 for i in 0..4 {
571 let orig_y = origs_y[i];
572 let orig_x = origs_x[i];
573 let check = |name: &str, vals: f32x4, expected: f32| {
574 let actual_arr: [f32; 4] = cast(vals);
575 let actual = actual_arr[i];
576 assert!(
577 (actual - expected).abs() < 0.0000006,
578 "Wanted {name}({orig_y}, {orig_x}) to be {expected} but got {actual}",
579 name = name,
580 orig_y = orig_y,
581 orig_x = orig_x,
582 expected = expected,
583 actual = actual
584 );
585 };
586 check("atan2", actual_atan2s, orig_y.atan2(orig_x));
587 }
588 }
589 }
590 }
591
592 #[test]
593 fn impl_f32x4_to_degrees() {
594 let pi = core::f32::consts::PI;
595 let a = f32x4::from([0.0, pi / 2.0, pi, 2.0 * pi]);
596 let expected = f32x4::from([0.0, 90.0, 180.0, 360.0]);
597 let actual = a.to_degrees();
598 assert_eq!(expected, actual);
599 }
600
601 #[test]
602 fn impl_f32x4_to_radians() {
603 let pi = core::f32::consts::PI;
604 let a = f32x4::from([0.0, 90.0, 180.0, 360.0]);
605 let expected = f32x4::from([0.0, pi / 2.0, pi, 2.0 * pi]);
606 let actual = a.to_radians();
607 assert_eq!(expected, actual);
608 }
609
610 #[test]
611 fn impl_f32x4_recip() {
612 {
613 let expected = f32x4::from(0.0);
614 let actual = f32x4::from(f32::INFINITY).recip();
615 assert_eq!(expected, actual);
616 }
617 {
618 let expected = f32x4::from(0.0);
619 let actual = f32x4::from(-f32::INFINITY).recip();
620 assert_eq!(expected, actual);
621 }
622 {
623 let actual = f32x4::from(f32::NAN).recip();
624 assert!(actual.is_nan().any());
625 }
626 {
627 let expected = f32x4::from(f32::INFINITY);
628 let actual = f32x4::from(0.0).recip();
629 assert_eq!(expected, actual);
630 }
631 {
632 let expected = f32x4::from(0.49987793);
633 let actual = f32x4::from(2.0).recip();
634 let diff: [f32; 4] = cast((actual - expected).abs());
635 assert!(diff[0] < 0.001);
636 }
637 {
638 let expected = f32x4::from(-0.08102417);
639 let actual = f32x4::from(-12.34).recip();
640 let diff: [f32; 4] = cast((actual - expected).abs());
641 assert!(diff[0] < 0.001);
642 }
643 }
644
645 #[test]
646 fn impl_f32x4_recip_sqrt() {
647 {
648 let expected = f32x4::from(0.0);
649 let actual = f32x4::from(f32::INFINITY).recip_sqrt();
650 assert_eq!(expected, actual);
651 }
652 {
653 let actual = f32x4::from(-f32::INFINITY).recip_sqrt();
654 assert!(actual.is_nan().any());
655 }
656 {
657 let actual = f32x4::from(f32::NAN).recip_sqrt();
658 assert!(actual.is_nan().any());
659 }
660 {
661 let expected = f32x4::from(f32::INFINITY);
662 let actual = f32x4::from(0.0).recip_sqrt();
663 assert_eq!(expected, actual);
664 }
665 {
666 let expected = f32x4::from(0.70703125);
667 let actual = f32x4::from(2.0).recip_sqrt();
668 let diff: [f32; 4] = cast((actual - expected).abs());
669 assert!(diff[0] < 0.001);
670 }
671 {
672 let actual = f32x4::from(-12.34).recip_sqrt();
673 assert!(actual.is_nan().any());
674 }
675 }
676
677 #[test]
678 fn impl_f32x4_sqrt() {
679 for (f, e) in [
680 (f32::INFINITY, f32::INFINITY),
681 (0.0, 0.0),
682 (-0.0, -0.0),
683 (4.0, 2.0),
684 (9.0, 3.0),
685 (16.0, 4.0),
686 (25.0, 5.0),
687 (5000.0 * 5000.0, 5000.0),
688 ]
689 .iter()
690 .copied()
691 {
692 let expected = f32x4::from(e);
693 let actual = f32x4::from(f).sqrt();
694 assert_eq!(expected, actual);
695 }
696 assert_eq!(
697 cast::<_, i32x4>(f32x4::from(f32::NAN).sqrt().is_nan()),
698 i32x4::from(-1)
699 );
700 assert_eq!(
701 cast::<_, i32x4>(f32x4::from(f32::NEG_INFINITY).sqrt().is_nan()),
702 i32x4::from(-1)
703 );
704 assert_eq!(
705 cast::<_, i32x4>(f32x4::from(-1.0).sqrt().is_nan()),
706 i32x4::from(-1)
707 );
708 }
709
710 #[test]
711 fn impl_f32x4_exp() {
712 for f in [(-2.0), (-1.0), (0.0), (1.0), (1.5), (2.0), (10.0)].iter().copied()
713 {
714 let expected = f32x4::from((f as f32).exp());
715 let actual = f32x4::from(f).exp();
716 let diff_from_std: [f32; 4] = cast((actual - expected).abs());
717 assert!(diff_from_std[0] < 0.000000000000001);
718 }
719 }
720
721 #[test]
722 fn test_f32x4_move_mask() {
723 let a = f32x4::from([-1.0, 0.0, -2.0, -3.0]);
724 let expected = 0b1101;
725 let actual = a.move_mask();
726 assert_eq!(expected, actual);
727 //
728 let a = f32x4::from([1.0, 0.0, 2.0, -3.0]);
729 let expected = 0b1000;
730 let actual = a.move_mask();
731 assert_eq!(expected, actual);
732 }
733
734 #[test]
735 fn test_f32x4_any() {
736 let a = f32x4::from([-1.0, 0.0, -2.0, f32::NAN]).is_nan();
737 assert!(a.any());
738 //
739 let a = f32x4::from([1.0, 0.0, 2.0, 3.0]).is_nan();
740 assert!(!a.any());
741 }
742
743 #[test]
744 fn test_f32x4_all() {
745 let a = f32x4::from([f32::NAN; 4]).is_nan();
746 assert!(a.all());
747 //
748 let a = f32x4::from([1.0, -0.0, 2.0, f32::NAN]).is_nan();
749 assert!(!a.all());
750 }
751
752 #[test]
753 fn test_f32x4_none() {
754 let a = f32x4::from([1.0, 0.0, 2.0, 3.0]).is_nan();
755 assert!(a.none());
756 //
757 let a = f32x4::from([1.0, -0.0, 2.0, f32::NAN]).is_nan();
758 assert!(!a.none());
759 }
760
761 #[test]
762 fn impl_f32x4_ln() {
763 for f in [0.1, 0.5, 1.0, 2.718282, 10.0, 35.0, 1250.0].iter().copied() {
764 let expected = f32x4::from((f as f32).ln());
765 let actual = f32x4::from(f).ln();
766 let diff_from_std: [f32; 4] = cast((actual - expected).abs());
767 assert!(diff_from_std[0] < 0.000001);
768 }
769 }
770
771 #[test]
772 fn impl_f32x4_pow() {
773 for f in [0.1, 0.5, 1.0, 2.718282, 3.0, 4.0, 2.5, -1.0].iter().copied() {
774 let expected = f32x4::splat(2.0 as f32).powf(f);
775 let actual = f32x4::from(2.0_f32.powf(f));
776 let diff_from_std: [f32; 4] = cast((actual - expected).abs());
777 assert!(diff_from_std[0] < 0.000001);
778 }
779 }
780
781 #[test]
782 fn impl_f32x4_pow_n() {
783 let p = f32x4::from([29.0, 0.1, 0.5, 1.0]);
784 let f = f32x4::from([1.2, 2.0, 3.0, 1.5]);
785 let res = f.pow_f32x4(p);
786
787 let p: [f32; 4] = cast(p);
788 let f: [f32; 4] = cast(f);
789 let res: [f32; 4] = cast(res);
790 for i in 0..p.len() {
791 let expected = f[i].powf(p[i]);
792 if !(expected.is_nan() && res[i].is_nan()) {
793 assert!((expected - res[i]).abs() < 0.0001);
794 }
795 }
796
797 let p = f32x4::from([2.718282, -0.2, -1.5, 3.4]);
798 let f = f32x4::from([9.2, 6.1, 2.5, -4.5]);
799 let res = f.pow_f32x4(p);
800
801 let p: [f32; 4] = cast(p);
802 let f: [f32; 4] = cast(f);
803 let res: [f32; 4] = cast(res);
804 for i in 0..p.len() {
805 let expected = f[i].powf(p[i]);
806 if !(expected.is_nan() && res[i].is_nan()) {
807 assert!((expected - res[i]).abs() < 0.0001);
808 }
809 }
810 }
811
812 #[test]
813 fn impl_f32x4_reduce_add() {
814 let p = f32x4::splat(0.001);
815 assert_eq!(p.reduce_add(), 0.004);
816 }
817
818 #[test]
819 fn impl_f32x4_sum() {
820 let mut p = Vec::with_capacity(250_000);
821 for _ in 0..250_000 {
822 p.push(f32x4::splat(0.001));
823 }
824 let now = std::time::Instant::now();
825 let sum: f32 = p.iter().map(|x| x.reduce_add()).sum();
826 let duration = now.elapsed().as_micros();
827 println!("Time take {} {}us", sum, duration);
828
829 let p = vec![0.001; 1_000_000];
830 let now = std::time::Instant::now();
831 let sum2: f32 = p.iter().sum();
832 let duration = now.elapsed().as_micros();
833 println!("Time take {} {}us", sum2, duration);
834 }
835
836 #[test]
837 fn impl_f32x4_transpose() {
838 let a = [
839 f32x4::from([1.0, 2.0, 3.0, 4.0]),
840 f32x4::from([5.0, 6.0, 7.0, 8.0]),
841 f32x4::from([9.0, 10.0, 11.0, 12.0]),
842 f32x4::from([13.0, 14.0, 15.0, 16.0]),
843 ];
844 let a_t = [
845 f32x4::from([1.0, 5.0, 9.0, 13.0]),
846 f32x4::from([2.0, 6.0, 10.0, 14.0]),
847 f32x4::from([3.0, 7.0, 11.0, 15.0]),
848 f32x4::from([4.0, 8.0, 12.0, 16.0]),
849 ];
850 assert_eq!(f32x4::transpose(a), a_t);
851 }
852
853 #[test]
854 fn impl_f32x4_from_i32x4() {
855 let i = i32x4::from([1, 2, 3, 4]);
856 let f = f32x4::from([1.0, 2.0, 3.0, 4.0]);
857 assert_eq!(f32x4::from_i32x4(i), f)
858 }
859
860 #[cfg(feature = "serde")]
861 #[test]
862 fn impl_f32x4_ser_de_roundtrip() {
863 let serialized =
864 bincode::serialize(&f32x4::ZERO).expect("serialization failed");
865 let deserialized =
866 bincode::deserialize(&serialized).expect("deserializaion failed");
867 assert_eq!(f32x4::ZERO, deserialized);
868 }