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
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
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 SseReg(pub __m128i);
impl SimdArch for SseReg {}
impl MaskArch for SseReg {}

impl SimdAddImpl for SseReg {
    #[inline(always)]
    unsafe fn f64_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i64_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn i8_add(self, rhs: Self) -> Self {
        self_from_op!(_mm_add_epi8, self, rhs)
    }
}

impl SimdSubImpl for SseReg {
    #[inline(always)]
    unsafe fn f64_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i64_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn i8_sub(self, rhs: Self) -> Self {
        self_from_op!(_mm_sub_epi8, self, rhs)
    }
}

impl SimdMulImpl for SseReg {
    #[inline(always)]
    unsafe fn f64_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm_mul_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn f32_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm_mul_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn i32_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm_mullo_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn i16_mul(self, rhs: Self) -> Self {
        self_from_op!(_mm_mullo_epi16, self, rhs)
    }
}

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

impl SimdBitwiseImpl for SseReg {
    #[inline(always)]
    unsafe fn and(self, rhs: Self) -> Self {
        self_from_op!(_mm_and_si128, self, rhs)
    }
    #[inline(always)]
    unsafe fn or(self, rhs: Self) -> Self {
        self_from_op!(_mm_or_si128, self, rhs)
    }
    #[inline(always)]
    unsafe fn xor(self, rhs: Self) -> Self {
        self_from_op!(_mm_xor_si128, 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!(_mm_andnot_si128, rhs, self)
    }
}

macro_rules! scalar_shift {
    ($self:expr, $rhs:expr, $lanes:expr, $elem:ty, $uelem:ty, $op:tt) => {{
        let mut a = [<$elem>::default(); $lanes];
        let mut b = [<$elem>::default(); $lanes];
        _mm_storeu_si128(a.as_mut_ptr() as *mut __m128i, $self.0);
        _mm_storeu_si128(b.as_mut_ptr() as *mut __m128i, $rhs.0);
        let width = (std::mem::size_of::<$elem>() * 8) as u32;
        let mut out = [<$elem>::default(); $lanes];
        for i in 0..$lanes {
            let shift = b[i] as $uelem as u32;
            out[i] = scalar_shift!(@apply $op, a[i], shift, width, $elem, $uelem);
        }
        Self(_mm_loadu_si128(out.as_ptr() as *const __m128i))
    }};
    (@apply sll, $val:expr, $shift:expr, $width:expr, $elem:ty, $uelem:ty) => {
        if $shift >= $width { 0 } else { (($val as $uelem) << $shift) as $elem }
    };
    (@apply srl, $val:expr, $shift:expr, $width:expr, $elem:ty, $uelem:ty) => {
        if $shift >= $width { 0 } else { (($val as $uelem) >> $shift) as $elem }
    };
    (@apply sra, $val:expr, $shift:expr, $width:expr, $elem:ty, $uelem:ty) => {
        $val >> $shift.min($width - 1)
    };
}

impl SimdShiftImpl for SseReg {
    #[inline(always)]
    unsafe fn sllv_64(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 2, i64, u64, sll) }
    }
    #[inline(always)]
    unsafe fn srlv_64(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 2, i64, u64, srl) }
    }
    #[inline(always)]
    unsafe fn srav_64(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 2, i64, u64, sra) }
    }
    #[inline(always)]
    unsafe fn sllv_32(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 4, i32, u32, sll) }
    }
    #[inline(always)]
    unsafe fn srlv_32(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 4, i32, u32, srl) }
    }
    #[inline(always)]
    unsafe fn srav_32(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 4, i32, u32, sra) }
    }
    #[inline(always)]
    unsafe fn sllv_16(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 8, i16, u16, sll) }
    }
    #[inline(always)]
    unsafe fn srlv_16(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 8, i16, u16, srl) }
    }
    #[inline(always)]
    unsafe fn srav_16(self, rhs: Self) -> Self {
        unsafe { scalar_shift!(self, rhs, 8, i16, u16, sra) }
    }
}
impl SimdLoadImpl for SseReg {
    type MaskType = Self;

    #[inline(always)]
    unsafe fn load_aligned<T>(ptr: *const T) -> Self {
        self_from_op!(_mm_load_si128, ptr)
    }
    #[inline(always)]
    unsafe fn load_unaligned<T>(ptr: *const T) -> Self {
        self_from_op!(_mm_loadu_si128, ptr)
    }

    #[inline(always)]
    unsafe fn masked_load_64<T>(ptr: *const T, mask: Self::MaskType) -> Self {
        unsafe {
            let mut m = [0i64; 2];
            _mm_storeu_si128(m.as_mut_ptr() as *mut __m128i, mask.0);
            let src = ptr as *const i64;
            let mut out = [0i64; 2];
            for i in 0..2 {
                // AVX2 semantics: only the top bit of each lane's mask element is checked
                if (m[i] as u64) & (1 << 63) != 0 {
                    out[i] = *src.add(i);
                }
            }
            Self(_mm_loadu_si128(out.as_ptr() as *const __m128i))
        }
    }

