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
#[allow(unused_macros)]
macro_rules! vec_impl_unary_op {
    ($vectype: ty, $op_trait: tt, $op_name: tt, $intrinsic: tt) => {
        impl $op_trait for $vectype {
            type Output = Self;
            #[inline]
            fn $op_name(self) -> Self::Output {
                // SAFETY: `cfg_if!` should guarantee the intrinsic is available.
                unsafe { $intrinsic(self.into()).into() }
            }
        }
    };
}

macro_rules! vec_impl_binary_op {
    ($vectype: ty, $op_trait: tt, $op_name: tt, $intrinsic: tt) => {
        impl $op_trait for $vectype {
            type Output = Self;
            #[inline]
            fn $op_name(self, rhs: Self) -> Self::Output {
                // SAFETY: `cfg_if!` should guarantee the intrinsic is available.
                unsafe { $intrinsic(self.into(), rhs.into()).into() }
            }
        }
    };
}

macro_rules! vec_overload_operator {
    ($vectype: ty, $eltype: ty, $op_trait: ident, $op_name: ident) => {
        impl $op_trait<$eltype> for $vectype {
            type Output = Self;
            #[inline]
            fn $op_name(self, rhs: $eltype) -> Self::Output {
                self.$op_name(<$vectype>::broadcast(rhs))
            }
        }

        impl $op_trait<$vectype> for $eltype {
            type Output = $vectype;
            #[inline]
            fn $op_name(self, rhs: $vectype) -> Self::Output {
                rhs.$op_name(self)
            }
        }

        paste::paste! {
        impl<T> [<$op_trait Assign>]<T> for $vectype
        where
            Self: $op_trait<T, Output = Self>,
        {
            #[inline]
            fn [<$op_name _assign>](&mut self, rhs: T) {
                *self = self.$op_name(rhs);
            }
        }
        }
    };
}

macro_rules! vec_impl_sum_prod {
    ($vectype: ty) => {
        impl std::iter::Sum for $vectype {
            #[inline]
            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(<$vectype>::default(), |a, b| a + b)
            }
        }

        impl std::iter::Product for $vectype {
            #[inline]
            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(<$vectype>::default(), |a, b| a * b)
            }
        }
    };
}

macro_rules! vec_impl_generic_traits {
    ($vectype: tt, $eltype: tt, $N: tt) => {
        vec_overload_operator!($vectype, $eltype, Add, add);
        vec_overload_operator!($vectype, $eltype, Sub, sub);
        vec_overload_operator!($vectype, $eltype, Mul, mul);
        vec_overload_operator!($vectype, $eltype, Div, div);
        vec_impl_sum_prod!($vectype);

        impl From<&[$eltype; $N]> for $vectype {
            /// Does same as [`load`](SIMDBase::load).
            #[inline]
            fn from(value: &[$eltype; $N]) -> Self {
                Self::load(value)
            }
        }

        impl From<[$eltype; $N]> for $vectype {
            #[inline]
            fn from(value: [$eltype; $N]) -> Self {
                (&value).into()
            }
        }

        impl From<$vectype> for [$eltype; $N] {
            #[inline]
            fn from(value: $vectype) -> Self {
                let mut result = MaybeUninit::<Self>::uninit();
                // SAFETY: currently all the supported platforms (x86 and NEON) store
                // SIMD types as a packed array of `f32`/`f64`/etc
                // Also the underlying intrinsic types are aligned enough
                // Hence the pointer casting is valid
                unsafe {
                    value.store_ptr(result.as_mut_ptr() as *mut $eltype);
                    result.assume_init()
                }
            }
        }

        impl From<&$vectype> for [$eltype; $N] {
            #[inline]
            fn from(value: &$vectype) -> Self {
                // SAFETY: see safety comment for `impl From<vectype> for [eltype; N]` above
                unsafe { std::mem::transmute_copy(value) }
            }
        }

        impl std::fmt::Debug for $vectype {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                let mut debug_tuple = f.debug_tuple("$vectype");
                for value in <[$eltype; Self::N]>::from(self) {
                    debug_tuple.field(&value);
                }
                debug_tuple.finish()
            }
        }

        impl std::ops::Index<usize> for $vectype {
            type Output = $eltype;
            #[inline]
            fn index(&self, index: usize) -> &Self::Output {
                if index >= Self::N {
                    panic!("invalid index");
                }
                // SAFETY: on both x86 and NEON intrinsic types are stored as an array of several values
                unsafe { &*(self as *const Self as *const $eltype).add(index) }
            }
        }

        impl std::ops::IndexMut<usize> for $vectype {
            #[inline]
            fn index_mut(&mut self, index: usize) -> &mut Self::Output {
                if index >= Self::N {
                    panic!("invalid index");
                }
                // SAFETY: on both x86 and NEON intrinsic types are stored as an array of several values
                unsafe { &mut *(self as *mut Self as *mut $eltype).add(index) }
            }
        }
    };
}

