Skip to main content

glam/f32/neon/
vec4.rs

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