thermite 0.2.1

High-performance, generic, ISA-portable SIMD library with a policy-configurable transcendental math library
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
762
763
#[rustfmt::skip]
macro_rules! decl_binary_ops {
    ($kind:ident $(: $unary:ident)?; $($trait_name:ident::$method_name:ident),*) => {paste::paste! {
        #[doc = "Combination of masked binary operation traits for " $kind " operations."]
        pub trait [<Masked $kind Ops>]<Mask, Rhs>: $($unary<Mask> +)? $([<$trait_name Masked>]<Mask, Rhs> + )* {}

        #[doc = "Combination of masked assignment binary operation traits for " $kind " operations."]
        pub trait [<AssignMasked $kind Ops>]<Mask, Rhs>: $([<$trait_name AssignMasked>]<Mask, Rhs> + )* {}

        impl<V, Mask, Rhs> [<Masked $kind Ops>]<Mask, Rhs> for V
        where
            $(V: $unary<Mask>,)?
            V: $([<$trait_name Masked>]<Mask, Rhs> + )*
        {}

        impl<V, Mask, Rhs> [<AssignMasked $kind Ops>]<Mask, Rhs> for V
        where
            $(V: [<$trait_name AssignMasked>]<Mask, Rhs>),*
        {}

        $(
            #[doc = "Masked variants of the [`" $trait_name "`] trait."]
            pub trait [<$trait_name Masked>]<Mask, Rhs = Self>: $trait_name<Rhs> {
                #[doc = "Computes [`" $trait_name "`] with `rhs` where `mask` is true."]
                fn [<$method_name _c>](self, mask: Mask, rhs: Rhs) -> Self::Output;

                #[doc = "Merges [`" $trait_name "`] with `src` using `mask`, returning `src` where mask is false."]
                fn [<$method_name _m>](self, src: Self, mask: Mask, rhs: Rhs) -> Self::Output;

                #[doc = "Computes [`" $trait_name "`] masked (zeroed where mask is false)."]
                fn [<$method_name _z>](self, mask: Mask, rhs: Rhs) -> Self::Output;
            }

            #[doc = "Masked assignment variants of the [`" $trait_name "`] trait."]
            pub trait [<$trait_name AssignMasked>]<Mask, Rhs = Self>: [<$trait_name Assign>]<Rhs> {
                #[doc = "Computes [`" $trait_name "Assign`] with `rhs` where `mask` is true."]
                fn [<$method_name _assign_c>](&mut self, mask: Mask, rhs: Rhs);

                #[doc = "Merges [`" $trait_name "Assign`] with `src` using `mask`, assigning `src` where mask is false."]
                fn [<$method_name _assign_m>](&mut self, src: Self, mask: Mask, rhs: Rhs);

                #[doc = "Computes [`" $trait_name "Assign`] masked (zeroed where mask is false)."]
                fn [<$method_name _assign_z>](&mut self, mask: Mask, rhs: Rhs);
            }
        )*
    }};
}

macro_rules! decl_unary_ops {
    ($($trait_name:ident::$method_name:ident),*) => {paste::paste! {$(
        #[doc = "Masked variants of the [`" $trait_name "`] trait."]
        pub trait [<$trait_name Masked>]<Mask>: $trait_name {
            #[doc = "Computes [`" $trait_name "`] where `mask` is true, does nothing where false."]
            fn [<$method_name _c>](self, mask: Mask) -> Self::Output;
            #[doc = "Merges [`" $trait_name "`] with `src` using `mask`, returning `src` where mask is false."]
            fn [<$method_name _m>](self, src: Self, mask: Mask) -> Self::Output;
            #[doc = "Computes [`" $trait_name "`] masked (zeroed where mask is false)."]
            fn [<$method_name _z>](self, mask: Mask) -> Self::Output;
        }
    )*}};
}

