simply-simd 0.2.0

No-boilerplate portable SIMD library for raw registers with runtime feature detection
Documentation
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
use std::arch::x86_64::*;
use std::mem::{transmute, transmute_copy};

use crate::architectures::interface::*;
use crate::architectures::macros::*;

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Avx512Reg(pub __m512i);
impl SimdArch for Avx512Reg {}

#[derive(Copy, Clone)]
pub struct Avx512Mask(pub __mmask64);
impl MaskArch for Avx512Mask {}

impl SimdAddImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn f64_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i64_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn i8_add(self, rhs: Self) -> Self {
        self_from_op!(_mm512_add_epi8, self, rhs)
    }
}

impl SimdSubImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn f64_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i64_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn i8_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sub_epi8, self, rhs)
    }
}

impl SimdMulImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn f64_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm512_mul_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm512_mul_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm512_mullo_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm512_mullo_epi16, self, rhs)
    }
}

impl SimdDivImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn f64_div(self, rhs: Self) -> Self {
        self_from_op!(_mm512_div_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_div(self, rhs: Self) -> Self {
        self_from_op!(_mm512_div_ps, self, rhs)
    }
}

impl SimdBitwiseImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn and(self, rhs: Self) -> Self {
        self_from_op!(_mm512_and_si512, self, rhs)
    }
    #[inline(always)]
    unsafe fn or(self, rhs: Self) -> Self {
        self_from_op!(_mm512_or_si512, self, rhs)
    }
    #[inline(always)]
    unsafe fn xor(self, rhs: Self) -> Self {
        self_from_op!(_mm512_xor_si512, self, rhs)
    }
    #[inline(always)]
    unsafe fn not(self) -> Self {
        unsafe { Self(self.xor(Self::splat_32(!0)).0) }
    }
    #[inline(always)]
    unsafe fn and_not(self, rhs: Self) -> Self {
        self_from_op!(_mm512_andnot_si512, rhs, self)
    }
}

impl SimdShiftImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn sllv_64(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sllv_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn srlv_64(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srlv_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn srav_64(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srav_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn sllv_32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sllv_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn srlv_32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srlv_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn srav_32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srav_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn sllv_16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_sllv_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn srlv_16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srlv_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn srav_16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_srav_epi16, self, rhs)
    }
}

impl SimdLoadImpl for Avx512Reg {
    type MaskType = Avx512Mask;
    #[inline(always)]
    unsafe fn load_aligned<T>(ptr: *const T) -> Self {
        self_from_op!(_mm512_load_si512, ptr)
    }
    #[inline(always)]
    unsafe fn load_unaligned<T>(ptr: *const T) -> Self {
        self_from_op!(_mm512_loadu_si512, ptr)
    }
    #[inline(always)]
    unsafe fn masked_load_64<T>(ptr: *const T, mask: Self::MaskType) -> Self {
        self_from_op!(_mm512_mask_loadu_epi64, Self::zero(), mask, ptr)
    }
    #[inline(always)]
    unsafe fn masked_load_32<T>(ptr: *const T, mask: Self::MaskType) -> Self {
        self_from_op!(_mm512_mask_loadu_epi32, Self::zero(), mask, ptr)
    }
}

impl SimdStoreImpl for Avx512Reg {
    type MaskType = Avx512Mask;
    #[inline(always)]
    unsafe fn store_aligned<T>(self, ptr: *mut T) {
        execute_intrinsic!(_mm512_store_si512, ptr, self);
    }
    #[inline(always)]
    unsafe fn store_unaligned<T>(self, ptr: *mut T) {
        execute_intrinsic!(_mm512_storeu_si512, ptr, self);
    }
    #[inline(always)]
    unsafe fn masked_store_64<T>(self, ptr: *mut T, mask: Self::MaskType) {
        execute_intrinsic!(_mm512_mask_storeu_epi64, ptr, mask, self);
    }
    #[inline(always)]
    unsafe fn masked_store_32<T>(self, ptr: *mut T, mask: Self::MaskType) {
        execute_intrinsic!(_mm512_mask_storeu_epi32, ptr, mask, self);
    }
}

