lightweight-simd 0.1.0

Lightweight generic fixed-array vector types relying on compiler auto-vectorization
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
// Most or all contents generated by AI (model glm-5.3)

//! Lane-wise operators, float functions, comparisons, and masks for the vector
//! types ([`Simd`] and its [`aligned`](crate::aligned) siblings).

use core::ops::*;
#[cfg(any(feature = "use_libm_fma", target_arch = "aarch64", target_feature = "fma"))]
use num_traits::MulAdd;
use num_traits::{Float, Num, NumAssignOps};

use crate::{Aligned4, Aligned8, Aligned16, Aligned32, Aligned64, Simd};

impl<const N: usize> Simd<bool, N> {
    /// Lane-wise select: picks lanes of `if_true` where the mask is `true` and
    /// lanes of `if_false` where it is `false`.
    #[inline(always)]
    pub fn select<T: Copy>(self, if_true: Simd<T, N>, if_false: Simd<T, N>) -> Simd<T, N> {
        let mut out = if_true.0;
        for ((o, f), m) in out.iter_mut().zip(if_false.0).zip(self.0) {
            if !m {
                *o = f;
            }
        }
        Simd(out)
    }

    /// Returns `true` if any lane is `true`.
    #[inline(always)]
    pub fn any_true(self) -> bool {
        self.0.iter().any(|b| *b)
    }

    /// Returns `true` if all lanes are `true`.
    #[inline(always)]
    pub fn all_true(self) -> bool {
        self.0.iter().all(|b| *b)
    }
}

#[duplicate::duplicate_item(
    _mod_        _vec_;
    [simd_impls] [Simd];
    [impls_a4]   [Aligned4];
    [impls_a8]   [Aligned8];
    [impls_a16]  [Aligned16];
    [impls_a32]  [Aligned32];
    [impls_a64]  [Aligned64];
)]
mod _mod_ {
    use super::*;