macro_rules! impl_binary_op {
    ($trait_name:ident::$method_name:ident for $reg:ident, $rhs:ty) => {
        paste::paste! {
            impl<R: $reg + Register> $trait_name<$rhs> for Vector<R> {
                type Output = Self;

                #[inline(always)]
                fn $method_name(self, rhs: $rhs) -> Self::Output {
                    Vector(R::$method_name(self.0, rhs.0))
                }
            }

            impl<R: $reg + Register> [<$trait_name Masked>]<Mask<R>, $rhs> for Vector<R> {
                #[inline(always)]
                fn [<$method_name _c>](self, mask: Mask<R>, rhs: $rhs) -> Self::Output {
                    Vector(R::[<$method_name _c>](mask.0, self.0, rhs.0))
                }

                #[inline(always)]
                fn [<$method_name _m>](self, src: Self, mask: Mask<R>, rhs: $rhs) -> Self::Output {
                    Vector(R::[<$method_name _m>](src.0, mask.0, self.0, rhs.0))
                }

                #[inline(always)]
                fn [<$method_name _z>](self, mask: Mask<R>, rhs: $rhs) -> Self::Output {
                    Vector(R::[<$method_name _z>](mask.0, self.0, rhs.0))
                }
            }

            impl<R: $reg + Register> [<$trait_name Assign>]<$rhs> for Vector<R> {
                #[inline(always)]
                fn [<$method_name _assign>](&mut self, rhs: $rhs) {
                    self.0 = R::$method_name(self.0, rhs.0);
                }
            }

            impl<R: $reg + Register> [<$trait_name AssignMasked>]<Mask<R>, $rhs> for Vector<R> {
                #[inline(always)]
                fn [<$method_name _assign_c>](&mut self, mask: Mask<R>, rhs: $rhs) {
                    self.0 = R::[<$method_name _c>](mask.0, self.0, rhs.0);
                }

                #[inline(always)]
                fn [<$method_name _assign_m>](&mut self, src: Self, mask: Mask<R>, rhs: $rhs) {
                    self.0 = R::[<$method_name _m>](src.0, mask.0, self.0, rhs.0);
                }

                #[inline(always)]
                fn [<$method_name _assign_z>](&mut self, mask: Mask<R>, rhs: $rhs) {
                    self.0 = R::[<$method_name _z>](mask.0, self.0, rhs.0);
                }
            }
        }
    };
}

use core::ops::{
    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign,
    Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};

use crate::{
    Mask, Vector,
    register::{BitshiftRegister, BitwiseRegister, FloatRegister, NumericRegister, Register, SignedRegister},
};

/// Trait for squaring a value: `self * self`
///
/// Some types are able to provide optimized implementations of squaring that are
/// faster or more accurate than a simple multiplication with itself.
pub trait Square {
    /// The squared value. Not always `Self`: a type may widen to hold the product.
    type Output;

    /// Computes `self * self`.
    fn square(self) -> Self::Output;
}

decl_binary_ops!(Num;
    Add::add,
    Sub::sub,
    Mul::mul,
    Div::div,
    Rem::rem
);

impl_binary_op!(Add::add for NumericRegister, Self);
impl_binary_op!(Sub::sub for NumericRegister, Self);
impl_binary_op!(Mul::mul for NumericRegister, Self);
impl_binary_op!(Div::div for NumericRegister, Self);
impl_binary_op!(Rem::rem for NumericRegister, Self);

/// Trait for the bitwise AND NOT operation: `self & !rhs`
pub trait BitAndNot<Rhs = Self> {
    /// The result of the AND NOT.
    type Output;

    /// Computes `self & !rhs`, one instruction on every SIMD backend.
    ///
    /// The **second** operand is the negated one here.
    #[must_use]
    fn bitandnot(self, rhs: Rhs) -> Self::Output;
}

/// Trait for the bitwise AND NOT assignment operation: `self &= !rhs`
pub trait BitAndNotAssign<Rhs = Self> {
    /// Assigns `self & !rhs` into `self`.
    fn bitandnot_assign(&mut self, rhs: Rhs);
}

decl_binary_ops!(Bitwise;
    BitAnd::bitand,
    BitAndNot::bitandnot,
    BitOr::bitor,
    BitXor::bitxor
);

impl_binary_op!(BitAnd::bitand for BitwiseRegister, Self);
impl_binary_op!(BitOr::bitor for BitwiseRegister, Self);
impl_binary_op!(BitXor::bitxor for BitwiseRegister, Self);

decl_binary_ops!(Bitshift;
    Shl::shl,
    Shr::shr
);

decl_unary_ops!(Not::not, Neg::neg, Square::square);

impl<R: BitwiseRegister + Register> Not for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn not(self) -> Self::Output {
        Vector(R::not(self.0))
    }
}

impl<R: BitwiseRegister + Register> NotMasked<Mask<R>> for Vector<R> {
    #[inline(always)]
    fn not_c(self, mask: Mask<R>) -> Self::Output {
        Vector(R::not_c(mask.0, self.0))
    }

