Debian Codegraph @

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

raw
1 use wide::*;
2
3 use bytemuck::*;
4
5 #[test]
6 fn size_align() {
7 assert_eq!(core::mem::size_of::<f32x8>(), 32);
8 assert_eq!(core::mem::align_of::<f32x8>(), 32);
9 }
10
11 #[test]
12 fn impl_debug_for_f32x8() {
13 let expected = "(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)";
14 let actual =
15 format!("{:?}", f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]));
16 assert_eq!(expected, actual);
17
18 let expected = "(1.000, 2.000, 3.000, 4.000, 5.000, 6.000, 7.000, 8.000)";
19 let actual =
20 format!("{:.3?}", f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]));
21 assert_eq!(expected, actual);
22 }
23
24 #[test]
25 fn impl_add_for_f32x8() {
26 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
27 let b = f32x8::from([5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]);
28 let expected = f32x8::from([6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0]);
29 let actual = a + b;
30 assert_eq!(expected, actual);
31 }
32
33 #[test]
34 fn impl_add_const_for_f32x8() {
35 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
36 let expected = f32x8::from([6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0]);
37 let actual = a + 5.0;
38 assert_eq!(expected, actual);
39 }
40
41 #[test]
42 fn impl_sub_const_for_f32x8() {
43 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
44 let expected = f32x8::from([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
45 let actual = a - 2.0;
46 assert_eq!(expected, actual);
47 }
48
49 #[test]
50 fn impl_mul_const_for_f32x8() {
51 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
52 let expected = f32x8::from([2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0]);
53 let actual = a * 2.0;
54 assert_eq!(expected, actual);
55 }
56
57 #[test]
58 fn impl_div_const_for_f32x8() {
59 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
60 let expected = f32x8::from([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]);
61 let actual = a / 2.0;
62 assert_eq!(expected, actual);
63 }
64
65 #[test]
66 fn impl_sub_for_f32x8() {
67 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
68 let b = f32x8::from([5.0, 7.0, 17.0, 1.0, 1.0, 9.0, 2.0, 6.0]);
69 let expected = f32x8::from([-4.0, -5.0, -14.0, 3.0, 4.0, -3.0, 5.0, 2.0]);
70 let actual = a - b;
71 assert_eq!(expected, actual);
72 }
73
74 #[test]
75 fn impl_mul_for_f32x8() {
76 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
77 let b = f32x8::from([5.0, 7.0, 17.0, 1.0, 5.0, 6.0, 7.0, 8.0]);
78 let expected = f32x8::from([5.0, 14.0, 51.0, 4.0, 25.0, 36.0, 49.0, 64.0]);
79 let actual = a * b;
80 assert_eq!(expected, actual);
81 }
82
83 #[test]
84 fn impl_div_for_f32x8() {
85 let a = f32x8::from([4.0, 9.0, 10.0, 12.0, 5.0, 6.0, 7.0, 8.0]);
86 let b = f32x8::from([2.0, 2.0, 5.0, -3.0, 2.0, 1.5, 3.0, 2.5]);
87 let expected = f32x8::from([2.0, 4.5, 2.0, -4.0, 2.5, 4.0, 2.3333333, 3.2]);
88 let actual = a / b;
89 assert_eq!(expected, actual);
90 }
91
92 #[test]
93 fn impl_bitand_for_f32x8() {
94 let a = f32x8::from([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]);
95 let b = f32x8::from([0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0]);
96 let expected = f32x8::from([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
97 let actual = a & b;
98 assert_eq!(expected, actual);
99 }
100
101 #[test]
102 fn impl_bitor_for_f32x8() {
103 let a = f32x8::from([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]);
104 let b = f32x8::from([0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0]);
105 let expected = f32x8::from([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]);
106 let actual = a | b;
107 assert_eq!(expected, actual);
108 }
109
110 #[test]
111 fn impl_bitxor_for_f32x8() {
112 let a = f32x8::from([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]);
113 let b = f32x8::from([0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0]);
114 let expected = f32x8::from([0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0]);
115 let actual = a ^ b;
116 assert_eq!(expected, actual);
117 }
118
119 #[test]
120 fn impl_f32x8_cmp_eq() {
121 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0]);
122 let b = f32x8::from([2.0; 8]);
123 let expected: [i32; 8] = [0, -1, 0, 0, 0, 0, -1, 0];
124 let actual: [i32; 8] = cast(a.cmp_eq(b));
125 assert_eq!(expected, actual);
126 }
127
128 #[test]
129 fn impl_f32x8_cmp_ne() {
130 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0]);
131 let b = f32x8::from([2.0; 8]);
132 let expected: [i32; 8] = [-1, 0, -1, -1, -1, -1, 0, -1];
133 let actual: [i32; 8] = cast(a.cmp_ne(b));
134 assert_eq!(expected, actual);
135 }
136
137 #[test]
138 fn impl_f32x8_cmp_ge() {
139 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0]);
140 let b = f32x8::from([2.0; 8]);
141 let expected: [i32; 8] = [0, -1, -1, -1, -1, -1, -1, 0];
142 let actual: [i32; 8] = cast(a.cmp_ge(b));
143 assert_eq!(expected, actual);
144 }
145
146 #[test]
147 fn impl_f32x8_cmp_gt() {
148 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 3.0, 1.0]);
149 let b = f32x8::from([3.0; 8]);
150 let expected: [i32; 8] = [0, 0, 0, -1, -1, -1, 0, 0];
151 let actual: [i32; 8] = cast(a.cmp_gt(b));
152 assert_eq!(expected, actual);
153 }
154
155 #[test]
156 fn impl_f32x8_cmp_le() {
157 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0]);
158 let b = f32x8::from([4.0; 8]);
159 let expected: [i32; 8] = [-1, -1, -1, -1, 0, 0, -1, -1];
160 let actual: [i32; 8] = cast(a.cmp_le(b));
161 assert_eq!(expected, actual);
162 }
163
164 #[test]
165 fn impl_f32x8_cmp_lt() {
166 let a = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0]);
167 let b = f32x8::from([3.0; 8]);
168 let expected: [i32; 8] = [-1, -1, 0, 0, 0, 0, -1, -1];
169 let actual: [i32; 8] = cast(a.cmp_lt(b));
170 assert_eq!(expected, actual);
171
172 let expected: [i32; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
173 let actual: [i32; 8] = cast(a.cmp_lt(a));
174 assert_eq!(expected, actual);
175 }
176
177 #[test]
178 fn impl_f32x8_blend() {
179 let use_t: f32 = f32::from_bits(u32::MAX);
180 let t = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
181 let f = f32x8::from([5.0, 6.0, 7.0, 8.0, 21.0, 22.0, 23.0, 24.0]);
182 let mask = f32x8::from([use_t, 0.0, use_t, 0.0, 0.0, 0.0, 0.0, use_t]);
183 let expected = f32x8::from([1.0, 6.0, 3.0, 8.0, 21.0, 22.0, 23.0, 8.0]);
184 let actual = mask.blend(t, f);
185 assert_eq!(expected, actual);
186 }
187
188 #[test]
189 fn impl_f32x8_abs() {
190 let a =
191 f32x8::from([-1.0, 2.0, -3.5, f32::NEG_INFINITY, 6.0, 15.0, -19.0, -9.0]);
192 let expected =
193 f32x8::from([1.0, 2.0, 3.5, f32::INFINITY, 6.0, 15.0, 19.0, 9.0]);
194 let actual = a.abs();
195 assert_eq!(expected, actual);
196 }
197
198 #[test]
199 fn impl_f32x8_floor() {
200 let a = f32x8::from([-1.1, 60.9, 1.1, f32::INFINITY, 96.6, -53.2, 0.1, 9.2]);
201 let expected =
202 f32x8::from([-2.0, 60.0, 1.0, f32::INFINITY, 96.0, -54.0, 0.0, 9.0]);
203 let actual = a.floor();
204 assert_eq!(expected, actual);
205 }
206
207 #[test]
208 fn impl_f64x4_ceil() {
209 let a =
210 f32x8::from([-1.1, 60.9, 1.1, f32::NEG_INFINITY, 96.6, -53.2, 0.1, 9.2]);
211 let expected =
212 f32x8::from([-1.0, 61.0, 2.0, f32::NEG_INFINITY, 97.0, -53.0, 1.0, 10.0]);
213 let actual = a.ceil();
214 assert_eq!(expected, actual);
215 }
216
217 #[test]
218 fn impl_f32x8_fast_max() {
219 let a = f32x8::from([1.0, 5.0, 3.0, 0.0, 6.0, -8.0, 12.0, 9.0]);
220 let b = f32x8::from([2.0, -3.0, f32::INFINITY, 10.0, 19.0, -5.0, -1.0, -9.0]);
221 let expected =
222 f32x8::from([2.0, 5.0, f32::INFINITY, 10.0, 19.0, -5.0, 12.0, 9.0]);
223 let actual = a.fast_max(b);
224 assert_eq!(expected, actual);
225 }
226
227 #[test]
228 fn impl_f32x8_max() {
229 let a = f32x8::from([1.0, 5.0, 3.0, f32::NAN, 6.0, -8.0, 12.0, f32::NAN]);
230 let b =
231 f32x8::from([2.0, -3.0, f32::INFINITY, 10.0, 19.0, f32::NAN, -1.0, -9.0]);
232 let expected =
233 f32x8::from([2.0, 5.0, f32::INFINITY, 10.0, 19.0, -8.0, 12.0, -9.0]);
234 let actual = a.max(b);
235 assert_eq!(expected, actual);
236 }
237
238 #[test]
239 fn impl_f32x8_fast_min() {
240 let a = f32x8::from([1.0, 5.0, 3.0, f32::NEG_INFINITY, 6.0, -8.0, 12.0, 9.0]);
241 let b = f32x8::from([2.0, -3.0, f32::INFINITY, 10.0, 19.0, -5.0, -1.0, -9.0]);
242 let expected =
243 f32x8::from([1.0, -3.0, 3.0, f32::NEG_INFINITY, 6.0, -8.0, -1.0, -9.0]);
244 let actual = a.fast_min(b);
245 assert_eq!(expected, actual);
246 }
247
248 #[test]
249 fn impl_f32x8_min() {
250 let a =
251 f32x8::from([1.0, 5.0, 3.0, f32::NEG_INFINITY, 6.0, -8.0, 12.0, f32::NAN]);
252 let b =
253 f32x8::from([2.0, -3.0, f32::INFINITY, 10.0, 19.0, f32::NAN, -1.0, -9.0]);
254 let expected =
255 f32x8::from([1.0, -3.0, 3.0, f32::NEG_INFINITY, 6.0, -8.0, -1.0, -9.0]);
256 let actual = a.min(b);
257 assert_eq!(expected, actual);
258 }
259
260 #[test]
261 fn impl_f32x8_is_nan() {
262 let a = f32x8::from([0.0, f32::NAN, f32::NAN, 0.0, 0.0, 0.0, f32::NAN, 0.0]);
263 let expected: [u32; 8] = [0, u32::MAX, u32::MAX, 0, 0, 0, u32::MAX, 0];
264 let actual: [u32; 8] = cast(a.is_nan());
265 assert_eq!(expected, actual);
266 }
267
268 #[test]
269 fn impl_f32x8_is_finite() {
270 let a = f32x8::from([
271 f32::NAN,
272 1.0,
273 f32::INFINITY,
274 f32::NEG_INFINITY,
275 2.0,
276 5.0,
277 f32::INFINITY,
278 9.0,
279 ]);
280 let expected: [u32; 8] = [0, u32::MAX, 0, 0, u32::MAX, u32::MAX, 0, u32::MAX];
281 let actual: [u32; 8] = cast(a.is_finite());
282 assert_eq!(expected, actual);
283 }
284
285 #[test]
286 fn impl_f32x8_round() {
287 let a = f32x8::from([1.1, 2.5, 3.7, 4.0, 7.2, 10.5, 12.7, 35.12]);
288 let expected = f32x8::from([1.0, 2.0, 4.0, 4.0, 7.0, 10.0, 13.0, 35.0]);
289 let actual = a.round();
290 assert_eq!(expected, actual);
291 //
292 let a = f32x8::from([-1.1, -2.5, -3.7, -4.0, -7.2, -10.5, -12.7, -35.12]);
293 let expected =
294 f32x8::from([-1.0, -2.0, -4.0, -4.0, -7.0, -10.0, -13.0, -35.0]);
295 let actual = a.round();
296 assert_eq!(expected, actual);
297 //
298 let a = f32x8::from([
299 f32::INFINITY,
300 f32::NEG_INFINITY,
301 5.5,
302 5.0,
303 7.2,
304 10.5,
305 12.7,
306 35.12,
307 ]);
308 let expected = f32x8::from([
309 f32::INFINITY,
310 f32::NEG_INFINITY,
311 6.0,
312 5.0,
313 7.0,
314 10.0,
315 13.0,
316 35.0,
317 ]);
318 let actual = a.round();
319 assert_eq!(expected, actual);
320 //
321 let a = f32x8::from(f32::NAN);
322 let expected: [u32; 8] = [u32::MAX; 8];
323 let actual: [u32; 8] = cast(a.round().is_nan());
324 assert_eq!(expected, actual);
325 //
326 let a = f32x8::from(-0.0);
327 let expected = a;
328 let actual = a.round();
329 assert_eq!(expected, actual);
330 }
331
332 #[test]
333 fn impl_f32x8_fast_round_int() {
334 for (f, i) in [(1.0, 1), (1.1, 1), (-2.1, -2), (2.5, 2), (0.0, 0), (-0.0, 0)]
335 .iter()
336 .copied()
337 {
338 let a = f32x8::from(f);
339 let expected = i32x8::from(i);
340 let actual = a.fast_round_int();
341 assert_eq!(expected, actual);
342 }
343 }
344
345 #[test]
346 fn impl_f32x8_round_int() {
347 for (f, i) in [
348 (1.0, 1),
349 (1.1, 1),
350 (-2.1, -2),
351 (2.5, 2),
352 (0.0, 0),
353 (-0.0, 0),
354 (f32::NAN, 0),
355 (f32::INFINITY, i32::MAX),
356 (f32::NEG_INFINITY, i32::MIN),
357 ]
358 .iter()
359 .copied()
360 {
361 let a = f32x8::from(f);
362 let expected = i32x8::from(i);
363 let actual = a.round_int();
364 assert_eq!(expected, actual);
365 }
366 }
367
368 #[test]
369 fn impl_f32x8_fast_trunc_int() {
370 for (f, i) in [(1.0, 1), (1.1, 1), (-2.1, -2), (2.5, 2), (3.7, 3), (-0.0, 0)]
371 .iter()
372 .copied()
373 {
374 let a = f32x8::from(f);
375 let expected = i32x8::from(i);
376 let actual = a.fast_trunc_int();
377 assert_eq!(expected, actual);
378 }
379 }
380
381 #[test]
382 fn impl_f32x8_trunc_int() {
383 for (f, i) in [
384 (1.0, 1),
385 (1.1, 1),
386 (-2.1, -2),
387 (2.5, 2),
388 (3.7, 3),
389 (-0.0, 0),
390 (f32::NAN, 0),
391 (f32::INFINITY, i32::MAX),
392 (f32::NEG_INFINITY, i32::MIN),
393 ]
394 .iter()
395 .copied()
396 {
397 let a = f32x8::from(f);
398 let expected = i32x8::from(i);
399 let actual = a.trunc_int();
400 assert_eq!(expected, actual);
401 }
402 }
403
404 #[test]
405 fn impl_f32x8_mul_add() {
406 let a = f32x8::from([2.0, 3.0, 4.0, 5.0, 6.7, 9.2, 11.5, 12.2]);
407 let b = f32x8::from([4.0, 5.0, 6.0, 7.0, 1.5, 8.9, 4.2, 5.6]);
408 let c = f32x8::from([1.0; 8]);
409 let expected: [f32; 8] =
410 cast(f32x8::from([9.0, 16.0, 25.0, 36.0, 11.05, 82.88, 49.3, 69.32]));
411 let actual: [f32; 8] = cast(a.mul_add(b, c));
412 for (act, exp) in actual.iter().zip(expected.iter()) {
413 assert!((exp - act).abs() < 0.000001);
414 }
415 }
416
417 #[test]
418 fn impl_f32x8_mul_neg_add() {
419 let a = f32x8::from([2.0, 3.0, 4.0, 5.0, 6.7, 9.2, 11.5, 12.2]);
420 let b = f32x8::from([4.0, 5.0, 6.0, 7.0, 1.5, 8.9, 4.2, -5.6]);
421 let c = f32x8::from([1.0; 8]);
422 let expected: [f32; 8] =
423 cast(f32x8::from([-7.0, -14.0, -23.0, -34.0, -9.05, -80.88, -47.3, 69.32]));
424 let actual: [f32; 8] = cast(a.mul_neg_add(b, c));
425 for (act, exp) in actual.iter().zip(expected.iter()) {
426 assert!((exp - act).abs() < 0.00001);
427 }
428 }
429
430 #[test]
431 fn impl_f32x8_flip_signs() {
432 let a = f32x8::from([1.0, 1.0, -1.0, -1.0, 5.2, 6.7, -8.2, -12.5]);
433 let b = f32x8::from([2.0, -3.0, 4.0, -5.0, 5.2, 6.7, -8.2, -12.5]);
434 let expected = f32x8::from([1.0, -1.0, -1.0, 1.0, 5.2, 6.7, 8.2, 12.5]);
435 let actual = a.flip_signs(b);
436 assert_eq!(expected, actual);
437 }
438
439 #[test]
440 fn impl_f32x8_copysign() {
441 let a = f32x8::from([1.0, 1.0, -1.0, -1.0, 5.2, 6.7, -8.2, -12.5]);
442 let b = f32x8::from([2.0, -3.0, 4.0, -5.0, 5.2, 6.7, -8.2, -12.5]);
443 let expected = f32x8::from([1.0, -1.0, 1.0, -1.0, 5.2, 6.7, -8.2, -12.5]);
444 let actual = a.copysign(b);
445 assert_eq!(expected, actual);
446 }
447
448 // NOTE: Disabled
449 #[cfg(target_feature = "sse")]
450 #[test]
451 fn impl_f32x8_asin_acos() {
452 let inc = 1.0 / 2501.0 / 8.0;
453 for x in -2500..=2500 {
454 let base = (x * 8) as f32 * inc;
455 let origs = [
456 base,
457 base + inc,
458 base + 2.0 * inc,
459 base + 3.0 * inc,
460 base + 4.0 * inc,
461 base + 5.0 * inc,
462 base + 6.0 * inc,
463 base + 7.0 * inc,
464 ];
465 let (actual_asins, actual_acoses) = f32x8::from(origs).asin_acos();
466 for i in 0..8 {
467 let orig = origs[i];
468 let check = |name: &str, vals: f32x8, expected: f32| {
469 let actual_arr: [f32; 8] = cast(vals);
470 let actual = actual_arr[i];
471 assert!(
472 (actual - expected).abs() < 0.0000006,
473 "Wanted {name}({orig}) to be {expected} but got {actual}",
474 name = name,
475 orig = orig,
476 expected = expected,
477 actual = actual
478 );
479 };
480 check("asin", actual_asins, orig.asin());
481 check("acos", actual_acoses, orig.acos());
482 }
483 }
484 }
485
486 // FIXME: remove cfg requirement once masks as their own types are implemented
487 #[cfg(target_feature = "avx")]
488 #[test]
489 fn impl_f32x8_asin() {
490 let inc = 1.0 / 2501.0 / 8.0;
491 for x in -2500..=2500 {
492 let base = (x * 4) as f32 * inc;
493 let origs = [
494 base,
495 base + inc,
496 base + 2.0 * inc,
497 base + 3.0 * inc,
498 base + 4.0 * inc,
499 base + 5.0 * inc,
500 base + 6.0 * inc,
501 base + 7.0 * inc,
502 ];
503 let actual_asins = f32x8::from(origs).asin();
504 for i in 0..8 {
505 let orig = origs[i];
506 let check = |name: &str, vals: f32x8, expected: f32| {
507 let actual_arr: [f32; 8] = cast(vals);
508 let actual = actual_arr[i];
509 assert!(
510 (actual - expected).abs() < 0.0000006,
511 "Wanted {name}({orig}) to be {expected} but got {actual}",
512 name = name,
513 orig = orig,
514 expected = expected,
515 actual = actual
516 );
517 };
518 check("asin", actual_asins, orig.asin());
519 }
520 }
521 }
522
523 // FIXME: remove cfg requirement once masks as their own types are implemented
524 #[cfg(target_feature = "avx")]
525 #[test]
526 fn impl_f32x8_acos() {
527 let inc = 1.0 / 2501.0 / 8.0;
528 for x in -2500..=2500 {
529 let base = (x * 8) as f32 * inc;
530 let origs = [
531 base,
532 base + inc,
533 base + 2.0 * inc,
534 base + 3.0 * inc,
535 base + 4.0 * inc,
536 base + 5.0 * inc,
537 base + 6.0 * inc,
538 base + 7.0 * inc,
539 ];
540 let actual_acoses = f32x8::from(origs).acos();
541 for i in 0..8 {
542 let orig = origs[i];
543 let check = |name: &str, vals: f32x8, expected: f32| {
544 let actual_arr: [f32; 8] = cast(vals);
545 let actual = actual_arr[i];
546 assert!(
547 (actual - expected).abs() < 0.0000006,
548 "Wanted {name}({orig}) to be {expected} but got {actual}",
549 name = name,
550 orig = orig,
551 expected = expected,
552 actual = actual
553 );
554 };
555 check("acos", actual_acoses, orig.acos());
556 }
557 }
558 }
559
560 // FIXME: remove cfg requirement once masks as their own types are implemented
561 #[cfg(target_feature = "avx")]
562 #[test]
563 fn impl_f32x8_atan() {
564 let inc = 1.0 / 2501.0 / 8.0;
565 for x in -2500..=2500 {
566 let base = (x * 8) as f32 * inc;
567 let origs = [
568 base,
569 base + inc,
570 base + 2.0 * inc,
571 base + 3.0 * inc,
572 base + 4.0 * inc,
573 base + 5.0 * inc,
574 base + 6.0 * inc,
575 base + 7.0 * inc,
576 ];
577 let actual_atans = f32x8::from(origs).atan();
578 for i in 0..8 {
579 let orig = origs[i];
580 let check = |name: &str, vals: f32x8, expected: f32| {
581 let actual_arr: [f32; 8] = cast(vals);
582 let actual = actual_arr[i];
583 assert!(
584 (actual - expected).abs() < 0.0000006,
585 "Wanted {name}({orig}) to be {expected} but got {actual}",
586 name = name,
587 orig = orig,
588 expected = expected,
589 actual = actual
590 );
591 };
592 check("atan", actual_atans, orig.atan());
593 }
594 }
595 }
596
597 // FIXME: remove cfg requirement once masks as their own types are implemented
598 #[cfg(target_feature = "avx")]
599 #[test]
600 fn impl_f32x8_atan2() {
601 let inc_y = 1.0 / 51.0 / 8.0;
602 let inc_x = 1.0 / 2501.0 / 8.0;
603 for y in -50..=50 {
604 let base_y = (y * 8) as f32 * inc_y;
605 let origs_y = [
606 base_y,
607 base_y + inc_y,
608 base_y + 2.0 * inc_y,
609 base_y + 3.0 * inc_y,
610 base_y + 4.0 * inc_y,
611 base_y + 5.0 * inc_y,
612 base_y + 6.0 * inc_y,
613 base_y + 7.0 * inc_y,
614 ];
615 let actual_y = f32x8::from(origs_y);
616 for x in -2500..=2500 {
617 let base_x = (x * 8) as f32 * inc_x;
618 let origs_x = [
619 base_x,
620 base_x + inc_x,
621 base_x + 2.0 * inc_x,
622 base_x + 3.0 * inc_x,
623 base_x + 4.0 * inc_x,
624 base_x + 5.0 * inc_x,
625 base_x + 6.0 * inc_x,
626 base_x + 7.0 * inc_x,
627 ];
628 let actual_x = f32x8::from(origs_x);
629 let actual_atan2s = actual_y.atan2(actual_x);
630 for i in 0..8 {
631 let orig_y = origs_y[i];
632 let orig_x = origs_x[i];
633 let check = |name: &str, vals: f32x8, expected: f32| {
634 let actual_arr: [f32; 8] = cast(vals);
635 let actual = actual_arr[i];
636 assert!(
637 (actual - expected).abs() < 0.0000006,
638 "Wanted {name}({orig_y}, {orig_x}) to be {expected} but got {actual}",
639 name = name,
640 orig_y = orig_y,
641 orig_x = orig_x,
642 expected = expected,
643 actual = actual
644 );
645 };
646 check("atan2", actual_atan2s, orig_y.atan2(orig_x));
647 }
648 }
649 }
650 }
651
652 #[test]
653 fn impl_f32x8_sin_cos() {
654 for x in -2500..=2500 {
655 let base = (x * 4) as f32;
656 let angles = [
657 base,
658 base + 1.0,
659 base + 2.0,
660 base + 3.0,
661 base + 4.0,
662 base + 5.0,
663 base + 6.0,
664 base + 7.0,
665 ];
666 let (actual_sins, actual_coses) = f32x8::from(angles).sin_cos();
667 for i in 0..4 {
668 let angle = angles[i];
669 let check = |name: &str, vals: f32x8, expected: f32| {
670 let actual_arr: [f32; 8] = cast(vals);
671 let actual = actual_arr[i];
672 assert!(
673 (actual - expected).abs() < 0.0000002,
674 "Wanted {name}({angle}) to be {expected} but got {actual}",
675 name = name,
676 angle = angle,
677 expected = expected,
678 actual = actual
679 );
680 };
681 check("sin", actual_sins, angle.sin());
682 check("cos", actual_coses, angle.cos());
683 }
684 }
685 }
686
687 #[test]
688 fn impl_f32x8_to_degrees() {
689 let pi = core::f32::consts::PI;
690 let a =
691 f32x8::from([0.0, pi / 2.0, pi, 2.0 * pi, 0.0, pi / 2.0, pi, 2.0 * pi]);
692 let expected =
693 f32x8::from([0.0, 90.0, 180.0, 360.0, 0.0, 90.0, 180.0, 360.0]);
694 let actual = a.to_degrees();
695 assert_eq!(expected, actual);
696 }
697
698 #[test]
699 fn impl_f32x8_to_radians() {
700 let pi = core::f32::consts::PI;
701 let a = f32x8::from([0.0, 90.0, 180.0, 360.0, 0.0, 90.0, 180.0, 360.0]);
702 let expected =
703 f32x8::from([0.0, pi / 2.0, pi, 2.0 * pi, 0.0, pi / 2.0, pi, 2.0 * pi]);
704 let actual = a.to_radians();
705 assert_eq!(expected, actual);
706 }
707
708 #[test]
709 fn impl_f32x8_recip() {
710 {
711 let expected = f32x8::from(0.0);
712 let actual = f32x8::from(f32::INFINITY).recip();
713 assert_eq!(expected, actual);
714 }
715 {
716 let expected = f32x8::from(0.0);
717 let actual = f32x8::from(-f32::INFINITY).recip();
718 assert_eq!(expected, actual);
719 }
720 {
721 let actual = f32x8::from(f32::NAN).recip();
722 assert!(actual.is_nan().any());
723 }
724 {
725 let expected = f32x8::from(f32::INFINITY);
726 let actual = f32x8::from(0.0).recip();
727 assert_eq!(expected, actual);
728 }
729 {
730 let expected = f32x8::from(0.49987793);
731 let actual = f32x8::from(2.0).recip();
732 let diff: [f32; 8] = cast((actual - expected).abs());
733 assert!(diff[0] < 0.001);
734 }
735 {
736 let expected = f32x8::from(-0.08102417);
737 let actual = f32x8::from(-12.34).recip();
738 let diff: [f32; 8] = cast((actual - expected).abs());
739 assert!(diff[0] < 0.001);
740 }
741 }
742
743 #[test]
744 fn impl_f32x8_recip_sqrt() {
745 {
746 let expected = f32x8::from(0.0);
747 let actual = f32x8::from(f32::INFINITY).recip_sqrt();
748 assert_eq!(expected, actual);
749 }
750 {
751 let actual = f32x8::from(-f32::INFINITY).recip_sqrt();
752 assert!(actual.is_nan().any());
753 }
754 {
755 let actual = f32x8::from(f32::NAN).recip_sqrt();
756 assert!(actual.is_nan().any());
757 }
758 {
759 let expected = f32x8::from(f32::INFINITY);
760 let actual = f32x8::from(0.0).recip_sqrt();
761 assert_eq!(expected, actual);
762 }
763 {
764 let expected = f32x8::from(0.70703125);
765 let actual = f32x8::from(2.0).recip_sqrt();
766 let diff: [f32; 8] = cast((actual - expected).abs());
767 assert!(diff[0] < 0.001);
768 }
769 {
770 let actual = f32x8::from(-12.34).recip_sqrt();
771 assert!(actual.is_nan().any());
772 }
773 }
774
775 #[test]
776 fn impl_f32x8_sqrt() {
777 for (f, e) in [
778 (f32::INFINITY, f32::INFINITY),
779 (0.0, 0.0),
780 (-0.0, -0.0),
781 (4.0, 2.0),
782 (9.0, 3.0),
783 (16.0, 4.0),
784 (25.0, 5.0),
785 (5000.0 * 5000.0, 5000.0),
786 ]
787 .iter()
788 .copied()
789 {
790 let expected = f32x8::from(e);
791 let actual = f32x8::from(f).sqrt();
792 assert_eq!(expected, actual);
793 }
794 assert_eq!(
795 cast::<_, i32x8>(f32x8::from(f32::NAN).sqrt().is_nan()),
796 i32x8::from(-1)
797 );
798 assert_eq!(
799 cast::<_, i32x8>(f32x8::from(f32::NEG_INFINITY).sqrt().is_nan()),
800 i32x8::from(-1)
801 );
802 assert_eq!(
803 cast::<_, i32x8>(f32x8::from(-1.0).sqrt().is_nan()),
804 i32x8::from(-1)
805 );
806 }
807
808 #[test]
809 fn impl_f32x8_exp() {
810 for f in [(-2.0), (-1.0), (0.0), (1.0), (1.5), (2.0), (10.0)].iter().copied()
811 {
812 let expected = f32x8::from((f as f32).exp());
813 let actual = f32x8::from(f).exp();
814 let diff_from_std: [f32; 8] = cast((actual - expected).abs());
815 assert!(diff_from_std[0] < 0.000000000000001);
816 }
817 }
818
819 #[test]
820 fn test_f32x8_move_mask() {
821 let a = f32x8::from([-1.0, 0.0, -2.0, -3.0, -1.0, 0.0, -2.0, -3.0]);
822 let expected = 0b11011101;
823 let actual = a.move_mask();
824 assert_eq!(expected, actual);
825 //
826 let a = f32x8::from([1.0, 0.0, 2.0, -3.0, 1.0, 0.0, 2.0, -3.0]);
827 let expected = 0b10001000;
828 let actual = a.move_mask();
829 assert_eq!(expected, actual);
830 }
831
832 #[test]
833 fn test_f32x8_any() {
834 let a =
835 f32x8::from([-1.0, 0.0, -2.0, -3.0, 2.0, -1.0, -2.0, f32::NAN]).is_nan();
836 assert!(a.any());
837 //
838 let a = f32x8::from([1.0, 0.0, 2.0, 3.0, 2.0, 5.0, 6.7, 7.1]).is_nan();
839 assert!(!a.any());
840 }
841
842 #[test]
843 fn test_f32x8_all() {
844 let a = f32x8::from([f32::NAN; 8]).is_nan();
845 assert!(a.all());
846 //
847 let a = f32x8::from([1.0, -0.0, 2.0, 3.0, 4.0, 9.0, 7.2, f32::NAN]).is_nan();
848 assert!(!a.all());
849 }
850
851 #[test]
852 fn test_f32x8_none() {
853 let a = f32x8::from([1.0, 0.0, 2.0, 3.0, 1.0, 0.0, 2.0, 3.0]).is_nan();
854 assert!(a.none());
855 //
856 let a = f32x8::from([1.0, -0.0, 2.0, 3.0, 1.0, -0.0, 2.0, f32::NAN]).is_nan();
857 assert!(!a.none());
858 }
859
860 #[test]
861 fn impl_f32x8_ln() {
862 for f in [0.1, 0.5, 1.0, 2.718282, 10.0, 35.0, 1250.0].iter().copied() {
863 let expected = f32x8::from((f as f32).ln());
864 let actual = f32x8::from(f).ln();
865 let diff_from_std: [f32; 8] = cast((actual - expected).abs());
866 assert!(diff_from_std[0] < 0.0000001);
867 }
868 }
869
870 #[test]
871 fn impl_f32x8_pow() {
872 for f in [0.1, 0.5, 1.0, 2.718282, 3.0, 4.0, 2.5, -1.0].iter().copied() {
873 let expected = f32x8::splat(2.0 as f32).powf(f);
874 let actual = f32x8::from(2.0_f32.powf(f));
875 let diff_from_std: [f32; 8] = cast((actual - expected).abs());
876 assert!(diff_from_std[0] < 0.000001);
877 }
878 }
879
880 #[test]
881 fn impl_f32x8_pow_n() {
882 let p = f32x8::from([29.0, 0.1, 0.5, 1.0, 2.718282, -0.2, -1.5, 3.4]);
883 let f = f32x8::from([1.2, 2.0, 3.0, 1.5, 9.2, 6.1, 2.5, -4.5]);
884 let res = f.pow_f32x8(p);
885
886 let p: [f32; 8] = cast(p);
887 let f: [f32; 8] = cast(f);
888 let res: [f32; 8] = cast(res);
889 for i in 0..p.len() {
890 let expected = f[i].powf(p[i]);
891 if !(expected.is_nan() && res[i].is_nan()) {
892 assert!((expected - res[i]).abs() < 0.0001);
893 }
894 }
895 }
896
897 #[test]
898 fn impl_f32x8_reduce_add() {
899 let p = f32x8::from([0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.009]);
900 assert!((p.reduce_add() - 0.037) < 0.000000001);
901 }
902
903 #[test]
904 fn impl_f32x8_sum() {
905 let mut p = Vec::with_capacity(250_000);
906 for _ in 0..125_000 {
907 p.push(f32x8::splat(0.001));
908 }
909 let now = std::time::Instant::now();
910 let sum: f32 = p.iter().map(|x| x.reduce_add()).sum();
911 let duration = now.elapsed().as_micros();
912 println!("Time take {} {}us", sum, duration);
913
914 let p = vec![0.001; 1_000_000];
915 let now = std::time::Instant::now();
916 let sum2: f32 = p.iter().sum();
917 let duration = now.elapsed().as_micros();
918 println!("Time take {} {}us", sum2, duration);
919 }
920
921 #[test]
922 fn impl_transpose_for_f32x8() {
923 let a = [
924 f32x8::new([0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1]),
925 f32x8::new([8.1, 9.1, 10.1, 11.1, 12.1, 13.1, 14.1, 15.1]),
926 f32x8::new([16.1, 17.1, 18.1, 19.1, 20.1, 21.1, 22.1, 23.1]),
927 f32x8::new([24.1, 25.1, 26.1, 27.1, 28.1, 29.1, 30.1, 31.1]),
928 f32x8::new([32.1, 33.1, 34.1, 35.1, 36.1, 37.1, 38.1, 39.1]),
929 f32x8::new([40.1, 41.1, 42.1, 43.1, 44.1, 45.1, 46.1, 47.1]),
930 f32x8::new([48.1, 49.1, 50.1, 51.1, 52.1, 53.1, 54.1, 55.1]),
931 f32x8::new([
932 5600000.1, 5700000.1, 5800000.1, 5900000.1, 6000000.1, 6100000.1,
933 6200000.1, 6300000.1,
934 ]),
935 ];
936
937 let result = f32x8::transpose(a);
938
939 let expected = [
940 f32x8::new([0.1, 8.1, 16.1, 24.1, 32.1, 40.1, 48.1, 5600000.1]),
941 f32x8::new([1.1, 9.1, 17.1, 25.1, 33.1, 41.1, 49.1, 5700000.1]),
942 f32x8::new([2.1, 10.1, 18.1, 26.1, 34.1, 42.1, 50.1, 5800000.1]),
943 f32x8::new([3.1, 11.1, 19.1, 27.1, 35.1, 43.1, 51.1, 5900000.1]),
944 f32x8::new([4.1, 12.1, 20.1, 28.1, 36.1, 44.1, 52.1, 6000000.1]),
945 f32x8::new([5.1, 13.1, 21.1, 29.1, 37.1, 45.1, 53.1, 6100000.1]),
946 f32x8::new([6.1, 14.1, 22.1, 30.1, 38.1, 46.1, 54.1, 6200000.1]),
947 f32x8::new([7.1, 15.1, 23.1, 31.1, 39.1, 47.1, 55.1, 6300000.1]),
948 ];
949
950 assert_eq!(result, expected);
951 }
952
953 #[test]
954 fn impl_f32x8_from_i32x8() {
955 let i = i32x8::from([1, 2, 3, 4, 5, 6, 7, 8]);
956 let f = f32x8::from([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
957 assert_eq!(f32x8::from_i32x8(i), f)
958 }
959
960 #[cfg(feature = "serde")]
961 #[test]
962 fn impl_f32x8_ser_de_roundtrip() {
963 let serialized =
964 bincode::serialize(&f32x8::ZERO).expect("serialization failed");
965 let deserialized =
966 bincode::deserialize(&serialized).expect("deserializaion failed");
967 assert_eq!(f32x8::ZERO, deserialized);
968 }