Debian Codegraph @

debian/missing-sources/glam-0.29.3/src/f32/neon/vec3a.rs

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