    #[inline(always)]
    fn not_m(self, src: Self, mask: Mask<R>) -> Self::Output {
        Vector(R::not_m(src.0, mask.0, self.0))
    }

    #[inline(always)]
    fn not_z(self, mask: Mask<R>) -> Self::Output {
        Vector(R::not_z(mask.0, self.0))
    }
}

impl<R: SignedRegister> Neg for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn neg(self) -> Self::Output {
        Vector(R::neg(self.0))
    }
}

impl<R: SignedRegister> NegMasked<Mask<R>> for Vector<R> {
    #[inline(always)]
    fn neg_c(self, mask: Mask<R>) -> Self::Output {
        Vector(R::neg_c(mask.0, self.0))
    }

    #[inline(always)]
    fn neg_m(self, src: Self, mask: Mask<R>) -> Self::Output {
        Vector(R::neg_m(src.0, mask.0, self.0))
    }

    #[inline(always)]
    fn neg_z(self, mask: Mask<R>) -> Self::Output {
        Vector(R::neg_z(mask.0, self.0))
    }
}

// NOTE: BitAndNot is unique in that the BitwiseRegister trait expects !lhs & rhs,
// but since we want lhs & !rhs, the order of parameters is reversed here.
impl<R: BitwiseRegister + Register> BitAndNot<Self> for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn bitandnot(self, rhs: Self) -> Self::Output {
        Vector(R::bitandnot(rhs.0, self.0))
    }
}

impl<R: BitwiseRegister + Register> BitAndNotMasked<Mask<R>, Self> for Vector<R> {
    #[inline(always)]
    fn bitandnot_c(self, mask: Mask<R>, rhs: Self) -> Self::Output {
        Vector(R::bitandnot_c(mask.0, rhs.0, self.0))
    }

    #[inline(always)]
    fn bitandnot_m(self, src: Self, mask: Mask<R>, rhs: Self) -> Self::Output {
        Vector(R::bitandnot_m(src.0, mask.0, rhs.0, self.0))
    }

    #[inline(always)]
    fn bitandnot_z(self, mask: Mask<R>, rhs: Self) -> Self::Output {
        Vector(R::bitandnot_z(mask.0, rhs.0, self.0))
    }
}

impl<R: BitwiseRegister + Register> BitAndNotAssign<Self> for Vector<R> {
    #[inline(always)]
    fn bitandnot_assign(&mut self, rhs: Self) {
        self.0 = R::bitandnot(rhs.0, self.0);
    }
}

impl<R: BitwiseRegister + Register> BitAndNotAssignMasked<Mask<R>, Self> for Vector<R> {
    #[inline(always)]
    fn bitandnot_assign_c(&mut self, mask: Mask<R>, rhs: Self) {
        self.0 = R::bitandnot_c(mask.0, rhs.0, self.0);
    }

    #[inline(always)]
    fn bitandnot_assign_m(&mut self, src: Self, mask: Mask<R>, rhs: Self) {
        self.0 = R::bitandnot_m(src.0, mask.0, rhs.0, self.0);
    }

    #[inline(always)]
    fn bitandnot_assign_z(&mut self, mask: Mask<R>, rhs: Self) {
        self.0 = R::bitandnot_z(mask.0, rhs.0, self.0);
    }
}