    #[inline(always)]
    unsafe fn masked_load_32<T>(ptr: *const T, mask: Self::MaskType) -> Self {
        unsafe {
            let mut m = [0i32; 4];
            _mm_storeu_si128(m.as_mut_ptr() as *mut __m128i, mask.0);
            let src = ptr as *const i32;
            let mut out = [0i32; 4];
            for i in 0..4 {
                if (m[i] as u32) & (1 << 31) != 0 {
                    out[i] = *src.add(i);
                }
            }
            Self(_mm_loadu_si128(out.as_ptr() as *const __m128i))
        }
    }
}

impl SimdStoreImpl for SseReg {
    type MaskType = Self;

    #[inline(always)]
    unsafe fn store_aligned<T>(self, ptr: *mut T) {
        execute_intrinsic!(_mm_store_si128, ptr, self);
    }
    #[inline(always)]
    unsafe fn store_unaligned<T>(self, ptr: *mut T) {
        execute_intrinsic!(_mm_storeu_si128, ptr, self);
    }

    #[inline(always)]
    unsafe fn masked_store_64<T>(self, ptr: *mut T, mask: Self::MaskType) {
        unsafe {
            let mut m = [0i64; 2];
            let mut v = [0i64; 2];
            _mm_storeu_si128(m.as_mut_ptr() as *mut __m128i, mask.0);
            _mm_storeu_si128(v.as_mut_ptr() as *mut __m128i, self.0);
            let dst = ptr as *mut i64;
            for i in 0..2 {
                if (m[i] as u64) & (1 << 63) != 0 {
                    *dst.add(i) = v[i];
                }
            }
        }
    }

    #[inline(always)]
    unsafe fn masked_store_32<T>(self, ptr: *mut T, mask: Self::MaskType) {
        unsafe {
            let mut m = [0i32; 4];
            let mut v = [0i32; 4];
            _mm_storeu_si128(m.as_mut_ptr() as *mut __m128i, mask.0);
            _mm_storeu_si128(v.as_mut_ptr() as *mut __m128i, self.0);
            let dst = ptr as *mut i32;
            for i in 0..4 {
                if (m[i] as u32) & (1 << 31) != 0 {
                    *dst.add(i) = v[i];
                }
            }
        }
    }
}

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

impl SimdFloatCastsImpl for SseReg {
    #[inline(always)]
    unsafe fn float_to_int_trunc(self) -> Self {
        self_from_op!(_mm_cvttps_epi32, self)
    }
    #[inline(always)]
    unsafe fn float_to_int_round(self) -> Self {
        self_from_op!(_mm_cvtps_epi32, self)
    }
}

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

impl SimdPermuteImpl for SseReg {
    #[inline(always)]
    unsafe fn permute_32(self, rhs: Self) -> Self {
        unsafe {
            let mut a = [0.0f32; 4];
            let mut idx = [0i32; 4];
            _mm_storeu_ps(a.as_mut_ptr(), std::mem::transmute(self.0));
            _mm_storeu_si128(idx.as_mut_ptr() as *mut __m128i, rhs.0);
            let mut out = [0.0f32; 4];
            for i in 0..4 {
                out[i] = a[(idx[i] & 0b11) as usize]; // low 2 bits select lane, same as hardware
            }
            Self(std::mem::transmute(_mm_loadu_ps(out.as_ptr())))
        }
    }

    #[inline(always)]
    unsafe fn permute_8(self, rhs: Self) -> Self {
        self_from_op!(_mm_shuffle_epi8, self, rhs)
    }
}
impl SimdVariableBlendImpl for SseReg {
    type VecType = Self;
    #[inline(always)]
    unsafe fn vblend_64(self, true_values: Self::VecType, false_values: Self::VecType) -> Self {
        self_from_op!(_mm_blendv_pd, false_values, true_values, self)
    }
    #[inline(always)]
    unsafe fn vblend_32(self, true_values: Self::VecType, false_values: Self::VecType) -> Self {
        self_from_op!(_mm_blendv_ps, false_values, true_values, self)
    }
    #[inline(always)]
    unsafe fn vblend_8(self, true_values: Self::VecType, false_values: Self::VecType) -> Self {
        self_from_op!(_mm_blendv_epi8, false_values, true_values, self)
    }
}

