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
use ::wide::{f32x4, f32x8, f64x2, f64x4, CmpEq, CmpGe, CmpGt, CmpLe, CmpLt, CmpNe};

use super::*;

macro_rules! impl_wide_float {
    ($($ty: ident { array: [$scalar: ident; $n: expr], powf: $pow_self: ident, }),+) => {
        $(
            impl Real for $ty {
                #[inline]
                fn from_f64(n: f64) -> $ty {
                    $ty::splat(n as $scalar)
                }

            }

            impl FromScalar for $ty {
                type Scalar = $scalar;

                #[inline]
                fn from_scalar(scalar: $scalar) -> Self {
                    $ty::splat(scalar)
                }
            }

            impl FromScalarArray<$n> for $ty {
                #[inline]
                fn from_array(scalars: [$scalar; $n]) -> Self {
                    scalars.into()
                }
            }

            impl IntoScalarArray<$n> for $ty {
                #[inline]
                fn into_array(self) -> [$scalar; $n] {
                    self.into()
                }
            }

            impl Zero for $ty {
                #[inline]
                fn zero() -> Self {
                    $ty::ZERO
                }
            }

            impl One for $ty {
                #[inline]
                fn one() -> Self {
                    $ty::ONE
                }
            }

            impl MinMax for $ty {
                #[inline]
                fn max(self, other: Self) -> Self {
                    $ty::max(self, other)
                }

                #[inline]
                fn min(self, other: Self) -> Self {
                    $ty::min(self, other)
                }

                #[inline]
                fn min_max(self, other: Self) -> (Self, Self) {
                    ($ty::min(self, other), $ty::max(self, other))
                }
            }

            impl Powu for $ty {
                #[inline]
                fn powu(self, exp: u32) -> Self {
                    pow(self, exp)
                }
            }

            impl IsValidDivisor for $ty {
                #[inline]
                fn is_valid_divisor(&self) -> Self {
                    !self.cmp_eq($ty::ZERO)
                }
            }

            impl Trigonometry for $ty {
                #[inline]
                fn sin(self) -> Self {
                    $ty::sin(self)
                }

                #[inline]
                fn cos(self) -> Self {
                    $ty::cos(self)
                }

                #[inline]
                fn sin_cos(self) -> (Self, Self) {
                    $ty::sin_cos(self)
                }

                #[inline]
                fn tan(self) -> Self {
                    $ty::tan(self)
                }

                #[inline]
                fn asin(self) -> Self {
                    $ty::asin(self)
                }

                #[inline]
                fn acos(self) -> Self {
                    $ty::acos(self)
                }

                #[inline]
                fn atan(self) -> Self {
                    $ty::atan(self)
                }

                #[inline]
                fn atan2(self, other: Self) -> Self {
                    $ty::atan2(self, other)
                }
            }

            impl Abs for $ty {
                #[inline]
                fn abs(self) -> Self {
                    $ty::abs(self)
                }
            }

            impl Sqrt for $ty {
                #[inline]
                fn sqrt(self) -> Self {
                    $ty::sqrt(self)
                }
            }

            impl Cbrt for $ty {
                #[inline]
                fn cbrt(self) -> Self {
                    let mut array = self.into_array();

                    for scalar in &mut array {
                        *scalar = scalar.cbrt();
                    }

                    array.into()
                }
            }

            impl Powf for $ty {
                #[inline]
                fn powf(self, exp: Self) -> Self {
                    $ty::$pow_self(self, exp)
                }
            }

            impl Powi for $ty {
                #[inline]
                fn powi(mut self, mut exp: i32) -> Self {
                    if exp < 0 {
                        exp = exp.wrapping_neg();
                        self = self.recip();
                    }

                    Powu::powu(self, exp as u32)
                }
            }

            // impl Recip for $ty {
            //     #[inline]
            //     fn recip(self) -> Self {
            //         $ty::recip(self)
            //     }
            // }

            impl Exp for $ty {
                #[inline]
                fn exp(self) -> Self {
                    $ty::exp(self)
                }
            }

            impl Hypot for $ty {
                #[inline]
                fn hypot(self, other: Self) -> Self {
                    (self * self + other * other).sqrt()
                }
            }

            impl Round for $ty {
                #[inline]
                fn round(self) -> Self {
                    $ty::round(self)
                }

                #[inline]
                fn floor(self) -> Self {
                    let mut array = self.into_array();

                    for scalar in &mut array {
                        *scalar = scalar.floor();
                    }

                    array.into()
                }

                #[inline]
                fn ceil(self) -> Self {
                    let mut array = self.into_array();

                    for scalar in &mut array {
                        *scalar = scalar.ceil();
                    }

                    array.into()
                }
            }

            impl Clamp for $ty {
                #[inline]
                fn clamp(self, min: Self, max: Self) -> Self {
                    self.min(max).max(min)
                }

                #[inline]
                fn clamp_min(self, min: Self) -> Self {
                    $ty::max(self, min)
                }

                #[inline]
                fn clamp_max(self, max: Self) -> Self {
                    $ty::min(self, max)
                }
            }

            impl ClampAssign for $ty {
                #[inline]
                fn clamp_assign(&mut self, min: Self, max: Self) {
                    *self = $ty::clamp(*self, min, max);
                }

                #[inline]
                fn clamp_min_assign(&mut self, min: Self) {
                    *self = $ty::max(*self, min);
                }

                #[inline]
                fn clamp_max_assign(&mut self, max: Self) {
                    *self = $ty::min(*self, max);
                }
            }

            impl PartialCmp for $ty {
                #[inline]
                fn lt(&self, other: &Self) -> Self::Mask {
                    self.cmp_lt(*other)
                }

                #[inline]
                fn lt_eq(&self, other: &Self) -> Self::Mask {
                    self.cmp_le(*other)
                }

                #[inline]
                fn eq(&self, other: &Self) -> Self::Mask {
                    self.cmp_eq(*other)
                }

                #[inline]
                fn neq(&self, other: &Self) -> Self::Mask {
                    self.cmp_ne(*other)
                }

                #[inline]
                fn gt_eq(&self, other: &Self) -> Self::Mask {
                    self.cmp_ge(*other)
                }

                #[inline]
                fn gt(&self, other: &Self) -> Self::Mask {
                    self.cmp_gt(*other)
                }
            }

            impl MulAdd for $ty {
                #[inline]
                fn mul_add(self, m: Self, a: Self) -> Self {
                    $ty::mul_add(self, m, a)
                }
            }

            impl MulSub for $ty {
                #[inline]
                fn mul_sub(self, m: Self, s: Self) -> Self {
                    $ty::mul_sub(self, m, s)
                }
            }
        )+
    };
}

impl_wide_float!(
    f32x4 {
        array: [f32; 4],
        powf: pow_f32x4,
    },
    f32x8 {
        array: [f32; 8],
        powf: pow_f32x8,
    },
    f64x2 {
        array: [f64; 2],
        powf: pow_f64x2,
    },
    f64x4 {
        array: [f64; 4],
        powf: pow_f64x4,
    }
);

impl Recip for f32x4 {
    #[inline]
    fn recip(self) -> Self {
        f32x4::recip(self)
    }
}

impl Recip for f32x8 {
    #[inline]
    fn recip(self) -> Self {
        f32x8::recip(self)
    }
}

impl Recip for f64x2 {
    #[inline]
    fn recip(self) -> Self {
        f64x2::ONE / self
    }
}

impl Recip for f64x4 {
    #[inline]
    fn recip(self) -> Self {
        f64x4::ONE / self
    }
}