macro_rules! mul_add_ext {
    ($($(#[$meta:meta])* $name:ident),*) => {paste::paste! {
        /// Trait for fused multiply-add operations. This contains all variants of
        /// fused multiply-add, including guaranteed FMA and maybe FMA versions.
        ///
        /// If the platform does _not_ have native FMA support, the guaranteed FMA
        /// will either use a fast compensated arithmetic algorithm, or fall back to
        /// slow scalar evaluation to ensure correctness.
        ///
        /// This trait is superior to `num_traits::MulAdd`, but does not include
        /// it as a supertrait due to the potential for multiple
        /// conflicting implementation warnings.
        pub trait MulAddExt<A = Self, B = Self> {
            /// The result of the fused operation.
            type Output;

            /// Indicates whether the implementation uses true fused-multiply-add instructions.
            ///
            /// Non-`e` variants will always be accurate, regardless of this flag, but the `e` variants
            /// will fallback to separate multiply and add operations if this is false.
            const HAS_TRUE_FMA: bool;

            $(
                $(#[$meta])*
                fn $name(self, a: A, b: B) -> Self::Output;
            )*
        }

        impl<R: FloatRegister> MulAddExt<Self, Self> for Vector<R> {
            type Output = Self;

            const HAS_TRUE_FMA: bool = R::HAS_TRUE_FMA;

            $(#[inline(always)] fn $name(self, a: Self, b: Self) -> Self::Output { Vector(R::$name(self.0, a.0, b.0)) } )*
        }

        /// Provides assignment variants of the fused multiply-add operations from [`MulAddExt`].
        ///
        /// Unlike regular assignment traits, this does require `MulAddExt` as a supertrait,
        /// so we can access the associated `HAS_TRUE_FMA` constant.
        pub trait MulAddAssignExt<A = Self, B = Self>: MulAddExt<A, B> {
            $(
                $(#[$meta])*
                fn [<$name _assign>](&mut self, a: A, b: B);
            )*
        }

        impl<R: FloatRegister> MulAddAssignExt<Self, Self> for Vector<R> {
            $(#[inline(always)] fn [<$name _assign>](&mut self, a: Self, b: Self) { self.0 = R::$name(self.0, a.0, b.0); } )*
        }

        /// Provides masked variants of the fused multiply-add operations from [`MulAddExt`].
        pub trait MulAddExtMasked<Mask, A = Self, B = Self>: MulAddExt<A, B> {
            $(
                $(#[$meta])*
                #[doc = "\n\nThis variant computes [`" $name "`](MulAddExt::" $name ") with `a` and `b` where `mask` is true."]
                fn [<$name _c>](self, mask: Mask, a: A, b: B) -> Self::Output;

                $(#[$meta])*
                #[doc = "\n\nThis variant merges [`" $name "`](MulAddExt::" $name ") with `src` using `mask`, returning `src` where mask is false."]
                fn [<$name _m>](self, src: Self, mask: Mask, a: A, b: B) -> Self::Output;

                $(#[$meta])*
                #[doc = "\n\nThis variant computes [`" $name "`](MulAddExt::" $name ") masked (zeroed where mask is false)."]
                fn [<$name _z>](self, mask: Mask, a: A, b: B) -> Self::Output;
            )*
        }

        impl<R: FloatRegister> MulAddExtMasked<Mask<R>, Self, Self> for Vector<R> {
            $(
                #[inline(always)] fn [<$name _c>](self, mask: Mask<R>, a: Self, b: Self) -> Self::Output { Vector(R::[<$name _c>](mask.0, self.0, a.0, b.0)) }
                #[inline(always)] fn [<$name _m>](self, src: Self, mask: Mask<R>, a: Self, b: Self) -> Self::Output { Vector(R::[<$name _m>](src.0, mask.0, self.0, a.0, b.0)) }
                #[inline(always)] fn [<$name _z>](self, mask: Mask<R>, a: Self, b: Self) -> Self::Output { Vector(R::[<$name _z>](mask.0, self.0, a.0, b.0)) }
            )*
        }

        /// Provides masked assignment variants of the fused multiply-add operations from [`MulAddExt`].
        pub trait MulAddAssignExtMasked<Mask, A = Self, B = Self>: MulAddAssignExt<A, B> {
            $(
                $(#[$meta])*
                #[doc = "\n\nThis variant computes [`" $name "_assign`](MulAddAssignExt::" $name "_assign) with `a` and `b` where `mask` is true."]
                fn [<$name _assign_c>](&mut self, mask: Mask, a: A, b: B);

                $(#[$meta])*
                #[doc = "\n\nThis variant merges [`" $name "_assign`](MulAddAssignExt::" $name "_assign) with `src` using `mask`, assigning `src` where mask is false."]
                fn [<$name _assign_m>](&mut self, src: Self, mask: Mask, a: A, b: B);

                $(#[$meta])*
                #[doc = "\n\nThis variant computes [`" $name "_assign`](MulAddAssignExt::" $name "_assign) masked (zeroed where mask is false)."]
                fn [<$name _assign_z>](&mut self, mask: Mask, a: A, b: B);
            )*
        }

        impl<R: FloatRegister> MulAddAssignExtMasked<Mask<R>, Self, Self> for Vector<R> {
            $(
                #[inline(always)] fn [<$name _assign_c>](&mut self, mask: Mask<R>, a: Self, b: Self) { self.0 = R::[<$name _c>](mask.0, self.0, a.0, b.0); }
                #[inline(always)] fn [<$name _assign_m>](&mut self, src: Self, mask: Mask<R>, a: Self, b: Self) { self.0 = R::[<$name _m>](src.0, mask.0, self.0, a.0, b.0); }
                #[inline(always)] fn [<$name _assign_z>](&mut self, mask: Mask<R>, a: Self, b: Self) { self.0 = R::[<$name _z>](mask.0, self.0, a.0, b.0); }
            )*
        }
    }};
}

mul_add_ext! {
    /// Guaranteed fused-multiply-add operation.
    ///
    /// If the target architecture does not support native FMA, this will use
    /// either compensated arithmetic or slow scalar evaluation to ensure correctness.
    mul_add,

    /// Guaranteed fused-multiply-subtract operation.
    ///
    /// If the target architecture does not support native FMA, this will use
    /// either compensated arithmetic or slow scalar evaluation to ensure correctness.
    mul_sub,

    /// Guaranteed fused-negated-multiply-add operation.
    ///
    /// If the target architecture does not support native FMA, this will use
    /// either compensated arithmetic or slow scalar evaluation to ensure correctness.
    nmul_add,

    /// Guaranteed fused-negated-multiply-subtract operation.
    ///
    /// If the target architecture does not support native FMA, this will use
    /// either compensated arithmetic or slow scalar evaluation to ensure correctness.
    nmul_sub,

    /// Fused-multiply-add operation where possible. May gracefully degrade to separate multiply and add
    /// if the target architecture does not support native FMA.
    mul_adde,

    /// Fused-multiply-subtract operation where possible. May gracefully degrade to separate multiply and subtract
    /// if the target architecture does not support native FMA.
    mul_sube,

    /// Fused-negated-multiply-add operation where possible. May gracefully degrade to separate multiply and add
    /// if the target architecture does not support native FMA.
    nmul_adde,

    /// Fused-negated-multiply-subtract operation where possible. May gracefully degrade to separate multiply and subtract
    /// if the target architecture does not support native FMA.
    nmul_sube
}

/// Lane-alternating add/subtract operations for interleaved data (e.g. complex
/// numbers packed as `[re, im, re, im, ...]`).
///
/// All three follow the x86 `ADDSUB`/`FMADDSUB` convention: **even lanes
/// subtract, odd lanes add** (`fmsubadd` is the opposite parity). See
/// [`FloatRegister::addsub`] for the
/// interleaved complex-multiply lowering these are built for.
pub trait AddSubExt: Sized {
    /// The result of the lane-alternating operation.
    type Output;

    /// `[a0 - b0, a1 + b1, a2 - b2, ...]` - even lanes subtract, odd lanes add.
    fn addsub(self, b: Self) -> Self::Output;

    /// `[a0*b0 - c0, a1*b1 + c1, ...]` - fused multiply then [`addsub`](Self::addsub).
    fn fmaddsub(self, b: Self, c: Self) -> Self::Output;

    /// `[a0*b0 + c0, a1*b1 - c1, ...]` - fused multiply then subadd (opposite parity).
    fn fmsubadd(self, b: Self, c: Self) -> Self::Output;
}

impl<R: FloatRegister> AddSubExt for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn addsub(self, b: Self) -> Self {
        Vector(R::addsub(self.0, b.0))
    }

    #[inline(always)]
    fn fmaddsub(self, b: Self, c: Self) -> Self {
        Vector(R::fmaddsub(self.0, b.0, c.0))
    }

    #[inline(always)]
    fn fmsubadd(self, b: Self, c: Self) -> Self {
        Vector(R::fmsubadd(self.0, b.0, c.0))
    }
}

/// Masked variants of [`AddSubExt`] (mask-first argument order, matching the rest
/// of Thermite's `_c`/`_m`/`_z` surface).
pub trait AddSubExtMasked<Mask>: AddSubExt {
    /// [`addsub`](AddSubExt::addsub) where `mask` is true, else `self`.
    fn addsub_c(self, mask: Mask, b: Self) -> Self::Output;
    /// [`addsub`](AddSubExt::addsub) where `mask` is true, else `src`.
    fn addsub_m(self, src: Self, mask: Mask, b: Self) -> Self::Output;
    /// [`addsub`](AddSubExt::addsub) where `mask` is true, else zero.
    fn addsub_z(self, mask: Mask, b: Self) -> Self::Output;

    /// [`fmaddsub`](AddSubExt::fmaddsub) where `mask` is true, else `self`.
    fn fmaddsub_c(self, mask: Mask, b: Self, c: Self) -> Self::Output;
    /// [`fmaddsub`](AddSubExt::fmaddsub) where `mask` is true, else `src`.
    fn fmaddsub_m(self, src: Self, mask: Mask, b: Self, c: Self) -> Self::Output;
    /// [`fmaddsub`](AddSubExt::fmaddsub) where `mask` is true, else zero.
    fn fmaddsub_z(self, mask: Mask, b: Self, c: Self) -> Self::Output;

    /// [`fmsubadd`](AddSubExt::fmsubadd) where `mask` is true, else `self`.
    fn fmsubadd_c(self, mask: Mask, b: Self, c: Self) -> Self::Output;
    /// [`fmsubadd`](AddSubExt::fmsubadd) where `mask` is true, else `src`.
    fn fmsubadd_m(self, src: Self, mask: Mask, b: Self, c: Self) -> Self::Output;
    /// [`fmsubadd`](AddSubExt::fmsubadd) where `mask` is true, else zero.
    fn fmsubadd_z(self, mask: Mask, b: Self, c: Self) -> Self::Output;
}

impl<R: FloatRegister> AddSubExtMasked<Mask<R>> for Vector<R> {
    #[inline(always)]
    fn addsub_c(self, mask: Mask<R>, b: Self) -> Self {
        Vector(R::addsub_c(mask.0, self.0, b.0))
    }
    #[inline(always)]
    fn addsub_m(self, src: Self, mask: Mask<R>, b: Self) -> Self {
        Vector(R::addsub_m(src.0, mask.0, self.0, b.0))
    }
    #[inline(always)]
    fn addsub_z(self, mask: Mask<R>, b: Self) -> Self {
        Vector(R::addsub_z(mask.0, self.0, b.0))
    }

    #[inline(always)]
    fn fmaddsub_c(self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmaddsub_c(mask.0, self.0, b.0, c.0))
    }
    #[inline(always)]
    fn fmaddsub_m(self, src: Self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmaddsub_m(src.0, mask.0, self.0, b.0, c.0))
    }
    #[inline(always)]
    fn fmaddsub_z(self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmaddsub_z(mask.0, self.0, b.0, c.0))
    }

    #[inline(always)]
    fn fmsubadd_c(self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmsubadd_c(mask.0, self.0, b.0, c.0))
    }
    #[inline(always)]
    fn fmsubadd_m(self, src: Self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmsubadd_m(src.0, mask.0, self.0, b.0, c.0))
    }
    #[inline(always)]
    fn fmsubadd_z(self, mask: Mask<R>, b: Self, c: Self) -> Self {
        Vector(R::fmsubadd_z(mask.0, self.0, b.0, c.0))
    }
}

// Vector shifts

impl<R: BitshiftRegister> Shl<Vector<R::Unsigned>> for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn shl(self, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shlv(self.0, rhs.0))
    }
}

impl<R: BitshiftRegister> ShlMasked<Mask<R>, Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shl_c(self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shlv_c(mask.0, self.0, rhs.0))
    }

    #[inline(always)]
    fn shl_m(self, src: Self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shlv_m(src.0, mask.0, self.0, rhs.0))
    }

    #[inline(always)]
    fn shl_z(self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shlv_z(mask.0, self.0, rhs.0))
    }
}