impl SimdImmediateBlendImpl for SseReg {
    #[inline(always)]
    unsafe fn blend_64<const N: i32>(self, false_values: Self) -> Self {
        self_from_const_op!(_mm_blend_pd, N, false_values, self)
    }
    #[inline(always)]
    unsafe fn blend_32<const N: i32>(self, false_values: Self) -> Self {
        self_from_const_op!(_mm_blend_ps, N, false_values, self)
    }
}

impl SimdMulAddImpl for SseReg {
    #[inline(always)]
    unsafe fn mul_add_f64(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm_add_pd, self_from_op!(_mm_mul_pd, self, mult), add)
    }
    #[inline(always)]
    unsafe fn mul_sub_f64(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm_sub_pd, self_from_op!(_mm_mul_pd, self, mult), sub)
    }
    #[inline(always)]
    unsafe fn negated_mul_add_f64(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm_sub_pd, add, self_from_op!(_mm_mul_pd, self, mult))
    }
    #[inline(always)]
    unsafe fn negated_mul_sub_f64(self, mult: Self, sub: Self) -> Self {
        let neg = self_from_op!(_mm_mul_pd, self, mult);
        self_from_op!(
            _mm_sub_pd,
            self_from_op!(_mm_xor_pd, neg, Self::splat_64(-0.0f64).0),
            sub
        )
    }

    #[inline(always)]
    unsafe fn mul_add_f32(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm_add_ps, self_from_op!(_mm_mul_ps, self, mult), add)
    }
    #[inline(always)]
    unsafe fn mul_sub_f32(self, mult: Self, sub: Self) -> Self {
        self_from_op!(_mm_sub_ps, self_from_op!(_mm_mul_ps, self, mult), sub)
    }
    #[inline(always)]
    unsafe fn negated_mul_add_f32(self, mult: Self, add: Self) -> Self {
        self_from_op!(_mm_sub_ps, add, self_from_op!(_mm_mul_ps, self, mult))
    }
    #[inline(always)]
    unsafe fn negated_mul_sub_f32(self, mult: Self, sub: Self) -> Self {
        let neg = self_from_op!(_mm_mul_ps, self, mult);
        self_from_op!(
            _mm_sub_ps,
            self_from_op!(_mm_xor_ps, neg, Self::splat_32(-0.0f32).0),
            sub
        )
    }
}

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

