Skip to main content

glam/f32/neon/
vec3a.rs

1// Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3use crate::{BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4, f32::math, neon::*};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9use core::arch::aarch64::*;
10
11#[cfg(feature = "zerocopy-08")]
12use zerocopy_derive_08::*;
13
14#[repr(C)]
15union UnionCast {
16    a: [f32; 4],
17    v: Vec3A,
18}
19
20/// Creates a 3-dimensional vector.
21#[inline(always)]
22#[must_use]
23pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
24    Vec3A::new(x, y, z)
25}
26
27/// A 3-dimensional vector.
28///
29/// SIMD vector types are used for storage on supported platforms for better
30/// performance than the [`Vec3`] type.
31///
32/// It is possible to convert between [`Vec3`] and [`Vec3A`] types using [`From`]
33/// or [`Into`] trait implementations.
34///
35/// This type is 16 byte aligned.
36#[derive(Clone, Copy)]
37#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
38#[cfg_attr(
39    feature = "zerocopy-08",
40    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
41)]
42#[repr(transparent)]
43pub struct Vec3A(pub(crate) float32x4_t);
44
45impl Vec3A {
46    /// All zeroes.
47    pub const ZERO: Self = Self::splat(0.0);
48
49    /// All ones.
50    pub const ONE: Self = Self::splat(1.0);
51
52    /// All negative ones.
53    pub const NEG_ONE: Self = Self::splat(-1.0);
54
55    /// All `f32::MIN`.
56    pub const MIN: Self = Self::splat(f32::MIN);
57
58    /// All `f32::MAX`.
59    pub const MAX: Self = Self::splat(f32::MAX);
60
61    /// All `f32::NAN`.
62    pub const NAN: Self = Self::splat(f32::NAN);
63
64    /// All `f32::INFINITY`.
65    pub const INFINITY: Self = Self::splat(f32::INFINITY);
66
67    /// All `f32::NEG_INFINITY`.
68    pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
69
70    /// A unit vector pointing along the positive X axis.
71    pub const X: Self = Self::new(1.0, 0.0, 0.0);
72
73    /// A unit vector pointing along the positive Y axis.
74    pub const Y: Self = Self::new(0.0, 1.0, 0.0);
75
76    /// A unit vector pointing along the positive Z axis.
77    pub const Z: Self = Self::new(0.0, 0.0, 1.0);
78
79    /// A unit vector pointing along the negative X axis.
80    pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
81
82    /// A unit vector pointing along the negative Y axis.
83    pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
84
85    /// A unit vector pointing along the negative Z axis.
86    pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
87
88    /// The unit axes.
89    pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
90
91    /// Vec3A uses Rust Portable SIMD
92    pub const USES_CORE_SIMD: bool = false;
93    /// Vec3A uses Arm NEON
94    pub const USES_NEON: bool = true;
95    /// Vec3A uses scalar math
96    pub const USES_SCALAR_MATH: bool = false;
97    /// Vec3A uses Intel SSE2
98    pub const USES_SSE2: bool = false;
99    /// Vec3A uses WebAssembly 128-bit SIMD
100    pub const USES_WASM_SIMD: bool = false;
101
102    /// Creates a new vector.
103    #[inline(always)]
104    #[must_use]
105    pub const fn new(x: f32, y: f32, z: f32) -> Self {
106        unsafe { UnionCast { a: [x, y, z, z] }.v }
107    }
108
109    /// Creates a vector with all elements set to `v`.
110    #[inline]
111    #[must_use]
112    pub const fn splat(v: f32) -> Self {
113        unsafe { UnionCast { a: [v; 4] }.v }
114    }
115
116    /// Returns a vector containing each element of `self` modified by a mapping function `f`.
117    #[inline]
118    #[must_use]
119    pub fn map<F>(self, mut f: F) -> Self
120    where
121        F: FnMut(f32) -> f32,
122    {
123        Self::new(f(self.x), f(self.y), f(self.z))
124    }
125
126    /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
127    /// for each element of `self`.
128    ///
129    /// A true element in the mask uses the corresponding element from `if_true`, and false
130    /// uses the element from `if_false`.
131    #[inline]
132    #[must_use]
133    pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
134        Self(unsafe { vbslq_f32(mask.0, if_true.0, if_false.0) })
135    }
136
137    /// Creates a new vector from an array.
138    #[inline]
139    #[must_use]
140    pub const fn from_array(a: [f32; 3]) -> Self {
141        Self::new(a[0], a[1], a[2])
142    }
143
144    /// Converts `self` to `[x, y, z]`
145    #[inline]
146    #[must_use]
147    pub const fn to_array(&self) -> [f32; 3] {
148        unsafe { *(self as *const Self as *const [f32; 3]) }
149    }
150
151    /// Creates a vector from the first 3 values in `slice`.
152    ///
153    /// # Panics
154    ///
155    /// Panics if `slice` is less than 3 elements long.
156    #[inline]
157    #[must_use]
158    #[track_caller]
159    pub const fn from_slice(slice: &[f32]) -> Self {
160        assert!(slice.len() >= 3);
161        Self::new(slice[0], slice[1], slice[2])
162    }
163
164    /// Writes the elements of `self` to the first 3 elements in `slice`.
165    ///
166    /// # Panics
167    ///
168    /// Panics if `slice` is less than 3 elements long.
169    #[inline]
170    #[track_caller]
171    pub fn write_to_slice(self, slice: &mut [f32]) {
172        slice[..3].copy_from_slice(&self.to_array());
173    }
174
175    /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
176    ///
177    /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
178    #[inline]
179    #[must_use]
180    pub fn from_vec4(v: Vec4) -> Self {
181        Self(v.0)
182    }
183
184    /// Creates a 4D vector from `self` and the given `w` value.
185    #[inline]
186    #[must_use]
187    pub fn extend(self, w: f32) -> Vec4 {
188        Vec4::new(self.x, self.y, self.z, w)
189    }
190
191    /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
192    ///
193    /// Truncation may also be performed by using [`self.xy()`][crate::swizzles::Vec3Swizzles::xy()].
194    #[inline]
195    #[must_use]
196    pub fn truncate(self) -> Vec2 {
197        use crate::swizzles::Vec3Swizzles;
198        self.xy()
199    }
200
201    /// Projects a homogeneous coordinate to 3D space by performing perspective divide.
202    ///
203    /// # Panics
204    ///
205    /// Will panic if `v.w` is `0` when `glam_assert` is enabled.
206    #[inline]
207    #[must_use]
208    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
209    pub fn from_homogeneous(v: Vec4) -> Self {
210        glam_assert!(v.w != 0.0);
211        Self::from_vec4(v) / v.w
212    }
213
214    /// Creates a homogeneous coordinate from `self`, equivalent to `self.extend(1.0)`.
215    #[inline]
216    #[must_use]
217    pub fn to_homogeneous(self) -> Vec4 {
218        self.extend(1.0)
219    }
220
221    // Converts `self` to a `Vec3`.
222    #[inline]
223    #[must_use]
224    pub fn to_vec3(self) -> Vec3 {
225        Vec3::from(self)
226    }
227
228    /// Creates a 3D vector from `self` with the given value of `x`.
229    #[inline]
230    #[must_use]
231    pub fn with_x(mut self, x: f32) -> Self {
232        self.x = x;
233        self
234    }
235
236    /// Creates a 3D vector from `self` with the given value of `y`.
237    #[inline]
238    #[must_use]
239    pub fn with_y(mut self, y: f32) -> Self {
240        self.y = y;
241        self
242    }
243
244    /// Creates a 3D vector from `self` with the given value of `z`.
245    #[inline]
246    #[must_use]
247    pub fn with_z(mut self, z: f32) -> Self {
248        self.z = z;
249        self
250    }
251
252    /// Computes the dot product of `self` and `rhs`.
253    #[inline]
254    #[must_use]
255    pub fn dot(self, rhs: Self) -> f32 {
256        // this was faster than intrinsics in testing
257        (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
258    }
259
260    /// Returns a vector where every component is the dot product of `self` and `rhs`.
261    #[inline]
262    #[must_use]
263    pub fn dot_into_vec(self, rhs: Self) -> Self {
264        Self(unsafe { dot3_into_f32x4(self.0, rhs.0) })
265    }
266
267    /// Computes the cross product of `self` and `rhs`.
268    #[inline]
269    #[must_use]
270    pub fn cross(self, rhs: Self) -> Self {
271        unsafe {
272            // Implementation taken from Realtime Math
273            let lhs = self.0;
274            let rhs = rhs.0;
275            // cross(a, b) = (a.yzx * b.zxy) - (a.zxy * b.yzx)
276            let lhs_yzwx = vextq_f32(lhs, lhs, 1);
277            let rhs_wxyz = vextq_f32(rhs, rhs, 3);
278
279            let lhs_yzx = vsetq_lane_f32(vgetq_lane_f32(lhs, 0), lhs_yzwx, 2);
280            let rhs_zxy = vsetq_lane_f32(vgetq_lane_f32(rhs, 2), rhs_wxyz, 0);
281
282            // part_a = (a.yzx * b.zxy)
283            let part_a = vmulq_f32(lhs_yzx, rhs_zxy);
284
285            let lhs_wxyz = vextq_f32(lhs, lhs, 3);
286            let rhs_yzwx = vextq_f32(rhs, rhs, 1);
287            let lhs_zxy = vsetq_lane_f32(vgetq_lane_f32(lhs, 2), lhs_wxyz, 0);
288            let rhs_yzx = vsetq_lane_f32(vgetq_lane_f32(rhs, 0), rhs_yzwx, 2);
289
290            // result = part_a - (a.zxy * b.yzx)
291            let result = vfmsq_f32(part_a, lhs_zxy, rhs_yzx);
292            Self(result)
293        }
294    }
295
296    /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
297    ///
298    /// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`.
299    ///
300    /// `-0.0` and `+0.0` compare equal, so `rhs` is returned.
301    ///
302    /// NaN propagation does not follow IEEE 754-2008 semantics for minNum.
303    #[inline]
304    #[must_use]
305    pub fn min(self, rhs: Self) -> Self {
306        Self(unsafe { vbslq_f32(vcltq_f32(self.0, rhs.0), self.0, rhs.0) })
307    }
308
309    /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
310    ///
311    /// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`.
312    ///
313    /// `-0.0` and `+0.0` compare equal, so `rhs` is returned.
314    ///
315    /// NaN propagation does not follow IEEE 754-2008 semantics for maxNum.
316    #[inline]
317    #[must_use]
318    pub fn max(self, rhs: Self) -> Self {
319        Self(unsafe { vbslq_f32(vcgtq_f32(self.0, rhs.0), self.0, rhs.0) })
320    }
321
322    /// Component-wise clamping of values, similar to [`f32::clamp`].
323    ///
324    /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
325    ///
326    /// NaN propagation does not follow IEEE 754-2008 semantics.
327    ///
328    /// # Panics
329    ///
330    /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
331    #[inline]
332    #[must_use]
333    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
334    pub fn clamp(self, min: Self, max: Self) -> Self {
335        glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
336        self.max(min).min(max)
337    }
338
339    /// Returns the horizontal minimum of `self`.
340    ///
341    /// In other words this computes `min(x, y, ..)`.
342    ///
343    /// NaN propagation does not follow IEEE 754-2008 semantics for minNum.
344    #[inline]
345    #[must_use]
346    pub fn min_element(self) -> f32 {
347        let min = |a: float32x4_t, b: float32x4_t| unsafe { vbslq_f32(vcltq_f32(a, b), a, b) };
348        let v = self.0;
349        let v = min(v, unsafe { vrev64q_f32(v) });
350        let v = min(v, unsafe { vextq_f32(self.0, self.0, 2) });
351        unsafe { vgetq_lane_f32::<0>(v) }
352    }
353
354    /// Returns the horizontal maximum of `self`.
355    ///
356    /// In other words this computes `max(x, y, ..)`.
357    ///
358    /// NaN propagation does not follow IEEE 754-2008 semantics for maxNum.
359    #[inline]
360    #[must_use]
361    pub fn max_element(self) -> f32 {
362        let max = |a: float32x4_t, b: float32x4_t| unsafe { vbslq_f32(vcgtq_f32(a, b), a, b) };
363        let v = self.0;
364        let v = max(v, unsafe { vrev64q_f32(v) });
365        let v = max(v, unsafe { vextq_f32(self.0, self.0, 2) });
366        unsafe { vgetq_lane_f32::<0>(v) }
367    }
368
369    /// Returns the index of the first minimum element of `self`.
370    #[doc(alias = "argmin")]
371    #[inline]
372    #[must_use]
373    pub fn min_position(self) -> usize {
374        let mut min = self.x;
375        let mut index = 0;
376        if self.y < min {
377            min = self.y;
378            index = 1;
379        }
380        if self.z < min {
381            index = 2;
382        }
383        index
384    }
385
386    /// Returns the index of the first maximum element of `self`.
387    #[doc(alias = "argmax")]
388    #[inline]
389    #[must_use]
390    pub fn max_position(self) -> usize {
391        let mut max = self.x;
392        let mut index = 0;
393        if self.y > max {
394            max = self.y;
395            index = 1;
396        }
397        if self.z > max {
398            index = 2;
399        }
400        index
401    }
402
403    /// Returns the sum of all elements of `self`.
404    ///
405    /// In other words, this computes `self.x + self.y + ..`.
406    #[inline]
407    #[must_use]
408    pub fn element_sum(self) -> f32 {
409        unsafe { vaddvq_f32(vsetq_lane_f32(0.0, self.0, 3)) }
410    }
411
412    /// Returns the product of all elements of `self`.
413    ///
414    /// In other words, this computes `self.x * self.y * ..`.
415    #[inline]
416    #[must_use]
417    pub fn element_product(self) -> f32 {
418        unsafe {
419            let s = vmuls_laneq_f32(vgetq_lane_f32(self.0, 0), self.0, 1);
420            vmuls_laneq_f32(s, self.0, 2)
421        }
422    }
423
424    /// Returns a vector mask containing the result of a `==` comparison for each element of
425    /// `self` and `rhs`.
426    ///
427    /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
428    /// elements.
429    #[inline]
430    #[must_use]
431    pub fn cmpeq(self, rhs: Self) -> BVec3A {
432        BVec3A(unsafe { vceqq_f32(self.0, rhs.0) })
433    }
434
435    /// Returns a vector mask containing the result of a `!=` comparison for each element of
436    /// `self` and `rhs`.
437    ///
438    /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
439    /// elements.
440    #[inline]
441    #[must_use]
442    pub fn cmpne(self, rhs: Self) -> BVec3A {
443        BVec3A(unsafe { vmvnq_u32(vceqq_f32(self.0, rhs.0)) })
444    }
445
446    /// Returns a vector mask containing the result of a `>=` comparison for each element of
447    /// `self` and `rhs`.
448    ///
449    /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
450    /// elements.
451    #[inline]
452    #[must_use]
453    pub fn cmpge(self, rhs: Self) -> BVec3A {
454        BVec3A(unsafe { vcgeq_f32(self.0, rhs.0) })
455    }
456
457    /// Returns a vector mask containing the result of a `>` comparison for each element of
458    /// `self` and `rhs`.
459    ///
460    /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
461    /// elements.
462    #[inline]
463    #[must_use]
464    pub fn cmpgt(self, rhs: Self) -> BVec3A {
465        BVec3A(unsafe { vcgtq_f32(self.0, rhs.0) })
466    }
467
468    /// Returns a vector mask containing the result of a `<=` comparison for each element of
469    /// `self` and `rhs`.
470    ///
471    /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
472    /// elements.
473    #[inline]
474    #[must_use]
475    pub fn cmple(self, rhs: Self) -> BVec3A {
476        BVec3A(unsafe { vcleq_f32(self.0, rhs.0) })
477    }
478
479    /// Returns a vector mask containing the result of a `<` comparison for each element of
480    /// `self` and `rhs`.
481    ///
482    /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
483    /// elements.
484    #[inline]
485    #[must_use]
486    pub fn cmplt(self, rhs: Self) -> BVec3A {
487        BVec3A(unsafe { vcltq_f32(self.0, rhs.0) })
488    }
489
490    /// Returns a vector containing the absolute value of each element of `self`.
491    #[inline]
492    #[must_use]
493    pub fn abs(self) -> Self {
494        Self(unsafe { vabsq_f32(self.0) })
495    }
496
497    /// Returns a vector with elements representing the sign of `self`.
498    ///
499    /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
500    /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
501    /// - `NAN` if the number is `NAN`
502    #[inline]
503    #[must_use]
504    pub fn signum(self) -> Self {
505        let result = Self(unsafe {
506            vreinterpretq_f32_u32(vorrq_u32(
507                vandq_u32(
508                    vreinterpretq_u32_f32(self.0),
509                    vreinterpretq_u32_f32(Self::NEG_ONE.0),
510                ),
511                vreinterpretq_u32_f32(Self::ONE.0),
512            ))
513        });
514        let mask = self.is_nan_mask();
515        Self::select(mask, self, result)
516    }
517
518    /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
519    #[inline]
520    #[must_use]
521    pub fn copysign(self, rhs: Self) -> Self {
522        let mask = Self::splat(-0.0);
523        Self(unsafe {
524            vreinterpretq_f32_u32(vorrq_u32(
525                vandq_u32(vreinterpretq_u32_f32(rhs.0), vreinterpretq_u32_f32(mask.0)),
526                vandq_u32(
527                    vreinterpretq_u32_f32(self.0),
528                    vmvnq_u32(vreinterpretq_u32_f32(mask.0)),
529                ),
530            ))
531        })
532    }
533
534    /// Returns a bitmask with the lowest 3 bits set to the sign bits from the elements of `self`.
535    ///
536    /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
537    /// into the first lowest bit, element `y` into the second, etc.
538    ///
539    /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
540    /// bit and negative infinity.
541    #[inline]
542    #[must_use]
543    pub fn is_negative_bitmask(self) -> u32 {
544        unsafe {
545            let nmask = vreinterpretq_u32_f32(vdupq_n_f32(-0.0));
546            let m = vandq_u32(vreinterpretq_u32_f32(self.0), nmask);
547            let x = vgetq_lane_u32(m, 0) >> 31;
548            let y = vgetq_lane_u32(m, 1) >> 31;
549            let z = vgetq_lane_u32(m, 2) >> 31;
550
551            x | y << 1 | z << 2
552        }
553    }
554
555    /// Returns a mask indicating which components are negative.
556    ///
557    /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
558    /// bit and negative infinity.
559    #[inline]
560    #[must_use]
561    pub fn is_negative_mask(self) -> BVec3A {
562        BVec3A(unsafe { vcltq_s32(vreinterpretq_s32_f32(self.0), vdupq_n_s32(0)) })
563    }
564
565    /// Returns `true` if, and only if, all elements are finite.  If any element is either
566    /// `NaN`, positive or negative infinity, this will return `false`.
567    #[inline]
568    #[must_use]
569    pub fn is_finite(self) -> bool {
570        self.is_finite_mask().all()
571    }
572
573    /// Performs `is_finite` on each element of self, returning a vector mask of the results.
574    ///
575    /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
576    #[inline]
577    #[must_use]
578    pub fn is_finite_mask(self) -> BVec3A {
579        BVec3A(unsafe { vcltq_f32(vabsq_f32(self.0), Self::INFINITY.0) })
580    }
581
582    /// Returns `true` if any elements are `NaN`.
583    #[inline]
584    #[must_use]
585    pub fn is_nan(self) -> bool {
586        self.is_nan_mask().any()
587    }
588
589    /// Performs `is_nan` on each element of self, returning a vector mask of the results.
590    ///
591    /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
592    #[inline]
593    #[must_use]
594    pub fn is_nan_mask(self) -> BVec3A {
595        BVec3A(unsafe { vmvnq_u32(vceqq_f32(self.0, self.0)) })
596    }
597
598    /// Computes the length of `self`.
599    #[doc(alias = "magnitude")]
600    #[inline]
601    #[must_use]
602    pub fn length(self) -> f32 {
603        math::sqrt(self.dot(self))
604    }
605
606    /// Returns `true` if the vector is not the zero vector (also rejects NaN).
607    #[allow(dead_code)]
608    fn is_non_zero(self) -> bool {
609        self.length_squared() > 0.0
610    }
611
612    /// Computes the squared length of `self`.
613    ///
614    /// This is faster than `length()` as it avoids a square root operation.
615    #[doc(alias = "magnitude2")]
616    #[inline]
617    #[must_use]
618    pub fn length_squared(self) -> f32 {
619        self.dot(self)
620    }
621
622    /// Computes `1.0 / length()`.
623    ///
624    /// For valid results, `self` must _not_ be of length zero.
625    #[inline]
626    #[must_use]
627    pub fn length_recip(self) -> f32 {
628        1.0 / self.length()
629    }
630
631    /// Computes the Euclidean distance between two points in space.
632    #[inline]
633    #[must_use]
634    pub fn distance(self, rhs: Self) -> f32 {
635        (self - rhs).length()
636    }
637
638    /// Compute the squared euclidean distance between two points in space.
639    #[inline]
640    #[must_use]
641    pub fn distance_squared(self, rhs: Self) -> f32 {
642        (self - rhs).length_squared()
643    }
644
645    /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
646    #[inline]
647    #[must_use]
648    pub fn div_euclid(self, rhs: Self) -> Self {
649        Self::new(
650            math::div_euclid(self.x, rhs.x),
651            math::div_euclid(self.y, rhs.y),
652            math::div_euclid(self.z, rhs.z),
653        )
654    }
655
656    /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
657    ///
658    /// [Euclidean division]: f32::rem_euclid
659    #[inline]
660    #[must_use]
661    pub fn rem_euclid(self, rhs: Self) -> Self {
662        Self::new(
663            math::rem_euclid(self.x, rhs.x),
664            math::rem_euclid(self.y, rhs.y),
665            math::rem_euclid(self.z, rhs.z),
666        )
667    }
668
669    /// Returns `self` normalized to length 1.0.
670    ///
671    /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
672    ///
673    /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
674    ///
675    /// # Panics
676    ///
677    /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
678    #[inline]
679    #[must_use]
680    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
681    pub fn normalize(self) -> Self {
682        #[allow(clippy::let_and_return)]
683        let normalized = self.mul(self.length_recip());
684        glam_assert!(normalized.is_finite());
685        normalized
686    }
687
688    /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
689    ///
690    /// In particular, if the input is zero (or very close to zero), or non-finite,
691    /// the result of this operation will be `None`.
692    ///
693    /// See also [`Self::normalize_or_zero()`].
694    #[inline]
695    #[must_use]
696    pub fn try_normalize(self) -> Option<Self> {
697        let rcp = self.length_recip();
698        if rcp.is_finite() && rcp > 0.0 {
699            Some(self * rcp)
700        } else {
701            None
702        }
703    }
704
705    /// Returns `self` normalized to length 1.0 if possible, else returns a
706    /// fallback value.
707    ///
708    /// In particular, if the input is zero (or very close to zero), or non-finite,
709    /// the result of this operation will be the fallback value.
710    ///
711    /// See also [`Self::try_normalize()`].
712    #[inline]
713    #[must_use]
714    pub fn normalize_or(self, fallback: Self) -> Self {
715        let rcp = self.length_recip();
716        if rcp.is_finite() && rcp > 0.0 {
717            self * rcp
718        } else {
719            fallback
720        }
721    }
722
723    /// Returns `self` normalized to length 1.0 if possible, else returns zero.
724    ///
725    /// In particular, if the input is zero (or very close to zero), or non-finite,
726    /// the result of this operation will be zero.
727    ///
728    /// See also [`Self::try_normalize()`].
729    #[inline]
730    #[must_use]
731    pub fn normalize_or_zero(self) -> Self {
732        self.normalize_or(Self::ZERO)
733    }
734
735    /// Returns `self` normalized to length 1.0 and the length of `self`.
736    ///
737    /// If `self` is zero length then `(Self::X, 0.0)` is returned.
738    #[inline]
739    #[must_use]
740    pub fn normalize_and_length(self) -> (Self, f32) {
741        let length = self.length();
742        let rcp = 1.0 / length;
743        if rcp.is_finite() && rcp > 0.0 {
744            (self * rcp, length)
745        } else {
746            (Self::X, 0.0)
747        }
748    }
749
750    /// Returns whether `self` is length `1.0` or not.
751    ///
752    /// Uses a precision threshold of approximately `1e-4`.
753    #[inline]
754    #[must_use]
755    pub fn is_normalized(self) -> bool {
756        (self.length_squared() - 1.0).abs() <= 2e-4
757    }
758
759    /// Returns the vector projection of `self` onto `rhs`.
760    ///
761    /// `rhs` must be of non-zero length.
762    ///
763    /// # Panics
764    ///
765    /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
766    #[inline]
767    #[must_use]
768    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
769    pub fn project_onto(self, rhs: Self) -> Self {
770        let rhs_len_sq = rhs.dot(rhs);
771        glam_assert!(rhs_len_sq != 0.0);
772        rhs * (self.dot(rhs) / rhs_len_sq)
773    }
774
775    /// Returns the vector rejection of `self` from `rhs`.
776    ///
777    /// The vector rejection is the vector perpendicular to the projection of `self` onto
778    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
779    ///
780    /// `rhs` must be of non-zero length.
781    ///
782    /// # Panics
783    ///
784    /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
785    #[doc(alias("plane"))]
786    #[inline]
787    #[must_use]
788    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
789    pub fn reject_from(self, rhs: Self) -> Self {
790        self - self.project_onto(rhs)
791    }
792
793    /// Returns the vector projection of `self` onto `rhs`.
794    ///
795    /// `rhs` must be normalized.
796    ///
797    /// # Panics
798    ///
799    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
800    #[inline]
801    #[must_use]
802    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
803    pub fn project_onto_normalized(self, rhs: Self) -> Self {
804        glam_assert!(rhs.is_normalized());
805        rhs * self.dot(rhs)
806    }
807
808    /// Returns the vector rejection of `self` from `rhs`.
809    ///
810    /// The vector rejection is the vector perpendicular to the projection of `self` onto
811    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
812    ///
813    /// `rhs` must be normalized.
814    ///
815    /// # Panics
816    ///
817    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
818    #[doc(alias("plane"))]
819    #[inline]
820    #[must_use]
821    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
822    pub fn reject_from_normalized(self, rhs: Self) -> Self {
823        self - self.project_onto_normalized(rhs)
824    }
825
826    /// Returns a vector containing the nearest integer to a number for each element of `self`.
827    /// Round half-way cases away from 0.0.
828    #[inline]
829    #[must_use]
830    pub fn round(self) -> Self {
831        Self(unsafe { vrndaq_f32(self.0) })
832    }
833
834    /// Returns a vector containing the largest integer less than or equal to a number for each
835    /// element of `self`.
836    #[inline]
837    #[must_use]
838    pub fn floor(self) -> Self {
839        Self(unsafe { vrndmq_f32(self.0) })
840    }
841
842    /// Returns a vector containing the smallest integer greater than or equal to a number for
843    /// each element of `self`.
844    #[inline]
845    #[must_use]
846    pub fn ceil(self) -> Self {
847        Self(unsafe { vrndpq_f32(self.0) })
848    }
849
850    /// Returns a vector containing the integer part each element of `self`. This means numbers are
851    /// always truncated towards zero.
852    #[inline]
853    #[must_use]
854    pub fn trunc(self) -> Self {
855        Self(unsafe { vrndq_f32(self.0) })
856    }
857
858    /// Returns a vector containing `0.0` if `rhs < self` and 1.0 otherwise.
859    ///
860    /// Similar to glsl's step(edge, x), which translates into edge.step(x)
861    #[inline]
862    #[must_use]
863    pub fn step(self, rhs: Self) -> Self {
864        Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
865    }
866
867    /// Performs Hermite interpolation between `0.0` and `1.0` using `x` normalized to `[edge0, edge1]`.
868    ///
869    /// This is equivalent to `t * t * (3.0 - 2.0 * t)`, where `t` is clamped to `[0.0, 1.0]`.
870    /// Results are undefined if any element of `edge0` is greater than or equal to the corresponding
871    /// element of `edge1`.
872    ///
873    /// # Panics
874    ///
875    /// Will panic if any element of `edge0` is greater than or equal to the corresponding element
876    /// of `edge1`, when `glam_assert` is enabled.
877    #[inline]
878    #[must_use]
879    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
880    pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
881        glam_assert!(edge0.cmplt(edge1).all());
882        let t = ((self - edge0) / (edge1 - edge0)).saturate();
883        t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
884    }
885
886    /// Returns a vector containing all elements of `self` clamped to the range of `[0, 1]`.
887    #[inline]
888    #[must_use]
889    pub fn saturate(self) -> Self {
890        self.clamp(Self::ZERO, Self::ONE)
891    }
892
893    /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
894    ///
895    /// Note that this differs from the GLSL implementation of `fract` which returns
896    /// `self - self.floor()`.
897    ///
898    /// Note that this is fast but not precise for large numbers.
899    #[inline]
900    #[must_use]
901    pub fn fract(self) -> Self {
902        self - self.trunc()
903    }
904
905    /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
906    ///
907    /// Note that this differs from the Rust implementation of `fract` which returns
908    /// `self - self.trunc()`.
909    ///
910    /// Note that this is fast but not precise for large numbers.
911    #[inline]
912    #[must_use]
913    pub fn fract_gl(self) -> Self {
914        self - self.floor()
915    }
916
917    /// Returns a vector containing `e^self` (the exponential function) for each element of
918    /// `self`.
919    #[inline]
920    #[must_use]
921    pub fn exp(self) -> Self {
922        Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
923    }
924
925    /// Returns a vector containing `2^self` for each element of `self`.
926    #[inline]
927    #[must_use]
928    pub fn exp2(self) -> Self {
929        Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
930    }
931
932    /// Returns a vector containing the natural logarithm for each element of `self`.
933    /// This returns NaN when the element is negative and negative infinity when the element is zero.
934    #[inline]
935    #[must_use]
936    pub fn ln(self) -> Self {
937        Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
938    }
939
940    /// Returns a vector containing the base 2 logarithm for each element of `self`.
941    /// This returns NaN when the element is negative and negative infinity when the element is zero.
942    #[inline]
943    #[must_use]
944    pub fn log2(self) -> Self {
945        Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
946    }
947
948    /// Returns a vector containing each element of `self` raised to the power of `n`.
949    #[inline]
950    #[must_use]
951    pub fn powf(self, n: f32) -> Self {
952        Self::new(
953            math::powf(self.x, n),
954            math::powf(self.y, n),
955            math::powf(self.z, n),
956        )
957    }
958
959    /// Returns a vector containing the square root for each element of `self`.
960    /// This returns NaN when the element is negative.
961    #[inline]
962    #[must_use]
963    pub fn sqrt(self) -> Self {
964        Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
965    }
966
967    /// Returns a vector containing the cosine for each element of `self`.
968    #[inline]
969    #[must_use]
970    pub fn cos(self) -> Self {
971        Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
972    }
973
974    /// Returns a vector containing the sine for each element of `self`.
975    #[inline]
976    #[must_use]
977    pub fn sin(self) -> Self {
978        Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
979    }
980
981    /// Returns a tuple of two vectors containing the sine and cosine for each element of `self`.
982    #[inline]
983    #[must_use]
984    pub fn sin_cos(self) -> (Self, Self) {
985        let (sin_x, cos_x) = math::sin_cos(self.x);
986        let (sin_y, cos_y) = math::sin_cos(self.y);
987        let (sin_z, cos_z) = math::sin_cos(self.z);
988
989        (
990            Self::new(sin_x, sin_y, sin_z),
991            Self::new(cos_x, cos_y, cos_z),
992        )
993    }
994
995    /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
996    #[inline]
997    #[must_use]
998    pub fn recip(self) -> Self {
999        Self(unsafe { vdivq_f32(Self::ONE.0, self.0) })
1000    }
1001
1002    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`, using the
1003    /// form `self * (1.0 - s) + rhs * s`.
1004    ///
1005    /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result will
1006    /// be equal to `rhs`. When `s` is outside of the range `[0, 1]`, the result is linearly
1007    /// extrapolated.
1008    ///
1009    /// The result is guaranteed to be `self` at `s == 0.0` and `rhs` at `s == 1.0`, even when the
1010    /// values differ greatly in magnitude, but it is not monotonic in `s` for nearly equal inputs
1011    /// and may not preserve equal inputs exactly. Consider [`lerp_monotonic`](Self::lerp_monotonic)
1012    /// instead when interpolating between values that may be equal or nearly equal.
1013    #[doc(alias = "mix")]
1014    #[inline]
1015    #[must_use]
1016    pub fn lerp(self, rhs: Self, s: f32) -> Self {
1017        self * (1.0 - s) + rhs * s
1018    }
1019
1020    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`, using the
1021    /// monotonic form `self + (rhs - self) * s`.
1022    ///
1023    /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result will
1024    /// be equal to `rhs`. When `s` is outside of the range `[0, 1]`, the result is linearly
1025    /// extrapolated.
1026    ///
1027    /// Prefer this over [`lerp`](Self::lerp) when interpolating between values that may be equal or
1028    /// nearly equal: the result is monotonic in `s` and equal inputs are preserved exactly, avoiding
1029    /// the rounding jitter that [`lerp`](Self::lerp) can introduce. The tradeoff is that
1030    /// `rhs - self` is evaluated first, so this is less accurate than [`lerp`](Self::lerp) when
1031    /// `self` and `rhs` differ greatly in magnitude, and overflows to infinity when they have
1032    /// opposite signs and large magnitudes.
1033    ///
1034    /// On SIMD back-ends the multiply and add are fused when the target supports it, which has a
1035    /// single rounding step and can be more accurate than a separate multiply and add.
1036    #[doc(alias = "mix")]
1037    #[inline]
1038    #[must_use]
1039    pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
1040        Self(unsafe { vfmaq_f32(self.0, vsubq_f32(rhs.0, self.0), vdupq_n_f32(s)) })
1041    }
1042
1043    /// Moves towards `rhs` based on the value `d`.
1044    ///
1045    /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
1046    /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
1047    #[inline]
1048    #[must_use]
1049    pub fn move_towards(self, rhs: Self, d: f32) -> Self {
1050        let a = rhs - self;
1051        let len = a.length();
1052        if len <= d || len <= 1e-4 {
1053            return rhs;
1054        }
1055        self + a / len * d
1056    }
1057
1058    /// Calculates the midpoint between `self` and `rhs`.
1059    ///
1060    /// The midpoint is the average of, or halfway point between, two vectors.
1061    /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
1062    /// while being slightly cheaper to compute.
1063    #[inline]
1064    pub fn midpoint(self, rhs: Self) -> Self {
1065        (self + rhs) * 0.5
1066    }
1067
1068    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
1069    /// less than or equal to `max_abs_diff`.
1070    ///
1071    /// This can be used to compare if two vectors contain similar elements. It works best when
1072    /// comparing with a known value. The `max_abs_diff` that should be used used depends on
1073    /// the values being compared against.
1074    ///
1075    /// For more see
1076    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1077    #[inline]
1078    #[must_use]
1079    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1080        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1081    }
1082
1083    /// Returns a vector with a length no less than `min` and no more than `max`.
1084    ///
1085    /// # Panics
1086    ///
1087    /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
1088    #[inline]
1089    #[must_use]
1090    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1091    pub fn clamp_length(self, min: f32, max: f32) -> Self {
1092        glam_assert!(0.0 <= min);
1093        glam_assert!(min <= max);
1094        let length_sq = self.length_squared();
1095        if length_sq < min * min {
1096            min * (self / math::sqrt(length_sq))
1097        } else if length_sq > max * max {
1098            max * (self / math::sqrt(length_sq))
1099        } else {
1100            self
1101        }
1102    }
1103
1104    /// Returns a vector with a length no more than `max`.
1105    ///
1106    /// # Panics
1107    ///
1108    /// Will panic if `max` is negative when `glam_assert` is enabled.
1109    #[inline]
1110    #[must_use]
1111    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1112    pub fn clamp_length_max(self, max: f32) -> Self {
1113        glam_assert!(0.0 <= max);
1114        let length_sq = self.length_squared();
1115        if length_sq > max * max {
1116            max * (self / math::sqrt(length_sq))
1117        } else {
1118            self
1119        }
1120    }
1121
1122    /// Returns a vector with a length no less than `min`.
1123    ///
1124    /// # Panics
1125    ///
1126    /// Will panic if `min` is negative when `glam_assert` is enabled.
1127    #[inline]
1128    #[must_use]
1129    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1130    pub fn clamp_length_min(self, min: f32) -> Self {
1131        glam_assert!(0.0 <= min);
1132        let length_sq = self.length_squared();
1133        if length_sq < min * min {
1134            min * (self / math::sqrt(length_sq))
1135        } else {
1136            self
1137        }
1138    }
1139
1140    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
1141    /// error, yielding a more accurate result than an unfused multiply-add.
1142    ///
1143    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
1144    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
1145    /// and will be heavily dependant on designing algorithms with specific target hardware in
1146    /// mind.
1147    #[inline]
1148    #[must_use]
1149    pub fn mul_add(self, a: Self, b: Self) -> Self {
1150        Self(unsafe { vfmaq_f32(b.0, self.0, a.0) })
1151    }
1152
1153    /// Returns the reflection vector for a given incident vector `self` and surface normal
1154    /// `normal`.
1155    ///
1156    /// `normal` must be normalized.
1157    ///
1158    /// # Panics
1159    ///
1160    /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
1161    #[inline]
1162    #[must_use]
1163    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1164    pub fn reflect(self, normal: Self) -> Self {
1165        glam_assert!(normal.is_normalized());
1166        self - 2.0 * self.dot(normal) * normal
1167    }
1168
1169    /// Returns the refraction direction for a given incident vector `self`, surface normal
1170    /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
1171    /// a zero vector will be returned.
1172    ///
1173    /// `self` and `normal` must be normalized.
1174    ///
1175    /// # Panics
1176    ///
1177    /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
1178    #[inline]
1179    #[must_use]
1180    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1181    pub fn refract(self, normal: Self, eta: f32) -> Self {
1182        glam_assert!(self.is_normalized());
1183        glam_assert!(normal.is_normalized());
1184        let n_dot_i = normal.dot(self);
1185        let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1186        if k >= 0.0 {
1187            eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1188        } else {
1189            Self::ZERO
1190        }
1191    }
1192
1193    /// Returns the angle (in radians) between two vectors in the range `[0, +Ï€]`.
1194    ///
1195    /// For the full rotation between two vectors as a quaternion, see
1196    /// [`Quat::from_rotation_arc`].
1197    ///
1198    /// The inputs do not need to be unit vectors however they must be non-zero.
1199    ///
1200    /// # Panics
1201    ///
1202    /// Will panic if `self` or `rhs` has zero length when `glam_assert` is enabled.
1203    #[inline]
1204    #[must_use]
1205    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1206    pub fn angle_between(self, rhs: Self) -> f32 {
1207        glam_assert!(self.is_non_zero());
1208        glam_assert!(rhs.is_non_zero());
1209        math::acos_approx(
1210            self.dot(rhs)
1211                .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1212        )
1213    }
1214
1215    /// Returns the signed angle (in radians) from `self` to `rhs` around `axis`
1216    /// in the range `[-Ï€, +Ï€]`.
1217    ///
1218    /// The `axis` must be a unit vector. The angle follows the right-hand rule
1219    /// around `axis` and can be used with [`Self::rotate_axis`], e.g.
1220    /// `self.rotate_axis(axis, self.angle_to(rhs, axis))` will be equal to `rhs`.
1221    ///
1222    /// For the unsigned angle without a reference axis, see [`Self::angle_between`].
1223    ///
1224    /// The inputs do not need to be unit vectors however they must be non-zero.
1225    ///
1226    /// # Panics
1227    ///
1228    /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
1229    /// Will panic if `self` or `rhs` has zero length when `glam_assert` is enabled.
1230    #[doc(alias = "signed_angle")]
1231    #[inline]
1232    #[must_use]
1233    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1234    pub fn angle_to(self, rhs: Self, axis: Self) -> f32 {
1235        glam_assert!(axis.is_normalized());
1236        glam_assert!(self.is_non_zero());
1237        glam_assert!(rhs.is_non_zero());
1238        math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1239    }
1240
1241    /// Rotates around the x axis by `angle` (in radians).
1242    #[inline]
1243    #[must_use]
1244    pub fn rotate_x(self, angle: f32) -> Self {
1245        let (sina, cosa) = math::sin_cos(angle);
1246        Self::new(
1247            self.x,
1248            self.y * cosa - self.z * sina,
1249            self.y * sina + self.z * cosa,
1250        )
1251    }
1252
1253    /// Rotates around the y axis by `angle` (in radians).
1254    #[inline]
1255    #[must_use]
1256    pub fn rotate_y(self, angle: f32) -> Self {
1257        let (sina, cosa) = math::sin_cos(angle);
1258        Self::new(
1259            self.x * cosa + self.z * sina,
1260            self.y,
1261            self.x * -sina + self.z * cosa,
1262        )
1263    }
1264
1265    /// Rotates around the z axis by `angle` (in radians).
1266    #[inline]
1267    #[must_use]
1268    pub fn rotate_z(self, angle: f32) -> Self {
1269        let (sina, cosa) = math::sin_cos(angle);
1270        Self::new(
1271            self.x * cosa - self.y * sina,
1272            self.x * sina + self.y * cosa,
1273            self.z,
1274        )
1275    }
1276
1277    /// Rotates around `axis` by `angle` (in radians).
1278    ///
1279    /// The axis must be a unit vector.
1280    ///
1281    /// # Panics
1282    ///
1283    /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
1284    #[inline]
1285    #[must_use]
1286    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1287    pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1288        Quat::from_axis_angle(axis.into(), angle) * self
1289    }
1290
1291    /// Rotates towards `rhs` up to `max_angle` (in radians).
1292    ///
1293    /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
1294    /// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative,
1295    /// rotates towards the exact opposite of `rhs`. Will not go past the target.
1296    ///
1297    /// # Panics
1298    ///
1299    /// Will panic if `self` or `rhs` are zero length when `glam_assert` is enabled.
1300    #[inline]
1301    #[must_use]
1302    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1303    pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1304        let angle_between = self.angle_between(rhs);
1305        // When `max_angle < 0`, rotate no further than `PI` radians away
1306        let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1307        let axis = self
1308            .cross(rhs)
1309            .try_normalize()
1310            .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1311        Quat::from_axis_angle(axis.into(), angle) * self
1312    }
1313
1314    /// Returns some vector that is orthogonal to the given one.
1315    ///
1316    /// The input vector must be finite and non-zero.
1317    ///
1318    /// The output vector is not necessarily unit length. For that use
1319    /// [`Self::any_orthonormal_vector()`] instead.
1320    #[inline]
1321    #[must_use]
1322    pub fn any_orthogonal_vector(self) -> Self {
1323        // This can probably be optimized
1324        if self.x.abs() > self.y.abs() {
1325            Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
1326        } else {
1327            Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
1328        }
1329    }
1330
1331    /// Returns any unit vector that is orthogonal to the given one.
1332    ///
1333    /// The input vector must be unit length.
1334    ///
1335    /// # Panics
1336    ///
1337    /// Will panic if `self` is not normalized when `glam_assert` is enabled.
1338    #[inline]
1339    #[must_use]
1340    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1341    pub fn any_orthonormal_vector(self) -> Self {
1342        glam_assert!(self.is_normalized());
1343        // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
1344        let sign = self.z.signum();
1345        let a = -1.0 / (sign + self.z);
1346        let b = self.x * self.y * a;
1347        Self::new(b, sign + self.y * self.y * a, -self.y)
1348    }
1349
1350    /// Given a unit vector return two other vectors that together form a right-handed orthonormal
1351    /// basis. That is, all three vectors are orthogonal to each other and are normalized.
1352    ///
1353    /// # Panics
1354    ///
1355    /// Will panic if `self` is not normalized when `glam_assert` is enabled.
1356    #[inline]
1357    #[must_use]
1358    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1359    pub fn any_orthonormal_pair(self) -> (Self, Self) {
1360        glam_assert!(self.is_normalized());
1361        // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
1362        let sign = self.z.signum();
1363        let a = -1.0 / (sign + self.z);
1364        let b = self.x * self.y * a;
1365        (
1366            Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1367            Self::new(b, sign + self.y * self.y * a, -self.y),
1368        )
1369    }
1370
1371    /// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`.
1372    ///
1373    /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
1374    /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
1375    /// extrapolated.
1376    ///
1377    /// # Panics
1378    ///
1379    /// Will panic if `self` or `rhs` has zero length when `glam_assert` is enabled.
1380    #[inline]
1381    #[must_use]
1382    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1383    pub fn slerp(self, rhs: Self, s: f32) -> Self {
1384        glam_assert!(self.is_non_zero());
1385        glam_assert!(rhs.is_non_zero());
1386        let self_length = self.length();
1387        let rhs_length = rhs.length();
1388        // Cosine of the angle between the vectors [-1, 1], or NaN if either vector has a zero length
1389        let dot = self.dot(rhs) / (self_length * rhs_length);
1390        // If dot is close to 1 or -1, or is NaN the calculations for t1 and t2 break down
1391        if dot.abs() < 1.0 - 3e-7 {
1392            // Angle between the vectors [0, +Ï€]
1393            let theta = math::acos_approx(dot);
1394            // Sine of the angle between vectors [0, 1]
1395            let coefs = [1.0 - s, s, 1.0, 0.0];
1396            let (sin_theta, t1, t2) = unsafe {
1397                let tmp = vmulq_f32(vdupq_n_f32(theta), vld1q_f32(coefs.as_ptr()));
1398                let tmp = f32x4_sin(tmp);
1399                (
1400                    vgetq_lane_f32(tmp, 2), // sin(theta)
1401                    vgetq_lane_f32(tmp, 0), // sin(theta * (1.0 - s))
1402                    vgetq_lane_f32(tmp, 1), // sin(theta * s)
1403                )
1404            };
1405
1406            // Interpolate vector lengths. The monotonic form keeps equal lengths exact.
1407            let result_length = self_length.lerp_monotonic(rhs_length, s);
1408            // Scale the vectors to the target length and interpolate them
1409            return (self * (result_length / self_length) * t1
1410                + rhs * (result_length / rhs_length) * t2)
1411                * (1.0 / sin_theta);
1412        }
1413        if dot < 0.0 {
1414            // Vectors are almost parallel in opposing directions
1415
1416            // Create a rotation from self to rhs along some axis
1417            let axis = self.any_orthogonal_vector().normalize().into();
1418            let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1419            // Interpolate vector lengths. The monotonic form keeps equal lengths exact.
1420            let result_length = self_length.lerp_monotonic(rhs_length, s);
1421            rotation * self * (result_length / self_length)
1422        } else {
1423            // Vectors are almost parallel in the same direction, or dot was NaN
1424            self.lerp(rhs, s)
1425        }
1426    }
1427
1428    /// Casts all elements of `self` to `f64`.
1429    #[cfg(feature = "f64")]
1430    #[inline]
1431    #[must_use]
1432    pub fn as_dvec3(self) -> crate::DVec3 {
1433        crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1434    }
1435
1436    /// Casts all elements of `self` to `i8`.
1437    #[cfg(feature = "i8")]
1438    #[inline]
1439    #[must_use]
1440    pub fn as_i8vec3(self) -> crate::I8Vec3 {
1441        crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1442    }
1443
1444    /// Casts all elements of `self` to `u8`.
1445    #[cfg(feature = "u8")]
1446    #[inline]
1447    #[must_use]
1448    pub fn as_u8vec3(self) -> crate::U8Vec3 {
1449        crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1450    }
1451
1452    /// Casts all elements of `self` to `i16`.
1453    #[cfg(feature = "i16")]
1454    #[inline]
1455    #[must_use]
1456    pub fn as_i16vec3(self) -> crate::I16Vec3 {
1457        crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1458    }
1459
1460    /// Casts all elements of `self` to `u16`.
1461    #[cfg(feature = "u16")]
1462    #[inline]
1463    #[must_use]
1464    pub fn as_u16vec3(self) -> crate::U16Vec3 {
1465        crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1466    }
1467
1468    /// Casts all elements of `self` to `i32`.
1469    #[cfg(feature = "i32")]
1470    #[inline]
1471    #[must_use]
1472    pub fn as_ivec3(self) -> crate::IVec3 {
1473        crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1474    }
1475
1476    /// Casts all elements of `self` to `u32`.
1477    #[cfg(feature = "u32")]
1478    #[inline]
1479    #[must_use]
1480    pub fn as_uvec3(self) -> crate::UVec3 {
1481        crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1482    }
1483
1484    /// Casts all elements of `self` to `i64`.
1485    #[cfg(feature = "i64")]
1486    #[inline]
1487    #[must_use]
1488    pub fn as_i64vec3(self) -> crate::I64Vec3 {
1489        crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1490    }
1491
1492    /// Casts all elements of `self` to `u64`.
1493    #[cfg(feature = "u64")]
1494    #[inline]
1495    #[must_use]
1496    pub fn as_u64vec3(self) -> crate::U64Vec3 {
1497        crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1498    }
1499
1500    /// Casts all elements of `self` to `isize`.
1501    #[cfg(feature = "isize")]
1502    #[inline]
1503    #[must_use]
1504    pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1505        crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1506    }
1507
1508    /// Casts all elements of `self` to `usize`.
1509    #[cfg(feature = "usize")]
1510    #[inline]
1511    #[must_use]
1512    pub fn as_usizevec3(self) -> crate::USizeVec3 {
1513        crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1514    }
1515}
1516
1517impl Default for Vec3A {
1518    #[inline(always)]
1519    fn default() -> Self {
1520        Self::ZERO
1521    }
1522}
1523
1524impl PartialEq for Vec3A {
1525    #[inline]
1526    fn eq(&self, rhs: &Self) -> bool {
1527        self.cmpeq(*rhs).all()
1528    }
1529}
1530
1531impl Div for Vec3A {
1532    type Output = Self;
1533    #[inline]
1534    fn div(self, rhs: Self) -> Self {
1535        Self(unsafe { vdivq_f32(self.0, rhs.0) })
1536    }
1537}
1538
1539impl Div<&Self> for Vec3A {
1540    type Output = Self;
1541    #[inline]
1542    fn div(self, rhs: &Self) -> Self {
1543        self.div(*rhs)
1544    }
1545}
1546
1547impl Div<&Vec3A> for &Vec3A {
1548    type Output = Vec3A;
1549    #[inline]
1550    fn div(self, rhs: &Vec3A) -> Vec3A {
1551        (*self).div(*rhs)
1552    }
1553}
1554
1555impl Div<Vec3A> for &Vec3A {
1556    type Output = Vec3A;
1557    #[inline]
1558    fn div(self, rhs: Vec3A) -> Vec3A {
1559        (*self).div(rhs)
1560    }
1561}
1562
1563impl DivAssign for Vec3A {
1564    #[inline]
1565    fn div_assign(&mut self, rhs: Self) {
1566        self.0 = unsafe { vdivq_f32(self.0, rhs.0) };
1567    }
1568}
1569
1570impl DivAssign<&Self> for Vec3A {
1571    #[inline]
1572    fn div_assign(&mut self, rhs: &Self) {
1573        self.div_assign(*rhs);
1574    }
1575}
1576
1577impl Div<f32> for Vec3A {
1578    type Output = Self;
1579    #[inline]
1580    fn div(self, rhs: f32) -> Self {
1581        Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) })
1582    }
1583}
1584
1585impl Div<&f32> for Vec3A {
1586    type Output = Self;
1587    #[inline]
1588    fn div(self, rhs: &f32) -> Self {
1589        self.div(*rhs)
1590    }
1591}
1592
1593impl Div<&f32> for &Vec3A {
1594    type Output = Vec3A;
1595    #[inline]
1596    fn div(self, rhs: &f32) -> Vec3A {
1597        (*self).div(*rhs)
1598    }
1599}
1600
1601impl Div<f32> for &Vec3A {
1602    type Output = Vec3A;
1603    #[inline]
1604    fn div(self, rhs: f32) -> Vec3A {
1605        (*self).div(rhs)
1606    }
1607}
1608
1609impl DivAssign<f32> for Vec3A {
1610    #[inline]
1611    fn div_assign(&mut self, rhs: f32) {
1612        self.0 = unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) };
1613    }
1614}
1615
1616impl DivAssign<&f32> for Vec3A {
1617    #[inline]
1618    fn div_assign(&mut self, rhs: &f32) {
1619        self.div_assign(*rhs);
1620    }
1621}
1622
1623impl Div<Vec3A> for f32 {
1624    type Output = Vec3A;
1625    #[inline]
1626    fn div(self, rhs: Vec3A) -> Vec3A {
1627        Vec3A(unsafe { vdivq_f32(vld1q_dup_f32(&self), rhs.0) })
1628    }
1629}
1630
1631impl Div<&Vec3A> for f32 {
1632    type Output = Vec3A;
1633    #[inline]
1634    fn div(self, rhs: &Vec3A) -> Vec3A {
1635        self.div(*rhs)
1636    }
1637}
1638
1639impl Div<&Vec3A> for &f32 {
1640    type Output = Vec3A;
1641    #[inline]
1642    fn div(self, rhs: &Vec3A) -> Vec3A {
1643        (*self).div(*rhs)
1644    }
1645}
1646
1647impl Div<Vec3A> for &f32 {
1648    type Output = Vec3A;
1649    #[inline]
1650    fn div(self, rhs: Vec3A) -> Vec3A {
1651        (*self).div(rhs)
1652    }
1653}
1654
1655impl Mul for Vec3A {
1656    type Output = Self;
1657    #[inline]
1658    fn mul(self, rhs: Self) -> Self {
1659        Self(unsafe { vmulq_f32(self.0, rhs.0) })
1660    }
1661}
1662
1663impl Mul<&Self> for Vec3A {
1664    type Output = Self;
1665    #[inline]
1666    fn mul(self, rhs: &Self) -> Self {
1667        self.mul(*rhs)
1668    }
1669}
1670
1671impl Mul<&Vec3A> for &Vec3A {
1672    type Output = Vec3A;
1673    #[inline]
1674    fn mul(self, rhs: &Vec3A) -> Vec3A {
1675        (*self).mul(*rhs)
1676    }
1677}
1678
1679impl Mul<Vec3A> for &Vec3A {
1680    type Output = Vec3A;
1681    #[inline]
1682    fn mul(self, rhs: Vec3A) -> Vec3A {
1683        (*self).mul(rhs)
1684    }
1685}
1686
1687impl MulAssign for Vec3A {
1688    #[inline]
1689    fn mul_assign(&mut self, rhs: Self) {
1690        self.0 = unsafe { vmulq_f32(self.0, rhs.0) };
1691    }
1692}
1693
1694impl MulAssign<&Self> for Vec3A {
1695    #[inline]
1696    fn mul_assign(&mut self, rhs: &Self) {
1697        self.mul_assign(*rhs);
1698    }
1699}
1700
1701impl Mul<f32> for Vec3A {
1702    type Output = Self;
1703    #[inline]
1704    fn mul(self, rhs: f32) -> Self {
1705        Self(unsafe { vmulq_n_f32(self.0, rhs) })
1706    }
1707}
1708
1709impl Mul<&f32> for Vec3A {
1710    type Output = Self;
1711    #[inline]
1712    fn mul(self, rhs: &f32) -> Self {
1713        self.mul(*rhs)
1714    }
1715}
1716
1717impl Mul<&f32> for &Vec3A {
1718    type Output = Vec3A;
1719    #[inline]
1720    fn mul(self, rhs: &f32) -> Vec3A {
1721        (*self).mul(*rhs)
1722    }
1723}
1724
1725impl Mul<f32> for &Vec3A {
1726    type Output = Vec3A;
1727    #[inline]
1728    fn mul(self, rhs: f32) -> Vec3A {
1729        (*self).mul(rhs)
1730    }
1731}
1732
1733impl MulAssign<f32> for Vec3A {
1734    #[inline]
1735    fn mul_assign(&mut self, rhs: f32) {
1736        self.0 = unsafe { vmulq_n_f32(self.0, rhs) };
1737    }
1738}
1739
1740impl MulAssign<&f32> for Vec3A {
1741    #[inline]
1742    fn mul_assign(&mut self, rhs: &f32) {
1743        self.mul_assign(*rhs);
1744    }
1745}
1746
1747impl Mul<Vec3A> for f32 {
1748    type Output = Vec3A;
1749    #[inline]
1750    fn mul(self, rhs: Vec3A) -> Vec3A {
1751        Vec3A(unsafe { vmulq_n_f32(rhs.0, self) })
1752    }
1753}
1754
1755impl Mul<&Vec3A> for f32 {
1756    type Output = Vec3A;
1757    #[inline]
1758    fn mul(self, rhs: &Vec3A) -> Vec3A {
1759        self.mul(*rhs)
1760    }
1761}
1762
1763impl Mul<&Vec3A> for &f32 {
1764    type Output = Vec3A;
1765    #[inline]
1766    fn mul(self, rhs: &Vec3A) -> Vec3A {
1767        (*self).mul(*rhs)
1768    }
1769}
1770
1771impl Mul<Vec3A> for &f32 {
1772    type Output = Vec3A;
1773    #[inline]
1774    fn mul(self, rhs: Vec3A) -> Vec3A {
1775        (*self).mul(rhs)
1776    }
1777}
1778
1779impl Add for Vec3A {
1780    type Output = Self;
1781    #[inline]
1782    fn add(self, rhs: Self) -> Self {
1783        Self(unsafe { vaddq_f32(self.0, rhs.0) })
1784    }
1785}
1786
1787impl Add<&Self> for Vec3A {
1788    type Output = Self;
1789    #[inline]
1790    fn add(self, rhs: &Self) -> Self {
1791        self.add(*rhs)
1792    }
1793}
1794
1795impl Add<&Vec3A> for &Vec3A {
1796    type Output = Vec3A;
1797    #[inline]
1798    fn add(self, rhs: &Vec3A) -> Vec3A {
1799        (*self).add(*rhs)
1800    }
1801}
1802
1803impl Add<Vec3A> for &Vec3A {
1804    type Output = Vec3A;
1805    #[inline]
1806    fn add(self, rhs: Vec3A) -> Vec3A {
1807        (*self).add(rhs)
1808    }
1809}
1810
1811impl AddAssign for Vec3A {
1812    #[inline]
1813    fn add_assign(&mut self, rhs: Self) {
1814        self.0 = unsafe { vaddq_f32(self.0, rhs.0) };
1815    }
1816}
1817
1818impl AddAssign<&Self> for Vec3A {
1819    #[inline]
1820    fn add_assign(&mut self, rhs: &Self) {
1821        self.add_assign(*rhs);
1822    }
1823}
1824
1825impl Add<f32> for Vec3A {
1826    type Output = Self;
1827    #[inline]
1828    fn add(self, rhs: f32) -> Self {
1829        Self(unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) })
1830    }
1831}
1832
1833impl Add<&f32> for Vec3A {
1834    type Output = Self;
1835    #[inline]
1836    fn add(self, rhs: &f32) -> Self {
1837        self.add(*rhs)
1838    }
1839}
1840
1841impl Add<&f32> for &Vec3A {
1842    type Output = Vec3A;
1843    #[inline]
1844    fn add(self, rhs: &f32) -> Vec3A {
1845        (*self).add(*rhs)
1846    }
1847}
1848
1849impl Add<f32> for &Vec3A {
1850    type Output = Vec3A;
1851    #[inline]
1852    fn add(self, rhs: f32) -> Vec3A {
1853        (*self).add(rhs)
1854    }
1855}
1856
1857impl AddAssign<f32> for Vec3A {
1858    #[inline]
1859    fn add_assign(&mut self, rhs: f32) {
1860        self.0 = unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) };
1861    }
1862}
1863
1864impl AddAssign<&f32> for Vec3A {
1865    #[inline]
1866    fn add_assign(&mut self, rhs: &f32) {
1867        self.add_assign(*rhs);
1868    }
1869}
1870
1871impl Add<Vec3A> for f32 {
1872    type Output = Vec3A;
1873    #[inline]
1874    fn add(self, rhs: Vec3A) -> Vec3A {
1875        Vec3A(unsafe { vaddq_f32(vld1q_dup_f32(&self), rhs.0) })
1876    }
1877}
1878
1879impl Add<&Vec3A> for f32 {
1880    type Output = Vec3A;
1881    #[inline]
1882    fn add(self, rhs: &Vec3A) -> Vec3A {
1883        self.add(*rhs)
1884    }
1885}
1886
1887impl Add<&Vec3A> for &f32 {
1888    type Output = Vec3A;
1889    #[inline]
1890    fn add(self, rhs: &Vec3A) -> Vec3A {
1891        (*self).add(*rhs)
1892    }
1893}
1894
1895impl Add<Vec3A> for &f32 {
1896    type Output = Vec3A;
1897    #[inline]
1898    fn add(self, rhs: Vec3A) -> Vec3A {
1899        (*self).add(rhs)
1900    }
1901}
1902
1903impl Sub for Vec3A {
1904    type Output = Self;
1905    #[inline]
1906    fn sub(self, rhs: Self) -> Self {
1907        Self(unsafe { vsubq_f32(self.0, rhs.0) })
1908    }
1909}
1910
1911impl Sub<&Self> for Vec3A {
1912    type Output = Self;
1913    #[inline]
1914    fn sub(self, rhs: &Self) -> Self {
1915        self.sub(*rhs)
1916    }
1917}
1918
1919impl Sub<&Vec3A> for &Vec3A {
1920    type Output = Vec3A;
1921    #[inline]
1922    fn sub(self, rhs: &Vec3A) -> Vec3A {
1923        (*self).sub(*rhs)
1924    }
1925}
1926
1927impl Sub<Vec3A> for &Vec3A {
1928    type Output = Vec3A;
1929    #[inline]
1930    fn sub(self, rhs: Vec3A) -> Vec3A {
1931        (*self).sub(rhs)
1932    }
1933}
1934
1935impl SubAssign for Vec3A {
1936    #[inline]
1937    fn sub_assign(&mut self, rhs: Self) {
1938        self.0 = unsafe { vsubq_f32(self.0, rhs.0) };
1939    }
1940}
1941
1942impl SubAssign<&Self> for Vec3A {
1943    #[inline]
1944    fn sub_assign(&mut self, rhs: &Self) {
1945        self.sub_assign(*rhs);
1946    }
1947}
1948
1949impl Sub<f32> for Vec3A {
1950    type Output = Self;
1951    #[inline]
1952    fn sub(self, rhs: f32) -> Self {
1953        Self(unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) })
1954    }
1955}
1956
1957impl Sub<&f32> for Vec3A {
1958    type Output = Self;
1959    #[inline]
1960    fn sub(self, rhs: &f32) -> Self {
1961        self.sub(*rhs)
1962    }
1963}
1964
1965impl Sub<&f32> for &Vec3A {
1966    type Output = Vec3A;
1967    #[inline]
1968    fn sub(self, rhs: &f32) -> Vec3A {
1969        (*self).sub(*rhs)
1970    }
1971}
1972
1973impl Sub<f32> for &Vec3A {
1974    type Output = Vec3A;
1975    #[inline]
1976    fn sub(self, rhs: f32) -> Vec3A {
1977        (*self).sub(rhs)
1978    }
1979}
1980
1981impl SubAssign<f32> for Vec3A {
1982    #[inline]
1983    fn sub_assign(&mut self, rhs: f32) {
1984        self.0 = unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) };
1985    }
1986}
1987
1988impl SubAssign<&f32> for Vec3A {
1989    #[inline]
1990    fn sub_assign(&mut self, rhs: &f32) {
1991        self.sub_assign(*rhs);
1992    }
1993}
1994
1995impl Sub<Vec3A> for f32 {
1996    type Output = Vec3A;
1997    #[inline]
1998    fn sub(self, rhs: Vec3A) -> Vec3A {
1999        Vec3A(unsafe { vsubq_f32(vld1q_dup_f32(&self), rhs.0) })
2000    }
2001}
2002
2003impl Sub<&Vec3A> for f32 {
2004    type Output = Vec3A;
2005    #[inline]
2006    fn sub(self, rhs: &Vec3A) -> Vec3A {
2007        self.sub(*rhs)
2008    }
2009}
2010
2011impl Sub<&Vec3A> for &f32 {
2012    type Output = Vec3A;
2013    #[inline]
2014    fn sub(self, rhs: &Vec3A) -> Vec3A {
2015        (*self).sub(*rhs)
2016    }
2017}
2018
2019impl Sub<Vec3A> for &f32 {
2020    type Output = Vec3A;
2021    #[inline]
2022    fn sub(self, rhs: Vec3A) -> Vec3A {
2023        (*self).sub(rhs)
2024    }
2025}
2026
2027impl Rem for Vec3A {
2028    type Output = Self;
2029    #[inline]
2030    fn rem(self, rhs: Self) -> Self {
2031        unsafe {
2032            let n = vrndmq_f32(vdivq_f32(self.0, rhs.0));
2033            Self(vsubq_f32(self.0, vmulq_f32(n, rhs.0)))
2034        }
2035    }
2036}
2037
2038impl Rem<&Self> for Vec3A {
2039    type Output = Self;
2040    #[inline]
2041    fn rem(self, rhs: &Self) -> Self {
2042        self.rem(*rhs)
2043    }
2044}
2045
2046impl Rem<&Vec3A> for &Vec3A {
2047    type Output = Vec3A;
2048    #[inline]
2049    fn rem(self, rhs: &Vec3A) -> Vec3A {
2050        (*self).rem(*rhs)
2051    }
2052}
2053
2054impl Rem<Vec3A> for &Vec3A {
2055    type Output = Vec3A;
2056    #[inline]
2057    fn rem(self, rhs: Vec3A) -> Vec3A {
2058        (*self).rem(rhs)
2059    }
2060}
2061
2062impl RemAssign for Vec3A {
2063    #[inline]
2064    fn rem_assign(&mut self, rhs: Self) {
2065        *self = self.rem(rhs);
2066    }
2067}
2068
2069impl RemAssign<&Self> for Vec3A {
2070    #[inline]
2071    fn rem_assign(&mut self, rhs: &Self) {
2072        self.rem_assign(*rhs);
2073    }
2074}
2075
2076impl Rem<f32> for Vec3A {
2077    type Output = Self;
2078    #[inline]
2079    fn rem(self, rhs: f32) -> Self {
2080        self.rem(Self::splat(rhs))
2081    }
2082}
2083
2084impl Rem<&f32> for Vec3A {
2085    type Output = Self;
2086    #[inline]
2087    fn rem(self, rhs: &f32) -> Self {
2088        self.rem(*rhs)
2089    }
2090}
2091
2092impl Rem<&f32> for &Vec3A {
2093    type Output = Vec3A;
2094    #[inline]
2095    fn rem(self, rhs: &f32) -> Vec3A {
2096        (*self).rem(*rhs)
2097    }
2098}
2099
2100impl Rem<f32> for &Vec3A {
2101    type Output = Vec3A;
2102    #[inline]
2103    fn rem(self, rhs: f32) -> Vec3A {
2104        (*self).rem(rhs)
2105    }
2106}
2107
2108impl RemAssign<f32> for Vec3A {
2109    #[inline]
2110    fn rem_assign(&mut self, rhs: f32) {
2111        *self = self.rem(Self::splat(rhs));
2112    }
2113}
2114
2115impl RemAssign<&f32> for Vec3A {
2116    #[inline]
2117    fn rem_assign(&mut self, rhs: &f32) {
2118        self.rem_assign(*rhs);
2119    }
2120}
2121
2122impl Rem<Vec3A> for f32 {
2123    type Output = Vec3A;
2124    #[inline]
2125    fn rem(self, rhs: Vec3A) -> Vec3A {
2126        Vec3A::splat(self).rem(rhs)
2127    }
2128}
2129
2130impl Rem<&Vec3A> for f32 {
2131    type Output = Vec3A;
2132    #[inline]
2133    fn rem(self, rhs: &Vec3A) -> Vec3A {
2134        self.rem(*rhs)
2135    }
2136}
2137
2138impl Rem<&Vec3A> for &f32 {
2139    type Output = Vec3A;
2140    #[inline]
2141    fn rem(self, rhs: &Vec3A) -> Vec3A {
2142        (*self).rem(*rhs)
2143    }
2144}
2145
2146impl Rem<Vec3A> for &f32 {
2147    type Output = Vec3A;
2148    #[inline]
2149    fn rem(self, rhs: Vec3A) -> Vec3A {
2150        (*self).rem(rhs)
2151    }
2152}
2153
2154impl AsRef<[f32; 3]> for Vec3A {
2155    #[inline]
2156    fn as_ref(&self) -> &[f32; 3] {
2157        unsafe { &*(self as *const Self as *const [f32; 3]) }
2158    }
2159}
2160
2161impl AsMut<[f32; 3]> for Vec3A {
2162    #[inline]
2163    fn as_mut(&mut self) -> &mut [f32; 3] {
2164        unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2165    }
2166}
2167
2168impl Sum for Vec3A {
2169    #[inline]
2170    fn sum<I>(iter: I) -> Self
2171    where
2172        I: Iterator<Item = Self>,
2173    {
2174        iter.fold(Self::ZERO, Self::add)
2175    }
2176}
2177
2178impl<'a> Sum<&'a Self> for Vec3A {
2179    #[inline]
2180    fn sum<I>(iter: I) -> Self
2181    where
2182        I: Iterator<Item = &'a Self>,
2183    {
2184        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2185    }
2186}
2187
2188impl Product for Vec3A {
2189    #[inline]
2190    fn product<I>(iter: I) -> Self
2191    where
2192        I: Iterator<Item = Self>,
2193    {
2194        iter.fold(Self::ONE, Self::mul)
2195    }
2196}
2197
2198impl<'a> Product<&'a Self> for Vec3A {
2199    #[inline]
2200    fn product<I>(iter: I) -> Self
2201    where
2202        I: Iterator<Item = &'a Self>,
2203    {
2204        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2205    }
2206}
2207
2208impl Neg for Vec3A {
2209    type Output = Self;
2210    #[inline]
2211    fn neg(self) -> Self {
2212        Self(unsafe { vnegq_f32(self.0) })
2213    }
2214}
2215
2216impl Neg for &Vec3A {
2217    type Output = Vec3A;
2218    #[inline]
2219    fn neg(self) -> Vec3A {
2220        (*self).neg()
2221    }
2222}
2223
2224impl Index<usize> for Vec3A {
2225    type Output = f32;
2226    #[inline]
2227    #[track_caller]
2228    fn index(&self, index: usize) -> &Self::Output {
2229        match index {
2230            0 => &self.x,
2231            1 => &self.y,
2232            2 => &self.z,
2233            _ => panic!("index out of bounds"),
2234        }
2235    }
2236}
2237
2238impl IndexMut<usize> for Vec3A {
2239    #[inline]
2240    #[track_caller]
2241    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2242        match index {
2243            0 => &mut self.x,
2244            1 => &mut self.y,
2245            2 => &mut self.z,
2246            _ => panic!("index out of bounds"),
2247        }
2248    }
2249}
2250
2251impl fmt::Display for Vec3A {
2252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2253        if let Some(p) = f.precision() {
2254            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2255        } else {
2256            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2257        }
2258    }
2259}
2260
2261impl fmt::Debug for Vec3A {
2262    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2263        fmt.debug_tuple(stringify!(Vec3A))
2264            .field(&self.x)
2265            .field(&self.y)
2266            .field(&self.z)
2267            .finish()
2268    }
2269}
2270
2271impl From<Vec3A> for float32x4_t {
2272    #[inline(always)]
2273    fn from(t: Vec3A) -> Self {
2274        t.0
2275    }
2276}
2277
2278impl From<float32x4_t> for Vec3A {
2279    #[inline(always)]
2280    fn from(t: float32x4_t) -> Self {
2281        Self(t)
2282    }
2283}
2284
2285impl From<[f32; 3]> for Vec3A {
2286    #[inline]
2287    fn from(a: [f32; 3]) -> Self {
2288        Self::new(a[0], a[1], a[2])
2289    }
2290}
2291
2292impl From<Vec3A> for [f32; 3] {
2293    #[inline]
2294    fn from(v: Vec3A) -> Self {
2295        use crate::align16::Align16;
2296        use core::mem::MaybeUninit;
2297        let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2298        unsafe {
2299            vst1q_f32(out.as_mut_ptr().cast(), v.0);
2300            out.assume_init().0
2301        }
2302    }
2303}
2304
2305impl From<(f32, f32, f32)> for Vec3A {
2306    #[inline]
2307    fn from(t: (f32, f32, f32)) -> Self {
2308        Self::new(t.0, t.1, t.2)
2309    }
2310}
2311
2312impl From<Vec3A> for (f32, f32, f32) {
2313    #[inline]
2314    fn from(v: Vec3A) -> Self {
2315        (v.x, v.y, v.z)
2316    }
2317}
2318
2319impl From<Vec3> for Vec3A {
2320    #[inline]
2321    fn from(v: Vec3) -> Self {
2322        Self::new(v.x, v.y, v.z)
2323    }
2324}
2325
2326impl From<Vec3A> for Vec3 {
2327    #[inline]
2328    fn from(v: Vec3A) -> Self {
2329        use crate::align16::Align16;
2330        use core::mem::MaybeUninit;
2331        let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2332        unsafe {
2333            vst1q_f32(out.as_mut_ptr().cast(), v.0);
2334            out.assume_init().0
2335        }
2336    }
2337}
2338
2339impl From<(Vec2, f32)> for Vec3A {
2340    #[inline]
2341    fn from((v, z): (Vec2, f32)) -> Self {
2342        Self::new(v.x, v.y, z)
2343    }
2344}
2345
2346impl Deref for Vec3A {
2347    type Target = crate::deref::Vec3<f32>;
2348    #[inline]
2349    fn deref(&self) -> &Self::Target {
2350        unsafe { &*(self as *const Self).cast() }
2351    }
2352}
2353
2354impl DerefMut for Vec3A {
2355    #[inline]
2356    fn deref_mut(&mut self) -> &mut Self::Target {
2357        unsafe { &mut *(self as *mut Self).cast() }
2358    }
2359}
2360
2361impl From<BVec3> for Vec3A {
2362    #[inline]
2363    fn from(v: BVec3) -> Self {
2364        Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2365    }
2366}
2367
2368impl From<BVec3A> for Vec3A {
2369    #[inline]
2370    fn from(v: BVec3A) -> Self {
2371        let bool_array: [bool; 3] = v.into();
2372        Self::new(
2373            f32::from(bool_array[0]),
2374            f32::from(bool_array[1]),
2375            f32::from(bool_array[2]),
2376        )
2377    }
2378}