impl<R: BitshiftRegister> Shr<Vector<R::Unsigned>> for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn shr(self, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shrv(self.0, rhs.0))
    }
}

impl<R: BitshiftRegister> ShrMasked<Mask<R>, Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shr_c(self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shrv_c(mask.0, self.0, rhs.0))
    }

    #[inline(always)]
    fn shr_m(self, src: Self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shrv_m(src.0, mask.0, self.0, rhs.0))
    }

    #[inline(always)]
    fn shr_z(self, mask: Mask<R>, rhs: Vector<R::Unsigned>) -> Self::Output {
        Vector(R::shrv_z(mask.0, self.0, rhs.0))
    }
}

impl<R: BitshiftRegister> ShlAssign<Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shl_assign(&mut self, rhs: Vector<R::Unsigned>) {
        self.0 = R::shlv(self.0, rhs.0);
    }
}

impl<R: BitshiftRegister> ShlAssignMasked<Mask<R>, Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shl_assign_c(&mut self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shlv_c(mask.0, self.0, rhs.0);
    }

    #[inline(always)]
    fn shl_assign_m(&mut self, src: Self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shlv_m(src.0, mask.0, self.0, rhs.0);
    }

    #[inline(always)]
    fn shl_assign_z(&mut self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shlv_z(mask.0, self.0, rhs.0);
    }
}