impl SimdZeroImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn zero() -> Self {
        self_from_op!(_mm512_setzero_si512,)
    }
}

impl SimdFloatCastsImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn float_to_int_trunc(self) -> Self {
        self_from_op!(_mm512_cvttps_epi32, self)
    }
    #[inline(always)]
    unsafe fn float_to_int_round(self) -> Self {
        self_from_op!(_mm512_cvtps_epi32, self)
    }
}

impl SimdIntCastsImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn int_to_float(self) -> Self {
        self_from_op!(_mm512_cvtepi32_ps, self)
    }
}

impl SimdPermuteImpl for Avx512Reg {
    // type BlockVec = Sse;
    #[inline(always)]
    unsafe fn permute_32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_permutexvar_epi32, rhs, self)
    }
    #[inline(always)]
    unsafe fn permute_8(self, rhs: Self) -> Self {
        self_from_op!(_mm512_shuffle_epi8, self, rhs)
    }
}

impl SimdVariableBlendImpl for Avx512Mask {
    type VecType = Avx512Reg;
    #[inline(always)]
    unsafe fn vblend_64(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType {
        unsafe {
            Avx512Reg(transmute(execute_intrinsic!(
                _mm512_mask_blend_pd,
                self,
                false_values,
                true_values
            )))
        }
    }
    #[inline(always)]
    unsafe fn vblend_32(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType {
        unsafe {
            Avx512Reg(transmute(execute_intrinsic!(
                _mm512_mask_blend_ps,
                self,
                false_values,
                true_values
            )))
        }
    }
    #[inline(always)]
    unsafe fn vblend_8(self, true_values: Self::VecType, false_values: Self::VecType) -> Self::VecType {
        unsafe {
            Avx512Reg(transmute(execute_intrinsic!(
                _mm512_mask_blend_epi8,
                self,
                false_values,
                true_values
            )))
        }
    }
}

impl SimdImmediateBlendImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn blend_64<const N: i32>(self, false_values: Self) -> Self {
        let mask = Avx512Mask(N as u64);
        unsafe { mask.vblend_64(self, false_values) }
    }
    #[inline(always)]
    unsafe fn blend_32<const N: i32>(self, false_values: Self) -> Self {
        let mask = Avx512Mask(N as u64);
        unsafe { mask.vblend_32(self, false_values) }
    }
}

impl SimdMulAddImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn mul_add_f64(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm512_fmadd_pd, self, mult, add)
    }
    #[inline(always)]
    unsafe fn mul_sub_f64(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm512_fmsub_pd, self, mult, sub)
    }
    #[inline(always)]
    unsafe fn negated_mul_add_f64(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm512_fnmadd_pd, self, mult, add)
    }
    #[inline(always)]
    unsafe fn negated_mul_sub_f64(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm512_fnmsub_pd, self, mult, sub)
    }
    #[inline(always)]
    unsafe fn mul_add_f32(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm512_fmadd_ps, self, mult, add)
    }
    #[inline(always)]
    unsafe fn mul_sub_f32(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm512_fmsub_ps, self, mult, sub)
    }
    #[inline(always)]
    unsafe fn negated_mul_add_f32(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm512_fnmadd_ps, self, mult, add)
    }
    #[inline(always)]
    unsafe fn negated_mul_sub_f32(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm512_fnmsub_ps, self, mult, sub)
    }
}

