• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2 
3 use crate::{coresimd::*, f32::math, BVec4, BVec4A, Vec2, Vec3, Vec3A};
4 
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::{f32, ops::*};
8 
9 use core::simd::{cmp::SimdPartialEq, cmp::SimdPartialOrd, num::SimdFloat, *};
10 use std::simd::StdFloat;
11 
12 /// Creates a 4-dimensional vector.
13 #[inline(always)]
14 #[must_use]
vec4(x: f32, y: f32, z: f32, w: f32) -> Vec415 pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
16     Vec4::new(x, y, z, w)
17 }
18 
19 /// A 4-dimensional vector.
20 ///
21 /// SIMD vector types are used for storage on supported platforms.
22 ///
23 /// This type is 16 byte aligned.
24 #[derive(Clone, Copy)]
25 #[repr(transparent)]
26 pub struct Vec4(pub(crate) f32x4);
27 
28 impl Vec4 {
29     /// All zeroes.
30     pub const ZERO: Self = Self::splat(0.0);
31 
32     /// All ones.
33     pub const ONE: Self = Self::splat(1.0);
34 
35     /// All negative ones.
36     pub const NEG_ONE: Self = Self::splat(-1.0);
37 
38     /// All `f32::MIN`.
39     pub const MIN: Self = Self::splat(f32::MIN);
40 
41     /// All `f32::MAX`.
42     pub const MAX: Self = Self::splat(f32::MAX);
43 
44     /// All `f32::NAN`.
45     pub const NAN: Self = Self::splat(f32::NAN);
46 
47     /// All `f32::INFINITY`.
48     pub const INFINITY: Self = Self::splat(f32::INFINITY);
49 
50     /// All `f32::NEG_INFINITY`.
51     pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
52 
53     /// A unit vector pointing along the positive X axis.
54     pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
55 
56     /// A unit vector pointing along the positive Y axis.
57     pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
58 
59     /// A unit vector pointing along the positive Z axis.
60     pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
61 
62     /// A unit vector pointing along the positive W axis.
63     pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
64 
65     /// A unit vector pointing along the negative X axis.
66     pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
67 
68     /// A unit vector pointing along the negative Y axis.
69     pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
70 
71     /// A unit vector pointing along the negative Z axis.
72     pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
73 
74     /// A unit vector pointing along the negative W axis.
75     pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
76 
77     /// The unit axes.
78     pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
79 
80     /// Creates a new vector.
81     #[inline(always)]
82     #[must_use]
new(x: f32, y: f32, z: f32, w: f32) -> Self83     pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
84         Self(f32x4::from_array([x, y, z, w]))
85     }
86 
87     /// Creates a vector with all elements set to `v`.
88     #[inline]
89     #[must_use]
splat(v: f32) -> Self90     pub const fn splat(v: f32) -> Self {
91         Self(Simd::from_array([v; 4]))
92     }
93 
94     /// Returns a vector containing each element of `self` modified by a mapping function `f`.
95     #[inline]
96     #[must_use]
map<F>(self, f: F) -> Self where F: Fn(f32) -> f32,97     pub fn map<F>(self, f: F) -> Self
98     where
99         F: Fn(f32) -> f32,
100     {
101         Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
102     }
103 
104     /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
105     /// for each element of `self`.
106     ///
107     /// A true element in the mask uses the corresponding element from `if_true`, and false
108     /// uses the element from `if_false`.
109     #[inline]
110     #[must_use]
select(mask: BVec4A, if_true: Self, if_false: Self) -> Self111     pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
112         Self(mask.0.select(if_true.0, if_false.0))
113     }
114 
115     /// Creates a new vector from an array.
116     #[inline]
117     #[must_use]
from_array(a: [f32; 4]) -> Self118     pub const fn from_array(a: [f32; 4]) -> Self {
119         Self::new(a[0], a[1], a[2], a[3])
120     }
121 
122     /// `[x, y, z, w]`
123     #[inline]
124     #[must_use]
to_array(&self) -> [f32; 4]125     pub const fn to_array(&self) -> [f32; 4] {
126         unsafe { *(self as *const Vec4 as *const [f32; 4]) }
127     }
128 
129     /// Creates a vector from the first 4 values in `slice`.
130     ///
131     /// # Panics
132     ///
133     /// Panics if `slice` is less than 4 elements long.
134     #[inline]
135     #[must_use]
from_slice(slice: &[f32]) -> Self136     pub const fn from_slice(slice: &[f32]) -> Self {
137         assert!(slice.len() >= 4);
138         Self::new(slice[0], slice[1], slice[2], slice[3])
139     }
140 
141     /// Writes the elements of `self` to the first 4 elements in `slice`.
142     ///
143     /// # Panics
144     ///
145     /// Panics if `slice` is less than 4 elements long.
146     #[inline]
write_to_slice(self, slice: &mut [f32])147     pub fn write_to_slice(self, slice: &mut [f32]) {
148         slice[..4].copy_from_slice(&self.to_array());
149     }
150 
151     /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
152     ///
153     /// Truncation to [`Vec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
154     ///
155     /// To truncate to [`Vec3A`] use [`Vec3A::from_vec4()`].
156     #[inline]
157     #[must_use]
truncate(self) -> Vec3158     pub fn truncate(self) -> Vec3 {
159         use crate::swizzles::Vec4Swizzles;
160         self.xyz()
161     }
162 
163     /// Creates a 4D vector from `self` with the given value of `x`.
164     #[inline]
165     #[must_use]
with_x(mut self, x: f32) -> Self166     pub fn with_x(mut self, x: f32) -> Self {
167         self.x = x;
168         self
169     }
170 
171     /// Creates a 4D vector from `self` with the given value of `y`.
172     #[inline]
173     #[must_use]
with_y(mut self, y: f32) -> Self174     pub fn with_y(mut self, y: f32) -> Self {
175         self.y = y;
176         self
177     }
178 
179     /// Creates a 4D vector from `self` with the given value of `z`.
180     #[inline]
181     #[must_use]
with_z(mut self, z: f32) -> Self182     pub fn with_z(mut self, z: f32) -> Self {
183         self.z = z;
184         self
185     }
186 
187     /// Creates a 4D vector from `self` with the given value of `w`.
188     #[inline]
189     #[must_use]
with_w(mut self, w: f32) -> Self190     pub fn with_w(mut self, w: f32) -> Self {
191         self.w = w;
192         self
193     }
194 
195     /// Computes the dot product of `self` and `rhs`.
196     #[inline]
197     #[must_use]
dot(self, rhs: Self) -> f32198     pub fn dot(self, rhs: Self) -> f32 {
199         dot4(self.0, rhs.0)
200     }
201 
202     /// Returns a vector where every component is the dot product of `self` and `rhs`.
203     #[inline]
204     #[must_use]
dot_into_vec(self, rhs: Self) -> Self205     pub fn dot_into_vec(self, rhs: Self) -> Self {
206         Self(dot4_into_f32x4(self.0, rhs.0))
207     }
208 
209     /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
210     ///
211     /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
212     #[inline]
213     #[must_use]
min(self, rhs: Self) -> Self214     pub fn min(self, rhs: Self) -> Self {
215         Self(self.0.simd_min(rhs.0))
216     }
217 
218     /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
219     ///
220     /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
221     #[inline]
222     #[must_use]
max(self, rhs: Self) -> Self223     pub fn max(self, rhs: Self) -> Self {
224         Self(self.0.simd_max(rhs.0))
225     }
226 
227     /// Component-wise clamping of values, similar to [`f32::clamp`].
228     ///
229     /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
230     ///
231     /// # Panics
232     ///
233     /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
234     #[inline]
235     #[must_use]
clamp(self, min: Self, max: Self) -> Self236     pub fn clamp(self, min: Self, max: Self) -> Self {
237         glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
238         self.max(min).min(max)
239     }
240 
241     /// Returns the horizontal minimum of `self`.
242     ///
243     /// In other words this computes `min(x, y, ..)`.
244     #[inline]
245     #[must_use]
min_element(self) -> f32246     pub fn min_element(self) -> f32 {
247         self.0.reduce_min()
248     }
249 
250     /// Returns the horizontal maximum of `self`.
251     ///
252     /// In other words this computes `max(x, y, ..)`.
253     #[inline]
254     #[must_use]
max_element(self) -> f32255     pub fn max_element(self) -> f32 {
256         self.0.reduce_max()
257     }
258 
259     /// Returns the sum of all elements of `self`.
260     ///
261     /// In other words, this computes `self.x + self.y + ..`.
262     #[inline]
263     #[must_use]
element_sum(self) -> f32264     pub fn element_sum(self) -> f32 {
265         self.0.reduce_sum()
266     }
267 
268     /// Returns the product of all elements of `self`.
269     ///
270     /// In other words, this computes `self.x * self.y * ..`.
271     #[inline]
272     #[must_use]
element_product(self) -> f32273     pub fn element_product(self) -> f32 {
274         self.0.reduce_product()
275     }
276 
277     /// Returns a vector mask containing the result of a `==` comparison for each element of
278     /// `self` and `rhs`.
279     ///
280     /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
281     /// elements.
282     #[inline]
283     #[must_use]
cmpeq(self, rhs: Self) -> BVec4A284     pub fn cmpeq(self, rhs: Self) -> BVec4A {
285         BVec4A(f32x4::simd_eq(self.0, rhs.0))
286     }
287 
288     /// Returns a vector mask containing the result of a `!=` comparison for each element of
289     /// `self` and `rhs`.
290     ///
291     /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
292     /// elements.
293     #[inline]
294     #[must_use]
cmpne(self, rhs: Self) -> BVec4A295     pub fn cmpne(self, rhs: Self) -> BVec4A {
296         BVec4A(f32x4::simd_ne(self.0, rhs.0))
297     }
298 
299     /// Returns a vector mask containing the result of a `>=` comparison for each element of
300     /// `self` and `rhs`.
301     ///
302     /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
303     /// elements.
304     #[inline]
305     #[must_use]
cmpge(self, rhs: Self) -> BVec4A306     pub fn cmpge(self, rhs: Self) -> BVec4A {
307         BVec4A(f32x4::simd_ge(self.0, rhs.0))
308     }
309 
310     /// Returns a vector mask containing the result of a `>` comparison for each element of
311     /// `self` and `rhs`.
312     ///
313     /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
314     /// elements.
315     #[inline]
316     #[must_use]
cmpgt(self, rhs: Self) -> BVec4A317     pub fn cmpgt(self, rhs: Self) -> BVec4A {
318         BVec4A(f32x4::simd_gt(self.0, rhs.0))
319     }
320 
321     /// Returns a vector mask containing the result of a `<=` comparison for each element of
322     /// `self` and `rhs`.
323     ///
324     /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
325     /// elements.
326     #[inline]
327     #[must_use]
cmple(self, rhs: Self) -> BVec4A328     pub fn cmple(self, rhs: Self) -> BVec4A {
329         BVec4A(f32x4::simd_le(self.0, rhs.0))
330     }
331 
332     /// Returns a vector mask containing the result of a `<` comparison for each element of
333     /// `self` and `rhs`.
334     ///
335     /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
336     /// elements.
337     #[inline]
338     #[must_use]
cmplt(self, rhs: Self) -> BVec4A339     pub fn cmplt(self, rhs: Self) -> BVec4A {
340         BVec4A(f32x4::simd_lt(self.0, rhs.0))
341     }
342 
343     /// Returns a vector containing the absolute value of each element of `self`.
344     #[inline]
345     #[must_use]
abs(self) -> Self346     pub fn abs(self) -> Self {
347         Self(self.0.abs())
348     }
349 
350     /// Returns a vector with elements representing the sign of `self`.
351     ///
352     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
353     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
354     /// - `NAN` if the number is `NAN`
355     #[inline]
356     #[must_use]
signum(self) -> Self357     pub fn signum(self) -> Self {
358         Self(self.0.signum())
359     }
360 
361     /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
362     #[inline]
363     #[must_use]
copysign(self, rhs: Self) -> Self364     pub fn copysign(self, rhs: Self) -> Self {
365         Self(self.0.copysign(rhs.0))
366     }
367 
368     /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
369     ///
370     /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
371     /// into the first lowest bit, element `y` into the second, etc.
372     #[inline]
373     #[must_use]
is_negative_bitmask(self) -> u32374     pub fn is_negative_bitmask(self) -> u32 {
375         self.0.is_sign_negative().to_bitmask() as u32
376     }
377 
378     /// Returns `true` if, and only if, all elements are finite.  If any element is either
379     /// `NaN`, positive or negative infinity, this will return `false`.
380     #[inline]
381     #[must_use]
is_finite(self) -> bool382     pub fn is_finite(self) -> bool {
383         self.is_finite_mask().all()
384     }
385 
386     /// Performs `is_finite` on each element of self, returning a vector mask of the results.
387     ///
388     /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
is_finite_mask(self) -> BVec4A389     pub fn is_finite_mask(self) -> BVec4A {
390         BVec4A(f32x4::is_finite(self.0))
391     }
392 
393     /// Returns `true` if any elements are `NaN`.
394     #[inline]
395     #[must_use]
is_nan(self) -> bool396     pub fn is_nan(self) -> bool {
397         self.is_nan_mask().any()
398     }
399 
400     /// Performs `is_nan` on each element of self, returning a vector mask of the results.
401     ///
402     /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
403     #[inline]
404     #[must_use]
is_nan_mask(self) -> BVec4A405     pub fn is_nan_mask(self) -> BVec4A {
406         BVec4A(f32x4::is_nan(self.0))
407     }
408 
409     /// Computes the length of `self`.
410     #[doc(alias = "magnitude")]
411     #[inline]
412     #[must_use]
length(self) -> f32413     pub fn length(self) -> f32 {
414         let dot = dot4_in_x(self.0, self.0);
415         dot.sqrt()[0]
416     }
417 
418     /// Computes the squared length of `self`.
419     ///
420     /// This is faster than `length()` as it avoids a square root operation.
421     #[doc(alias = "magnitude2")]
422     #[inline]
423     #[must_use]
length_squared(self) -> f32424     pub fn length_squared(self) -> f32 {
425         self.dot(self)
426     }
427 
428     /// Computes `1.0 / length()`.
429     ///
430     /// For valid results, `self` must _not_ be of length zero.
431     #[inline]
432     #[must_use]
length_recip(self) -> f32433     pub fn length_recip(self) -> f32 {
434         let dot = dot4_in_x(self.0, self.0);
435         dot.sqrt().recip()[0]
436     }
437 
438     /// Computes the Euclidean distance between two points in space.
439     #[inline]
440     #[must_use]
distance(self, rhs: Self) -> f32441     pub fn distance(self, rhs: Self) -> f32 {
442         (self - rhs).length()
443     }
444 
445     /// Compute the squared euclidean distance between two points in space.
446     #[inline]
447     #[must_use]
distance_squared(self, rhs: Self) -> f32448     pub fn distance_squared(self, rhs: Self) -> f32 {
449         (self - rhs).length_squared()
450     }
451 
452     /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
453     #[inline]
454     #[must_use]
div_euclid(self, rhs: Self) -> Self455     pub fn div_euclid(self, rhs: Self) -> Self {
456         Self::new(
457             math::div_euclid(self.x, rhs.x),
458             math::div_euclid(self.y, rhs.y),
459             math::div_euclid(self.z, rhs.z),
460             math::div_euclid(self.w, rhs.w),
461         )
462     }
463 
464     /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
465     ///
466     /// [Euclidean division]: f32::rem_euclid
467     #[inline]
468     #[must_use]
rem_euclid(self, rhs: Self) -> Self469     pub fn rem_euclid(self, rhs: Self) -> Self {
470         Self::new(
471             math::rem_euclid(self.x, rhs.x),
472             math::rem_euclid(self.y, rhs.y),
473             math::rem_euclid(self.z, rhs.z),
474             math::rem_euclid(self.w, rhs.w),
475         )
476     }
477 
478     /// Returns `self` normalized to length 1.0.
479     ///
480     /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
481     ///
482     /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
483     ///
484     /// Panics
485     ///
486     /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
487     #[inline]
488     #[must_use]
normalize(self) -> Self489     pub fn normalize(self) -> Self {
490         let length = dot4_into_f32x4(self.0, self.0).sqrt();
491         #[allow(clippy::let_and_return)]
492         let normalized = Self(self.0 / length);
493         glam_assert!(normalized.is_finite());
494         normalized
495     }
496 
497     /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
498     ///
499     /// In particular, if the input is zero (or very close to zero), or non-finite,
500     /// the result of this operation will be `None`.
501     ///
502     /// See also [`Self::normalize_or_zero()`].
503     #[inline]
504     #[must_use]
try_normalize(self) -> Option<Self>505     pub fn try_normalize(self) -> Option<Self> {
506         let rcp = self.length_recip();
507         if rcp.is_finite() && rcp > 0.0 {
508             Some(self * rcp)
509         } else {
510             None
511         }
512     }
513 
514     /// Returns `self` normalized to length 1.0 if possible, else returns a
515     /// fallback value.
516     ///
517     /// In particular, if the input is zero (or very close to zero), or non-finite,
518     /// the result of this operation will be the fallback value.
519     ///
520     /// See also [`Self::try_normalize()`].
521     #[inline]
522     #[must_use]
normalize_or(self, fallback: Self) -> Self523     pub fn normalize_or(self, fallback: Self) -> Self {
524         let rcp = self.length_recip();
525         if rcp.is_finite() && rcp > 0.0 {
526             self * rcp
527         } else {
528             fallback
529         }
530     }
531 
532     /// Returns `self` normalized to length 1.0 if possible, else returns zero.
533     ///
534     /// In particular, if the input is zero (or very close to zero), or non-finite,
535     /// the result of this operation will be zero.
536     ///
537     /// See also [`Self::try_normalize()`].
538     #[inline]
539     #[must_use]
normalize_or_zero(self) -> Self540     pub fn normalize_or_zero(self) -> Self {
541         self.normalize_or(Self::ZERO)
542     }
543 
544     /// Returns whether `self` is length `1.0` or not.
545     ///
546     /// Uses a precision threshold of approximately `1e-4`.
547     #[inline]
548     #[must_use]
is_normalized(self) -> bool549     pub fn is_normalized(self) -> bool {
550         math::abs(self.length_squared() - 1.0) <= 2e-4
551     }
552 
553     /// Returns the vector projection of `self` onto `rhs`.
554     ///
555     /// `rhs` must be of non-zero length.
556     ///
557     /// # Panics
558     ///
559     /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
560     #[inline]
561     #[must_use]
project_onto(self, rhs: Self) -> Self562     pub fn project_onto(self, rhs: Self) -> Self {
563         let other_len_sq_rcp = rhs.dot(rhs).recip();
564         glam_assert!(other_len_sq_rcp.is_finite());
565         rhs * self.dot(rhs) * other_len_sq_rcp
566     }
567 
568     /// Returns the vector rejection of `self` from `rhs`.
569     ///
570     /// The vector rejection is the vector perpendicular to the projection of `self` onto
571     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
572     ///
573     /// `rhs` must be of non-zero length.
574     ///
575     /// # Panics
576     ///
577     /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
578     #[doc(alias("plane"))]
579     #[inline]
580     #[must_use]
reject_from(self, rhs: Self) -> Self581     pub fn reject_from(self, rhs: Self) -> Self {
582         self - self.project_onto(rhs)
583     }
584 
585     /// Returns the vector projection of `self` onto `rhs`.
586     ///
587     /// `rhs` must be normalized.
588     ///
589     /// # Panics
590     ///
591     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
592     #[inline]
593     #[must_use]
project_onto_normalized(self, rhs: Self) -> Self594     pub fn project_onto_normalized(self, rhs: Self) -> Self {
595         glam_assert!(rhs.is_normalized());
596         rhs * self.dot(rhs)
597     }
598 
599     /// Returns the vector rejection of `self` from `rhs`.
600     ///
601     /// The vector rejection is the vector perpendicular to the projection of `self` onto
602     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
603     ///
604     /// `rhs` must be normalized.
605     ///
606     /// # Panics
607     ///
608     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
609     #[doc(alias("plane"))]
610     #[inline]
611     #[must_use]
reject_from_normalized(self, rhs: Self) -> Self612     pub fn reject_from_normalized(self, rhs: Self) -> Self {
613         self - self.project_onto_normalized(rhs)
614     }
615 
616     /// Returns a vector containing the nearest integer to a number for each element of `self`.
617     /// Round half-way cases away from 0.0.
618     #[inline]
619     #[must_use]
round(self) -> Self620     pub fn round(self) -> Self {
621         Self(self.0.round())
622     }
623 
624     /// Returns a vector containing the largest integer less than or equal to a number for each
625     /// element of `self`.
626     #[inline]
627     #[must_use]
floor(self) -> Self628     pub fn floor(self) -> Self {
629         Self(self.0.floor())
630     }
631 
632     /// Returns a vector containing the smallest integer greater than or equal to a number for
633     /// each element of `self`.
634     #[inline]
635     #[must_use]
ceil(self) -> Self636     pub fn ceil(self) -> Self {
637         Self(self.0.ceil())
638     }
639 
640     /// Returns a vector containing the integer part each element of `self`. This means numbers are
641     /// always truncated towards zero.
642     #[inline]
643     #[must_use]
trunc(self) -> Self644     pub fn trunc(self) -> Self {
645         Self(self.0.trunc())
646     }
647 
648     /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
649     ///
650     /// Note that this differs from the GLSL implementation of `fract` which returns
651     /// `self - self.floor()`.
652     ///
653     /// Note that this is fast but not precise for large numbers.
654     #[inline]
655     #[must_use]
fract(self) -> Self656     pub fn fract(self) -> Self {
657         self - self.trunc()
658     }
659 
660     /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
661     ///
662     /// Note that this differs from the Rust implementation of `fract` which returns
663     /// `self - self.trunc()`.
664     ///
665     /// Note that this is fast but not precise for large numbers.
666     #[inline]
667     #[must_use]
fract_gl(self) -> Self668     pub fn fract_gl(self) -> Self {
669         self - self.floor()
670     }
671 
672     /// Returns a vector containing `e^self` (the exponential function) for each element of
673     /// `self`.
674     #[inline]
675     #[must_use]
exp(self) -> Self676     pub fn exp(self) -> Self {
677         Self::new(
678             math::exp(self.x),
679             math::exp(self.y),
680             math::exp(self.z),
681             math::exp(self.w),
682         )
683     }
684 
685     /// Returns a vector containing each element of `self` raised to the power of `n`.
686     #[inline]
687     #[must_use]
powf(self, n: f32) -> Self688     pub fn powf(self, n: f32) -> Self {
689         Self::new(
690             math::powf(self.x, n),
691             math::powf(self.y, n),
692             math::powf(self.z, n),
693             math::powf(self.w, n),
694         )
695     }
696 
697     /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
698     #[inline]
699     #[must_use]
recip(self) -> Self700     pub fn recip(self) -> Self {
701         Self(self.0.recip())
702     }
703 
704     /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
705     ///
706     /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
707     /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
708     /// extrapolated.
709     #[doc(alias = "mix")]
710     #[inline]
711     #[must_use]
lerp(self, rhs: Self, s: f32) -> Self712     pub fn lerp(self, rhs: Self, s: f32) -> Self {
713         self * (1.0 - s) + rhs * s
714     }
715 
716     /// Moves towards `rhs` based on the value `d`.
717     ///
718     /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
719     /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
720     #[inline]
721     #[must_use]
move_towards(&self, rhs: Self, d: f32) -> Self722     pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
723         let a = rhs - *self;
724         let len = a.length();
725         if len <= d || len <= 1e-4 {
726             return rhs;
727         }
728         *self + a / len * d
729     }
730 
731     /// Calculates the midpoint between `self` and `rhs`.
732     ///
733     /// The midpoint is the average of, or halfway point between, two vectors.
734     /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
735     /// while being slightly cheaper to compute.
736     #[inline]
midpoint(self, rhs: Self) -> Self737     pub fn midpoint(self, rhs: Self) -> Self {
738         (self + rhs) * 0.5
739     }
740 
741     /// Returns true if the absolute difference of all elements between `self` and `rhs` is
742     /// less than or equal to `max_abs_diff`.
743     ///
744     /// This can be used to compare if two vectors contain similar elements. It works best when
745     /// comparing with a known value. The `max_abs_diff` that should be used used depends on
746     /// the values being compared against.
747     ///
748     /// For more see
749     /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
750     #[inline]
751     #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool752     pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
753         self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
754     }
755 
756     /// Returns a vector with a length no less than `min` and no more than `max`.
757     ///
758     /// # Panics
759     ///
760     /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
761     #[inline]
762     #[must_use]
clamp_length(self, min: f32, max: f32) -> Self763     pub fn clamp_length(self, min: f32, max: f32) -> Self {
764         glam_assert!(0.0 <= min);
765         glam_assert!(min <= max);
766         let length_sq = self.length_squared();
767         if length_sq < min * min {
768             min * (self / math::sqrt(length_sq))
769         } else if length_sq > max * max {
770             max * (self / math::sqrt(length_sq))
771         } else {
772             self
773         }
774     }
775 
776     /// Returns a vector with a length no more than `max`.
777     ///
778     /// # Panics
779     ///
780     /// Will panic if `max` is negative when `glam_assert` is enabled.
781     #[inline]
782     #[must_use]
clamp_length_max(self, max: f32) -> Self783     pub fn clamp_length_max(self, max: f32) -> Self {
784         glam_assert!(0.0 <= max);
785         let length_sq = self.length_squared();
786         if length_sq > max * max {
787             max * (self / math::sqrt(length_sq))
788         } else {
789             self
790         }
791     }
792 
793     /// Returns a vector with a length no less than `min`.
794     ///
795     /// # Panics
796     ///
797     /// Will panic if `min` is negative when `glam_assert` is enabled.
798     #[inline]
799     #[must_use]
clamp_length_min(self, min: f32) -> Self800     pub fn clamp_length_min(self, min: f32) -> Self {
801         glam_assert!(0.0 <= min);
802         let length_sq = self.length_squared();
803         if length_sq < min * min {
804             min * (self / math::sqrt(length_sq))
805         } else {
806             self
807         }
808     }
809 
810     /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
811     /// error, yielding a more accurate result than an unfused multiply-add.
812     ///
813     /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
814     /// architecture has a dedicated fma CPU instruction. However, this is not always true,
815     /// and will be heavily dependant on designing algorithms with specific target hardware in
816     /// mind.
817     #[inline]
818     #[must_use]
mul_add(self, a: Self, b: Self) -> Self819     pub fn mul_add(self, a: Self, b: Self) -> Self {
820         Self(self.0.mul_add(a.0, b.0))
821     }
822 
823     /// Returns the reflection vector for a given incident vector `self` and surface normal
824     /// `normal`.
825     ///
826     /// `normal` must be normalized.
827     ///
828     /// # Panics
829     ///
830     /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
831     #[inline]
832     #[must_use]
reflect(self, normal: Self) -> Self833     pub fn reflect(self, normal: Self) -> Self {
834         glam_assert!(normal.is_normalized());
835         self - 2.0 * self.dot(normal) * normal
836     }
837 
838     /// Returns the refraction direction for a given incident vector `self`, surface normal
839     /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
840     /// a zero vector will be returned.
841     ///
842     /// `self` and `normal` must be normalized.
843     ///
844     /// # Panics
845     ///
846     /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
847     #[inline]
848     #[must_use]
refract(self, normal: Self, eta: f32) -> Self849     pub fn refract(self, normal: Self, eta: f32) -> Self {
850         glam_assert!(self.is_normalized());
851         glam_assert!(normal.is_normalized());
852         let n_dot_i = normal.dot(self);
853         let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
854         if k >= 0.0 {
855             eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
856         } else {
857             Self::ZERO
858         }
859     }
860 
861     /// Casts all elements of `self` to `f64`.
862     #[inline]
863     #[must_use]
as_dvec4(&self) -> crate::DVec4864     pub fn as_dvec4(&self) -> crate::DVec4 {
865         crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
866     }
867 
868     /// Casts all elements of `self` to `i8`.
869     #[inline]
870     #[must_use]
as_i8vec4(&self) -> crate::I8Vec4871     pub fn as_i8vec4(&self) -> crate::I8Vec4 {
872         crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
873     }
874 
875     /// Casts all elements of `self` to `u8`.
876     #[inline]
877     #[must_use]
as_u8vec4(&self) -> crate::U8Vec4878     pub fn as_u8vec4(&self) -> crate::U8Vec4 {
879         crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
880     }
881 
882     /// Casts all elements of `self` to `i16`.
883     #[inline]
884     #[must_use]
as_i16vec4(&self) -> crate::I16Vec4885     pub fn as_i16vec4(&self) -> crate::I16Vec4 {
886         crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
887     }
888 
889     /// Casts all elements of `self` to `u16`.
890     #[inline]
891     #[must_use]
as_u16vec4(&self) -> crate::U16Vec4892     pub fn as_u16vec4(&self) -> crate::U16Vec4 {
893         crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
894     }
895 
896     /// Casts all elements of `self` to `i32`.
897     #[inline]
898     #[must_use]
as_ivec4(&self) -> crate::IVec4899     pub fn as_ivec4(&self) -> crate::IVec4 {
900         crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
901     }
902 
903     /// Casts all elements of `self` to `u32`.
904     #[inline]
905     #[must_use]
as_uvec4(&self) -> crate::UVec4906     pub fn as_uvec4(&self) -> crate::UVec4 {
907         crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
908     }
909 
910     /// Casts all elements of `self` to `i64`.
911     #[inline]
912     #[must_use]
as_i64vec4(&self) -> crate::I64Vec4913     pub fn as_i64vec4(&self) -> crate::I64Vec4 {
914         crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
915     }
916 
917     /// Casts all elements of `self` to `u64`.
918     #[inline]
919     #[must_use]
as_u64vec4(&self) -> crate::U64Vec4920     pub fn as_u64vec4(&self) -> crate::U64Vec4 {
921         crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
922     }
923 }
924 
925 impl Default for Vec4 {
926     #[inline(always)]
default() -> Self927     fn default() -> Self {
928         Self::ZERO
929     }
930 }
931 
932 impl PartialEq for Vec4 {
933     #[inline]
eq(&self, rhs: &Self) -> bool934     fn eq(&self, rhs: &Self) -> bool {
935         self.cmpeq(*rhs).all()
936     }
937 }
938 
939 impl Div<Vec4> for Vec4 {
940     type Output = Self;
941     #[inline]
div(self, rhs: Self) -> Self942     fn div(self, rhs: Self) -> Self {
943         Self(self.0 / rhs.0)
944     }
945 }
946 
947 impl Div<&Vec4> for Vec4 {
948     type Output = Vec4;
949     #[inline]
div(self, rhs: &Vec4) -> Vec4950     fn div(self, rhs: &Vec4) -> Vec4 {
951         self.div(*rhs)
952     }
953 }
954 
955 impl Div<&Vec4> for &Vec4 {
956     type Output = Vec4;
957     #[inline]
div(self, rhs: &Vec4) -> Vec4958     fn div(self, rhs: &Vec4) -> Vec4 {
959         (*self).div(*rhs)
960     }
961 }
962 
963 impl Div<Vec4> for &Vec4 {
964     type Output = Vec4;
965     #[inline]
div(self, rhs: Vec4) -> Vec4966     fn div(self, rhs: Vec4) -> Vec4 {
967         (*self).div(rhs)
968     }
969 }
970 
971 impl DivAssign<Vec4> for Vec4 {
972     #[inline]
div_assign(&mut self, rhs: Self)973     fn div_assign(&mut self, rhs: Self) {
974         self.0 /= rhs.0;
975     }
976 }
977 
978 impl DivAssign<&Vec4> for Vec4 {
979     #[inline]
div_assign(&mut self, rhs: &Vec4)980     fn div_assign(&mut self, rhs: &Vec4) {
981         self.div_assign(*rhs)
982     }
983 }
984 
985 impl Div<f32> for Vec4 {
986     type Output = Self;
987     #[inline]
div(self, rhs: f32) -> Self988     fn div(self, rhs: f32) -> Self {
989         Self(self.0 / f32x4::splat(rhs))
990     }
991 }
992 
993 impl Div<&f32> for Vec4 {
994     type Output = Vec4;
995     #[inline]
div(self, rhs: &f32) -> Vec4996     fn div(self, rhs: &f32) -> Vec4 {
997         self.div(*rhs)
998     }
999 }
1000 
1001 impl Div<&f32> for &Vec4 {
1002     type Output = Vec4;
1003     #[inline]
div(self, rhs: &f32) -> Vec41004     fn div(self, rhs: &f32) -> Vec4 {
1005         (*self).div(*rhs)
1006     }
1007 }
1008 
1009 impl Div<f32> for &Vec4 {
1010     type Output = Vec4;
1011     #[inline]
div(self, rhs: f32) -> Vec41012     fn div(self, rhs: f32) -> Vec4 {
1013         (*self).div(rhs)
1014     }
1015 }
1016 
1017 impl DivAssign<f32> for Vec4 {
1018     #[inline]
div_assign(&mut self, rhs: f32)1019     fn div_assign(&mut self, rhs: f32) {
1020         self.0 /= f32x4::splat(rhs);
1021     }
1022 }
1023 
1024 impl DivAssign<&f32> for Vec4 {
1025     #[inline]
div_assign(&mut self, rhs: &f32)1026     fn div_assign(&mut self, rhs: &f32) {
1027         self.div_assign(*rhs)
1028     }
1029 }
1030 
1031 impl Div<Vec4> for f32 {
1032     type Output = Vec4;
1033     #[inline]
div(self, rhs: Vec4) -> Vec41034     fn div(self, rhs: Vec4) -> Vec4 {
1035         Vec4(f32x4::splat(self) / rhs.0)
1036     }
1037 }
1038 
1039 impl Div<&Vec4> for f32 {
1040     type Output = Vec4;
1041     #[inline]
div(self, rhs: &Vec4) -> Vec41042     fn div(self, rhs: &Vec4) -> Vec4 {
1043         self.div(*rhs)
1044     }
1045 }
1046 
1047 impl Div<&Vec4> for &f32 {
1048     type Output = Vec4;
1049     #[inline]
div(self, rhs: &Vec4) -> Vec41050     fn div(self, rhs: &Vec4) -> Vec4 {
1051         (*self).div(*rhs)
1052     }
1053 }
1054 
1055 impl Div<Vec4> for &f32 {
1056     type Output = Vec4;
1057     #[inline]
div(self, rhs: Vec4) -> Vec41058     fn div(self, rhs: Vec4) -> Vec4 {
1059         (*self).div(rhs)
1060     }
1061 }
1062 
1063 impl Mul<Vec4> for Vec4 {
1064     type Output = Self;
1065     #[inline]
mul(self, rhs: Self) -> Self1066     fn mul(self, rhs: Self) -> Self {
1067         Self(self.0 * rhs.0)
1068     }
1069 }
1070 
1071 impl Mul<&Vec4> for Vec4 {
1072     type Output = Vec4;
1073     #[inline]
mul(self, rhs: &Vec4) -> Vec41074     fn mul(self, rhs: &Vec4) -> Vec4 {
1075         self.mul(*rhs)
1076     }
1077 }
1078 
1079 impl Mul<&Vec4> for &Vec4 {
1080     type Output = Vec4;
1081     #[inline]
mul(self, rhs: &Vec4) -> Vec41082     fn mul(self, rhs: &Vec4) -> Vec4 {
1083         (*self).mul(*rhs)
1084     }
1085 }
1086 
1087 impl Mul<Vec4> for &Vec4 {
1088     type Output = Vec4;
1089     #[inline]
mul(self, rhs: Vec4) -> Vec41090     fn mul(self, rhs: Vec4) -> Vec4 {
1091         (*self).mul(rhs)
1092     }
1093 }
1094 
1095 impl MulAssign<Vec4> for Vec4 {
1096     #[inline]
mul_assign(&mut self, rhs: Self)1097     fn mul_assign(&mut self, rhs: Self) {
1098         self.0 *= rhs.0;
1099     }
1100 }
1101 
1102 impl MulAssign<&Vec4> for Vec4 {
1103     #[inline]
mul_assign(&mut self, rhs: &Vec4)1104     fn mul_assign(&mut self, rhs: &Vec4) {
1105         self.mul_assign(*rhs)
1106     }
1107 }
1108 
1109 impl Mul<f32> for Vec4 {
1110     type Output = Self;
1111     #[inline]
mul(self, rhs: f32) -> Self1112     fn mul(self, rhs: f32) -> Self {
1113         Self(self.0 * f32x4::splat(rhs))
1114     }
1115 }
1116 
1117 impl Mul<&f32> for Vec4 {
1118     type Output = Vec4;
1119     #[inline]
mul(self, rhs: &f32) -> Vec41120     fn mul(self, rhs: &f32) -> Vec4 {
1121         self.mul(*rhs)
1122     }
1123 }
1124 
1125 impl Mul<&f32> for &Vec4 {
1126     type Output = Vec4;
1127     #[inline]
mul(self, rhs: &f32) -> Vec41128     fn mul(self, rhs: &f32) -> Vec4 {
1129         (*self).mul(*rhs)
1130     }
1131 }
1132 
1133 impl Mul<f32> for &Vec4 {
1134     type Output = Vec4;
1135     #[inline]
mul(self, rhs: f32) -> Vec41136     fn mul(self, rhs: f32) -> Vec4 {
1137         (*self).mul(rhs)
1138     }
1139 }
1140 
1141 impl MulAssign<f32> for Vec4 {
1142     #[inline]
mul_assign(&mut self, rhs: f32)1143     fn mul_assign(&mut self, rhs: f32) {
1144         self.0 *= f32x4::splat(rhs);
1145     }
1146 }
1147 
1148 impl MulAssign<&f32> for Vec4 {
1149     #[inline]
mul_assign(&mut self, rhs: &f32)1150     fn mul_assign(&mut self, rhs: &f32) {
1151         self.mul_assign(*rhs)
1152     }
1153 }
1154 
1155 impl Mul<Vec4> for f32 {
1156     type Output = Vec4;
1157     #[inline]
mul(self, rhs: Vec4) -> Vec41158     fn mul(self, rhs: Vec4) -> Vec4 {
1159         Vec4(f32x4::splat(self) * rhs.0)
1160     }
1161 }
1162 
1163 impl Mul<&Vec4> for f32 {
1164     type Output = Vec4;
1165     #[inline]
mul(self, rhs: &Vec4) -> Vec41166     fn mul(self, rhs: &Vec4) -> Vec4 {
1167         self.mul(*rhs)
1168     }
1169 }
1170 
1171 impl Mul<&Vec4> for &f32 {
1172     type Output = Vec4;
1173     #[inline]
mul(self, rhs: &Vec4) -> Vec41174     fn mul(self, rhs: &Vec4) -> Vec4 {
1175         (*self).mul(*rhs)
1176     }
1177 }
1178 
1179 impl Mul<Vec4> for &f32 {
1180     type Output = Vec4;
1181     #[inline]
mul(self, rhs: Vec4) -> Vec41182     fn mul(self, rhs: Vec4) -> Vec4 {
1183         (*self).mul(rhs)
1184     }
1185 }
1186 
1187 impl Add<Vec4> for Vec4 {
1188     type Output = Self;
1189     #[inline]
add(self, rhs: Self) -> Self1190     fn add(self, rhs: Self) -> Self {
1191         Self(self.0 + rhs.0)
1192     }
1193 }
1194 
1195 impl Add<&Vec4> for Vec4 {
1196     type Output = Vec4;
1197     #[inline]
add(self, rhs: &Vec4) -> Vec41198     fn add(self, rhs: &Vec4) -> Vec4 {
1199         self.add(*rhs)
1200     }
1201 }
1202 
1203 impl Add<&Vec4> for &Vec4 {
1204     type Output = Vec4;
1205     #[inline]
add(self, rhs: &Vec4) -> Vec41206     fn add(self, rhs: &Vec4) -> Vec4 {
1207         (*self).add(*rhs)
1208     }
1209 }
1210 
1211 impl Add<Vec4> for &Vec4 {
1212     type Output = Vec4;
1213     #[inline]
add(self, rhs: Vec4) -> Vec41214     fn add(self, rhs: Vec4) -> Vec4 {
1215         (*self).add(rhs)
1216     }
1217 }
1218 
1219 impl AddAssign<Vec4> for Vec4 {
1220     #[inline]
add_assign(&mut self, rhs: Self)1221     fn add_assign(&mut self, rhs: Self) {
1222         self.0 += rhs.0;
1223     }
1224 }
1225 
1226 impl AddAssign<&Vec4> for Vec4 {
1227     #[inline]
add_assign(&mut self, rhs: &Vec4)1228     fn add_assign(&mut self, rhs: &Vec4) {
1229         self.add_assign(*rhs)
1230     }
1231 }
1232 
1233 impl Add<f32> for Vec4 {
1234     type Output = Self;
1235     #[inline]
add(self, rhs: f32) -> Self1236     fn add(self, rhs: f32) -> Self {
1237         Self(self.0 + f32x4::splat(rhs))
1238     }
1239 }
1240 
1241 impl Add<&f32> for Vec4 {
1242     type Output = Vec4;
1243     #[inline]
add(self, rhs: &f32) -> Vec41244     fn add(self, rhs: &f32) -> Vec4 {
1245         self.add(*rhs)
1246     }
1247 }
1248 
1249 impl Add<&f32> for &Vec4 {
1250     type Output = Vec4;
1251     #[inline]
add(self, rhs: &f32) -> Vec41252     fn add(self, rhs: &f32) -> Vec4 {
1253         (*self).add(*rhs)
1254     }
1255 }
1256 
1257 impl Add<f32> for &Vec4 {
1258     type Output = Vec4;
1259     #[inline]
add(self, rhs: f32) -> Vec41260     fn add(self, rhs: f32) -> Vec4 {
1261         (*self).add(rhs)
1262     }
1263 }
1264 
1265 impl AddAssign<f32> for Vec4 {
1266     #[inline]
add_assign(&mut self, rhs: f32)1267     fn add_assign(&mut self, rhs: f32) {
1268         self.0 += f32x4::splat(rhs);
1269     }
1270 }
1271 
1272 impl AddAssign<&f32> for Vec4 {
1273     #[inline]
add_assign(&mut self, rhs: &f32)1274     fn add_assign(&mut self, rhs: &f32) {
1275         self.add_assign(*rhs)
1276     }
1277 }
1278 
1279 impl Add<Vec4> for f32 {
1280     type Output = Vec4;
1281     #[inline]
add(self, rhs: Vec4) -> Vec41282     fn add(self, rhs: Vec4) -> Vec4 {
1283         Vec4(f32x4::splat(self) + rhs.0)
1284     }
1285 }
1286 
1287 impl Add<&Vec4> for f32 {
1288     type Output = Vec4;
1289     #[inline]
add(self, rhs: &Vec4) -> Vec41290     fn add(self, rhs: &Vec4) -> Vec4 {
1291         self.add(*rhs)
1292     }
1293 }
1294 
1295 impl Add<&Vec4> for &f32 {
1296     type Output = Vec4;
1297     #[inline]
add(self, rhs: &Vec4) -> Vec41298     fn add(self, rhs: &Vec4) -> Vec4 {
1299         (*self).add(*rhs)
1300     }
1301 }
1302 
1303 impl Add<Vec4> for &f32 {
1304     type Output = Vec4;
1305     #[inline]
add(self, rhs: Vec4) -> Vec41306     fn add(self, rhs: Vec4) -> Vec4 {
1307         (*self).add(rhs)
1308     }
1309 }
1310 
1311 impl Sub<Vec4> for Vec4 {
1312     type Output = Self;
1313     #[inline]
sub(self, rhs: Self) -> Self1314     fn sub(self, rhs: Self) -> Self {
1315         Self(self.0 - rhs.0)
1316     }
1317 }
1318 
1319 impl Sub<&Vec4> for Vec4 {
1320     type Output = Vec4;
1321     #[inline]
sub(self, rhs: &Vec4) -> Vec41322     fn sub(self, rhs: &Vec4) -> Vec4 {
1323         self.sub(*rhs)
1324     }
1325 }
1326 
1327 impl Sub<&Vec4> for &Vec4 {
1328     type Output = Vec4;
1329     #[inline]
sub(self, rhs: &Vec4) -> Vec41330     fn sub(self, rhs: &Vec4) -> Vec4 {
1331         (*self).sub(*rhs)
1332     }
1333 }
1334 
1335 impl Sub<Vec4> for &Vec4 {
1336     type Output = Vec4;
1337     #[inline]
sub(self, rhs: Vec4) -> Vec41338     fn sub(self, rhs: Vec4) -> Vec4 {
1339         (*self).sub(rhs)
1340     }
1341 }
1342 
1343 impl SubAssign<Vec4> for Vec4 {
1344     #[inline]
sub_assign(&mut self, rhs: Vec4)1345     fn sub_assign(&mut self, rhs: Vec4) {
1346         self.0 -= rhs.0;
1347     }
1348 }
1349 
1350 impl SubAssign<&Vec4> for Vec4 {
1351     #[inline]
sub_assign(&mut self, rhs: &Vec4)1352     fn sub_assign(&mut self, rhs: &Vec4) {
1353         self.sub_assign(*rhs)
1354     }
1355 }
1356 
1357 impl Sub<f32> for Vec4 {
1358     type Output = Self;
1359     #[inline]
sub(self, rhs: f32) -> Self1360     fn sub(self, rhs: f32) -> Self {
1361         Self(self.0 - f32x4::splat(rhs))
1362     }
1363 }
1364 
1365 impl Sub<&f32> for Vec4 {
1366     type Output = Vec4;
1367     #[inline]
sub(self, rhs: &f32) -> Vec41368     fn sub(self, rhs: &f32) -> Vec4 {
1369         self.sub(*rhs)
1370     }
1371 }
1372 
1373 impl Sub<&f32> for &Vec4 {
1374     type Output = Vec4;
1375     #[inline]
sub(self, rhs: &f32) -> Vec41376     fn sub(self, rhs: &f32) -> Vec4 {
1377         (*self).sub(*rhs)
1378     }
1379 }
1380 
1381 impl Sub<f32> for &Vec4 {
1382     type Output = Vec4;
1383     #[inline]
sub(self, rhs: f32) -> Vec41384     fn sub(self, rhs: f32) -> Vec4 {
1385         (*self).sub(rhs)
1386     }
1387 }
1388 
1389 impl SubAssign<f32> for Vec4 {
1390     #[inline]
sub_assign(&mut self, rhs: f32)1391     fn sub_assign(&mut self, rhs: f32) {
1392         self.0 -= f32x4::splat(rhs);
1393     }
1394 }
1395 
1396 impl SubAssign<&f32> for Vec4 {
1397     #[inline]
sub_assign(&mut self, rhs: &f32)1398     fn sub_assign(&mut self, rhs: &f32) {
1399         self.sub_assign(*rhs)
1400     }
1401 }
1402 
1403 impl Sub<Vec4> for f32 {
1404     type Output = Vec4;
1405     #[inline]
sub(self, rhs: Vec4) -> Vec41406     fn sub(self, rhs: Vec4) -> Vec4 {
1407         Vec4(f32x4::splat(self) - rhs.0)
1408     }
1409 }
1410 
1411 impl Sub<&Vec4> for f32 {
1412     type Output = Vec4;
1413     #[inline]
sub(self, rhs: &Vec4) -> Vec41414     fn sub(self, rhs: &Vec4) -> Vec4 {
1415         self.sub(*rhs)
1416     }
1417 }
1418 
1419 impl Sub<&Vec4> for &f32 {
1420     type Output = Vec4;
1421     #[inline]
sub(self, rhs: &Vec4) -> Vec41422     fn sub(self, rhs: &Vec4) -> Vec4 {
1423         (*self).sub(*rhs)
1424     }
1425 }
1426 
1427 impl Sub<Vec4> for &f32 {
1428     type Output = Vec4;
1429     #[inline]
sub(self, rhs: Vec4) -> Vec41430     fn sub(self, rhs: Vec4) -> Vec4 {
1431         (*self).sub(rhs)
1432     }
1433 }
1434 
1435 impl Rem<Vec4> for Vec4 {
1436     type Output = Self;
1437     #[inline]
rem(self, rhs: Self) -> Self1438     fn rem(self, rhs: Self) -> Self {
1439         Self(self.0 % rhs.0)
1440     }
1441 }
1442 
1443 impl Rem<&Vec4> for Vec4 {
1444     type Output = Vec4;
1445     #[inline]
rem(self, rhs: &Vec4) -> Vec41446     fn rem(self, rhs: &Vec4) -> Vec4 {
1447         self.rem(*rhs)
1448     }
1449 }
1450 
1451 impl Rem<&Vec4> for &Vec4 {
1452     type Output = Vec4;
1453     #[inline]
rem(self, rhs: &Vec4) -> Vec41454     fn rem(self, rhs: &Vec4) -> Vec4 {
1455         (*self).rem(*rhs)
1456     }
1457 }
1458 
1459 impl Rem<Vec4> for &Vec4 {
1460     type Output = Vec4;
1461     #[inline]
rem(self, rhs: Vec4) -> Vec41462     fn rem(self, rhs: Vec4) -> Vec4 {
1463         (*self).rem(rhs)
1464     }
1465 }
1466 
1467 impl RemAssign<Vec4> for Vec4 {
1468     #[inline]
rem_assign(&mut self, rhs: Self)1469     fn rem_assign(&mut self, rhs: Self) {
1470         self.0 %= rhs.0;
1471     }
1472 }
1473 
1474 impl RemAssign<&Vec4> for Vec4 {
1475     #[inline]
rem_assign(&mut self, rhs: &Vec4)1476     fn rem_assign(&mut self, rhs: &Vec4) {
1477         self.rem_assign(*rhs)
1478     }
1479 }
1480 
1481 impl Rem<f32> for Vec4 {
1482     type Output = Self;
1483     #[inline]
rem(self, rhs: f32) -> Self1484     fn rem(self, rhs: f32) -> Self {
1485         self.rem(Self::splat(rhs))
1486     }
1487 }
1488 
1489 impl Rem<&f32> for Vec4 {
1490     type Output = Vec4;
1491     #[inline]
rem(self, rhs: &f32) -> Vec41492     fn rem(self, rhs: &f32) -> Vec4 {
1493         self.rem(*rhs)
1494     }
1495 }
1496 
1497 impl Rem<&f32> for &Vec4 {
1498     type Output = Vec4;
1499     #[inline]
rem(self, rhs: &f32) -> Vec41500     fn rem(self, rhs: &f32) -> Vec4 {
1501         (*self).rem(*rhs)
1502     }
1503 }
1504 
1505 impl Rem<f32> for &Vec4 {
1506     type Output = Vec4;
1507     #[inline]
rem(self, rhs: f32) -> Vec41508     fn rem(self, rhs: f32) -> Vec4 {
1509         (*self).rem(rhs)
1510     }
1511 }
1512 
1513 impl RemAssign<f32> for Vec4 {
1514     #[inline]
rem_assign(&mut self, rhs: f32)1515     fn rem_assign(&mut self, rhs: f32) {
1516         self.0 %= f32x4::splat(rhs);
1517     }
1518 }
1519 
1520 impl RemAssign<&f32> for Vec4 {
1521     #[inline]
rem_assign(&mut self, rhs: &f32)1522     fn rem_assign(&mut self, rhs: &f32) {
1523         self.rem_assign(*rhs)
1524     }
1525 }
1526 
1527 impl Rem<Vec4> for f32 {
1528     type Output = Vec4;
1529     #[inline]
rem(self, rhs: Vec4) -> Vec41530     fn rem(self, rhs: Vec4) -> Vec4 {
1531         Vec4::splat(self).rem(rhs)
1532     }
1533 }
1534 
1535 impl Rem<&Vec4> for f32 {
1536     type Output = Vec4;
1537     #[inline]
rem(self, rhs: &Vec4) -> Vec41538     fn rem(self, rhs: &Vec4) -> Vec4 {
1539         self.rem(*rhs)
1540     }
1541 }
1542 
1543 impl Rem<&Vec4> for &f32 {
1544     type Output = Vec4;
1545     #[inline]
rem(self, rhs: &Vec4) -> Vec41546     fn rem(self, rhs: &Vec4) -> Vec4 {
1547         (*self).rem(*rhs)
1548     }
1549 }
1550 
1551 impl Rem<Vec4> for &f32 {
1552     type Output = Vec4;
1553     #[inline]
rem(self, rhs: Vec4) -> Vec41554     fn rem(self, rhs: Vec4) -> Vec4 {
1555         (*self).rem(rhs)
1556     }
1557 }
1558 
1559 #[cfg(not(target_arch = "spirv"))]
1560 impl AsRef<[f32; 4]> for Vec4 {
1561     #[inline]
as_ref(&self) -> &[f32; 4]1562     fn as_ref(&self) -> &[f32; 4] {
1563         unsafe { &*(self as *const Vec4 as *const [f32; 4]) }
1564     }
1565 }
1566 
1567 #[cfg(not(target_arch = "spirv"))]
1568 impl AsMut<[f32; 4]> for Vec4 {
1569     #[inline]
as_mut(&mut self) -> &mut [f32; 4]1570     fn as_mut(&mut self) -> &mut [f32; 4] {
1571         unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) }
1572     }
1573 }
1574 
1575 impl Sum for Vec4 {
1576     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1577     fn sum<I>(iter: I) -> Self
1578     where
1579         I: Iterator<Item = Self>,
1580     {
1581         iter.fold(Self::ZERO, Self::add)
1582     }
1583 }
1584 
1585 impl<'a> Sum<&'a Self> for Vec4 {
1586     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1587     fn sum<I>(iter: I) -> Self
1588     where
1589         I: Iterator<Item = &'a Self>,
1590     {
1591         iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1592     }
1593 }
1594 
1595 impl Product for Vec4 {
1596     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1597     fn product<I>(iter: I) -> Self
1598     where
1599         I: Iterator<Item = Self>,
1600     {
1601         iter.fold(Self::ONE, Self::mul)
1602     }
1603 }
1604 
1605 impl<'a> Product<&'a Self> for Vec4 {
1606     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1607     fn product<I>(iter: I) -> Self
1608     where
1609         I: Iterator<Item = &'a Self>,
1610     {
1611         iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1612     }
1613 }
1614 
1615 impl Neg for Vec4 {
1616     type Output = Self;
1617     #[inline]
neg(self) -> Self1618     fn neg(self) -> Self {
1619         Self(-self.0)
1620     }
1621 }
1622 
1623 impl Neg for &Vec4 {
1624     type Output = Vec4;
1625     #[inline]
neg(self) -> Vec41626     fn neg(self) -> Vec4 {
1627         (*self).neg()
1628     }
1629 }
1630 
1631 impl Index<usize> for Vec4 {
1632     type Output = f32;
1633     #[inline]
index(&self, index: usize) -> &Self::Output1634     fn index(&self, index: usize) -> &Self::Output {
1635         &self.0[index]
1636     }
1637 }
1638 
1639 impl IndexMut<usize> for Vec4 {
1640     #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1641     fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1642         &mut self.0[index]
1643     }
1644 }
1645 
1646 impl fmt::Display for Vec4 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1647     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1648         if let Some(p) = f.precision() {
1649             write!(
1650                 f,
1651                 "[{:.*}, {:.*}, {:.*}, {:.*}]",
1652                 p, self.x, p, self.y, p, self.z, p, self.w
1653             )
1654         } else {
1655             write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1656         }
1657     }
1658 }
1659 
1660 impl fmt::Debug for Vec4 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1661     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1662         fmt.debug_tuple(stringify!(Vec4))
1663             .field(&self.x)
1664             .field(&self.y)
1665             .field(&self.z)
1666             .field(&self.w)
1667             .finish()
1668     }
1669 }
1670 
1671 impl From<Vec4> for f32x4 {
1672     #[inline(always)]
from(t: Vec4) -> Self1673     fn from(t: Vec4) -> Self {
1674         t.0
1675     }
1676 }
1677 
1678 impl From<f32x4> for Vec4 {
1679     #[inline(always)]
from(t: f32x4) -> Self1680     fn from(t: f32x4) -> Self {
1681         Self(t)
1682     }
1683 }
1684 
1685 impl From<[f32; 4]> for Vec4 {
1686     #[inline]
from(a: [f32; 4]) -> Self1687     fn from(a: [f32; 4]) -> Self {
1688         Self(f32x4::from_array(a))
1689     }
1690 }
1691 
1692 impl From<Vec4> for [f32; 4] {
1693     #[inline]
from(v: Vec4) -> Self1694     fn from(v: Vec4) -> Self {
1695         v.0.to_array()
1696     }
1697 }
1698 
1699 impl From<(f32, f32, f32, f32)> for Vec4 {
1700     #[inline]
from(t: (f32, f32, f32, f32)) -> Self1701     fn from(t: (f32, f32, f32, f32)) -> Self {
1702         Self::new(t.0, t.1, t.2, t.3)
1703     }
1704 }
1705 
1706 impl From<Vec4> for (f32, f32, f32, f32) {
1707     #[inline]
from(v: Vec4) -> Self1708     fn from(v: Vec4) -> Self {
1709         unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1710     }
1711 }
1712 
1713 impl From<(Vec3A, f32)> for Vec4 {
1714     #[inline]
from((v, w): (Vec3A, f32)) -> Self1715     fn from((v, w): (Vec3A, f32)) -> Self {
1716         v.extend(w)
1717     }
1718 }
1719 
1720 impl From<(f32, Vec3A)> for Vec4 {
1721     #[inline]
from((x, v): (f32, Vec3A)) -> Self1722     fn from((x, v): (f32, Vec3A)) -> Self {
1723         Self::new(x, v.x, v.y, v.z)
1724     }
1725 }
1726 
1727 impl From<(Vec3, f32)> for Vec4 {
1728     #[inline]
from((v, w): (Vec3, f32)) -> Self1729     fn from((v, w): (Vec3, f32)) -> Self {
1730         Self::new(v.x, v.y, v.z, w)
1731     }
1732 }
1733 
1734 impl From<(f32, Vec3)> for Vec4 {
1735     #[inline]
from((x, v): (f32, Vec3)) -> Self1736     fn from((x, v): (f32, Vec3)) -> Self {
1737         Self::new(x, v.x, v.y, v.z)
1738     }
1739 }
1740 
1741 impl From<(Vec2, f32, f32)> for Vec4 {
1742     #[inline]
from((v, z, w): (Vec2, f32, f32)) -> Self1743     fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1744         Self::new(v.x, v.y, z, w)
1745     }
1746 }
1747 
1748 impl From<(Vec2, Vec2)> for Vec4 {
1749     #[inline]
from((v, u): (Vec2, Vec2)) -> Self1750     fn from((v, u): (Vec2, Vec2)) -> Self {
1751         Self::new(v.x, v.y, u.x, u.y)
1752     }
1753 }
1754 
1755 impl Deref for Vec4 {
1756     type Target = crate::deref::Vec4<f32>;
1757     #[inline]
deref(&self) -> &Self::Target1758     fn deref(&self) -> &Self::Target {
1759         unsafe { &*(self as *const Self).cast() }
1760     }
1761 }
1762 
1763 impl DerefMut for Vec4 {
1764     #[inline]
deref_mut(&mut self) -> &mut Self::Target1765     fn deref_mut(&mut self) -> &mut Self::Target {
1766         unsafe { &mut *(self as *mut Self).cast() }
1767     }
1768 }
1769 
1770 impl From<BVec4> for Vec4 {
1771     #[inline]
from(v: BVec4) -> Self1772     fn from(v: BVec4) -> Self {
1773         Self::new(
1774             f32::from(v.x),
1775             f32::from(v.y),
1776             f32::from(v.z),
1777             f32::from(v.w),
1778         )
1779     }
1780 }
1781 
1782 #[cfg(not(feature = "scalar-math"))]
1783 impl From<BVec4A> for Vec4 {
1784     #[inline]
from(v: BVec4A) -> Self1785     fn from(v: BVec4A) -> Self {
1786         let bool_array: [bool; 4] = v.into();
1787         Self::new(
1788             f32::from(bool_array[0]),
1789             f32::from(bool_array[1]),
1790             f32::from(bool_array[2]),
1791             f32::from(bool_array[3]),
1792         )
1793     }
1794 }
1795