1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{f32::math, wasm32::*, BVec4A, Vec2, Vec3, Vec3A};
4
5 #[cfg(not(target_arch = "spirv"))]
6 use core::fmt;
7 use core::iter::{Product, Sum};
8 use core::{f32, ops::*};
9
10 use core::arch::wasm32::*;
11
12 #[repr(C)]
13 union UnionCast {
14 a: [f32; 4],
15 v: Vec4,
16 }
17
18 /// Creates a 4-dimensional vector.
19 #[inline(always)]
20 #[must_use]
vec4(x: f32, y: f32, z: f32, w: f32) -> Vec421 pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
22 Vec4::new(x, y, z, w)
23 }
24
25 /// A 4-dimensional vector.
26 ///
27 /// SIMD vector types are used for storage on supported platforms.
28 ///
29 /// This type is 16 byte aligned.
30 #[derive(Clone, Copy)]
31 #[repr(transparent)]
32 pub struct Vec4(pub(crate) v128);
33
34 impl Vec4 {
35 /// All zeroes.
36 pub const ZERO: Self = Self::splat(0.0);
37
38 /// All ones.
39 pub const ONE: Self = Self::splat(1.0);
40
41 /// All negative ones.
42 pub const NEG_ONE: Self = Self::splat(-1.0);
43
44 /// All `f32::MIN`.
45 pub const MIN: Self = Self::splat(f32::MIN);
46
47 /// All `f32::MAX`.
48 pub const MAX: Self = Self::splat(f32::MAX);
49
50 /// All `f32::NAN`.
51 pub const NAN: Self = Self::splat(f32::NAN);
52
53 /// All `f32::INFINITY`.
54 pub const INFINITY: Self = Self::splat(f32::INFINITY);
55
56 /// All `f32::NEG_INFINITY`.
57 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
58
59 /// A unit vector pointing along the positive X axis.
60 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
61
62 /// A unit vector pointing along the positive Y axis.
63 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
64
65 /// A unit vector pointing along the positive Z axis.
66 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
67
68 /// A unit vector pointing along the positive W axis.
69 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
70
71 /// A unit vector pointing along the negative X axis.
72 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
73
74 /// A unit vector pointing along the negative Y axis.
75 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
76
77 /// A unit vector pointing along the negative Z axis.
78 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
79
80 /// A unit vector pointing along the negative W axis.
81 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
82
83 /// The unit axes.
84 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
85
86 /// Creates a new vector.
87 #[inline(always)]
88 #[must_use]
new(x: f32, y: f32, z: f32, w: f32) -> Self89 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
90 Self(f32x4(x, y, z, w))
91 }
92
93 /// Creates a vector with all elements set to `v`.
94 #[inline]
95 #[must_use]
splat(v: f32) -> Self96 pub const fn splat(v: f32) -> Self {
97 unsafe { UnionCast { a: [v; 4] }.v }
98 }
99
100 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
101 /// for each element of `self`.
102 ///
103 /// A true element in the mask uses the corresponding element from `if_true`, and false
104 /// uses the element from `if_false`.
105 #[inline]
106 #[must_use]
select(mask: BVec4A, if_true: Self, if_false: Self) -> Self107 pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
108 Self(v128_bitselect(if_true.0, if_false.0, mask.0))
109 }
110
111 /// Creates a new vector from an array.
112 #[inline]
113 #[must_use]
from_array(a: [f32; 4]) -> Self114 pub const fn from_array(a: [f32; 4]) -> Self {
115 Self::new(a[0], a[1], a[2], a[3])
116 }
117
118 /// `[x, y, z, w]`
119 #[inline]
120 #[must_use]
to_array(&self) -> [f32; 4]121 pub const fn to_array(&self) -> [f32; 4] {
122 unsafe { *(self as *const Vec4 as *const [f32; 4]) }
123 }
124
125 /// Creates a vector from the first 4 values in `slice`.
126 ///
127 /// # Panics
128 ///
129 /// Panics if `slice` is less than 4 elements long.
130 #[inline]
131 #[must_use]
from_slice(slice: &[f32]) -> Self132 pub const fn from_slice(slice: &[f32]) -> Self {
133 Self::new(slice[0], slice[1], slice[2], slice[3])
134 }
135
136 /// Writes the elements of `self` to the first 4 elements in `slice`.
137 ///
138 /// # Panics
139 ///
140 /// Panics if `slice` is less than 4 elements long.
141 #[inline]
write_to_slice(self, slice: &mut [f32])142 pub fn write_to_slice(self, slice: &mut [f32]) {
143 slice[0] = self.x;
144 slice[1] = self.y;
145 slice[2] = self.z;
146 slice[3] = self.w;
147 }
148
149 /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
150 ///
151 /// Truncation to [`Vec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
152 ///
153 /// To truncate to [`Vec3A`] use [`Vec3A::from()`].
154 #[inline]
155 #[must_use]
truncate(self) -> Vec3156 pub fn truncate(self) -> Vec3 {
157 use crate::swizzles::Vec4Swizzles;
158 self.xyz()
159 }
160
161 /// Computes the dot product of `self` and `rhs`.
162 #[inline]
163 #[must_use]
dot(self, rhs: Self) -> f32164 pub fn dot(self, rhs: Self) -> f32 {
165 dot4(self.0, rhs.0)
166 }
167
168 /// Returns a vector where every component is the dot product of `self` and `rhs`.
169 #[inline]
170 #[must_use]
dot_into_vec(self, rhs: Self) -> Self171 pub fn dot_into_vec(self, rhs: Self) -> Self {
172 Self(dot4_into_v128(self.0, rhs.0))
173 }
174
175 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
176 ///
177 /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
178 #[inline]
179 #[must_use]
min(self, rhs: Self) -> Self180 pub fn min(self, rhs: Self) -> Self {
181 Self(f32x4_pmin(self.0, rhs.0))
182 }
183
184 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
185 ///
186 /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
187 #[inline]
188 #[must_use]
max(self, rhs: Self) -> Self189 pub fn max(self, rhs: Self) -> Self {
190 Self(f32x4_pmax(self.0, rhs.0))
191 }
192
193 /// Component-wise clamping of values, similar to [`f32::clamp`].
194 ///
195 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
196 ///
197 /// # Panics
198 ///
199 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
200 #[inline]
201 #[must_use]
clamp(self, min: Self, max: Self) -> Self202 pub fn clamp(self, min: Self, max: Self) -> Self {
203 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
204 self.max(min).min(max)
205 }
206
207 /// Returns the horizontal minimum of `self`.
208 ///
209 /// In other words this computes `min(x, y, ..)`.
210 #[inline]
211 #[must_use]
min_element(self) -> f32212 pub fn min_element(self) -> f32 {
213 let v = self.0;
214 let v = f32x4_pmin(v, i32x4_shuffle::<2, 3, 0, 0>(v, v));
215 let v = f32x4_pmin(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
216 f32x4_extract_lane::<0>(v)
217 }
218
219 /// Returns the horizontal maximum of `self`.
220 ///
221 /// In other words this computes `max(x, y, ..)`.
222 #[inline]
223 #[must_use]
max_element(self) -> f32224 pub fn max_element(self) -> f32 {
225 let v = self.0;
226 let v = f32x4_pmax(v, i32x4_shuffle::<2, 3, 0, 0>(v, v));
227 let v = f32x4_pmax(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
228 f32x4_extract_lane::<0>(v)
229 }
230
231 /// Returns a vector mask containing the result of a `==` comparison for each element of
232 /// `self` and `rhs`.
233 ///
234 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
235 /// elements.
236 #[inline]
237 #[must_use]
cmpeq(self, rhs: Self) -> BVec4A238 pub fn cmpeq(self, rhs: Self) -> BVec4A {
239 BVec4A(f32x4_eq(self.0, rhs.0))
240 }
241
242 /// Returns a vector mask containing the result of a `!=` comparison for each element of
243 /// `self` and `rhs`.
244 ///
245 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
246 /// elements.
247 #[inline]
248 #[must_use]
cmpne(self, rhs: Self) -> BVec4A249 pub fn cmpne(self, rhs: Self) -> BVec4A {
250 BVec4A(f32x4_ne(self.0, rhs.0))
251 }
252
253 /// Returns a vector mask containing the result of a `>=` comparison for each element of
254 /// `self` and `rhs`.
255 ///
256 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
257 /// elements.
258 #[inline]
259 #[must_use]
cmpge(self, rhs: Self) -> BVec4A260 pub fn cmpge(self, rhs: Self) -> BVec4A {
261 BVec4A(f32x4_ge(self.0, rhs.0))
262 }
263
264 /// Returns a vector mask containing the result of a `>` comparison for each element of
265 /// `self` and `rhs`.
266 ///
267 /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
268 /// elements.
269 #[inline]
270 #[must_use]
cmpgt(self, rhs: Self) -> BVec4A271 pub fn cmpgt(self, rhs: Self) -> BVec4A {
272 BVec4A(f32x4_gt(self.0, rhs.0))
273 }
274
275 /// Returns a vector mask containing the result of a `<=` comparison for each element of
276 /// `self` and `rhs`.
277 ///
278 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
279 /// elements.
280 #[inline]
281 #[must_use]
cmple(self, rhs: Self) -> BVec4A282 pub fn cmple(self, rhs: Self) -> BVec4A {
283 BVec4A(f32x4_le(self.0, rhs.0))
284 }
285
286 /// Returns a vector mask containing the result of a `<` comparison for each element of
287 /// `self` and `rhs`.
288 ///
289 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
290 /// elements.
291 #[inline]
292 #[must_use]
cmplt(self, rhs: Self) -> BVec4A293 pub fn cmplt(self, rhs: Self) -> BVec4A {
294 BVec4A(f32x4_lt(self.0, rhs.0))
295 }
296
297 /// Returns a vector containing the absolute value of each element of `self`.
298 #[inline]
299 #[must_use]
abs(self) -> Self300 pub fn abs(self) -> Self {
301 Self(f32x4_abs(self.0))
302 }
303
304 /// Returns a vector with elements representing the sign of `self`.
305 ///
306 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
307 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
308 /// - `NAN` if the number is `NAN`
309 #[inline]
310 #[must_use]
signum(self) -> Self311 pub fn signum(self) -> Self {
312 unsafe {
313 let result = Self(v128_or(v128_and(self.0, Self::NEG_ONE.0), Self::ONE.0));
314 let mask = self.is_nan_mask();
315 Self::select(mask, self, result)
316 }
317 }
318
319 /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
320 #[inline]
321 #[must_use]
copysign(self, rhs: Self) -> Self322 pub fn copysign(self, rhs: Self) -> Self {
323 unsafe {
324 let mask = Self::splat(-0.0);
325 Self(v128_or(
326 v128_and(rhs.0, mask.0),
327 v128_andnot(self.0, mask.0),
328 ))
329 }
330 }
331
332 /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
333 ///
334 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
335 /// into the first lowest bit, element `y` into the second, etc.
336 #[inline]
337 #[must_use]
is_negative_bitmask(self) -> u32338 pub fn is_negative_bitmask(self) -> u32 {
339 u32x4_bitmask(self.0) as u32
340 }
341
342 /// Returns `true` if, and only if, all elements are finite. If any element is either
343 /// `NaN`, positive or negative infinity, this will return `false`.
344 #[inline]
345 #[must_use]
is_finite(self) -> bool346 pub fn is_finite(self) -> bool {
347 self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
348 }
349
350 /// Returns `true` if any elements are `NaN`.
351 #[inline]
352 #[must_use]
is_nan(self) -> bool353 pub fn is_nan(self) -> bool {
354 self.is_nan_mask().any()
355 }
356
357 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
358 ///
359 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
360 #[inline]
361 #[must_use]
is_nan_mask(self) -> BVec4A362 pub fn is_nan_mask(self) -> BVec4A {
363 BVec4A(f32x4_ne(self.0, self.0))
364 }
365
366 /// Computes the length of `self`.
367 #[doc(alias = "magnitude")]
368 #[inline]
369 #[must_use]
length(self) -> f32370 pub fn length(self) -> f32 {
371 let dot = dot4_in_x(self.0, self.0);
372 f32x4_extract_lane::<0>(f32x4_sqrt(dot))
373 }
374
375 /// Computes the squared length of `self`.
376 ///
377 /// This is faster than `length()` as it avoids a square root operation.
378 #[doc(alias = "magnitude2")]
379 #[inline]
380 #[must_use]
length_squared(self) -> f32381 pub fn length_squared(self) -> f32 {
382 self.dot(self)
383 }
384
385 /// Computes `1.0 / length()`.
386 ///
387 /// For valid results, `self` must _not_ be of length zero.
388 #[inline]
389 #[must_use]
length_recip(self) -> f32390 pub fn length_recip(self) -> f32 {
391 let dot = dot4_in_x(self.0, self.0);
392 f32x4_extract_lane::<0>(f32x4_div(Self::ONE.0, f32x4_sqrt(dot)))
393 }
394
395 /// Computes the Euclidean distance between two points in space.
396 #[inline]
397 #[must_use]
distance(self, rhs: Self) -> f32398 pub fn distance(self, rhs: Self) -> f32 {
399 (self - rhs).length()
400 }
401
402 /// Compute the squared euclidean distance between two points in space.
403 #[inline]
404 #[must_use]
distance_squared(self, rhs: Self) -> f32405 pub fn distance_squared(self, rhs: Self) -> f32 {
406 (self - rhs).length_squared()
407 }
408
409 /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
410 #[inline]
411 #[must_use]
div_euclid(self, rhs: Self) -> Self412 pub fn div_euclid(self, rhs: Self) -> Self {
413 Self::new(
414 math::div_euclid(self.x, rhs.x),
415 math::div_euclid(self.y, rhs.y),
416 math::div_euclid(self.z, rhs.z),
417 math::div_euclid(self.w, rhs.w),
418 )
419 }
420
421 /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
422 ///
423 /// [Euclidean division]: f32::rem_euclid
424 #[inline]
425 #[must_use]
rem_euclid(self, rhs: Self) -> Self426 pub fn rem_euclid(self, rhs: Self) -> Self {
427 Self::new(
428 math::rem_euclid(self.x, rhs.x),
429 math::rem_euclid(self.y, rhs.y),
430 math::rem_euclid(self.z, rhs.z),
431 math::rem_euclid(self.w, rhs.w),
432 )
433 }
434
435 /// Returns `self` normalized to length 1.0.
436 ///
437 /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
438 ///
439 /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
440 ///
441 /// Panics
442 ///
443 /// Will panic if `self` is zero length when `glam_assert` is enabled.
444 #[inline]
445 #[must_use]
normalize(self) -> Self446 pub fn normalize(self) -> Self {
447 let length = f32x4_sqrt(dot4_into_v128(self.0, self.0));
448 #[allow(clippy::let_and_return)]
449 let normalized = Self(f32x4_div(self.0, length));
450 glam_assert!(normalized.is_finite());
451 normalized
452 }
453
454 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
455 ///
456 /// In particular, if the input is zero (or very close to zero), or non-finite,
457 /// the result of this operation will be `None`.
458 ///
459 /// See also [`Self::normalize_or_zero()`].
460 #[inline]
461 #[must_use]
try_normalize(self) -> Option<Self>462 pub fn try_normalize(self) -> Option<Self> {
463 let rcp = self.length_recip();
464 if rcp.is_finite() && rcp > 0.0 {
465 Some(self * rcp)
466 } else {
467 None
468 }
469 }
470
471 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
472 ///
473 /// In particular, if the input is zero (or very close to zero), or non-finite,
474 /// the result of this operation will be zero.
475 ///
476 /// See also [`Self::try_normalize()`].
477 #[inline]
478 #[must_use]
normalize_or_zero(self) -> Self479 pub fn normalize_or_zero(self) -> Self {
480 let rcp = self.length_recip();
481 if rcp.is_finite() && rcp > 0.0 {
482 self * rcp
483 } else {
484 Self::ZERO
485 }
486 }
487
488 /// Returns whether `self` is length `1.0` or not.
489 ///
490 /// Uses a precision threshold of `1e-6`.
491 #[inline]
492 #[must_use]
is_normalized(self) -> bool493 pub fn is_normalized(self) -> bool {
494 // TODO: do something with epsilon
495 math::abs(self.length_squared() - 1.0) <= 1e-4
496 }
497
498 /// Returns the vector projection of `self` onto `rhs`.
499 ///
500 /// `rhs` must be of non-zero length.
501 ///
502 /// # Panics
503 ///
504 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
505 #[inline]
506 #[must_use]
project_onto(self, rhs: Self) -> Self507 pub fn project_onto(self, rhs: Self) -> Self {
508 let other_len_sq_rcp = rhs.dot(rhs).recip();
509 glam_assert!(other_len_sq_rcp.is_finite());
510 rhs * self.dot(rhs) * other_len_sq_rcp
511 }
512
513 /// Returns the vector rejection of `self` from `rhs`.
514 ///
515 /// The vector rejection is the vector perpendicular to the projection of `self` onto
516 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
517 ///
518 /// `rhs` must be of non-zero length.
519 ///
520 /// # Panics
521 ///
522 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
523 #[inline]
524 #[must_use]
reject_from(self, rhs: Self) -> Self525 pub fn reject_from(self, rhs: Self) -> Self {
526 self - self.project_onto(rhs)
527 }
528
529 /// Returns the vector projection of `self` onto `rhs`.
530 ///
531 /// `rhs` must be normalized.
532 ///
533 /// # Panics
534 ///
535 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
536 #[inline]
537 #[must_use]
project_onto_normalized(self, rhs: Self) -> Self538 pub fn project_onto_normalized(self, rhs: Self) -> Self {
539 glam_assert!(rhs.is_normalized());
540 rhs * self.dot(rhs)
541 }
542
543 /// Returns the vector rejection of `self` from `rhs`.
544 ///
545 /// The vector rejection is the vector perpendicular to the projection of `self` onto
546 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
547 ///
548 /// `rhs` must be normalized.
549 ///
550 /// # Panics
551 ///
552 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
553 #[inline]
554 #[must_use]
reject_from_normalized(self, rhs: Self) -> Self555 pub fn reject_from_normalized(self, rhs: Self) -> Self {
556 self - self.project_onto_normalized(rhs)
557 }
558
559 /// Returns a vector containing the nearest integer to a number for each element of `self`.
560 /// Round half-way cases away from 0.0.
561 #[inline]
562 #[must_use]
round(self) -> Self563 pub fn round(self) -> Self {
564 Self(f32x4_nearest(self.0))
565 }
566
567 /// Returns a vector containing the largest integer less than or equal to a number for each
568 /// element of `self`.
569 #[inline]
570 #[must_use]
floor(self) -> Self571 pub fn floor(self) -> Self {
572 Self(f32x4_floor(self.0))
573 }
574
575 /// Returns a vector containing the smallest integer greater than or equal to a number for
576 /// each element of `self`.
577 #[inline]
578 #[must_use]
ceil(self) -> Self579 pub fn ceil(self) -> Self {
580 Self(f32x4_ceil(self.0))
581 }
582
583 /// Returns a vector containing the integer part each element of `self`. This means numbers are
584 /// always truncated towards zero.
585 #[inline]
586 #[must_use]
trunc(self) -> Self587 pub fn trunc(self) -> Self {
588 Self(f32x4_trunc(self.0))
589 }
590
591 /// Returns a vector containing the fractional part of the vector, e.g. `self -
592 /// self.floor()`.
593 ///
594 /// Note that this is fast but not precise for large numbers.
595 #[inline]
596 #[must_use]
fract(self) -> Self597 pub fn fract(self) -> Self {
598 self - self.floor()
599 }
600
601 /// Returns a vector containing `e^self` (the exponential function) for each element of
602 /// `self`.
603 #[inline]
604 #[must_use]
exp(self) -> Self605 pub fn exp(self) -> Self {
606 Self::new(
607 math::exp(self.x),
608 math::exp(self.y),
609 math::exp(self.z),
610 math::exp(self.w),
611 )
612 }
613
614 /// Returns a vector containing each element of `self` raised to the power of `n`.
615 #[inline]
616 #[must_use]
powf(self, n: f32) -> Self617 pub fn powf(self, n: f32) -> Self {
618 Self::new(
619 math::powf(self.x, n),
620 math::powf(self.y, n),
621 math::powf(self.z, n),
622 math::powf(self.w, n),
623 )
624 }
625
626 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
627 #[inline]
628 #[must_use]
recip(self) -> Self629 pub fn recip(self) -> Self {
630 Self(f32x4_div(Self::ONE.0, self.0))
631 }
632
633 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
634 ///
635 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
636 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
637 /// extrapolated.
638 #[doc(alias = "mix")]
639 #[inline]
640 #[must_use]
lerp(self, rhs: Self, s: f32) -> Self641 pub fn lerp(self, rhs: Self, s: f32) -> Self {
642 self + ((rhs - self) * s)
643 }
644
645 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
646 /// less than or equal to `max_abs_diff`.
647 ///
648 /// This can be used to compare if two vectors contain similar elements. It works best when
649 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
650 /// the values being compared against.
651 ///
652 /// For more see
653 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
654 #[inline]
655 #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool656 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
657 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
658 }
659
660 /// Returns a vector with a length no less than `min` and no more than `max`
661 ///
662 /// # Panics
663 ///
664 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
665 #[inline]
666 #[must_use]
clamp_length(self, min: f32, max: f32) -> Self667 pub fn clamp_length(self, min: f32, max: f32) -> Self {
668 glam_assert!(min <= max);
669 let length_sq = self.length_squared();
670 if length_sq < min * min {
671 min * (self / math::sqrt(length_sq))
672 } else if length_sq > max * max {
673 max * (self / math::sqrt(length_sq))
674 } else {
675 self
676 }
677 }
678
679 /// Returns a vector with a length no more than `max`
680 #[inline]
681 #[must_use]
clamp_length_max(self, max: f32) -> Self682 pub fn clamp_length_max(self, max: f32) -> Self {
683 let length_sq = self.length_squared();
684 if length_sq > max * max {
685 max * (self / math::sqrt(length_sq))
686 } else {
687 self
688 }
689 }
690
691 /// Returns a vector with a length no less than `min`
692 #[inline]
693 #[must_use]
clamp_length_min(self, min: f32) -> Self694 pub fn clamp_length_min(self, min: f32) -> Self {
695 let length_sq = self.length_squared();
696 if length_sq < min * min {
697 min * (self / math::sqrt(length_sq))
698 } else {
699 self
700 }
701 }
702
703 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
704 /// error, yielding a more accurate result than an unfused multiply-add.
705 ///
706 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
707 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
708 /// and will be heavily dependant on designing algorithms with specific target hardware in
709 /// mind.
710 #[inline]
711 #[must_use]
mul_add(self, a: Self, b: Self) -> Self712 pub fn mul_add(self, a: Self, b: Self) -> Self {
713 Self::new(
714 math::mul_add(self.x, a.x, b.x),
715 math::mul_add(self.y, a.y, b.y),
716 math::mul_add(self.z, a.z, b.z),
717 math::mul_add(self.w, a.w, b.w),
718 )
719 }
720
721 /// Casts all elements of `self` to `f64`.
722 #[inline]
723 #[must_use]
as_dvec4(&self) -> crate::DVec4724 pub fn as_dvec4(&self) -> crate::DVec4 {
725 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
726 }
727
728 /// Casts all elements of `self` to `i16`.
729 #[inline]
730 #[must_use]
as_i16vec4(&self) -> crate::I16Vec4731 pub fn as_i16vec4(&self) -> crate::I16Vec4 {
732 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
733 }
734
735 /// Casts all elements of `self` to `u16`.
736 #[inline]
737 #[must_use]
as_u16vec4(&self) -> crate::U16Vec4738 pub fn as_u16vec4(&self) -> crate::U16Vec4 {
739 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
740 }
741
742 /// Casts all elements of `self` to `i32`.
743 #[inline]
744 #[must_use]
as_ivec4(&self) -> crate::IVec4745 pub fn as_ivec4(&self) -> crate::IVec4 {
746 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
747 }
748
749 /// Casts all elements of `self` to `u32`.
750 #[inline]
751 #[must_use]
as_uvec4(&self) -> crate::UVec4752 pub fn as_uvec4(&self) -> crate::UVec4 {
753 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
754 }
755
756 /// Casts all elements of `self` to `i64`.
757 #[inline]
758 #[must_use]
as_i64vec4(&self) -> crate::I64Vec4759 pub fn as_i64vec4(&self) -> crate::I64Vec4 {
760 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
761 }
762
763 /// Casts all elements of `self` to `u64`.
764 #[inline]
765 #[must_use]
as_u64vec4(&self) -> crate::U64Vec4766 pub fn as_u64vec4(&self) -> crate::U64Vec4 {
767 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
768 }
769 }
770
771 impl Default for Vec4 {
772 #[inline(always)]
default() -> Self773 fn default() -> Self {
774 Self::ZERO
775 }
776 }
777
778 impl PartialEq for Vec4 {
779 #[inline]
eq(&self, rhs: &Self) -> bool780 fn eq(&self, rhs: &Self) -> bool {
781 self.cmpeq(*rhs).all()
782 }
783 }
784
785 impl Div<Vec4> for Vec4 {
786 type Output = Self;
787 #[inline]
div(self, rhs: Self) -> Self788 fn div(self, rhs: Self) -> Self {
789 Self(f32x4_div(self.0, rhs.0))
790 }
791 }
792
793 impl DivAssign<Vec4> for Vec4 {
794 #[inline]
div_assign(&mut self, rhs: Self)795 fn div_assign(&mut self, rhs: Self) {
796 self.0 = f32x4_div(self.0, rhs.0);
797 }
798 }
799
800 impl Div<f32> for Vec4 {
801 type Output = Self;
802 #[inline]
div(self, rhs: f32) -> Self803 fn div(self, rhs: f32) -> Self {
804 Self(f32x4_div(self.0, f32x4_splat(rhs)))
805 }
806 }
807
808 impl DivAssign<f32> for Vec4 {
809 #[inline]
div_assign(&mut self, rhs: f32)810 fn div_assign(&mut self, rhs: f32) {
811 self.0 = f32x4_div(self.0, f32x4_splat(rhs))
812 }
813 }
814
815 impl Div<Vec4> for f32 {
816 type Output = Vec4;
817 #[inline]
div(self, rhs: Vec4) -> Vec4818 fn div(self, rhs: Vec4) -> Vec4 {
819 Vec4(f32x4_div(f32x4_splat(self), rhs.0))
820 }
821 }
822
823 impl Mul<Vec4> for Vec4 {
824 type Output = Self;
825 #[inline]
mul(self, rhs: Self) -> Self826 fn mul(self, rhs: Self) -> Self {
827 Self(f32x4_mul(self.0, rhs.0))
828 }
829 }
830
831 impl MulAssign<Vec4> for Vec4 {
832 #[inline]
mul_assign(&mut self, rhs: Self)833 fn mul_assign(&mut self, rhs: Self) {
834 self.0 = f32x4_mul(self.0, rhs.0);
835 }
836 }
837
838 impl Mul<f32> for Vec4 {
839 type Output = Self;
840 #[inline]
mul(self, rhs: f32) -> Self841 fn mul(self, rhs: f32) -> Self {
842 Self(f32x4_mul(self.0, f32x4_splat(rhs)))
843 }
844 }
845
846 impl MulAssign<f32> for Vec4 {
847 #[inline]
mul_assign(&mut self, rhs: f32)848 fn mul_assign(&mut self, rhs: f32) {
849 self.0 = f32x4_mul(self.0, f32x4_splat(rhs))
850 }
851 }
852
853 impl Mul<Vec4> for f32 {
854 type Output = Vec4;
855 #[inline]
mul(self, rhs: Vec4) -> Vec4856 fn mul(self, rhs: Vec4) -> Vec4 {
857 Vec4(f32x4_mul(f32x4_splat(self), rhs.0))
858 }
859 }
860
861 impl Add<Vec4> for Vec4 {
862 type Output = Self;
863 #[inline]
add(self, rhs: Self) -> Self864 fn add(self, rhs: Self) -> Self {
865 Self(f32x4_add(self.0, rhs.0))
866 }
867 }
868
869 impl AddAssign<Vec4> for Vec4 {
870 #[inline]
add_assign(&mut self, rhs: Self)871 fn add_assign(&mut self, rhs: Self) {
872 self.0 = f32x4_add(self.0, rhs.0);
873 }
874 }
875
876 impl Add<f32> for Vec4 {
877 type Output = Self;
878 #[inline]
add(self, rhs: f32) -> Self879 fn add(self, rhs: f32) -> Self {
880 Self(f32x4_add(self.0, f32x4_splat(rhs)))
881 }
882 }
883
884 impl AddAssign<f32> for Vec4 {
885 #[inline]
add_assign(&mut self, rhs: f32)886 fn add_assign(&mut self, rhs: f32) {
887 self.0 = f32x4_add(self.0, f32x4_splat(rhs));
888 }
889 }
890
891 impl Add<Vec4> for f32 {
892 type Output = Vec4;
893 #[inline]
add(self, rhs: Vec4) -> Vec4894 fn add(self, rhs: Vec4) -> Vec4 {
895 Vec4(f32x4_add(f32x4_splat(self), rhs.0))
896 }
897 }
898
899 impl Sub<Vec4> for Vec4 {
900 type Output = Self;
901 #[inline]
sub(self, rhs: Self) -> Self902 fn sub(self, rhs: Self) -> Self {
903 Self(f32x4_sub(self.0, rhs.0))
904 }
905 }
906
907 impl SubAssign<Vec4> for Vec4 {
908 #[inline]
sub_assign(&mut self, rhs: Vec4)909 fn sub_assign(&mut self, rhs: Vec4) {
910 self.0 = f32x4_sub(self.0, rhs.0);
911 }
912 }
913
914 impl Sub<f32> for Vec4 {
915 type Output = Self;
916 #[inline]
sub(self, rhs: f32) -> Self917 fn sub(self, rhs: f32) -> Self {
918 Self(f32x4_sub(self.0, f32x4_splat(rhs)))
919 }
920 }
921
922 impl SubAssign<f32> for Vec4 {
923 #[inline]
sub_assign(&mut self, rhs: f32)924 fn sub_assign(&mut self, rhs: f32) {
925 self.0 = f32x4_sub(self.0, f32x4_splat(rhs))
926 }
927 }
928
929 impl Sub<Vec4> for f32 {
930 type Output = Vec4;
931 #[inline]
sub(self, rhs: Vec4) -> Vec4932 fn sub(self, rhs: Vec4) -> Vec4 {
933 Vec4(f32x4_sub(f32x4_splat(self), rhs.0))
934 }
935 }
936
937 impl Rem<Vec4> for Vec4 {
938 type Output = Self;
939 #[inline]
rem(self, rhs: Self) -> Self940 fn rem(self, rhs: Self) -> Self {
941 let n = f32x4_floor(f32x4_div(self.0, rhs.0));
942 Self(f32x4_sub(self.0, f32x4_mul(n, rhs.0)))
943 }
944 }
945
946 impl RemAssign<Vec4> for Vec4 {
947 #[inline]
rem_assign(&mut self, rhs: Self)948 fn rem_assign(&mut self, rhs: Self) {
949 *self = self.rem(rhs);
950 }
951 }
952
953 impl Rem<f32> for Vec4 {
954 type Output = Self;
955 #[inline]
rem(self, rhs: f32) -> Self956 fn rem(self, rhs: f32) -> Self {
957 self.rem(Self::splat(rhs))
958 }
959 }
960
961 impl RemAssign<f32> for Vec4 {
962 #[inline]
rem_assign(&mut self, rhs: f32)963 fn rem_assign(&mut self, rhs: f32) {
964 *self = self.rem(Self::splat(rhs));
965 }
966 }
967
968 impl Rem<Vec4> for f32 {
969 type Output = Vec4;
970 #[inline]
rem(self, rhs: Vec4) -> Vec4971 fn rem(self, rhs: Vec4) -> Vec4 {
972 Vec4::splat(self).rem(rhs)
973 }
974 }
975
976 #[cfg(not(target_arch = "spirv"))]
977 impl AsRef<[f32; 4]> for Vec4 {
978 #[inline]
as_ref(&self) -> &[f32; 4]979 fn as_ref(&self) -> &[f32; 4] {
980 unsafe { &*(self as *const Vec4 as *const [f32; 4]) }
981 }
982 }
983
984 #[cfg(not(target_arch = "spirv"))]
985 impl AsMut<[f32; 4]> for Vec4 {
986 #[inline]
as_mut(&mut self) -> &mut [f32; 4]987 fn as_mut(&mut self) -> &mut [f32; 4] {
988 unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) }
989 }
990 }
991
992 impl Sum for Vec4 {
993 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,994 fn sum<I>(iter: I) -> Self
995 where
996 I: Iterator<Item = Self>,
997 {
998 iter.fold(Self::ZERO, Self::add)
999 }
1000 }
1001
1002 impl<'a> Sum<&'a Self> for Vec4 {
1003 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1004 fn sum<I>(iter: I) -> Self
1005 where
1006 I: Iterator<Item = &'a Self>,
1007 {
1008 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1009 }
1010 }
1011
1012 impl Product for Vec4 {
1013 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1014 fn product<I>(iter: I) -> Self
1015 where
1016 I: Iterator<Item = Self>,
1017 {
1018 iter.fold(Self::ONE, Self::mul)
1019 }
1020 }
1021
1022 impl<'a> Product<&'a Self> for Vec4 {
1023 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1024 fn product<I>(iter: I) -> Self
1025 where
1026 I: Iterator<Item = &'a Self>,
1027 {
1028 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1029 }
1030 }
1031
1032 impl Neg for Vec4 {
1033 type Output = Self;
1034 #[inline]
neg(self) -> Self1035 fn neg(self) -> Self {
1036 Self(f32x4_neg(self.0))
1037 }
1038 }
1039
1040 impl Index<usize> for Vec4 {
1041 type Output = f32;
1042 #[inline]
index(&self, index: usize) -> &Self::Output1043 fn index(&self, index: usize) -> &Self::Output {
1044 match index {
1045 0 => &self.x,
1046 1 => &self.y,
1047 2 => &self.z,
1048 3 => &self.w,
1049 _ => panic!("index out of bounds"),
1050 }
1051 }
1052 }
1053
1054 impl IndexMut<usize> for Vec4 {
1055 #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1056 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1057 match index {
1058 0 => &mut self.x,
1059 1 => &mut self.y,
1060 2 => &mut self.z,
1061 3 => &mut self.w,
1062 _ => panic!("index out of bounds"),
1063 }
1064 }
1065 }
1066
1067 #[cfg(not(target_arch = "spirv"))]
1068 impl fmt::Display for Vec4 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1069 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1070 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1071 }
1072 }
1073
1074 #[cfg(not(target_arch = "spirv"))]
1075 impl fmt::Debug for Vec4 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1076 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1077 fmt.debug_tuple(stringify!(Vec4))
1078 .field(&self.x)
1079 .field(&self.y)
1080 .field(&self.z)
1081 .field(&self.w)
1082 .finish()
1083 }
1084 }
1085
1086 impl From<Vec4> for v128 {
1087 #[inline]
from(t: Vec4) -> Self1088 fn from(t: Vec4) -> Self {
1089 t.0
1090 }
1091 }
1092
1093 impl From<v128> for Vec4 {
1094 #[inline]
from(t: v128) -> Self1095 fn from(t: v128) -> Self {
1096 Self(t)
1097 }
1098 }
1099
1100 impl From<[f32; 4]> for Vec4 {
1101 #[inline]
from(a: [f32; 4]) -> Self1102 fn from(a: [f32; 4]) -> Self {
1103 Self::new(a[0], a[1], a[2], a[3])
1104 }
1105 }
1106
1107 impl From<Vec4> for [f32; 4] {
1108 #[inline]
from(v: Vec4) -> Self1109 fn from(v: Vec4) -> Self {
1110 unsafe { *(&v.0 as *const v128 as *const Self) }
1111 }
1112 }
1113
1114 impl From<(f32, f32, f32, f32)> for Vec4 {
1115 #[inline]
from(t: (f32, f32, f32, f32)) -> Self1116 fn from(t: (f32, f32, f32, f32)) -> Self {
1117 Self::new(t.0, t.1, t.2, t.3)
1118 }
1119 }
1120
1121 impl From<Vec4> for (f32, f32, f32, f32) {
1122 #[inline]
from(v: Vec4) -> Self1123 fn from(v: Vec4) -> Self {
1124 unsafe { *(&v.0 as *const v128 as *const Self) }
1125 }
1126 }
1127
1128 impl From<(Vec3A, f32)> for Vec4 {
1129 #[inline]
from((v, w): (Vec3A, f32)) -> Self1130 fn from((v, w): (Vec3A, f32)) -> Self {
1131 v.extend(w)
1132 }
1133 }
1134
1135 impl From<(f32, Vec3A)> for Vec4 {
1136 #[inline]
from((x, v): (f32, Vec3A)) -> Self1137 fn from((x, v): (f32, Vec3A)) -> Self {
1138 Self::new(x, v.x, v.y, v.z)
1139 }
1140 }
1141
1142 impl From<(Vec3, f32)> for Vec4 {
1143 #[inline]
from((v, w): (Vec3, f32)) -> Self1144 fn from((v, w): (Vec3, f32)) -> Self {
1145 Self::new(v.x, v.y, v.z, w)
1146 }
1147 }
1148
1149 impl From<(f32, Vec3)> for Vec4 {
1150 #[inline]
from((x, v): (f32, Vec3)) -> Self1151 fn from((x, v): (f32, Vec3)) -> Self {
1152 Self::new(x, v.x, v.y, v.z)
1153 }
1154 }
1155
1156 impl From<(Vec2, f32, f32)> for Vec4 {
1157 #[inline]
from((v, z, w): (Vec2, f32, f32)) -> Self1158 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1159 Self::new(v.x, v.y, z, w)
1160 }
1161 }
1162
1163 impl From<(Vec2, Vec2)> for Vec4 {
1164 #[inline]
from((v, u): (Vec2, Vec2)) -> Self1165 fn from((v, u): (Vec2, Vec2)) -> Self {
1166 Self::new(v.x, v.y, u.x, u.y)
1167 }
1168 }
1169
1170 impl Deref for Vec4 {
1171 type Target = crate::deref::Vec4<f32>;
1172 #[inline]
deref(&self) -> &Self::Target1173 fn deref(&self) -> &Self::Target {
1174 unsafe { &*(self as *const Self).cast() }
1175 }
1176 }
1177
1178 impl DerefMut for Vec4 {
1179 #[inline]
deref_mut(&mut self) -> &mut Self::Target1180 fn deref_mut(&mut self) -> &mut Self::Target {
1181 unsafe { &mut *(self as *mut Self).cast() }
1182 }
1183 }
1184