Debian Codegraph @

src/arm/mod.rs

raw symbols SCIP
1 // pathfinder/simd/src/arm.rs
2 //
3 // Copyright © 2019 The Pathfinder Project Developers.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::arch::aarch64::{self, float32x2_t, float32x4_t, int32x2_t, int32x4_t};
12 use std::arch::aarch64::{uint32x2_t, uint32x4_t};
13 use std::f32;
14 use std::fmt::{self, Debug, Formatter};
15 use std::intrinsics::simd::*;
16 use std::mem;
17 use std::ops::{Add, BitAnd, BitOr, Div, Index, IndexMut, Mul, Not, Shr, Sub};
18
19 mod swizzle_f32x4;
20 mod swizzle_i32x4;
21
22 #[repr(simd)]
23 pub(crate) struct Simd<T, const N: usize>([T; N]);
24
25 macro_rules! simd_shuffle2 {
26 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+> $idx:expr $(,)?) => {{
27 struct ConstParam<$(const $imm: $ty),+>;
28 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> {
29 const IDX: Simd<u32, 2> = Simd($idx);
30 }
31
32 simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX)
33 }};
34 ($x:expr, $y:expr, $idx:expr $(,)?) => {{
35 const IDX: Simd<u32, 2> = Simd($idx);
36 simd_shuffle($x, $y, IDX)
37 }};
38 }
39
40 macro_rules! simd_shuffle4 {
41 ($x:expr, $y:expr, <$(const $imm:ident : $ty:ty),+> $idx:expr $(,)?) => {{
42 struct ConstParam<$(const $imm: $ty),+>;
43 impl<$(const $imm: $ty),+> ConstParam<$($imm),+> {
44 const IDX: Simd<u32; 4> = Simd($idx);
45 }
46
47 simd_shuffle($x, $y, ConstParam::<$($imm),+>::IDX)
48 }};
49 ($x:expr, $y:expr, $idx:expr $(,)?) => {{
50 const IDX: Simd<u32, 4> = Simd($idx);
51 simd_shuffle($x, $y, IDX)
52 }};
53 }
54
55 // Two 32-bit floats
56
57 #[derive(Clone, Copy)]
58 pub struct F32x2(pub float32x2_t);
59
60 impl F32x2 {
61 // Constructors
62
63 #[inline]
64 pub fn new(a: f32, b: f32) -> F32x2 {
65 unsafe { F32x2(mem::transmute([a, b])) }
66 }
67
68 #[inline]
69 pub fn splat(x: f32) -> F32x2 {
70 F32x2::new(x, x)
71 }
72
73 // Basic operations
74
75 #[inline]
76 pub fn approx_recip(self) -> F32x2 {
77 unsafe { F32x2(vrecpe_v2f32(self.0)) }
78 }
79
80 #[inline]
81 pub fn min(self, other: F32x2) -> F32x2 {
82 unsafe { F32x2(simd_fmin(self.0, other.0)) }
83 }
84
85 #[inline]
86 pub fn max(self, other: F32x2) -> F32x2 {
87 unsafe { F32x2(simd_fmax(self.0, other.0)) }
88 }
89
90 #[inline]
91 pub fn clamp(self, min: F32x2, max: F32x2) -> F32x2 {
92 self.max(min).min(max)
93 }
94
95 #[inline]
96 pub fn abs(self) -> F32x2 {
97 unsafe { F32x2(fabs_v2f32(self.0)) }
98 }
99
100 #[inline]
101 pub fn floor(self) -> F32x2 {
102 unsafe { F32x2(floor_v2f32(self.0)) }
103 }
104
105 #[inline]
106 pub fn ceil(self) -> F32x2 {
107 unsafe { F32x2(ceil_v2f32(self.0)) }
108 }
109
110 #[inline]
111 pub fn sqrt(self) -> F32x2 {
112 unsafe { F32x2(sqrt_v2f32(self.0)) }
113 }
114
115 // Packed comparisons
116
117 #[inline]
118 pub fn packed_eq(self, other: F32x2) -> U32x2 {
119 unsafe { U32x2(simd_eq(self.0, other.0)) }
120 }
121
122 #[inline]
123 pub fn packed_gt(self, other: F32x2) -> U32x2 {
124 unsafe { U32x2(simd_gt(self.0, other.0)) }
125 }
126
127 #[inline]
128 pub fn packed_lt(self, other: F32x2) -> U32x2 {
129 unsafe { U32x2(simd_lt(self.0, other.0)) }
130 }
131
132 #[inline]
133 pub fn packed_le(self, other: F32x2) -> U32x2 {
134 unsafe { U32x2(simd_le(self.0, other.0)) }
135 }
136
137 // Conversions
138
139 #[inline]
140 pub fn to_f32x4(self) -> F32x4 {
141 self.concat_xy_xy(F32x2::default())
142 }
143
144 /// Converts these packed floats to integers via rounding.
145 #[inline]
146 pub fn to_i32x2(self) -> I32x2 {
147 unsafe { I32x2(simd_cast(round_v2f32(self.0))) }
148 }
149
150 #[inline]
151 pub fn to_i32x4(self) -> I32x4 {
152 self.to_i32x2().concat_xy_xy(I32x2::default())
153 }
154
155 // Swizzle
156
157 #[inline]
158 pub fn yx(self) -> F32x2 {
159 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [1, 0])) }
160 }
161
162 // Concatenations
163
164 #[inline]
165 pub fn concat_xy_xy(self, other: F32x2) -> F32x4 {
166 unsafe { F32x4(simd_shuffle4!(self.0, other.0, [0, 1, 2, 3])) }
167 }
168 }
169
170 impl Default for F32x2 {
171 #[inline]
172 fn default() -> F32x2 {
173 F32x2::new(0.0, 0.0)
174 }
175 }
176
177 impl Debug for F32x2 {
178 #[inline]
179 fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
180 write!(f, "<{}, {}>", self[0], self[1])
181 }
182 }
183
184 impl Index<usize> for F32x2 {
185 type Output = f32;
186 #[inline]
187 fn index(&self, index: usize) -> &f32 {
188 unsafe {
189 assert!(index < 2);
190 let ptr = &self.0 as *const float32x2_t as *const f32;
191 mem::transmute::<*const f32, &f32>(ptr.offset(index as isize))
192 }
193 }
194 }
195
196 impl IndexMut<usize> for F32x2 {
197 #[inline]
198 fn index_mut(&mut self, index: usize) -> &mut f32 {
199 unsafe {
200 assert!(index < 2);
201 let ptr = &mut self.0 as *mut float32x2_t as *mut f32;
202 mem::transmute::<*mut f32, &mut f32>(ptr.offset(index as isize))
203 }
204 }
205 }
206
207 impl Add<F32x2> for F32x2 {
208 type Output = F32x2;
209 #[inline]
210 fn add(self, other: F32x2) -> F32x2 {
211 unsafe { F32x2(simd_add(self.0, other.0)) }
212 }
213 }
214
215 impl Div<F32x2> for F32x2 {
216 type Output = F32x2;
217 #[inline]
218 fn div(self, other: F32x2) -> F32x2 {
219 unsafe { F32x2(simd_div(self.0, other.0)) }
220 }
221 }
222
223 impl Mul<F32x2> for F32x2 {
224 type Output = F32x2;
225 #[inline]
226 fn mul(self, other: F32x2) -> F32x2 {
227 unsafe { F32x2(simd_mul(self.0, other.0)) }
228 }
229 }
230
231 impl Sub<F32x2> for F32x2 {
232 type Output = F32x2;
233 #[inline]
234 fn sub(self, other: F32x2) -> F32x2 {
235 unsafe { F32x2(simd_sub(self.0, other.0)) }
236 }
237 }
238
239 impl PartialEq for F32x2 {
240 #[inline]
241 fn eq(&self, other: &F32x2) -> bool {
242 self.packed_eq(*other).all_true()
243 }
244 }
245
246 // Four 32-bit floats
247
248 #[derive(Clone, Copy)]
249 pub struct F32x4(pub float32x4_t);
250
251 impl F32x4 {
252 #[inline]
253 pub fn new(a: f32, b: f32, c: f32, d: f32) -> F32x4 {
254 unsafe { F32x4(mem::transmute([a, b, c, d])) }
255 }
256
257 #[inline]
258 pub fn splat(x: f32) -> F32x4 {
259 F32x4::new(x, x, x, x)
260 }
261
262 // Basic operations
263
264 #[inline]
265 pub fn approx_recip(self) -> F32x4 {
266 unsafe { F32x4(vrecpe_v4f32(self.0)) }
267 }
268
269 #[inline]
270 pub fn min(self, other: F32x4) -> F32x4 {
271 unsafe { F32x4(simd_fmin(self.0, other.0)) }
272 }
273
274 #[inline]
275 pub fn max(self, other: F32x4) -> F32x4 {
276 unsafe { F32x4(simd_fmax(self.0, other.0)) }
277 }
278
279 #[inline]
280 pub fn clamp(self, min: F32x4, max: F32x4) -> F32x4 {
281 self.max(min).min(max)
282 }
283
284 #[inline]
285 pub fn abs(self) -> F32x4 {
286 unsafe { F32x4(fabs_v4f32(self.0)) }
287 }
288
289 #[inline]
290 pub fn floor(self) -> F32x4 {
291 unsafe { F32x4(floor_v4f32(self.0)) }
292 }
293
294 #[inline]
295 pub fn ceil(self) -> F32x4 {
296 unsafe { F32x4(ceil_v4f32(self.0)) }
297 }
298
299 #[inline]
300 pub fn sqrt(self) -> F32x4 {
301 unsafe { F32x4(sqrt_v4f32(self.0)) }
302 }
303
304 // Packed comparisons
305
306 #[inline]
307 pub fn packed_eq(self, other: F32x4) -> U32x4 {
308 unsafe { U32x4(simd_eq(self.0, other.0)) }
309 }
310
311 #[inline]
312 pub fn packed_gt(self, other: F32x4) -> U32x4 {
313 unsafe { U32x4(simd_gt(self.0, other.0)) }
314 }
315
316 #[inline]
317 pub fn packed_le(self, other: F32x4) -> U32x4 {
318 unsafe { U32x4(simd_le(self.0, other.0)) }
319 }
320
321 #[inline]
322 pub fn packed_lt(self, other: F32x4) -> U32x4 {
323 unsafe { U32x4(simd_lt(self.0, other.0)) }
324 }
325
326 // Swizzle conversions
327
328 #[inline]
329 pub fn xy(self) -> F32x2 {
330 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [0, 1])) }
331 }
332
333 #[inline]
334 pub fn yx(self) -> F32x2 {
335 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [1, 0])) }
336 }
337
338 #[inline]
339 pub fn xw(self) -> F32x2 {
340 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [0, 3])) }
341 }
342
343 #[inline]
344 pub fn zy(self) -> F32x2 {
345 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [2, 1])) }
346 }
347
348 #[inline]
349 pub fn zw(self) -> F32x2 {
350 unsafe { F32x2(simd_shuffle2!(self.0, self.0, [2, 3])) }
351 }
352
353 // Concatenations
354
355 #[inline]
356 pub fn concat_xy_xy(self, other: F32x4) -> F32x4 {
357 unsafe { F32x4(simd_shuffle4!(self.0, other.0, [0, 1, 4, 5])) }
358 }
359
360 #[inline]
361 pub fn concat_xy_zw(self, other: F32x4) -> F32x4 {
362 unsafe { F32x4(simd_shuffle4!(self.0, other.0, [0, 1, 6, 7])) }
363 }
364
365 #[inline]
366 pub fn concat_zw_zw(self, other: F32x4) -> F32x4 {
367 unsafe { F32x4(simd_shuffle4!(self.0, other.0, [2, 3, 6, 7])) }
368 }
369
370 #[inline]
371 pub fn concat_wz_yx(self, other: F32x4) -> F32x4 {
372 unsafe { F32x4(simd_shuffle4!(self.0, other.0, [3, 2, 5, 4])) }
373 }
374
375 // Conversions
376
377 /// Converts these packed floats to integers via rounding.
378 #[inline]
379 pub fn to_i32x4(self) -> I32x4 {
380 unsafe { I32x4(simd_cast(round_v4f32(self.0))) }
381 }
382 }
383
384 impl Default for F32x4 {
385 #[inline]
386 fn default() -> F32x4 {
387 F32x4::new(0.0, 0.0, 0.0, 0.0)
388 }
389 }
390
391 impl Index<usize> for F32x4 {
392 type Output = f32;
393 #[inline]
394 fn index(&self, index: usize) -> &f32 {
395 unsafe {
396 assert!(index < 4);
397 let ptr = &self.0 as *const float32x4_t as *const f32;
398 mem::transmute::<*const f32, &f32>(ptr.offset(index as isize))
399 }
400 }
401 }
402
403 impl IndexMut<usize> for F32x4 {
404 #[inline]
405 fn index_mut(&mut self, index: usize) -> &mut f32 {
406 unsafe {
407 assert!(index < 4);
408 let ptr = &mut self.0 as *mut float32x4_t as *mut f32;
409 mem::transmute::<*mut f32, &mut f32>(ptr.offset(index as isize))
410 }
411 }
412 }
413
414 impl Debug for F32x4 {
415 #[inline]
416 fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
417 write!(f, "<{}, {}, {}, {}>", self[0], self[1], self[2], self[3])
418 }
419 }
420
421 impl PartialEq for F32x4 {
422 #[inline]
423 fn eq(&self, other: &F32x4) -> bool {
424 self.packed_eq(*other).all_true()
425 }
426 }
427
428 impl Add<F32x4> for F32x4 {
429 type Output = F32x4;
430 #[inline]
431 fn add(self, other: F32x4) -> F32x4 {
432 unsafe { F32x4(simd_add(self.0, other.0)) }
433 }
434 }
435
436 impl Div<F32x4> for F32x4 {
437 type Output = F32x4;
438 #[inline]
439 fn div(self, other: F32x4) -> F32x4 {
440 unsafe { F32x4(simd_div(self.0, other.0)) }
441 }
442 }
443
444 impl Mul<F32x4> for F32x4 {
445 type Output = F32x4;
446 #[inline]
447 fn mul(self, other: F32x4) -> F32x4 {
448 unsafe { F32x4(simd_mul(self.0, other.0)) }
449 }
450 }
451
452 impl Sub<F32x4> for F32x4 {
453 type Output = F32x4;
454 #[inline]
455 fn sub(self, other: F32x4) -> F32x4 {
456 unsafe { F32x4(simd_sub(self.0, other.0)) }
457 }
458 }
459
460 // Two 32-bit signed integers
461
462 #[derive(Clone, Copy, Debug)]
463 pub struct I32x2(pub int32x2_t);
464
465 impl I32x2 {
466 #[inline]
467 pub fn new(x: i32, y: i32) -> I32x2 {
468 unsafe { I32x2(mem::transmute([x, y])) }
469 }
470
471 #[inline]
472 pub fn splat(x: i32) -> I32x2 {
473 I32x2::new(x, x)
474 }
475
476 // Accessors
477
478 #[inline]
479 pub fn x(self) -> i32 {
480 self[0]
481 }
482
483 #[inline]
484 pub fn y(self) -> i32 {
485 self[1]
486 }
487
488 #[inline]
489 pub fn packed_eq(self, other: I32x2) -> U32x2 {
490 unsafe { U32x2(simd_eq(self.0, other.0)) }
491 }
492
493 // Basic operations
494
495 #[inline]
496 pub fn max(self, other: I32x2) -> I32x2 {
497 self.to_i32x4().max(other.to_i32x4()).xy()
498 }
499
500 #[inline]
501 pub fn min(self, other: I32x2) -> I32x2 {
502 self.to_i32x4().min(other.to_i32x4()).xy()
503 }
504
505 // Concatenations
506
507 #[inline]
508 pub fn concat_xy_xy(self, other: I32x2) -> I32x4 {
509 unsafe { I32x4(simd_shuffle4!(self.0, other.0, [0, 1, 2, 3])) }
510 }
511
512 // Conversions
513
514 /// Converts these packed integers to floats.
515 #[inline]
516 pub fn to_f32x2(self) -> F32x2 {
517 unsafe { F32x2(simd_cast(self.0)) }
518 }
519
520 #[inline]
521 pub fn to_i32x4(self) -> I32x4 {
522 self.concat_xy_xy(I32x2::default())
523 }
524 }
525
526 impl Default for I32x2 {
527 #[inline]
528 fn default() -> I32x2 {
529 I32x2::splat(0)
530 }
531 }
532
533 impl PartialEq for I32x2 {
534 #[inline]
535 fn eq(&self, other: &I32x2) -> bool {
536 self.packed_eq(*other).all_true()
537 }
538 }
539
540 impl Index<usize> for I32x2 {
541 type Output = i32;
542 #[inline]
543 fn index(&self, index: usize) -> &i32 {
544 unsafe {
545 assert!(index < 2);
546 let ptr = &self.0 as *const int32x2_t as *const i32;
547 mem::transmute::<*const i32, &i32>(ptr.offset(index as isize))
548 }
549 }
550 }
551
552 impl IndexMut<usize> for I32x2 {
553 #[inline]
554 fn index_mut(&mut self, index: usize) -> &mut i32 {
555 unsafe {
556 assert!(index < 2);
557 let ptr = &mut self.0 as *mut int32x2_t as *mut i32;
558 mem::transmute::<*mut i32, &mut i32>(ptr.offset(index as isize))
559 }
560 }
561 }
562
563 impl Add<I32x2> for I32x2 {
564 type Output = I32x2;
565 #[inline]
566 fn add(self, other: I32x2) -> I32x2 {
567 unsafe { I32x2(simd_add(self.0, other.0)) }
568 }
569 }
570
571 impl Sub<I32x2> for I32x2 {
572 type Output = I32x2;
573 #[inline]
574 fn sub(self, other: I32x2) -> I32x2 {
575 unsafe { I32x2(simd_sub(self.0, other.0)) }
576 }
577 }
578
579 impl Mul<I32x2> for I32x2 {
580 type Output = I32x2;
581 #[inline]
582 fn mul(self, other: I32x2) -> I32x2 {
583 unsafe { I32x2(simd_mul(self.0, other.0)) }
584 }
585 }
586
587 // Four 32-bit signed integers
588
589 #[derive(Clone, Copy, Debug)]
590 pub struct I32x4(pub int32x4_t);
591
592 impl I32x4 {
593 #[inline]
594 pub fn new(a: i32, b: i32, c: i32, d: i32) -> I32x4 {
595 unsafe { I32x4(mem::transmute([a, b, c, d])) }
596 }
597
598 #[inline]
599 pub fn splat(x: i32) -> I32x4 {
600 I32x4::new(x, x, x, x)
601 }
602
603 // Basic operations
604
605 #[inline]
606 pub fn max(self, other: I32x4) -> I32x4 {
607 unsafe { I32x4(simd_cast(simd_fmax(self.to_f32x4().0, other.to_f32x4().0))) }
608 }
609
610 #[inline]
611 pub fn min(self, other: I32x4) -> I32x4 {
612 unsafe { I32x4(simd_cast(simd_fmin(self.to_f32x4().0, other.to_f32x4().0))) }
613 }
614
615 // Packed comparisons
616
617 #[inline]
618 pub fn packed_eq(self, other: I32x4) -> U32x4 {
619 unsafe { U32x4(simd_eq(self.0, other.0)) }
620 }
621
622 #[inline]
623 pub fn packed_le(self, other: I32x4) -> U32x4 {
624 unsafe { U32x4(simd_le(self.0, other.0)) }
625 }
626
627 #[inline]
628 pub fn packed_lt(self, other: I32x4) -> U32x4 {
629 unsafe { U32x4(simd_lt(self.0, other.0)) }
630 }
631
632 // Concatenations
633
634 #[inline]
635 pub fn concat_xy_xy(self, other: I32x4) -> I32x4 {
636 unsafe { I32x4(simd_shuffle4!(self.0, other.0, [0, 1, 4, 5])) }
637 }
638
639 #[inline]
640 pub fn concat_zw_zw(self, other: I32x4) -> I32x4 {
641 unsafe { I32x4(simd_shuffle4!(self.0, other.0, [2, 3, 6, 7])) }
642 }
643
644 // Swizzle conversions
645
646 #[inline]
647 pub fn xy(self) -> I32x2 {
648 unsafe { I32x2(simd_shuffle2!(self.0, self.0, [0, 1])) }
649 }
650
651 #[inline]
652 pub fn yx(self) -> I32x2 {
653 unsafe { I32x2(simd_shuffle2!(self.0, self.0, [1, 0])) }
654 }
655
656 #[inline]
657 pub fn xw(self) -> I32x2 {
658 unsafe { I32x2(simd_shuffle2!(self.0, self.0, [0, 3])) }
659 }
660
661 #[inline]
662 pub fn zy(self) -> I32x2 {
663 unsafe { I32x2(simd_shuffle2!(self.0, self.0, [2, 1])) }
664 }
665
666 #[inline]
667 pub fn zw(self) -> I32x2 {
668 unsafe { I32x2(simd_shuffle2!(self.0, self.0, [2, 3])) }
669 }
670
671 // Conversions
672
673 /// Converts these packed integers to floats.
674 #[inline]
675 pub fn to_f32x4(self) -> F32x4 {
676 unsafe { F32x4(simd_cast(self.0)) }
677 }
678 }
679
680 impl Default for I32x4 {
681 #[inline]
682 fn default() -> I32x4 {
683 I32x4::new(0, 0, 0, 0)
684 }
685 }
686
687 impl Index<usize> for I32x4 {
688 type Output = i32;
689 #[inline]
690 fn index(&self, index: usize) -> &i32 {
691 unsafe {
692 assert!(index < 4);
693 let ptr = &self.0 as *const int32x4_t as *const i32;
694 mem::transmute::<*const i32, &i32>(ptr.offset(index as isize))
695 }
696 }
697 }
698
699 impl IndexMut<usize> for I32x4 {
700 #[inline]
701 fn index_mut(&mut self, index: usize) -> &mut i32 {
702 unsafe {
703 assert!(index < 4);
704 let ptr = &mut self.0 as *mut int32x4_t as *mut i32;
705 mem::transmute::<*mut i32, &mut i32>(ptr.offset(index as isize))
706 }
707 }
708 }
709
710 impl Add<I32x4> for I32x4 {
711 type Output = I32x4;
712 #[inline]
713 fn add(self, other: I32x4) -> I32x4 {
714 unsafe { I32x4(simd_add(self.0, other.0)) }
715 }
716 }
717
718 impl Sub<I32x4> for I32x4 {
719 type Output = I32x4;
720 #[inline]
721 fn sub(self, other: I32x4) -> I32x4 {
722 unsafe { I32x4(simd_sub(self.0, other.0)) }
723 }
724 }
725
726 impl Mul<I32x4> for I32x4 {
727 type Output = I32x4;
728 #[inline]
729 fn mul(self, other: I32x4) -> I32x4 {
730 unsafe { I32x4(simd_mul(self.0, other.0)) }
731 }
732 }
733
734 impl PartialEq for I32x4 {
735 #[inline]
736 fn eq(&self, other: &I32x4) -> bool {
737 self.packed_eq(*other).all_true()
738 }
739 }
740
741 impl BitAnd<I32x4> for I32x4 {
742 type Output = I32x4;
743 #[inline]
744 fn bitand(self, other: I32x4) -> I32x4 {
745 unsafe { I32x4(simd_and(self.0, other.0)) }
746 }
747 }
748
749 impl BitOr<I32x4> for I32x4 {
750 type Output = I32x4;
751 #[inline]
752 fn bitor(self, other: I32x4) -> I32x4 {
753 unsafe { I32x4(simd_or(self.0, other.0)) }
754 }
755 }
756
757 impl Shr<I32x4> for I32x4 {
758 type Output = I32x4;
759 #[inline]
760 fn shr(self, other: I32x4) -> I32x4 {
761 unsafe { I32x4(simd_shr(self.0, other.0)) }
762 }
763 }
764
765 // Two 32-bit unsigned integers
766
767 #[derive(Clone, Copy)]
768 pub struct U32x2(pub uint32x2_t);
769
770 impl U32x2 {
771 #[inline]
772 pub fn new(x: u32, y: u32) -> U32x2 {
773 unsafe { U32x2(mem::transmute([x, y])) }
774 }
775
776 #[inline]
777 pub fn splat(x: u32) -> U32x2 {
778 U32x2::new(x, x)
779 }
780
781 /// Returns true if both booleans in this vector are true.
782 ///
783 /// The result is *undefined* if both values in this vector are not booleans. A boolean is a
784 /// value with all bits set or all bits clear (i.e. !0 or 0).
785 #[inline]
786 pub fn all_true(&self) -> bool {
787 unsafe { aarch64::vminv_u32(self.0) == !0 }
788 }
789
790 /// Returns true if both booleans in this vector are false.
791 ///
792 /// The result is *undefined* if both values in this vector are not booleans. A boolean is a
793 /// value with all bits set or all bits clear (i.e. !0 or 0).
794 #[inline]
795 pub fn all_false(&self) -> bool {
796 unsafe { aarch64::vmaxv_u32(self.0) == 0 }
797 }
798
799 #[inline]
800 pub fn to_i32x2(self) -> I32x2 {
801 unsafe { I32x2(simd_cast(self.0)) }
802 }
803 }
804
805 impl Index<usize> for U32x2 {
806 type Output = u32;
807 #[inline]
808 fn index(&self, index: usize) -> &u32 {
809 unsafe {
810 assert!(index < 2);
811 let ptr = &self.0 as *const uint32x2_t as *const u32;
812 mem::transmute::<*const u32, &u32>(ptr.offset(index as isize))
813 }
814 }
815 }
816
817 impl Not for U32x2 {
818 type Output = U32x2;
819 #[inline]
820 fn not(self) -> U32x2 {
821 // FIXME(pcwalton): Is there a better way to do this?
822 unsafe { U32x2(simd_xor(self.0, U32x2::splat(!0).0)) }
823 }
824 }
825
826 impl BitAnd<U32x2> for U32x2 {
827 type Output = U32x2;
828 #[inline]
829 fn bitand(self, other: U32x2) -> U32x2 {
830 unsafe { U32x2(simd_and(self.0, other.0)) }
831 }
832 }
833
834 impl BitOr<U32x2> for U32x2 {
835 type Output = U32x2;
836 #[inline]
837 fn bitor(self, other: U32x2) -> U32x2 {
838 unsafe { U32x2(simd_or(self.0, other.0)) }
839 }
840 }
841
842 // Four 32-bit unsigned integers
843
844 #[derive(Clone, Copy)]
845 pub struct U32x4(pub uint32x4_t);
846
847 impl U32x4 {
848 #[inline]
849 pub fn new(a: u32, b: u32, c: u32, d: u32) -> U32x4 {
850 unsafe { U32x4(mem::transmute([a, b, c, d])) }
851 }
852
853 #[inline]
854 pub fn splat(x: u32) -> U32x4 {
855 U32x4::new(x, x, x, x)
856 }
857
858 /// Returns true if all four booleans in this vector are true.
859 ///
860 /// The result is *undefined* if all four values in this vector are not booleans. A boolean is
861 /// a value with all bits set or all bits clear (i.e. !0 or 0).
862 #[inline]
863 pub fn all_true(&self) -> bool {
864 unsafe { aarch64::vminvq_u32(self.0) == !0 }
865 }
866
867 /// Returns true if all four booleans in this vector are false.
868 ///
869 /// The result is *undefined* if all four values in this vector are not booleans. A boolean is
870 /// a value with all bits set or all bits clear (i.e. !0 or 0).
871 #[inline]
872 pub fn all_false(&self) -> bool {
873 unsafe { aarch64::vmaxvq_u32(self.0) == 0 }
874 }
875
876 // Packed comparisons
877
878 #[inline]
879 pub fn packed_eq(self, other: U32x4) -> U32x4 {
880 unsafe { U32x4(simd_eq(self.0, other.0)) }
881 }
882 }
883
884 impl Debug for U32x4 {
885 #[inline]
886 fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
887 write!(f, "<{}, {}, {}, {}>", self[0], self[1], self[2], self[3])
888 }
889 }
890
891 impl Index<usize> for U32x4 {
892 type Output = u32;
893 #[inline]
894 fn index(&self, index: usize) -> &u32 {
895 unsafe {
896 assert!(index < 4);
897 let ptr = &self.0 as *const uint32x4_t as *const u32;
898 mem::transmute::<*const u32, &u32>(ptr.offset(index as isize))
899 }
900 }
901 }
902
903 impl PartialEq for U32x4 {
904 #[inline]
905 fn eq(&self, other: &U32x4) -> bool {
906 self.packed_eq(*other).all_true()
907 }
908 }
909
910 extern "C" {
911 #[link_name = "llvm.fabs.v2f32"]
912 fn fabs_v2f32(a: float32x2_t) -> float32x2_t;
913 #[link_name = "llvm.floor.v2f32"]
914 fn floor_v2f32(a: float32x2_t) -> float32x2_t;
915 #[link_name = "llvm.ceil.v2f32"]
916 fn ceil_v2f32(a: float32x2_t) -> float32x2_t;
917 #[link_name = "llvm.round.v2f32"]
918 fn round_v2f32(a: float32x2_t) -> float32x2_t;
919 #[link_name = "llvm.sqrt.v2f32"]
920 fn sqrt_v2f32(a: float32x2_t) -> float32x2_t;
921
922 #[link_name = "llvm.fabs.v4f32"]
923 fn fabs_v4f32(a: float32x4_t) -> float32x4_t;
924 #[link_name = "llvm.floor.v4f32"]
925 fn floor_v4f32(a: float32x4_t) -> float32x4_t;
926 #[link_name = "llvm.ceil.v4f32"]
927 fn ceil_v4f32(a: float32x4_t) -> float32x4_t;
928 #[link_name = "llvm.round.v4f32"]
929 fn round_v4f32(a: float32x4_t) -> float32x4_t;
930 #[link_name = "llvm.sqrt.v4f32"]
931 fn sqrt_v4f32(a: float32x4_t) -> float32x4_t;
932
933 #[link_name = "llvm.aarch64.neon.frecpe.v2f32"]
934 fn vrecpe_v2f32(a: float32x2_t) -> float32x2_t;
935
936 #[link_name = "llvm.aarch64.neon.frecpe.v4f32"]
937 fn vrecpe_v4f32(a: float32x4_t) -> float32x4_t;
938 }