Debian Codegraph @

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

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