-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimd_avx512.rs
More file actions
3817 lines (3335 loc) · 124 KB
/
Copy pathsimd_avx512.rs
File metadata and controls
3817 lines (3335 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! AVX-512 SIMD compatibility layer — stable Rust std::arch wrappers.
//!
//! Drop-in replacement for `std::simd` portable_simd types. Provides the same
//! API surface (methods, operators, type names) backed by `std::arch::x86_64`
//! intrinsics. All intrinsics used here are stable on Rust 1.89+.
//!
//! # Types
//!
//! | Compat type | portable_simd equiv | Backing type | Width |
//! |-------------|--------------------|--------------| ------|
//! | `F32x16` | `f32x16` | `__m512` | 512b |
//! | `F64x8` | `f64x8` | `__m512d` | 512b |
//! | `U8x64` | `u8x64` | `__m512i` | 512b |
//! | `I32x16` | `i32x16` | `__m512i` | 512b |
//! | `I64x8` | `i64x8` | `__m512i` | 512b |
//! | `U32x16` | `u32x16` | `__m512i` | 512b |
//! | `U64x8` | `u64x8` | `__m512i` | 512b |
//!
//! # Migration guide
//!
//! ```rust,ignore
//! // Before (nightly):
//! use std::simd::f32x16;
//! use std::simd::num::SimdFloat;
//!
//! // After (stable 1.93):
//! use crate::simd::f32x16;
//! // No trait imports needed — all methods are inherent.
//! ```
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
use core::fmt;
use core::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign,
Neg, Not, Shl, Shr, Sub, SubAssign,
};
// ============================================================================
// Operator macros — reduce boilerplate for the 7 wrapper types
// ============================================================================
macro_rules! impl_bin_op {
($ty:ident, $trait:ident, $method:ident, $intr:path) => {
impl $trait for $ty {
type Output = Self;
#[inline(always)]
fn $method(self, rhs: Self) -> Self {
Self(unsafe { $intr(self.0, rhs.0) })
}
}
};
}
macro_rules! impl_assign_op {
($ty:ident, $trait:ident, $method:ident, $intr:path) => {
impl $trait for $ty {
#[inline(always)]
fn $method(&mut self, rhs: Self) {
self.0 = unsafe { $intr(self.0, rhs.0) };
}
}
};
}
// ============================================================================
// F32x16 — 16 × f32 in one AVX-512 register (__m512)
// ============================================================================
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F32x16(pub __m512);
impl Default for F32x16 {
#[inline(always)]
fn default() -> Self {
Self(unsafe { _mm512_setzero_ps() })
}
}
impl F32x16 {
pub const LANES: usize = 16;
#[inline(always)]
pub fn splat(v: f32) -> Self {
Self(unsafe { _mm512_set1_ps(v) })
}
#[inline(always)]
pub fn from_slice(s: &[f32]) -> Self {
assert!(s.len() >= 16);
Self(unsafe { _mm512_loadu_ps(s.as_ptr()) })
}
#[inline(always)]
pub fn from_array(arr: [f32; 16]) -> Self {
Self(unsafe { _mm512_loadu_ps(arr.as_ptr()) })
}
#[inline(always)]
pub fn to_array(self) -> [f32; 16] {
let mut arr = [0.0f32; 16];
unsafe { _mm512_storeu_ps(arr.as_mut_ptr(), self.0) };
arr
}
#[inline(always)]
pub fn copy_to_slice(self, s: &mut [f32]) {
assert!(s.len() >= 16);
unsafe { _mm512_storeu_ps(s.as_mut_ptr(), self.0) };
}
// --- Reductions ---
#[inline(always)]
pub fn reduce_sum(self) -> f32 {
unsafe { _mm512_reduce_add_ps(self.0) }
}
#[inline(always)]
pub fn reduce_min(self) -> f32 {
unsafe { _mm512_reduce_min_ps(self.0) }
}
#[inline(always)]
pub fn reduce_max(self) -> f32 {
unsafe { _mm512_reduce_max_ps(self.0) }
}
// --- Element-wise min/max/clamp ---
#[inline(always)]
pub fn simd_min(self, other: Self) -> Self {
Self(unsafe { _mm512_min_ps(self.0, other.0) })
}
#[inline(always)]
pub fn simd_max(self, other: Self) -> Self {
Self(unsafe { _mm512_max_ps(self.0, other.0) })
}
#[inline(always)]
pub fn simd_clamp(self, lo: Self, hi: Self) -> Self {
self.simd_max(lo).simd_min(hi)
}
// --- Math (StdFloat equivalents) ---
#[inline(always)]
pub fn mul_add(self, b: Self, c: Self) -> Self {
Self(unsafe { _mm512_fmadd_ps(self.0, b.0, c.0) })
}
#[inline(always)]
pub fn sqrt(self) -> Self {
Self(unsafe { _mm512_sqrt_ps(self.0) })
}
/// Round to nearest integer (ties to even).
#[inline(always)]
pub fn round(self) -> Self {
// IMM8: bits[1:0]=0 (nearest), bit[3]=1 (suppress exceptions) = 0x08
Self(unsafe { _mm512_roundscale_ps::<0x08>(self.0) })
}
/// Floor (round toward negative infinity).
#[inline(always)]
pub fn floor(self) -> Self {
// IMM8: bits[1:0]=1 (floor), bit[3]=1 (suppress exceptions) = 0x09
Self(unsafe { _mm512_roundscale_ps::<0x09>(self.0) })
}
#[inline(always)]
pub fn abs(self) -> Self {
unsafe {
let mask = _mm512_set1_epi32(0x7FFF_FFFFi32);
Self(_mm512_castsi512_ps(_mm512_and_si512(_mm512_castps_si512(self.0), mask)))
}
}
// --- Bit reinterpretation ---
#[inline(always)]
pub fn to_bits(self) -> U32x16 {
U32x16(unsafe { _mm512_castps_si512(self.0) })
}
#[inline(always)]
pub fn from_bits(bits: U32x16) -> Self {
Self(unsafe { _mm512_castsi512_ps(bits.0) })
}
// --- Type casts ---
/// Truncating cast f32→i32 (equivalent to `portable_simd .cast::<i32>()`).
#[inline(always)]
pub fn cast_i32(self) -> I32x16 {
I32x16(unsafe { _mm512_cvttps_epi32(self.0) })
}
// --- Comparisons (return typed masks) ---
#[inline(always)]
pub fn simd_eq(self, other: Self) -> F32Mask16 {
F32Mask16(unsafe { _mm512_cmp_ps_mask::<_CMP_EQ_OQ>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_ne(self, other: Self) -> F32Mask16 {
F32Mask16(unsafe { _mm512_cmp_ps_mask::<_CMP_NEQ_UQ>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_lt(self, other: Self) -> F32Mask16 {
F32Mask16(unsafe { _mm512_cmp_ps_mask::<_CMP_LT_OS>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_le(self, other: Self) -> F32Mask16 {
F32Mask16(unsafe { _mm512_cmp_ps_mask::<_CMP_LE_OS>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_gt(self, other: Self) -> F32Mask16 {
// GT(a, b) = LT(b, a)
other.simd_lt(self)
}
#[inline(always)]
pub fn simd_ge(self, other: Self) -> F32Mask16 {
// GE(a, b) = LE(b, a)
other.simd_le(self)
}
/// Gather 16 f32 values from `base_ptr` using 16 i32 indices.
///
/// Equivalent to `_mm512_i32gather_ps::<4>(indices, base_ptr)`:
/// each lane loads `base_ptr[indices[lane]]`.
///
/// # Safety
/// Caller must ensure all indices are valid offsets into the memory at `base_ptr`.
#[inline(always)]
pub unsafe fn gather(indices: I32x16, base_ptr: *const f32) -> Self {
Self(_mm512_i32gather_ps::<4>(indices.0, base_ptr))
}
}
impl_bin_op!(F32x16, Add, add, _mm512_add_ps);
impl_bin_op!(F32x16, Sub, sub, _mm512_sub_ps);
impl_bin_op!(F32x16, Mul, mul, _mm512_mul_ps);
impl_bin_op!(F32x16, Div, div, _mm512_div_ps);
impl_assign_op!(F32x16, AddAssign, add_assign, _mm512_add_ps);
impl_assign_op!(F32x16, SubAssign, sub_assign, _mm512_sub_ps);
impl_assign_op!(F32x16, MulAssign, mul_assign, _mm512_mul_ps);
impl_assign_op!(F32x16, DivAssign, div_assign, _mm512_div_ps);
impl Neg for F32x16 {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
unsafe {
let sign = _mm512_set1_epi32(i32::MIN); // 0x80000000
Self(_mm512_castsi512_ps(_mm512_xor_si512(_mm512_castps_si512(self.0), sign)))
}
}
}
impl fmt::Debug for F32x16 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "F32x16({:?})", self.to_array())
}
}
impl PartialEq for F32x16 {
fn eq(&self, other: &Self) -> bool {
self.to_array() == other.to_array()
}
}
// ============================================================================
// F32Mask16 — 16-bit mask from f32 comparisons
// ============================================================================
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct F32Mask16(pub __mmask16);
impl F32Mask16 {
/// Select: for each lane, if mask bit is 1 → true_val, else false_val.
#[inline(always)]
pub fn select(self, true_val: F32x16, false_val: F32x16) -> F32x16 {
// _mm512_mask_blend_ps(k, a, b): if k[i] then b[i] else a[i]
F32x16(unsafe { _mm512_mask_blend_ps(self.0, false_val.0, true_val.0) })
}
}
// ============================================================================
// F64x8 — 8 × f64 in one AVX-512 register (__m512d)
// ============================================================================
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F64x8(pub __m512d);
impl Default for F64x8 {
#[inline(always)]
fn default() -> Self {
Self(unsafe { _mm512_setzero_pd() })
}
}
impl F64x8 {
pub const LANES: usize = 8;
#[inline(always)]
pub fn splat(v: f64) -> Self {
Self(unsafe { _mm512_set1_pd(v) })
}
#[inline(always)]
pub fn from_slice(s: &[f64]) -> Self {
assert!(s.len() >= 8);
Self(unsafe { _mm512_loadu_pd(s.as_ptr()) })
}
#[inline(always)]
pub fn from_array(arr: [f64; 8]) -> Self {
Self(unsafe { _mm512_loadu_pd(arr.as_ptr()) })
}
#[inline(always)]
pub fn to_array(self) -> [f64; 8] {
let mut arr = [0.0f64; 8];
unsafe { _mm512_storeu_pd(arr.as_mut_ptr(), self.0) };
arr
}
#[inline(always)]
pub fn copy_to_slice(self, s: &mut [f64]) {
assert!(s.len() >= 8);
unsafe { _mm512_storeu_pd(s.as_mut_ptr(), self.0) };
}
#[inline(always)]
pub fn reduce_sum(self) -> f64 {
unsafe { _mm512_reduce_add_pd(self.0) }
}
#[inline(always)]
pub fn reduce_min(self) -> f64 {
unsafe { _mm512_reduce_min_pd(self.0) }
}
#[inline(always)]
pub fn reduce_max(self) -> f64 {
unsafe { _mm512_reduce_max_pd(self.0) }
}
#[inline(always)]
pub fn simd_min(self, other: Self) -> Self {
Self(unsafe { _mm512_min_pd(self.0, other.0) })
}
#[inline(always)]
pub fn simd_max(self, other: Self) -> Self {
Self(unsafe { _mm512_max_pd(self.0, other.0) })
}
#[inline(always)]
pub fn simd_clamp(self, lo: Self, hi: Self) -> Self {
self.simd_max(lo).simd_min(hi)
}
#[inline(always)]
pub fn mul_add(self, b: Self, c: Self) -> Self {
Self(unsafe { _mm512_fmadd_pd(self.0, b.0, c.0) })
}
#[inline(always)]
pub fn sqrt(self) -> Self {
Self(unsafe { _mm512_sqrt_pd(self.0) })
}
#[inline(always)]
pub fn round(self) -> Self {
Self(unsafe { _mm512_roundscale_pd::<0x08>(self.0) })
}
#[inline(always)]
pub fn floor(self) -> Self {
Self(unsafe { _mm512_roundscale_pd::<0x09>(self.0) })
}
#[inline(always)]
pub fn abs(self) -> Self {
unsafe {
let mask = _mm512_set1_epi64(0x7FFF_FFFF_FFFF_FFFFi64);
Self(_mm512_castsi512_pd(_mm512_and_si512(_mm512_castpd_si512(self.0), mask)))
}
}
#[inline(always)]
pub fn to_bits(self) -> U64x8 {
U64x8(unsafe { _mm512_castpd_si512(self.0) })
}
#[inline(always)]
pub fn from_bits(bits: U64x8) -> Self {
Self(unsafe { _mm512_castsi512_pd(bits.0) })
}
// --- Comparisons ---
#[inline(always)]
pub fn simd_eq(self, other: Self) -> F64Mask8 {
F64Mask8(unsafe { _mm512_cmp_pd_mask::<_CMP_EQ_OQ>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_ne(self, other: Self) -> F64Mask8 {
F64Mask8(unsafe { _mm512_cmp_pd_mask::<_CMP_NEQ_UQ>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_lt(self, other: Self) -> F64Mask8 {
F64Mask8(unsafe { _mm512_cmp_pd_mask::<_CMP_LT_OS>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_le(self, other: Self) -> F64Mask8 {
F64Mask8(unsafe { _mm512_cmp_pd_mask::<_CMP_LE_OS>(self.0, other.0) })
}
#[inline(always)]
pub fn simd_gt(self, other: Self) -> F64Mask8 {
other.simd_lt(self)
}
#[inline(always)]
pub fn simd_ge(self, other: Self) -> F64Mask8 {
other.simd_le(self)
}
}
impl_bin_op!(F64x8, Add, add, _mm512_add_pd);
impl_bin_op!(F64x8, Sub, sub, _mm512_sub_pd);
impl_bin_op!(F64x8, Mul, mul, _mm512_mul_pd);
impl_bin_op!(F64x8, Div, div, _mm512_div_pd);
impl_assign_op!(F64x8, AddAssign, add_assign, _mm512_add_pd);
impl_assign_op!(F64x8, SubAssign, sub_assign, _mm512_sub_pd);
impl_assign_op!(F64x8, MulAssign, mul_assign, _mm512_mul_pd);
impl_assign_op!(F64x8, DivAssign, div_assign, _mm512_div_pd);
impl Neg for F64x8 {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self {
unsafe {
let sign = _mm512_set1_epi64(i64::MIN); // 0x8000000000000000
Self(_mm512_castsi512_pd(_mm512_xor_si512(_mm512_castpd_si512(self.0), sign)))
}
}
}
impl fmt::Debug for F64x8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "F64x8({:?})", self.to_array())
}
}
impl PartialEq for F64x8 {
fn eq(&self, other: &Self) -> bool {
self.to_array() == other.to_array()
}
}
// ============================================================================
// F64Mask8 — 8-bit mask from f64 comparisons
// ============================================================================
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct F64Mask8(pub __mmask8);
impl F64Mask8 {
#[inline(always)]
pub fn select(self, true_val: F64x8, false_val: F64x8) -> F64x8 {
F64x8(unsafe { _mm512_mask_blend_pd(self.0, false_val.0, true_val.0) })
}
}
// ============================================================================
// U8x64 — 64 × u8 in one AVX-512 register (__m512i)
// ============================================================================
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct U8x64(pub __m512i);
impl U8x64 {
pub const LANES: usize = 64;
#[inline(always)]
pub fn splat(v: u8) -> Self {
Self(unsafe { _mm512_set1_epi8(v as i8) })
}
#[inline(always)]
pub fn from_slice(s: &[u8]) -> Self {
assert!(s.len() >= 64);
Self(unsafe { _mm512_loadu_si512(s.as_ptr() as *const _) })
}
#[inline(always)]
pub fn from_array(arr: [u8; 64]) -> Self {
Self(unsafe { _mm512_loadu_si512(arr.as_ptr() as *const _) })
}
#[inline(always)]
pub fn to_array(self) -> [u8; 64] {
let mut arr = [0u8; 64];
unsafe { _mm512_storeu_si512(arr.as_mut_ptr() as *mut _, self.0) };
arr
}
#[inline(always)]
pub fn copy_to_slice(self, s: &mut [u8]) {
assert!(s.len() >= 64);
unsafe { _mm512_storeu_si512(s.as_mut_ptr() as *mut _, self.0) };
}
/// Wrapping sum of all 64 bytes → u8 (matches portable_simd semantics).
#[inline(always)]
pub fn reduce_sum(self) -> u8 {
unsafe {
// SAD against zero sums groups of 8 bytes → 8 × u64
let sad = _mm512_sad_epu8(self.0, _mm512_setzero_si512());
_mm512_reduce_add_epi64(sad) as u8
}
}
/// Minimum of all 64 bytes.
#[inline(always)]
pub fn reduce_min(self) -> u8 {
// Tree reduction: 512→256→128→scalar
let arr = self.to_array();
let mut m = arr[0];
for &val in arr.iter().skip(1) {
if val < m {
m = val;
}
}
m
}
/// Maximum of all 64 bytes.
#[inline(always)]
pub fn reduce_max(self) -> u8 {
let arr = self.to_array();
let mut m = arr[0];
for &val in arr.iter().skip(1) {
if val > m {
m = val;
}
}
m
}
#[inline(always)]
pub fn simd_min(self, other: Self) -> Self {
Self(unsafe { _mm512_min_epu8(self.0, other.0) })
}
#[inline(always)]
pub fn simd_max(self, other: Self) -> Self {
Self(unsafe { _mm512_max_epu8(self.0, other.0) })
}
// ── Byte-level operations for palette codec, nibble, byte scan ──────
// Reference: Pumpkin/Minecraft-derived modules (palette_codec.rs,
// nibble.rs, byte_scan.rs) use these for 4-bit packing and scanning.
/// Byte-wise equality comparison. Returns 64-bit mask: bit i set if a[i] == b[i].
#[inline(always)]
pub fn cmpeq_mask(self, other: Self) -> u64 {
unsafe { _mm512_cmpeq_epi8_mask(self.0, other.0) }
}
/// Shift right each 16-bit lane by immediate bits (for nibble extraction).
/// Note: operates on 16-bit lanes, not 8-bit — matches _mm512_srli_epi16.
#[inline(always)]
pub fn shr_epi16(self, imm: u32) -> Self {
// _mm512_srli_epi16 shifts each 16-bit lane right
// Use match for const immediate (intrinsic requires const)
Self(unsafe {
match imm {
1 => _mm512_srli_epi16(self.0, 1),
2 => _mm512_srli_epi16(self.0, 2),
3 => _mm512_srli_epi16(self.0, 3),
4 => _mm512_srli_epi16(self.0, 4),
5 => _mm512_srli_epi16(self.0, 5),
6 => _mm512_srli_epi16(self.0, 6),
7 => _mm512_srli_epi16(self.0, 7),
8 => _mm512_srli_epi16(self.0, 8),
_ => _mm512_setzero_si512(),
}
})
}
/// Saturating unsigned subtraction: max(a - b, 0) per byte.
#[inline(always)]
pub fn saturating_sub(self, other: Self) -> Self {
Self(unsafe { _mm512_subs_epu8(self.0, other.0) })
}
// ── Tier 1: seismon rasterizer primitives ─────────────────────────
/// Pairwise unsigned byte average: (a[i] + b[i] + 1) >> 1 per byte.
/// Core op for 4×4 mipmap downsample (vpavgb + horizontal pair = 2 ops).
#[inline(always)]
pub fn pairwise_avg(self, other: Self) -> Self {
// SAFETY: AVX-512BW instruction, operates on all 64 bytes.
Self(unsafe { _mm512_avg_epu8(self.0, other.0) })
}
/// Byte-wise unsigned greater-than comparison. Returns 64-bit mask:
/// bit i set if self[i] > other[i]. Symmetric to `cmpeq_mask`.
/// Used for threshold density fields, depth/Z-test, hit-tests.
#[inline(always)]
pub fn cmpgt_mask(self, other: Self) -> u64 {
// SAFETY: AVX-512BW instruction. Unsigned compare via _epu8.
unsafe { _mm512_cmpgt_epu8_mask(self.0, other.0) }
}
/// Masked blend: for each bit in `mask`, select from `b` if set, else `a`.
/// Sprite alpha blit: write atlas pixel where mask bit set, keep framebuffer otherwise.
#[inline(always)]
pub fn mask_blend(mask: u64, a: Self, b: Self) -> Self {
// SAFETY: AVX-512BW instruction. mask selects between a and b per byte.
Self(unsafe { _mm512_mask_blend_epi8(mask, a.0, b.0) })
}
/// Shift left each 16-bit lane by immediate bits (nibble write: place high nibble).
/// Completes the nibble shift pair with `shr_epi16`.
#[inline(always)]
pub fn shl_epi16(self, imm: u32) -> Self {
Self(unsafe {
match imm {
1 => _mm512_slli_epi16(self.0, 1),
2 => _mm512_slli_epi16(self.0, 2),
3 => _mm512_slli_epi16(self.0, 3),
4 => _mm512_slli_epi16(self.0, 4),
5 => _mm512_slli_epi16(self.0, 5),
6 => _mm512_slli_epi16(self.0, 6),
7 => _mm512_slli_epi16(self.0, 7),
8 => _mm512_slli_epi16(self.0, 8),
_ => _mm512_setzero_si512(),
}
})
}
// ── Tier 2: sprite blit + palette LUT + cross-lane shuffle ────────
/// Masked store: write only bytes where mask bit is set.
/// Partial-tile writes at framebuffer edges without scalar fallback.
///
/// # Safety
/// `ptr` must point to at least 64 writable bytes (may be unaligned).
#[inline(always)]
pub unsafe fn mask_store(self, ptr: *mut u8, mask: u64) {
// SAFETY: AVX-512BW masked store. Caller guarantees ptr validity.
_mm512_mask_storeu_epi8(ptr as *mut i8, mask, self.0);
}
/// Saturating unsigned addition: min(a + b, 255) per byte.
/// Additive blend without overflow wrap. Symmetric to `saturating_sub`.
#[inline(always)]
pub fn saturating_add(self, other: Self) -> Self {
// SAFETY: AVX-512BW instruction.
Self(unsafe { _mm512_adds_epu8(self.0, other.0) })
}
/// Cross-lane byte permute: rearrange all 64 bytes by index vector.
/// `idx[i]` selects which byte of `self` appears at position `i & 63`.
/// Unlike `shuffle_bytes` (within-lane), this crosses 128-bit lane boundaries.
/// Needed for sprite atlas reorder and palette remap > 16 entries.
///
/// Dispatch (one LazyLock check via `simd_caps()`):
/// - VBMI present (Ice Lake+, Tiger Lake+, Sapphire Rapids+, Zen 4): hardware
/// `_mm512_permutexvar_epi8` — one instruction.
/// - AVX-512F without VBMI (Skylake-X, Cascade Lake, Ice Lake-SP): scalar
/// permute via stack. Slower but does not SIGILL.
#[inline]
pub fn permute_bytes(self, idx: Self) -> Self {
if crate::hpc::simd_caps::simd_caps().avx512vbmi {
// SAFETY: avx512vbmi was verified by simd_caps() at startup
// (one LazyLock detect for the whole process).
unsafe { Self(permute_bytes_vbmi(self.0, idx.0)) }
} else {
// AVX-512F-only fallback: scalar permute via stack arrays.
// Same shape as the AVX2-tier fallback in simd_avx2.rs:1435.
let src = self.to_array();
let idx_arr = idx.to_array();
let mut out = [0u8; 64];
for i in 0..64 {
out[i] = src[(idx_arr[i] & 63) as usize];
}
Self::from_array(out)
}
}
/// Extract sign bits of all 64 bytes as a 64-bit mask.
/// Bit i is set if byte i has its MSB (bit 7) set.
/// Useful for empty-tile skip ("any pixel non-zero in this 64-pixel row").
#[inline(always)]
pub fn movemask(self) -> u64 {
// SAFETY: AVX-512BW. Compare each byte > 0x7F is equivalent to MSB set.
// Using cmpgt with 0x7F splat: set bit if byte > 127 (i.e. MSB = 1).
unsafe { _mm512_movepi8_mask(self.0) }
}
/// Interleave low bytes: [a0,b0,a1,b1,...] from lower halves.
#[inline(always)]
pub fn unpack_lo_epi8(self, other: Self) -> Self {
Self(unsafe { _mm512_unpacklo_epi8(self.0, other.0) })
}
/// Interleave high bytes: [a8,b8,a9,b9,...] from upper halves.
#[inline(always)]
pub fn unpack_hi_epi8(self, other: Self) -> Self {
Self(unsafe { _mm512_unpackhi_epi8(self.0, other.0) })
}
/// Byte-wise shuffle: use `self` as a LUT, `idx` selects bytes within each 128-bit lane.
/// Equivalent to `_mm512_shuffle_epi8(self.0, idx.0)`.
#[inline(always)]
pub fn shuffle_bytes(self, idx: Self) -> Self {
Self(unsafe { _mm512_shuffle_epi8(self.0, idx.0) })
}
/// Sum all 64 bytes into a single `u64` without wrapping.
///
/// Uses `_mm512_sad_epu8` (groups of 8 bytes → u64 lanes) then horizontal add.
/// Range: 0..=64*255 = 16_320, always fits in u64.
#[inline(always)]
pub fn sum_bytes_u64(self) -> u64 {
unsafe {
let sad = _mm512_sad_epu8(self.0, _mm512_setzero_si512());
_mm512_reduce_add_epi64(sad) as u64
}
}
/// Build a nibble-popcount lookup table (replicated across all 4 × 128-bit lanes).
///
/// Entry `i` = popcount of `i` for i in 0..16. Used with `shuffle_bytes` for
/// SIMD popcount via the Mula nibble-LUT algorithm.
#[inline(always)]
pub fn nibble_popcount_lut() -> Self {
// 0x04030302_03020201_03020201_02010100 replicated ×4
Self(unsafe {
_mm512_set4_epi32(
0x04030302_u32 as i32, 0x03020201_u32 as i32, 0x03020201_u32 as i32, 0x02010100_u32 as i32,
)
})
}
}
/// AVX-512VBMI cross-lane byte permute. Inner unsafe leaf — `#[target_feature]`
/// is required by Rust to call the VBMI intrinsic from a function not compiled
/// with VBMI globally. Caller (`U8x64::permute_bytes`) gates this behind
/// `simd_caps().avx512vbmi` so the SIGILL on Skylake-X / Cascade Lake / Ice
/// Lake-SP is impossible by construction.
///
/// SAFETY: caller must verify `simd_caps().avx512vbmi == true` before calling.
#[inline]
#[target_feature(enable = "avx512vbmi")]
unsafe fn permute_bytes_vbmi(v: __m512i, idx: __m512i) -> __m512i {
_mm512_permutexvar_epi8(idx, v)
}
// u8 add/sub use AVX-512BW instructions
impl_bin_op!(U8x64, Add, add, _mm512_add_epi8);
impl_bin_op!(U8x64, Sub, sub, _mm512_sub_epi8);
impl_assign_op!(U8x64, AddAssign, add_assign, _mm512_add_epi8);
impl_assign_op!(U8x64, SubAssign, sub_assign, _mm512_sub_epi8);
// u8 multiply — no single instruction; widen to u16, multiply, truncate back.
impl Mul for U8x64 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: Self) -> Self {
unsafe {
// Split into lower/upper 32-byte halves
let a_lo = _mm512_castsi512_si256(self.0);
let a_hi = _mm512_extracti64x4_epi64::<1>(self.0);
let b_lo = _mm512_castsi512_si256(rhs.0);
let b_hi = _mm512_extracti64x4_epi64::<1>(rhs.0);
// Zero-extend u8→u16 (256→512 bits, 32 elements each)
let a16_lo = _mm512_cvtepu8_epi16(a_lo);
let a16_hi = _mm512_cvtepu8_epi16(a_hi);
let b16_lo = _mm512_cvtepu8_epi16(b_lo);
let b16_hi = _mm512_cvtepu8_epi16(b_hi);
// Multiply as u16 (wrapping at 16-bit)
let prod_lo = _mm512_mullo_epi16(a16_lo, b16_lo);
let prod_hi = _mm512_mullo_epi16(a16_hi, b16_hi);
// Truncate u16→u8 (keep low byte)
let packed_lo = _mm512_cvtepi16_epi8(prod_lo);
let packed_hi = _mm512_cvtepi16_epi8(prod_hi);
Self(_mm512_inserti64x4::<1>(_mm512_castsi256_si512(packed_lo), packed_hi))
}
}
}
impl MulAssign for U8x64 {
#[inline(always)]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
// Bitwise ops for u8
impl_bin_op!(U8x64, BitAnd, bitand, _mm512_and_si512);
impl_bin_op!(U8x64, BitXor, bitxor, _mm512_xor_si512);
impl_bin_op!(U8x64, BitOr, bitor, _mm512_or_si512);
impl_assign_op!(U8x64, BitAndAssign, bitand_assign, _mm512_and_si512);
impl_assign_op!(U8x64, BitXorAssign, bitxor_assign, _mm512_xor_si512);
impl_assign_op!(U8x64, BitOrAssign, bitor_assign, _mm512_or_si512);
impl Not for U8x64 {
type Output = Self;
#[inline(always)]
fn not(self) -> Self {
unsafe {
let all_ones = _mm512_set1_epi8(-1);
Self(_mm512_xor_si512(self.0, all_ones))
}
}
}
impl fmt::Debug for U8x64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "U8x64({:?})", &self.to_array()[..])
}
}
impl PartialEq for U8x64 {
fn eq(&self, other: &Self) -> bool {
self.to_array() == other.to_array()
}
}
// ============================================================================
// I32x16 — 16 × i32 in one AVX-512 register (__m512i)
// ============================================================================
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct I32x16(pub __m512i);
impl I32x16 {
pub const LANES: usize = 16;
#[inline(always)]
pub fn splat(v: i32) -> Self {
Self(unsafe { _mm512_set1_epi32(v) })
}
#[inline(always)]
pub fn from_slice(s: &[i32]) -> Self {
assert!(s.len() >= 16);
Self(unsafe { _mm512_loadu_si512(s.as_ptr() as *const _) })
}
#[inline(always)]
pub fn from_array(arr: [i32; 16]) -> Self {
Self(unsafe { _mm512_loadu_si512(arr.as_ptr() as *const _) })
}
#[inline(always)]
pub fn to_array(self) -> [i32; 16] {
let mut arr = [0i32; 16];
unsafe { _mm512_storeu_si512(arr.as_mut_ptr() as *mut _, self.0) };
arr
}
#[inline(always)]
pub fn copy_to_slice(self, s: &mut [i32]) {
assert!(s.len() >= 16);
unsafe { _mm512_storeu_si512(s.as_mut_ptr() as *mut _, self.0) };
}
#[inline(always)]
pub fn reduce_sum(self) -> i32 {
unsafe { _mm512_reduce_add_epi32(self.0) }
}
#[inline(always)]
pub fn reduce_min(self) -> i32 {
unsafe { _mm512_reduce_min_epi32(self.0) }
}
#[inline(always)]
pub fn reduce_max(self) -> i32 {
unsafe { _mm512_reduce_max_epi32(self.0) }
}
// ── Base17 i16[17] operations: load-widen, abs, narrow ──────────────
// Used by bgz17_bridge.rs for L1 distance, weighted L1, sign agreement, xor_bind.
/// Load 16 × i16 from slice, sign-extend to 16 × i32.
/// This is the first step of every Base17 kernel: i16 → i32 to avoid overflow.
#[inline(always)]
pub fn from_i16_slice(s: &[i16]) -> Self {
assert!(s.len() >= 16);
Self(unsafe { _mm512_cvtepi16_epi32(_mm256_loadu_si256(s.as_ptr() as *const __m256i)) })
}
/// Absolute value per lane.
#[inline(always)]
pub fn abs(self) -> Self {
Self(unsafe { _mm512_abs_epi32(self.0) })
}
/// Narrow 16 × i32 back to 16 × i16 (truncation, no saturation).
#[inline(always)]
pub fn to_i16_array(self) -> [i16; 16] {
unsafe {
let packed = _mm512_cvtepi32_epi16(self.0);
let mut arr = [0i16; 16];
_mm256_storeu_si256(arr.as_mut_ptr() as *mut __m256i, packed);
arr
}
}
/// Compare >= 0: returns 16-bit mask. Bit i set where lane i >= 0.
#[inline(always)]
pub fn cmpge_zero_mask(self) -> u16 {
unsafe { _mm512_cmpge_epi32_mask(self.0, _mm512_setzero_si512()) }
}
#[inline(always)]
pub fn simd_min(self, other: Self) -> Self {
Self(unsafe { _mm512_min_epi32(self.0, other.0) })
}
#[inline(always)]
pub fn simd_max(self, other: Self) -> Self {
Self(unsafe { _mm512_max_epi32(self.0, other.0) })
}
/// Cast i32→f32 (equivalent to `portable_simd .cast::<f32>()`).
#[inline(always)]
pub fn cast_f32(self) -> F32x16 {
F32x16(unsafe { _mm512_cvtepi32_ps(self.0) })
}
}
impl_bin_op!(I32x16, Add, add, _mm512_add_epi32);
impl_bin_op!(I32x16, Sub, sub, _mm512_sub_epi32);
impl_assign_op!(I32x16, AddAssign, add_assign, _mm512_add_epi32);
impl_assign_op!(I32x16, SubAssign, sub_assign, _mm512_sub_epi32);
// i32 multiply: _mm512_mullo_epi32 (AVX-512F)
impl_bin_op!(I32x16, Mul, mul, _mm512_mullo_epi32);
impl_assign_op!(I32x16, MulAssign, mul_assign, _mm512_mullo_epi32);
// i32 divide: no SIMD instruction — array fallback
impl Div for I32x16 {
type Output = Self;
#[inline(always)]
fn div(self, rhs: Self) -> Self {
let a = self.to_array();
let b = rhs.to_array();
let mut c = [0i32; 16];
for i in 0..16 {
c[i] = a[i] / b[i];
}
Self::from_array(c)
}
}
impl DivAssign for I32x16 {
#[inline(always)]
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}
// Bitwise
impl_bin_op!(I32x16, BitAnd, bitand, _mm512_and_si512);
impl_bin_op!(I32x16, BitXor, bitxor, _mm512_xor_si512);
impl_bin_op!(I32x16, BitOr, bitor, _mm512_or_si512);
impl_assign_op!(I32x16, BitAndAssign, bitand_assign, _mm512_and_si512);
impl_assign_op!(I32x16, BitXorAssign, bitxor_assign, _mm512_xor_si512);
impl_assign_op!(I32x16, BitOrAssign, bitor_assign, _mm512_or_si512);