impl<R: BitshiftRegister> ShrAssign<Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shr_assign(&mut self, rhs: Vector<R::Unsigned>) {
        self.0 = R::shrv(self.0, rhs.0);
    }
}

impl<R: BitshiftRegister> ShrAssignMasked<Mask<R>, Vector<R::Unsigned>> for Vector<R> {
    #[inline(always)]
    fn shr_assign_c(&mut self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shrv_c(mask.0, self.0, rhs.0);
    }

    #[inline(always)]
    fn shr_assign_m(&mut self, src: Self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shrv_m(src.0, mask.0, self.0, rhs.0);
    }

    #[inline(always)]
    fn shr_assign_z(&mut self, mask: Mask<R>, rhs: Vector<R::Unsigned>) {
        self.0 = R::shrv_z(mask.0, self.0, rhs.0);
    }
}

// Scalar shifts

impl<R: BitshiftRegister> Shl<u32> for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn shl(self, rhs: u32) -> Self::Output {
        Vector(R::shl(self.0, rhs))
    }
}

impl<R: BitshiftRegister> ShlMasked<Mask<R>, u32> for Vector<R> {
    #[inline(always)]
    fn shl_c(self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shl_c(mask.0, self.0, rhs))
    }

    #[inline(always)]
    fn shl_m(self, src: Self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shl_m(src.0, mask.0, self.0, rhs))
    }

    #[inline(always)]
    fn shl_z(self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shl_z(mask.0, self.0, rhs))
    }
}