macro_rules! vec_impl_partial_load {
    ($vectype: ty, $eltype: ty, 4) => {
        impl crate::common::SIMDPartialLoad<$eltype> for $vectype {
            #[inline]
            fn load_partial(data: &[$eltype]) -> Self {
                match data.len() {
                    // SAFETY: if data.len() is at least 4 hence it's safe to simply load the prefix
                    4.. => unsafe { Self::load_ptr(data.as_ptr()) },
                    3 => Self::new(data[0], data[1], data[2], 0 as $eltype),
                    2 => Self::new(data[0], data[1], 0 as $eltype, 0 as $eltype),
                    1 => Self::new(data[0], 0 as $eltype, 0 as $eltype, 0 as $eltype),
                    0 => Self::default(),
                }
            }
        }
    };

    ($vectype: ty, $eltype: ty, $halfvectype: ty, 8) => {
        impl crate::common::SIMDPartialLoad<$eltype> for $vectype {
            #[inline]
            fn load_partial(data: &[$eltype]) -> Self {
                match data.len() {
                    // SAFETY: if data.len() is at least 8 hence it's safe to simply load the prefix
                    8.. => unsafe { Self::load_ptr(data.as_ptr()) },
                    4.. => Self::join(
                        // SAFETY: data.len() is at least 4 hence it's safe to load the first 4
                        // element into vector type of size 4.
                        unsafe { <$halfvectype>::load_ptr(data.as_ptr()) },
                        <$halfvectype>::load_partial(data.split_at(4).1),
                    ),
                    0.. => Self::join(
                        <$halfvectype>::load_partial(data),
                        <$halfvectype>::default(),
                    ),
                }
            }
        }
    };
}

macro_rules! vec_impl_partial_store {
    ($vectype: ty, $eltype: ty, 4) => {
        impl crate::common::SIMDPartialStore<$eltype> for $vectype {
            #[inline]
            fn store_partial(&self, slice: &mut [$eltype]) {
                match slice.len() {
                    // SAFETY: slice.len() is at least 4 hence it's valid to write the vector into the prefix
                    4.. => unsafe { self.store_ptr(slice.as_mut_ptr()) },
                    _ => slice.copy_from_slice(&<[$eltype; 4]>::from(self)[..slice.len()]),
                }
            }
        }
    };
    ($vectype: ty, $eltype: ty, 8) => {
        impl crate::common::SIMDPartialStore<$eltype> for $vectype {
            #[inline]
            fn store_partial(&self, slice: &mut [$eltype]) {
                match slice.len() {
                    // SAFETY: slice.len() is at least 8 hence it's valid to write the vector into the prefix
                    8.. => unsafe { self.store_ptr(slice.as_mut_ptr()) },
                    4.. => {
                        // SAFETY: slice.len() is at least 4 hence it's valid to write the first 4
                        // elements of the vector into the slice's prefix
                        unsafe { self.low().store_ptr(slice.as_mut_ptr()) };
                        self.high().store_partial(slice.split_at_mut(4).1)
                    }
                    0.. => self.low().store_partial(slice),
                }
            }
        }
    };
}

#[allow(unused_macros)]
macro_rules! vec_impl_fused_low_high {
    ($vectype: ty) => {
        impl SIMDFusedCalc for $vectype {
            #[inline]
            fn mul_add(self, b: Self, c: Self) -> Self {
                (
                    self.low().mul_add(b.low(), c.low()),
                    self.high().mul_add(b.high(), c.high()),
                )
                    .into()
            }

            #[inline]
            fn mul_sub(self, b: Self, c: Self) -> Self {
                (
                    self.low().mul_sub(b.low(), c.low()),
                    self.high().mul_sub(b.high(), c.high()),
                )
                    .into()
            }

            #[inline]
            fn nmul_add(self, b: Self, c: Self) -> Self {
                (
                    self.low().nmul_add(b.low(), c.low()),
                    self.high().nmul_add(b.high(), c.high()),
                )
                    .into()
            }

            #[inline]
            fn nmul_sub(self, b: Self, c: Self) -> Self {
                (
                    self.low().nmul_sub(b.low(), c.low()),
                    self.high().nmul_sub(b.high(), c.high()),
                )
                    .into()
            }
        }
    };
}

#[allow(unused_macros)]
macro_rules! vec_impl_simdfloat_low_high {
    ($vectype: ty) => {
        impl crate::common::SIMDFloat for $vectype {
            fn round(self) -> Self {
                (self.low().round(), self.high().round()).into()
            }
        }
    };
}

#[allow(unused_macros)]
macro_rules! vec_impl_broadcast_default {
    ($vectype: ty, $zero: literal) => {
        impl Default for $vectype {
            #[inline]
            fn default() -> Self {
                Self::broadcast($zero)
            }
        }
    };
}

#[allow(unused_imports)]
pub(crate) use {
    vec_impl_binary_op, vec_impl_broadcast_default, vec_impl_fused_low_high,
    vec_impl_generic_traits, vec_impl_partial_load, vec_impl_partial_store,
    vec_impl_simdfloat_low_high, vec_impl_sum_prod, vec_impl_unary_op, vec_overload_operator,
};