impl SimdPartialOrdImpl for SseReg {
    type MaskType = Self;
    #[inline(always)]
    unsafe fn cmp_f64_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f64_lt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmplt_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f64_le(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmple_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f64_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f64_ge(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpge_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f64_neq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpneq_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_lt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmplt_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_le(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmple_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_ge(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpge_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_f32_neq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpneq_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i64_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i64_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_epi64, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i32_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i32_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i16_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i16_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i8_eq(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpeq_epi8, self, rhs)
    }
    #[inline(always)]
    unsafe fn cmp_i8_gt(self, rhs: Self) -> Self {
        self_from_op!(_mm_cmpgt_epi8, self, rhs)
    }

    #[inline(always)]
    unsafe fn max_f64(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_f64(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_pd, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_f32(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_f32(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_ps, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i32(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i32(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epi32, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i16(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i16(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epi16, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_i8(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epi8, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_i8(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epi8, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u32(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epu32, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u32(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epu32, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u16(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epu16, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u16(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epu16, self, rhs)
    }
    #[inline(always)]
    unsafe fn max_u8(self, rhs: Self) -> Self {
        self_from_op!(_mm_max_epu8, self, rhs)
    }
    #[inline(always)]
    unsafe fn min_u8(self, rhs: Self) -> Self {
        self_from_op!(_mm_min_epu8, self, rhs)
    }
}

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

impl SimdGatherImpl for SseReg {
    #[inline(always)]
    unsafe fn gather_32_from_32<T, const B: i32>(self, ptr: *const T) -> Self {
        unsafe {
            let mut idx = [0i32; 4];
            _mm_storeu_si128(idx.as_mut_ptr() as *mut __m128i, self.0);
            let base = ptr as *const u8;
            let mut out = [0i32; 4];
            for i in 0..4 {
                let byte_offset = (idx[i] as isize) * (B as isize);
                out[i] = *(base.offset(byte_offset) as *const i32);
            }
            Self(_mm_loadu_si128(out.as_ptr() as *const __m128i))
        }
    }

    #[inline(always)]
    unsafe fn gather_64_from_64<T, const B: i32>(self, ptr: *const T) -> Self {
        unsafe {
            let mut idx = [0i64; 2];
            _mm_storeu_si128(idx.as_mut_ptr() as *mut __m128i, self.0);
            let base = ptr as *const u8;
            let mut out = [0i64; 2];
            for i in 0..2 {
                let byte_offset = (idx[i] as isize) * (B as isize);
                out[i] = *(base.offset(byte_offset) as *const i64);
            }
            Self(_mm_loadu_si128(out.as_ptr() as *const __m128i))
        }
    }
}

impl SimdSqrtImpl for SseReg {
    #[inline(always)]
    unsafe fn sqrt_f64(self) -> Self {
        self_from_op!(_mm_sqrt_pd, self)
    }
    #[inline(always)]
    unsafe fn sqrt_f32(self) -> Self {
        self_from_op!(_mm_sqrt_ps, self)
    }
    #[inline(always)]
    unsafe fn rsqrt_f32(self) -> Self {
        self_from_op!(_mm_rsqrt_ps, self)
    }
}

impl SimdAllBitsImpl for SseReg {
    #[inline(always)]
    unsafe fn all_zero(self) -> bool {
        execute_intrinsic!(_mm_testz_si128, self, self) != 0
    }
}

impl SimdNegateImpl for SseReg {
    #[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) }
    }
}

// TODO: THIS IS BACKWARDS COMPARED TO NON-BLOCKED, figure out why and fix.
impl SimdBlockShiftImpl for SseReg {
    #[inline(always)]
    unsafe fn block_left_byte_shift<const N: i32>(self) -> Self {
        self_from_const_op!(_mm_bslli_si128, N, self)
    }
    #[inline(always)]
    unsafe fn block_right_byte_shift<const N: i32>(self) -> Self {
        self_from_const_op!(_mm_bsrli_si128, N, self)
    }
}

impl SimdMaskBitConversion for SseReg {
    #[inline(always)]
    unsafe fn to_bits_64(self) -> u64 {
        execute_intrinsic!(_mm_movemask_pd, self) as u64
    }
    #[inline(always)]
    unsafe fn to_bits_32(self) -> u64 {
        execute_intrinsic!(_mm_movemask_ps, self) as u64
    }
    #[inline(always)]
    unsafe fn to_bits_8(self) -> u64 {
        execute_intrinsic!(_mm_movemask_epi8, self) as u64
    }
    #[inline(always)]
    unsafe fn from_bits_64(bitmask: u64) -> Self {
        let mask = self_from_op!(_mm_set_epi64x, 1, 2);
        let bits = self_from_op!(_mm_set1_epi64x, bitmask as i64);
        self_from_op!(_mm_cmpgt_epi64, bits.and(mask), Self::zero()) //TODO: only sse4.2?
    }
    #[inline(always)]
    unsafe fn from_bits_32(bitmask: u64) -> Self {
        let mask = self_from_op!(_mm_set_epi32, 1, 2, 4, 8);
        let bits = self_from_op!(_mm_set1_epi32, bitmask as u32);
        self_from_op!(_mm_cmpgt_epi32, bits.and(mask), Self::zero())
    }
    #[inline(always)]
    unsafe fn from_bits_16(bitmask: u64) -> Self {
        #[rustfmt::skip]
        let mask = self_from_op!(_mm_set_epi16, 1, 2, 4, 8, 16, 32, 64, 128);
        let bits = self_from_op!(_mm_set1_epi16, bitmask as i16);
        unsafe { self_from_op!(_mm_cmpeq_epi16, bits.and(mask), Self::zero()).not() }
    }
    #[inline(always)]
    unsafe fn from_bits_8(bitmask: u64) -> Self {
        let b1 = bitmask as i8;
        let b2 = (bitmask >> 8) as i8;
        #[rustfmt::skip]
        let mask = self_from_op!(_mm_set_epi8,
            1, 2, 4, 8, 16, 32, 64, -128,
            1, 2, 4, 8, 16, 32, 64, -128
        );
        #[rustfmt::skip]
        let bits = self_from_op!(_mm_set_epi8,
            b1, b1, b1, b1, b1, b1, b1, b1,
            b2, b2, b2, b2, b2, b2, b2, b2
        );
        unsafe { self_from_op!(_mm_cmpeq_epi8, bits.and(mask), Self::zero()).not() }
    }
}

impl SimdLaneShiftImpl for SseReg {
    #[inline(always)]
    unsafe fn left_lane_shift_32(self, n: u32) -> Self {
        match n {
            0 => self,
            1 => self_from_const_op!(_mm_bsrli_si128, 4, self),
            2 => self_from_const_op!(_mm_bsrli_si128, 8, self),
            3 => self_from_const_op!(_mm_bsrli_si128, 12, self),
            _ => unsafe { Self::zero() },
        }
    }
    #[inline(always)]
    unsafe fn right_lane_shift_32(self, n: u32) -> Self {
        match n {
            0 => self,
            1 => self_from_const_op!(_mm_bslli_si128, 4, self),
            2 => self_from_const_op!(_mm_bslli_si128, 8, self),
            3 => self_from_const_op!(_mm_bslli_si128, 12, self),
            _ => unsafe { Self::zero() },
        }
    }
}