Debian Codegraph @

debian/missing-sources/glam/src/f32/wasm32/vec4.rs

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