Skip to main content

glam/f32/sse2/
vec4.rs

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