impl<R: BitshiftRegister> Shr<u32> for Vector<R> {
    type Output = Self;

    #[inline(always)]
    fn shr(self, rhs: u32) -> Self::Output {
        Vector(R::shr(self.0, rhs))
    }
}

impl<R: BitshiftRegister> ShrMasked<Mask<R>, u32> for Vector<R> {
    #[inline(always)]
    fn shr_c(self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shr_c(mask.0, self.0, rhs))
    }

    #[inline(always)]
    fn shr_m(self, src: Self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shr_m(src.0, mask.0, self.0, rhs))
    }

    #[inline(always)]
    fn shr_z(self, mask: Mask<R>, rhs: u32) -> Self::Output {
        Vector(R::shr_z(mask.0, self.0, rhs))
    }
}

impl<R: BitshiftRegister> ShlAssign<u32> for Vector<R> {
    #[inline(always)]
    fn shl_assign(&mut self, rhs: u32) {
        self.0 = R::shl(self.0, rhs);
    }
}

impl<R: BitshiftRegister> ShlAssignMasked<Mask<R>, u32> for Vector<R> {
    #[inline(always)]
    fn shl_assign_c(&mut self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shl_c(mask.0, self.0, rhs);
    }

    #[inline(always)]
    fn shl_assign_m(&mut self, src: Self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shl_m(src.0, mask.0, self.0, rhs);
    }

    #[inline(always)]
    fn shl_assign_z(&mut self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shl_z(mask.0, self.0, rhs);
    }
}

impl<R: BitshiftRegister> ShrAssign<u32> for Vector<R> {
    #[inline(always)]
    fn shr_assign(&mut self, rhs: u32) {
        self.0 = R::shr(self.0, rhs);
    }
}

impl<R: BitshiftRegister> ShrAssignMasked<Mask<R>, u32> for Vector<R> {
    #[inline(always)]
    fn shr_assign_c(&mut self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shr_c(mask.0, self.0, rhs);
    }

    #[inline(always)]
    fn shr_assign_m(&mut self, src: Self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shr_m(src.0, mask.0, self.0, rhs);
    }

    #[inline(always)]
    fn shr_assign_z(&mut self, mask: Mask<R>, rhs: u32) {
        self.0 = R::shr_z(mask.0, self.0, rhs);
    }
}

macro_rules! impl_square {
    ($($ty:ty),*) => {$(
        impl Square for $ty {
            type Output = Self;

            #[inline(always)]
            fn square(self) -> Self::Output {
                self * self
            }
        }
    )*};
}

impl_square!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64);