impl SimdRoundImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn round_f64(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_pd,
            _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC,
            self
        )
    }
    #[inline(always)]
    unsafe fn round_f32(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_ps,
            _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC,
            self
        )
    }
    #[inline(always)]
    unsafe fn floor_f64(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_pd,
            _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC,
            self
        )
    }
    #[inline(always)]
    unsafe fn floor_f32(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_ps,
            _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC,
            self
        )
    }
    #[inline(always)]
    unsafe fn ceil_f64(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_pd,
            _MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC,
            self
        )
    }
    #[inline(always)]
    unsafe fn ceil_f32(self) -> Self {
        self_from_const_op!(
            _mm512_roundscale_ps,
            _MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC,
            self
        )
    }
}

impl SimdPartialOrdImpl for Avx512Reg {
    type MaskType = Avx512Mask;
    #[inline(always)]
    unsafe fn cmp_f64_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_EQ_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f64_lt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_LT_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f64_le(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_LE_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f64_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_GT_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f64_ge(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_GE_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f64_neq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_pd_mask, _CMP_NEQ_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_EQ_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_lt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_LT_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_le(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_LE_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_GT_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_ge(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_GE_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_f32_neq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_const_intrinsic!(_mm512_cmp_ps_mask, _CMP_NEQ_OQ, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i64_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpeq_epi64_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i64_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpgt_epi64_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i32_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpeq_epi32_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i32_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpgt_epi32_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i16_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpeq_epi16_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i16_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpgt_epi16_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i8_eq(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpeq_epi8_mask, self, rhs) as u64)
    }
    #[inline(always)]
    unsafe fn cmp_i8_gt(self, rhs: Self) -> Self::MaskType {
        Avx512Mask(execute_intrinsic!(_mm512_cmpgt_epi8_mask, self, rhs) as u64)
    }

    #[inline(always)]
    unsafe fn max_f64(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_f64(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_f32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_f32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i8(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epi8, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i8(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epi8, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epu32, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u32(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epu32, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epu16, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u16(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epu16, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u8(self, rhs: Self) -> Self {
        self_from_op!(_mm512_max_epu8, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u8(self, rhs: Self) -> Self {
        self_from_op!(_mm512_min_epu8, self, rhs)
    }
}

// TODO: Make a custom trait for handling this transmutation into i*.
impl SimdSplatImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn splat_64<T>(val: T) -> Self {
        self_from_op!(_mm512_set1_epi64, val)
    }
    #[inline(always)]
    unsafe fn splat_32<T>(val: T) -> Self {
        self_from_op!(_mm512_set1_epi32, val)
    }
    #[inline(always)]
    unsafe fn splat_16<T>(val: T) -> Self {
        self_from_op!(_mm512_set1_epi16, val)
    }
    #[inline(always)]
    unsafe fn splat_8<T>(val: T) -> Self {
        self_from_op!(_mm512_set1_epi8, val)
    }
}

impl SimdBitwiseImpl for Avx512Mask {
    #[inline(always)]
    unsafe fn and(self, rhs: Self) -> Self {
        self_from_op!(_kand_mask64, self, rhs)
    }
    #[inline(always)]
    unsafe fn or(self, rhs: Self) -> Self {
        self_from_op!(_kor_mask64, self, rhs)
    }
    #[inline(always)]
    unsafe fn xor(self, rhs: Self) -> Self {
        self_from_op!(_kxor_mask64, self, rhs)
    }
    #[inline(always)]
    unsafe fn not(self) -> Self {
        self_from_op!(_knot_mask64, self)
    }
    #[inline(always)]
    unsafe fn and_not(self, rhs: Self) -> Self {
        self_from_op!(_kandn_mask64, rhs, self)
    }
}

impl SimdGatherImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn gather_32_from_32<T, const B: i32>(self, ptr: *const T) -> Self {
        self_from_const_op!(_mm512_i32gather_epi32, B, self, ptr)
    }
    // #[inline(always)] unsafe fn gather_64_from_32<T, const B: i32>(self, ptr: *const T) -> Self { self_from_const_op!(_mm512_i32gather_epi64, B, ptr, self) }
    // #[inline(always)] unsafe fn gather_32_from_64<T, const B: i32>(self, ptr: *const T) -> Self { self_from_const_op!(_mm512_i64gather_epi32, B, ptr, self) }
    #[inline(always)]
    unsafe fn gather_64_from_64<T, const B: i32>(self, ptr: *const T) -> Self {
        self_from_const_op!(_mm512_i64gather_epi64, B, self, ptr)
    }
}

impl SimdSqrtImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn sqrt_f64(self) -> Self {
        self_from_op!(_mm512_sqrt_pd, self)
    }
    #[inline(always)]
    unsafe fn sqrt_f32(self) -> Self {
        self_from_op!(_mm512_sqrt_ps, self)
    }
    #[inline(always)]
    unsafe fn rsqrt_f32(self) -> Self {
        self_from_op!(_mm512_rsqrt14_ps, self)
    }
}

impl SimdAllBitsImpl for Avx512Mask {
    #[inline(always)]
    unsafe fn all_zero(self) -> bool {
        self.0 == 0
    }
}

impl SimdNegateImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn negate_f64(self) -> Self {
        unsafe { Self::splat_64(-0.0f64).xor(self) }
    }
    #[inline(always)]
    unsafe fn negate_f32(self) -> Self {
        unsafe { Self::splat_32(-0.0f64).xor(self) }
    }
}

impl SimdBlockShiftImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn block_left_byte_shift<const N: i32>(self) -> Self {
        self_from_const_op!(_mm512_bslli_epi128, N, self)
    }
    #[inline(always)]
    unsafe fn block_right_byte_shift<const N: i32>(self) -> Self {
        self_from_const_op!(_mm512_bsrli_epi128, N, self)
    }
}

impl SimdMaskBitConversion for Avx512Mask {
    #[inline(always)]
    unsafe fn to_bits_64(self) -> u64 {
        self.0
    }
    #[inline(always)]
    unsafe fn to_bits_32(self) -> u64 {
        self.0
    }
    #[inline(always)]
    unsafe fn to_bits_8(self) -> u64 {
        self.0
    }
    #[inline(always)]
    unsafe fn from_bits_64(bitmask: u64) -> Self {
        unsafe { transmute(bitmask) }
    }
    #[inline(always)]
    unsafe fn from_bits_32(bitmask: u64) -> Self {
        unsafe { transmute(bitmask) }
    }
    #[inline(always)]
    unsafe fn from_bits_16(bitmask: u64) -> Self {
        unsafe { transmute(bitmask) }
    }
    #[inline(always)]
    unsafe fn from_bits_8(bitmask: u64) -> Self {
        unsafe { transmute(bitmask) }
    }
}

impl SimdLaneShiftImpl for Avx512Reg {
    #[inline(always)]
    unsafe fn right_lane_shift_32(self, n: u32) -> Self {
        match n {
            0 => self,
            1 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 15) }),
            2 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 14) }),
            3 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 13) }),
            4 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 12) }),
            5 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 11) }),
            6 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 10) }),
            7 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 9) }),
            8 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 8) }),
            9 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 7) }),
            10 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 6) }),
            11 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 5) }),
            12 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 4) }),
            13 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 3) }),
            14 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 2) }),
            15 => Self(unsafe { _mm512_alignr_epi32(self.0, _mm512_setzero_si512(), 1) }),
            _ => unsafe { Self::zero() },
        }
    }

    #[inline(always)]
    unsafe fn left_lane_shift_32(self, n: u32) -> Self {
        match n {
            0 => self,
            1 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 1) }),
            2 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 2) }),
            3 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 3) }),
            4 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 4) }),
            5 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 5) }),
            6 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 6) }),
            7 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 7) }),
            8 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 8) }),
            9 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 9) }),
            10 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 10) }),
            11 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 11) }),
            12 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 12) }),
            13 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 13) }),
            14 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 14) }),
            15 => Self(unsafe { _mm512_alignr_epi32(_mm512_setzero_si512(), self.0, 15) }),
            _ => unsafe { Self::zero() },
        }
    }
}