Skip to main content

glam/f32/sse2/
vec3a.rs

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