    /* #region arithmetic operators */

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [Add]  [add] ;
        [Sub]  [sub] ;
        [Mul]  [mul] ;
        [Div]  [div] ;
    )]
    impl<T, const N: usize> _trait_ for _vec_<T, N>
    where
        T: Num + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn _method_(self, rhs: Self) -> Self {
            let mut out = self.0;
            for (o, r) in out.iter_mut().zip(rhs.0) {
                *o = _trait_::_method_(*o, r);
            }
            Self(out)
        }
    }

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [Add]  [add] ;
        [Sub]  [sub] ;
        [Mul]  [mul] ;
        [Div]  [div] ;
    )]
    impl<T, const N: usize> _trait_<T> for _vec_<T, N>
    where
        T: Num + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn _method_(self, rhs: T) -> Self {
            let mut out = self.0;
            for o in out.iter_mut() {
                *o = _trait_::_method_(*o, rhs);
            }
            Self(out)
        }
    }

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [AddAssign] [add_assign];
        [SubAssign] [sub_assign];
        [MulAssign] [mul_assign];
        [DivAssign] [div_assign];
    )]
    impl<T, const N: usize> _trait_ for _vec_<T, N>
    where
        T: NumAssignOps + Copy,
    {
        #[inline(always)]
        fn _method_(&mut self, rhs: Self) {
            for (o, r) in self.0.iter_mut().zip(rhs.0) {
                _trait_::_method_(o, r);
            }
        }
    }

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [AddAssign] [add_assign];
        [SubAssign] [sub_assign];
        [MulAssign] [mul_assign];
        [DivAssign] [div_assign];
    )]
    impl<T, const N: usize> _trait_<T> for _vec_<T, N>
    where
        T: NumAssignOps + Copy,
    {
        #[inline(always)]
        fn _method_(&mut self, rhs: T) {
            for o in self.0.iter_mut() {
                _trait_::_method_(o, rhs);
            }
        }
    }

    impl<T, const N: usize> Neg for _vec_<T, N>
    where
        T: Neg<Output = T> + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn neg(self) -> Self {
            let mut out = self.0;
            for (o, x) in out.iter_mut().zip(self.0) {
                *o = Neg::neg(x);
            }
            Self(out)
        }
    }

    /* #endregion */

    /* #region bitwise and shift operators */

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [BitAnd] [bitand];
        [BitOr]  [bitor];
        [BitXor] [bitxor];
    )]
    impl<T, const N: usize> _trait_ for _vec_<T, N>
    where
        T: _trait_<Output = T> + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn _method_(self, rhs: Self) -> Self {
            let mut out = self.0;
            for (o, r) in out.iter_mut().zip(rhs.0) {
                *o = _trait_::_method_(*o, r);
            }
            Self(out)
        }
    }

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [BitAndAssign] [bitand_assign];
        [BitOrAssign]  [bitor_assign];
        [BitXorAssign] [bitxor_assign];
    )]
    impl<T, const N: usize> _trait_ for _vec_<T, N>
    where
        T: _trait_ + Copy,
    {
        #[inline(always)]
        fn _method_(&mut self, rhs: Self) {
            for (o, r) in self.0.iter_mut().zip(rhs.0) {
                _trait_::_method_(o, r);
            }
        }
    }

    impl<T, const N: usize> Not for _vec_<T, N>
    where
        T: Not<Output = T> + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn not(self) -> Self {
            let mut out = self.0;
            for (o, x) in out.iter_mut().zip(self.0) {
                *o = Not::not(x);
            }
            Self(out)
        }
    }

    #[duplicate::duplicate_item(
        _trait_ _method_;
        [Shl] [shl];
        [Shr] [shr];
    )]
    impl<T, const N: usize> _trait_<usize> for _vec_<T, N>
    where
        T: _trait_<usize, Output = T> + Copy,
    {
        type Output = Self;

        #[inline(always)]
        fn _method_(self, rhs: usize) -> Self {
            let mut out = self.0;
            for o in out.iter_mut() {
                *o = _trait_::_method_(*o, rhs);
            }
            Self(out)
        }
    }

    /* #endregion */

    /* #region fused multiply-add */
    //
    // The fused path is selected when the compilation target has hardware
    // FMA (`target_feature = "fma"`, which follows `-C target-cpu`, i.e. any
    // x86-64-v3+ or `native` build; on aarch64 scalar/vector FMA is baseline)
    // or when the `use_libm_fma` feature pins it on. Everything else —
    // including architectures the predicate does not know — gets the fast
    // separated form, never the slow libm software `fma`. See docs/adr/0005.

    #[cfg(any(feature = "use_libm_fma", target_arch = "aarch64", target_feature = "fma"))]
    impl<T, const N: usize> _vec_<T, N>
    where
        T: MulAdd<Output = T> + Copy,
    {
        /// Lane-wise fused multiply-add: `self * b + c` with a single
        /// rounding per lane.
        ///
        /// This is the default on targets with hardware FMA (x86 built with
        /// `-C target-cpu` of `x86-64-v3` or higher, `native`, and all
        /// aarch64), where it lowers to one FMA instruction per register
        /// with no extra flags — and is also faster than a separated
        /// multiply and add there. The non-default cargo feature
        /// `use_libm_fma` selects this path on *every* target, accepting a
        /// correctly-rounded but slow libm `fma` call (~4.6x a separated
        /// mul+add) where hardware FMA is absent.
        #[inline(always)]
        pub fn mul_add(self, b: Self, c: Self) -> Self {
            let mut out = self.0;
            for ((o, b), c) in out.iter_mut().zip(b.0).zip(c.0) {
                *o = MulAdd::mul_add(*o, b, c);
            }
            Self(out)
        }

        /// Lane-wise fused `*self = b * c + *self`, by the receiver.
        ///
        /// Note that the operand order differs from [`mul_add`](Self::mul_add):
        /// the accumulator is the receiver, matching `self += b * c`.
        #[inline(always)]
        pub fn fma_from(&mut self, b: Self, c: Self) {
            for ((b, c), o) in b.0.iter().zip(c.0.iter()).zip(self.0.iter_mut()) {
                *o = MulAdd::mul_add(*b, *c, *o);
            }
        }
    }

    #[cfg(not(any(feature = "use_libm_fma", target_arch = "aarch64", target_feature = "fma")))]
    impl<T, const N: usize> _vec_<T, N>
    where
        T: Mul<Output = T> + Add<Output = T> + Copy,
    {
        /// Lane-wise multiply-add: `self * b + c`, evaluated as a separate
        /// multiply and add (two roundings per lane).
        ///
        /// This is the fallback on targets without hardware FMA (generic
        /// x86-64, `x86-64-v2`), where it avoids the slow software-fused
        /// libm `fma` calls; it also fuses into a single hardware FMA
        /// instruction when compiled with `-C llvm-args=-fp-contract=fast`.
        /// Enable the `use_libm_fma` feature to get the single-rounding
        /// fused form on such targets instead.
        #[inline(always)]
        pub fn mul_add(self, b: Self, c: Self) -> Self {
            let mut out = self.0;
            for ((o, b), c) in out.iter_mut().zip(b.0).zip(c.0) {
                *o = *o * b + c;
            }
            Self(out)
        }

        /// Lane-wise `*self = b * c + *self`, by the receiver.
        ///
        /// Note that the operand order differs from [`mul_add`](Self::mul_add):
        /// the accumulator is the receiver, matching `self += b * c`. The
        /// evaluation strategy is the same as [`mul_add`](Self::mul_add)'s.
        #[inline(always)]
        pub fn fma_from(&mut self, b: Self, c: Self) {
            for ((b, c), o) in b.0.iter().zip(c.0.iter()).zip(self.0.iter_mut()) {
                *o = *b * *c + *o;
            }
        }
    }

    /* #endregion */

    /* #region float functions */

    impl<T, const N: usize> _vec_<T, N>
    where
        T: Float + Copy,
    {
        #[duplicate::duplicate_item(
            _name_;
            [abs];
            [sqrt];
            [recip];
            [floor];
            [ceil];
            [round];
            [trunc];
            [fract];
        )]
        #[doc = concat!("Lane-wise [`Float::", stringify!(_name_), "`].")]
        #[inline(always)]
        pub fn _name_(self) -> Self {
            Self(self.0.map(|x| x._name_()))
        }

        /// Lane-wise minimum; NaN handling follows the element's scalar
        /// [`Float::min`].
        #[inline(always)]
        pub fn min(self, other: Self) -> Self {
            let mut out = self.0;
            for (o, r) in out.iter_mut().zip(other.0) {
                *o = o.min(r);
            }
            Self(out)
        }

        /// Lane-wise maximum; NaN handling follows the element's scalar
        /// [`Float::max`].
        #[inline(always)]
        pub fn max(self, other: Self) -> Self {
            let mut out = self.0;
            for (o, r) in out.iter_mut().zip(other.0) {
                *o = o.max(r);
            }
            Self(out)
        }

        /// Lane-wise [`Float::copysign`].
        #[inline(always)]
        pub fn copysign(self, sign: Self) -> Self {
            let mut out = self.0;
            for (o, r) in out.iter_mut().zip(sign.0) {
                *o = o.copysign(r);
            }
            Self(out)
        }
    }

    impl<const N: usize> _vec_<f32, N> {
        /// Lane-wise [`f32::round_ties_even`].
        ///
        /// Not available generically through [`num_traits::Float`], so this is
        /// implemented only for the builtin float types.
        #[inline(always)]
        pub fn round_ties_even(self) -> Self {
            Self(self.0.map(f32::round_ties_even))
        }
    }

    impl<const N: usize> _vec_<f64, N> {
        /// Lane-wise [`f64::round_ties_even`].
        ///
        /// Not available generically through [`num_traits::Float`], so this is
        /// implemented only for the builtin float types.
        #[inline(always)]
        pub fn round_ties_even(self) -> Self {
            Self(self.0.map(f64::round_ties_even))
        }
    }

    /* #endregion */

    /* #region comparisons */

    impl<T, const N: usize> _vec_<T, N>
    where
        T: PartialOrd + Copy,
    {
        /// Lane-wise `self < rhs`.
        #[inline(always)]
        pub fn simd_lt(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] < rhs.0[i]))
        }

        /// Lane-wise `self <= rhs`.
        #[inline(always)]
        pub fn simd_le(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] <= rhs.0[i]))
        }

        /// Lane-wise `self > rhs`.
        #[inline(always)]
        pub fn simd_gt(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] > rhs.0[i]))
        }

        /// Lane-wise `self >= rhs`.
        #[inline(always)]
        pub fn simd_ge(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] >= rhs.0[i]))
        }
    }

    impl<T, const N: usize> _vec_<T, N>
    where
        T: PartialEq + Copy,
    {
        /// Lane-wise `self == rhs`.
        #[inline(always)]
        pub fn simd_eq(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] == rhs.0[i]))
        }

        /// Lane-wise `self != rhs`.
        #[inline(always)]
        pub fn simd_ne(self, rhs: Self) -> Simd<bool, N> {
            Simd(core::array::from_fn(|i| self.0[i] != rhs.0[i]))
        }
    }

    /* #endregion */
}