1use crate::{BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4, f32::math, neon::*};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9use core::arch::aarch64::*;
10
11#[cfg(feature = "zerocopy-08")]
12use zerocopy_derive_08::*;
13
14#[repr(C)]
15union UnionCast {
16 a: [f32; 4],
17 v: Vec3A,
18}
19
20#[inline(always)]
22#[must_use]
23pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
24 Vec3A::new(x, y, z)
25}
26
27#[derive(Clone, Copy)]
37#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
38#[cfg_attr(
39 feature = "zerocopy-08",
40 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
41)]
42#[repr(transparent)]
43pub struct Vec3A(pub(crate) float32x4_t);
44
45impl Vec3A {
46 pub const ZERO: Self = Self::splat(0.0);
48
49 pub const ONE: Self = Self::splat(1.0);
51
52 pub const NEG_ONE: Self = Self::splat(-1.0);
54
55 pub const MIN: Self = Self::splat(f32::MIN);
57
58 pub const MAX: Self = Self::splat(f32::MAX);
60
61 pub const NAN: Self = Self::splat(f32::NAN);
63
64 pub const INFINITY: Self = Self::splat(f32::INFINITY);
66
67 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
69
70 pub const X: Self = Self::new(1.0, 0.0, 0.0);
72
73 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
75
76 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
78
79 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
81
82 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
84
85 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
87
88 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
90
91 pub const USES_CORE_SIMD: bool = false;
93 pub const USES_NEON: bool = true;
95 pub const USES_SCALAR_MATH: bool = false;
97 pub const USES_SSE2: bool = false;
99 pub const USES_WASM_SIMD: bool = false;
101
102 #[inline(always)]
104 #[must_use]
105 pub const fn new(x: f32, y: f32, z: f32) -> Self {
106 unsafe { UnionCast { a: [x, y, z, z] }.v }
107 }
108
109 #[inline]
111 #[must_use]
112 pub const fn splat(v: f32) -> Self {
113 unsafe { UnionCast { a: [v; 4] }.v }
114 }
115
116 #[inline]
118 #[must_use]
119 pub fn map<F>(self, mut f: F) -> Self
120 where
121 F: FnMut(f32) -> f32,
122 {
123 Self::new(f(self.x), f(self.y), f(self.z))
124 }
125
126 #[inline]
132 #[must_use]
133 pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
134 Self(unsafe { vbslq_f32(mask.0, if_true.0, if_false.0) })
135 }
136
137 #[inline]
139 #[must_use]
140 pub const fn from_array(a: [f32; 3]) -> Self {
141 Self::new(a[0], a[1], a[2])
142 }
143
144 #[inline]
146 #[must_use]
147 pub const fn to_array(&self) -> [f32; 3] {
148 unsafe { *(self as *const Self as *const [f32; 3]) }
149 }
150
151 #[inline]
157 #[must_use]
158 #[track_caller]
159 pub const fn from_slice(slice: &[f32]) -> Self {
160 assert!(slice.len() >= 3);
161 Self::new(slice[0], slice[1], slice[2])
162 }
163
164 #[inline]
170 #[track_caller]
171 pub fn write_to_slice(self, slice: &mut [f32]) {
172 slice[..3].copy_from_slice(&self.to_array());
173 }
174
175 #[inline]
179 #[must_use]
180 pub fn from_vec4(v: Vec4) -> Self {
181 Self(v.0)
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn extend(self, w: f32) -> Vec4 {
188 Vec4::new(self.x, self.y, self.z, w)
189 }
190
191 #[inline]
195 #[must_use]
196 pub fn truncate(self) -> Vec2 {
197 use crate::swizzles::Vec3Swizzles;
198 self.xy()
199 }
200
201 #[inline]
207 #[must_use]
208 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
209 pub fn from_homogeneous(v: Vec4) -> Self {
210 glam_assert!(v.w != 0.0);
211 Self::from_vec4(v) / v.w
212 }
213
214 #[inline]
216 #[must_use]
217 pub fn to_homogeneous(self) -> Vec4 {
218 self.extend(1.0)
219 }
220
221 #[inline]
223 #[must_use]
224 pub fn to_vec3(self) -> Vec3 {
225 Vec3::from(self)
226 }
227
228 #[inline]
230 #[must_use]
231 pub fn with_x(mut self, x: f32) -> Self {
232 self.x = x;
233 self
234 }
235
236 #[inline]
238 #[must_use]
239 pub fn with_y(mut self, y: f32) -> Self {
240 self.y = y;
241 self
242 }
243
244 #[inline]
246 #[must_use]
247 pub fn with_z(mut self, z: f32) -> Self {
248 self.z = z;
249 self
250 }
251
252 #[inline]
254 #[must_use]
255 pub fn dot(self, rhs: Self) -> f32 {
256 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
258 }
259
260 #[inline]
262 #[must_use]
263 pub fn dot_into_vec(self, rhs: Self) -> Self {
264 Self(unsafe { dot3_into_f32x4(self.0, rhs.0) })
265 }
266
267 #[inline]
269 #[must_use]
270 pub fn cross(self, rhs: Self) -> Self {
271 unsafe {
272 let lhs = self.0;
274 let rhs = rhs.0;
275 let lhs_yzwx = vextq_f32(lhs, lhs, 1);
277 let rhs_wxyz = vextq_f32(rhs, rhs, 3);
278
279 let lhs_yzx = vsetq_lane_f32(vgetq_lane_f32(lhs, 0), lhs_yzwx, 2);
280 let rhs_zxy = vsetq_lane_f32(vgetq_lane_f32(rhs, 2), rhs_wxyz, 0);
281
282 let part_a = vmulq_f32(lhs_yzx, rhs_zxy);
284
285 let lhs_wxyz = vextq_f32(lhs, lhs, 3);
286 let rhs_yzwx = vextq_f32(rhs, rhs, 1);
287 let lhs_zxy = vsetq_lane_f32(vgetq_lane_f32(lhs, 2), lhs_wxyz, 0);
288 let rhs_yzx = vsetq_lane_f32(vgetq_lane_f32(rhs, 0), rhs_yzwx, 2);
289
290 let result = vfmsq_f32(part_a, lhs_zxy, rhs_yzx);
292 Self(result)
293 }
294 }
295
296 #[inline]
304 #[must_use]
305 pub fn min(self, rhs: Self) -> Self {
306 Self(unsafe { vbslq_f32(vcltq_f32(self.0, rhs.0), self.0, rhs.0) })
307 }
308
309 #[inline]
317 #[must_use]
318 pub fn max(self, rhs: Self) -> Self {
319 Self(unsafe { vbslq_f32(vcgtq_f32(self.0, rhs.0), self.0, rhs.0) })
320 }
321
322 #[inline]
332 #[must_use]
333 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
334 pub fn clamp(self, min: Self, max: Self) -> Self {
335 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
336 self.max(min).min(max)
337 }
338
339 #[inline]
345 #[must_use]
346 pub fn min_element(self) -> f32 {
347 let min = |a: float32x4_t, b: float32x4_t| unsafe { vbslq_f32(vcltq_f32(a, b), a, b) };
348 let v = self.0;
349 let v = min(v, unsafe { vrev64q_f32(v) });
350 let v = min(v, unsafe { vextq_f32(self.0, self.0, 2) });
351 unsafe { vgetq_lane_f32::<0>(v) }
352 }
353
354 #[inline]
360 #[must_use]
361 pub fn max_element(self) -> f32 {
362 let max = |a: float32x4_t, b: float32x4_t| unsafe { vbslq_f32(vcgtq_f32(a, b), a, b) };
363 let v = self.0;
364 let v = max(v, unsafe { vrev64q_f32(v) });
365 let v = max(v, unsafe { vextq_f32(self.0, self.0, 2) });
366 unsafe { vgetq_lane_f32::<0>(v) }
367 }
368
369 #[doc(alias = "argmin")]
371 #[inline]
372 #[must_use]
373 pub fn min_position(self) -> usize {
374 let mut min = self.x;
375 let mut index = 0;
376 if self.y < min {
377 min = self.y;
378 index = 1;
379 }
380 if self.z < min {
381 index = 2;
382 }
383 index
384 }
385
386 #[doc(alias = "argmax")]
388 #[inline]
389 #[must_use]
390 pub fn max_position(self) -> usize {
391 let mut max = self.x;
392 let mut index = 0;
393 if self.y > max {
394 max = self.y;
395 index = 1;
396 }
397 if self.z > max {
398 index = 2;
399 }
400 index
401 }
402
403 #[inline]
407 #[must_use]
408 pub fn element_sum(self) -> f32 {
409 unsafe { vaddvq_f32(vsetq_lane_f32(0.0, self.0, 3)) }
410 }
411
412 #[inline]
416 #[must_use]
417 pub fn element_product(self) -> f32 {
418 unsafe {
419 let s = vmuls_laneq_f32(vgetq_lane_f32(self.0, 0), self.0, 1);
420 vmuls_laneq_f32(s, self.0, 2)
421 }
422 }
423
424 #[inline]
430 #[must_use]
431 pub fn cmpeq(self, rhs: Self) -> BVec3A {
432 BVec3A(unsafe { vceqq_f32(self.0, rhs.0) })
433 }
434
435 #[inline]
441 #[must_use]
442 pub fn cmpne(self, rhs: Self) -> BVec3A {
443 BVec3A(unsafe { vmvnq_u32(vceqq_f32(self.0, rhs.0)) })
444 }
445
446 #[inline]
452 #[must_use]
453 pub fn cmpge(self, rhs: Self) -> BVec3A {
454 BVec3A(unsafe { vcgeq_f32(self.0, rhs.0) })
455 }
456
457 #[inline]
463 #[must_use]
464 pub fn cmpgt(self, rhs: Self) -> BVec3A {
465 BVec3A(unsafe { vcgtq_f32(self.0, rhs.0) })
466 }
467
468 #[inline]
474 #[must_use]
475 pub fn cmple(self, rhs: Self) -> BVec3A {
476 BVec3A(unsafe { vcleq_f32(self.0, rhs.0) })
477 }
478
479 #[inline]
485 #[must_use]
486 pub fn cmplt(self, rhs: Self) -> BVec3A {
487 BVec3A(unsafe { vcltq_f32(self.0, rhs.0) })
488 }
489
490 #[inline]
492 #[must_use]
493 pub fn abs(self) -> Self {
494 Self(unsafe { vabsq_f32(self.0) })
495 }
496
497 #[inline]
503 #[must_use]
504 pub fn signum(self) -> Self {
505 let result = Self(unsafe {
506 vreinterpretq_f32_u32(vorrq_u32(
507 vandq_u32(
508 vreinterpretq_u32_f32(self.0),
509 vreinterpretq_u32_f32(Self::NEG_ONE.0),
510 ),
511 vreinterpretq_u32_f32(Self::ONE.0),
512 ))
513 });
514 let mask = self.is_nan_mask();
515 Self::select(mask, self, result)
516 }
517
518 #[inline]
520 #[must_use]
521 pub fn copysign(self, rhs: Self) -> Self {
522 let mask = Self::splat(-0.0);
523 Self(unsafe {
524 vreinterpretq_f32_u32(vorrq_u32(
525 vandq_u32(vreinterpretq_u32_f32(rhs.0), vreinterpretq_u32_f32(mask.0)),
526 vandq_u32(
527 vreinterpretq_u32_f32(self.0),
528 vmvnq_u32(vreinterpretq_u32_f32(mask.0)),
529 ),
530 ))
531 })
532 }
533
534 #[inline]
542 #[must_use]
543 pub fn is_negative_bitmask(self) -> u32 {
544 unsafe {
545 let nmask = vreinterpretq_u32_f32(vdupq_n_f32(-0.0));
546 let m = vandq_u32(vreinterpretq_u32_f32(self.0), nmask);
547 let x = vgetq_lane_u32(m, 0) >> 31;
548 let y = vgetq_lane_u32(m, 1) >> 31;
549 let z = vgetq_lane_u32(m, 2) >> 31;
550
551 x | y << 1 | z << 2
552 }
553 }
554
555 #[inline]
560 #[must_use]
561 pub fn is_negative_mask(self) -> BVec3A {
562 BVec3A(unsafe { vcltq_s32(vreinterpretq_s32_f32(self.0), vdupq_n_s32(0)) })
563 }
564
565 #[inline]
568 #[must_use]
569 pub fn is_finite(self) -> bool {
570 self.is_finite_mask().all()
571 }
572
573 #[inline]
577 #[must_use]
578 pub fn is_finite_mask(self) -> BVec3A {
579 BVec3A(unsafe { vcltq_f32(vabsq_f32(self.0), Self::INFINITY.0) })
580 }
581
582 #[inline]
584 #[must_use]
585 pub fn is_nan(self) -> bool {
586 self.is_nan_mask().any()
587 }
588
589 #[inline]
593 #[must_use]
594 pub fn is_nan_mask(self) -> BVec3A {
595 BVec3A(unsafe { vmvnq_u32(vceqq_f32(self.0, self.0)) })
596 }
597
598 #[doc(alias = "magnitude")]
600 #[inline]
601 #[must_use]
602 pub fn length(self) -> f32 {
603 math::sqrt(self.dot(self))
604 }
605
606 #[allow(dead_code)]
608 fn is_non_zero(self) -> bool {
609 self.length_squared() > 0.0
610 }
611
612 #[doc(alias = "magnitude2")]
616 #[inline]
617 #[must_use]
618 pub fn length_squared(self) -> f32 {
619 self.dot(self)
620 }
621
622 #[inline]
626 #[must_use]
627 pub fn length_recip(self) -> f32 {
628 1.0 / self.length()
629 }
630
631 #[inline]
633 #[must_use]
634 pub fn distance(self, rhs: Self) -> f32 {
635 (self - rhs).length()
636 }
637
638 #[inline]
640 #[must_use]
641 pub fn distance_squared(self, rhs: Self) -> f32 {
642 (self - rhs).length_squared()
643 }
644
645 #[inline]
647 #[must_use]
648 pub fn div_euclid(self, rhs: Self) -> Self {
649 Self::new(
650 math::div_euclid(self.x, rhs.x),
651 math::div_euclid(self.y, rhs.y),
652 math::div_euclid(self.z, rhs.z),
653 )
654 }
655
656 #[inline]
660 #[must_use]
661 pub fn rem_euclid(self, rhs: Self) -> Self {
662 Self::new(
663 math::rem_euclid(self.x, rhs.x),
664 math::rem_euclid(self.y, rhs.y),
665 math::rem_euclid(self.z, rhs.z),
666 )
667 }
668
669 #[inline]
679 #[must_use]
680 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
681 pub fn normalize(self) -> Self {
682 #[allow(clippy::let_and_return)]
683 let normalized = self.mul(self.length_recip());
684 glam_assert!(normalized.is_finite());
685 normalized
686 }
687
688 #[inline]
695 #[must_use]
696 pub fn try_normalize(self) -> Option<Self> {
697 let rcp = self.length_recip();
698 if rcp.is_finite() && rcp > 0.0 {
699 Some(self * rcp)
700 } else {
701 None
702 }
703 }
704
705 #[inline]
713 #[must_use]
714 pub fn normalize_or(self, fallback: Self) -> Self {
715 let rcp = self.length_recip();
716 if rcp.is_finite() && rcp > 0.0 {
717 self * rcp
718 } else {
719 fallback
720 }
721 }
722
723 #[inline]
730 #[must_use]
731 pub fn normalize_or_zero(self) -> Self {
732 self.normalize_or(Self::ZERO)
733 }
734
735 #[inline]
739 #[must_use]
740 pub fn normalize_and_length(self) -> (Self, f32) {
741 let length = self.length();
742 let rcp = 1.0 / length;
743 if rcp.is_finite() && rcp > 0.0 {
744 (self * rcp, length)
745 } else {
746 (Self::X, 0.0)
747 }
748 }
749
750 #[inline]
754 #[must_use]
755 pub fn is_normalized(self) -> bool {
756 (self.length_squared() - 1.0).abs() <= 2e-4
757 }
758
759 #[inline]
767 #[must_use]
768 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
769 pub fn project_onto(self, rhs: Self) -> Self {
770 let rhs_len_sq = rhs.dot(rhs);
771 glam_assert!(rhs_len_sq != 0.0);
772 rhs * (self.dot(rhs) / rhs_len_sq)
773 }
774
775 #[doc(alias("plane"))]
786 #[inline]
787 #[must_use]
788 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
789 pub fn reject_from(self, rhs: Self) -> Self {
790 self - self.project_onto(rhs)
791 }
792
793 #[inline]
801 #[must_use]
802 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
803 pub fn project_onto_normalized(self, rhs: Self) -> Self {
804 glam_assert!(rhs.is_normalized());
805 rhs * self.dot(rhs)
806 }
807
808 #[doc(alias("plane"))]
819 #[inline]
820 #[must_use]
821 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
822 pub fn reject_from_normalized(self, rhs: Self) -> Self {
823 self - self.project_onto_normalized(rhs)
824 }
825
826 #[inline]
829 #[must_use]
830 pub fn round(self) -> Self {
831 Self(unsafe { vrndaq_f32(self.0) })
832 }
833
834 #[inline]
837 #[must_use]
838 pub fn floor(self) -> Self {
839 Self(unsafe { vrndmq_f32(self.0) })
840 }
841
842 #[inline]
845 #[must_use]
846 pub fn ceil(self) -> Self {
847 Self(unsafe { vrndpq_f32(self.0) })
848 }
849
850 #[inline]
853 #[must_use]
854 pub fn trunc(self) -> Self {
855 Self(unsafe { vrndq_f32(self.0) })
856 }
857
858 #[inline]
862 #[must_use]
863 pub fn step(self, rhs: Self) -> Self {
864 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
865 }
866
867 #[inline]
878 #[must_use]
879 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
880 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
881 glam_assert!(edge0.cmplt(edge1).all());
882 let t = ((self - edge0) / (edge1 - edge0)).saturate();
883 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
884 }
885
886 #[inline]
888 #[must_use]
889 pub fn saturate(self) -> Self {
890 self.clamp(Self::ZERO, Self::ONE)
891 }
892
893 #[inline]
900 #[must_use]
901 pub fn fract(self) -> Self {
902 self - self.trunc()
903 }
904
905 #[inline]
912 #[must_use]
913 pub fn fract_gl(self) -> Self {
914 self - self.floor()
915 }
916
917 #[inline]
920 #[must_use]
921 pub fn exp(self) -> Self {
922 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
923 }
924
925 #[inline]
927 #[must_use]
928 pub fn exp2(self) -> Self {
929 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
930 }
931
932 #[inline]
935 #[must_use]
936 pub fn ln(self) -> Self {
937 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
938 }
939
940 #[inline]
943 #[must_use]
944 pub fn log2(self) -> Self {
945 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
946 }
947
948 #[inline]
950 #[must_use]
951 pub fn powf(self, n: f32) -> Self {
952 Self::new(
953 math::powf(self.x, n),
954 math::powf(self.y, n),
955 math::powf(self.z, n),
956 )
957 }
958
959 #[inline]
962 #[must_use]
963 pub fn sqrt(self) -> Self {
964 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
965 }
966
967 #[inline]
969 #[must_use]
970 pub fn cos(self) -> Self {
971 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
972 }
973
974 #[inline]
976 #[must_use]
977 pub fn sin(self) -> Self {
978 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
979 }
980
981 #[inline]
983 #[must_use]
984 pub fn sin_cos(self) -> (Self, Self) {
985 let (sin_x, cos_x) = math::sin_cos(self.x);
986 let (sin_y, cos_y) = math::sin_cos(self.y);
987 let (sin_z, cos_z) = math::sin_cos(self.z);
988
989 (
990 Self::new(sin_x, sin_y, sin_z),
991 Self::new(cos_x, cos_y, cos_z),
992 )
993 }
994
995 #[inline]
997 #[must_use]
998 pub fn recip(self) -> Self {
999 Self(unsafe { vdivq_f32(Self::ONE.0, self.0) })
1000 }
1001
1002 #[doc(alias = "mix")]
1014 #[inline]
1015 #[must_use]
1016 pub fn lerp(self, rhs: Self, s: f32) -> Self {
1017 self * (1.0 - s) + rhs * s
1018 }
1019
1020 #[doc(alias = "mix")]
1037 #[inline]
1038 #[must_use]
1039 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
1040 Self(unsafe { vfmaq_f32(self.0, vsubq_f32(rhs.0, self.0), vdupq_n_f32(s)) })
1041 }
1042
1043 #[inline]
1048 #[must_use]
1049 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
1050 let a = rhs - self;
1051 let len = a.length();
1052 if len <= d || len <= 1e-4 {
1053 return rhs;
1054 }
1055 self + a / len * d
1056 }
1057
1058 #[inline]
1064 pub fn midpoint(self, rhs: Self) -> Self {
1065 (self + rhs) * 0.5
1066 }
1067
1068 #[inline]
1078 #[must_use]
1079 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1080 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1081 }
1082
1083 #[inline]
1089 #[must_use]
1090 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1091 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1092 glam_assert!(0.0 <= min);
1093 glam_assert!(min <= max);
1094 let length_sq = self.length_squared();
1095 if length_sq < min * min {
1096 min * (self / math::sqrt(length_sq))
1097 } else if length_sq > max * max {
1098 max * (self / math::sqrt(length_sq))
1099 } else {
1100 self
1101 }
1102 }
1103
1104 #[inline]
1110 #[must_use]
1111 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1112 pub fn clamp_length_max(self, max: f32) -> Self {
1113 glam_assert!(0.0 <= max);
1114 let length_sq = self.length_squared();
1115 if length_sq > max * max {
1116 max * (self / math::sqrt(length_sq))
1117 } else {
1118 self
1119 }
1120 }
1121
1122 #[inline]
1128 #[must_use]
1129 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1130 pub fn clamp_length_min(self, min: f32) -> Self {
1131 glam_assert!(0.0 <= min);
1132 let length_sq = self.length_squared();
1133 if length_sq < min * min {
1134 min * (self / math::sqrt(length_sq))
1135 } else {
1136 self
1137 }
1138 }
1139
1140 #[inline]
1148 #[must_use]
1149 pub fn mul_add(self, a: Self, b: Self) -> Self {
1150 Self(unsafe { vfmaq_f32(b.0, self.0, a.0) })
1151 }
1152
1153 #[inline]
1162 #[must_use]
1163 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1164 pub fn reflect(self, normal: Self) -> Self {
1165 glam_assert!(normal.is_normalized());
1166 self - 2.0 * self.dot(normal) * normal
1167 }
1168
1169 #[inline]
1179 #[must_use]
1180 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1181 pub fn refract(self, normal: Self, eta: f32) -> Self {
1182 glam_assert!(self.is_normalized());
1183 glam_assert!(normal.is_normalized());
1184 let n_dot_i = normal.dot(self);
1185 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1186 if k >= 0.0 {
1187 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1188 } else {
1189 Self::ZERO
1190 }
1191 }
1192
1193 #[inline]
1204 #[must_use]
1205 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1206 pub fn angle_between(self, rhs: Self) -> f32 {
1207 glam_assert!(self.is_non_zero());
1208 glam_assert!(rhs.is_non_zero());
1209 math::acos_approx(
1210 self.dot(rhs)
1211 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1212 )
1213 }
1214
1215 #[doc(alias = "signed_angle")]
1231 #[inline]
1232 #[must_use]
1233 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1234 pub fn angle_to(self, rhs: Self, axis: Self) -> f32 {
1235 glam_assert!(axis.is_normalized());
1236 glam_assert!(self.is_non_zero());
1237 glam_assert!(rhs.is_non_zero());
1238 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1239 }
1240
1241 #[inline]
1243 #[must_use]
1244 pub fn rotate_x(self, angle: f32) -> Self {
1245 let (sina, cosa) = math::sin_cos(angle);
1246 Self::new(
1247 self.x,
1248 self.y * cosa - self.z * sina,
1249 self.y * sina + self.z * cosa,
1250 )
1251 }
1252
1253 #[inline]
1255 #[must_use]
1256 pub fn rotate_y(self, angle: f32) -> Self {
1257 let (sina, cosa) = math::sin_cos(angle);
1258 Self::new(
1259 self.x * cosa + self.z * sina,
1260 self.y,
1261 self.x * -sina + self.z * cosa,
1262 )
1263 }
1264
1265 #[inline]
1267 #[must_use]
1268 pub fn rotate_z(self, angle: f32) -> Self {
1269 let (sina, cosa) = math::sin_cos(angle);
1270 Self::new(
1271 self.x * cosa - self.y * sina,
1272 self.x * sina + self.y * cosa,
1273 self.z,
1274 )
1275 }
1276
1277 #[inline]
1285 #[must_use]
1286 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1287 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1288 Quat::from_axis_angle(axis.into(), angle) * self
1289 }
1290
1291 #[inline]
1301 #[must_use]
1302 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1303 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1304 let angle_between = self.angle_between(rhs);
1305 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1307 let axis = self
1308 .cross(rhs)
1309 .try_normalize()
1310 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1311 Quat::from_axis_angle(axis.into(), angle) * self
1312 }
1313
1314 #[inline]
1321 #[must_use]
1322 pub fn any_orthogonal_vector(self) -> Self {
1323 if self.x.abs() > self.y.abs() {
1325 Self::new(-self.z, 0.0, self.x) } else {
1327 Self::new(0.0, self.z, -self.y) }
1329 }
1330
1331 #[inline]
1339 #[must_use]
1340 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1341 pub fn any_orthonormal_vector(self) -> Self {
1342 glam_assert!(self.is_normalized());
1343 let sign = self.z.signum();
1345 let a = -1.0 / (sign + self.z);
1346 let b = self.x * self.y * a;
1347 Self::new(b, sign + self.y * self.y * a, -self.y)
1348 }
1349
1350 #[inline]
1357 #[must_use]
1358 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1359 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1360 glam_assert!(self.is_normalized());
1361 let sign = self.z.signum();
1363 let a = -1.0 / (sign + self.z);
1364 let b = self.x * self.y * a;
1365 (
1366 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1367 Self::new(b, sign + self.y * self.y * a, -self.y),
1368 )
1369 }
1370
1371 #[inline]
1381 #[must_use]
1382 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1383 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1384 glam_assert!(self.is_non_zero());
1385 glam_assert!(rhs.is_non_zero());
1386 let self_length = self.length();
1387 let rhs_length = rhs.length();
1388 let dot = self.dot(rhs) / (self_length * rhs_length);
1390 if dot.abs() < 1.0 - 3e-7 {
1392 let theta = math::acos_approx(dot);
1394 let coefs = [1.0 - s, s, 1.0, 0.0];
1396 let (sin_theta, t1, t2) = unsafe {
1397 let tmp = vmulq_f32(vdupq_n_f32(theta), vld1q_f32(coefs.as_ptr()));
1398 let tmp = f32x4_sin(tmp);
1399 (
1400 vgetq_lane_f32(tmp, 2), vgetq_lane_f32(tmp, 0), vgetq_lane_f32(tmp, 1), )
1404 };
1405
1406 let result_length = self_length.lerp_monotonic(rhs_length, s);
1408 return (self * (result_length / self_length) * t1
1410 + rhs * (result_length / rhs_length) * t2)
1411 * (1.0 / sin_theta);
1412 }
1413 if dot < 0.0 {
1414 let axis = self.any_orthogonal_vector().normalize().into();
1418 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1419 let result_length = self_length.lerp_monotonic(rhs_length, s);
1421 rotation * self * (result_length / self_length)
1422 } else {
1423 self.lerp(rhs, s)
1425 }
1426 }
1427
1428 #[cfg(feature = "f64")]
1430 #[inline]
1431 #[must_use]
1432 pub fn as_dvec3(self) -> crate::DVec3 {
1433 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1434 }
1435
1436 #[cfg(feature = "i8")]
1438 #[inline]
1439 #[must_use]
1440 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1441 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1442 }
1443
1444 #[cfg(feature = "u8")]
1446 #[inline]
1447 #[must_use]
1448 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1449 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1450 }
1451
1452 #[cfg(feature = "i16")]
1454 #[inline]
1455 #[must_use]
1456 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1457 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1458 }
1459
1460 #[cfg(feature = "u16")]
1462 #[inline]
1463 #[must_use]
1464 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1465 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1466 }
1467
1468 #[cfg(feature = "i32")]
1470 #[inline]
1471 #[must_use]
1472 pub fn as_ivec3(self) -> crate::IVec3 {
1473 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1474 }
1475
1476 #[cfg(feature = "u32")]
1478 #[inline]
1479 #[must_use]
1480 pub fn as_uvec3(self) -> crate::UVec3 {
1481 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1482 }
1483
1484 #[cfg(feature = "i64")]
1486 #[inline]
1487 #[must_use]
1488 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1489 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1490 }
1491
1492 #[cfg(feature = "u64")]
1494 #[inline]
1495 #[must_use]
1496 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1497 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1498 }
1499
1500 #[cfg(feature = "isize")]
1502 #[inline]
1503 #[must_use]
1504 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1505 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1506 }
1507
1508 #[cfg(feature = "usize")]
1510 #[inline]
1511 #[must_use]
1512 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1513 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1514 }
1515}
1516
1517impl Default for Vec3A {
1518 #[inline(always)]
1519 fn default() -> Self {
1520 Self::ZERO
1521 }
1522}
1523
1524impl PartialEq for Vec3A {
1525 #[inline]
1526 fn eq(&self, rhs: &Self) -> bool {
1527 self.cmpeq(*rhs).all()
1528 }
1529}
1530
1531impl Div for Vec3A {
1532 type Output = Self;
1533 #[inline]
1534 fn div(self, rhs: Self) -> Self {
1535 Self(unsafe { vdivq_f32(self.0, rhs.0) })
1536 }
1537}
1538
1539impl Div<&Self> for Vec3A {
1540 type Output = Self;
1541 #[inline]
1542 fn div(self, rhs: &Self) -> Self {
1543 self.div(*rhs)
1544 }
1545}
1546
1547impl Div<&Vec3A> for &Vec3A {
1548 type Output = Vec3A;
1549 #[inline]
1550 fn div(self, rhs: &Vec3A) -> Vec3A {
1551 (*self).div(*rhs)
1552 }
1553}
1554
1555impl Div<Vec3A> for &Vec3A {
1556 type Output = Vec3A;
1557 #[inline]
1558 fn div(self, rhs: Vec3A) -> Vec3A {
1559 (*self).div(rhs)
1560 }
1561}
1562
1563impl DivAssign for Vec3A {
1564 #[inline]
1565 fn div_assign(&mut self, rhs: Self) {
1566 self.0 = unsafe { vdivq_f32(self.0, rhs.0) };
1567 }
1568}
1569
1570impl DivAssign<&Self> for Vec3A {
1571 #[inline]
1572 fn div_assign(&mut self, rhs: &Self) {
1573 self.div_assign(*rhs);
1574 }
1575}
1576
1577impl Div<f32> for Vec3A {
1578 type Output = Self;
1579 #[inline]
1580 fn div(self, rhs: f32) -> Self {
1581 Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) })
1582 }
1583}
1584
1585impl Div<&f32> for Vec3A {
1586 type Output = Self;
1587 #[inline]
1588 fn div(self, rhs: &f32) -> Self {
1589 self.div(*rhs)
1590 }
1591}
1592
1593impl Div<&f32> for &Vec3A {
1594 type Output = Vec3A;
1595 #[inline]
1596 fn div(self, rhs: &f32) -> Vec3A {
1597 (*self).div(*rhs)
1598 }
1599}
1600
1601impl Div<f32> for &Vec3A {
1602 type Output = Vec3A;
1603 #[inline]
1604 fn div(self, rhs: f32) -> Vec3A {
1605 (*self).div(rhs)
1606 }
1607}
1608
1609impl DivAssign<f32> for Vec3A {
1610 #[inline]
1611 fn div_assign(&mut self, rhs: f32) {
1612 self.0 = unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) };
1613 }
1614}
1615
1616impl DivAssign<&f32> for Vec3A {
1617 #[inline]
1618 fn div_assign(&mut self, rhs: &f32) {
1619 self.div_assign(*rhs);
1620 }
1621}
1622
1623impl Div<Vec3A> for f32 {
1624 type Output = Vec3A;
1625 #[inline]
1626 fn div(self, rhs: Vec3A) -> Vec3A {
1627 Vec3A(unsafe { vdivq_f32(vld1q_dup_f32(&self), rhs.0) })
1628 }
1629}
1630
1631impl Div<&Vec3A> for f32 {
1632 type Output = Vec3A;
1633 #[inline]
1634 fn div(self, rhs: &Vec3A) -> Vec3A {
1635 self.div(*rhs)
1636 }
1637}
1638
1639impl Div<&Vec3A> for &f32 {
1640 type Output = Vec3A;
1641 #[inline]
1642 fn div(self, rhs: &Vec3A) -> Vec3A {
1643 (*self).div(*rhs)
1644 }
1645}
1646
1647impl Div<Vec3A> for &f32 {
1648 type Output = Vec3A;
1649 #[inline]
1650 fn div(self, rhs: Vec3A) -> Vec3A {
1651 (*self).div(rhs)
1652 }
1653}
1654
1655impl Mul for Vec3A {
1656 type Output = Self;
1657 #[inline]
1658 fn mul(self, rhs: Self) -> Self {
1659 Self(unsafe { vmulq_f32(self.0, rhs.0) })
1660 }
1661}
1662
1663impl Mul<&Self> for Vec3A {
1664 type Output = Self;
1665 #[inline]
1666 fn mul(self, rhs: &Self) -> Self {
1667 self.mul(*rhs)
1668 }
1669}
1670
1671impl Mul<&Vec3A> for &Vec3A {
1672 type Output = Vec3A;
1673 #[inline]
1674 fn mul(self, rhs: &Vec3A) -> Vec3A {
1675 (*self).mul(*rhs)
1676 }
1677}
1678
1679impl Mul<Vec3A> for &Vec3A {
1680 type Output = Vec3A;
1681 #[inline]
1682 fn mul(self, rhs: Vec3A) -> Vec3A {
1683 (*self).mul(rhs)
1684 }
1685}
1686
1687impl MulAssign for Vec3A {
1688 #[inline]
1689 fn mul_assign(&mut self, rhs: Self) {
1690 self.0 = unsafe { vmulq_f32(self.0, rhs.0) };
1691 }
1692}
1693
1694impl MulAssign<&Self> for Vec3A {
1695 #[inline]
1696 fn mul_assign(&mut self, rhs: &Self) {
1697 self.mul_assign(*rhs);
1698 }
1699}
1700
1701impl Mul<f32> for Vec3A {
1702 type Output = Self;
1703 #[inline]
1704 fn mul(self, rhs: f32) -> Self {
1705 Self(unsafe { vmulq_n_f32(self.0, rhs) })
1706 }
1707}
1708
1709impl Mul<&f32> for Vec3A {
1710 type Output = Self;
1711 #[inline]
1712 fn mul(self, rhs: &f32) -> Self {
1713 self.mul(*rhs)
1714 }
1715}
1716
1717impl Mul<&f32> for &Vec3A {
1718 type Output = Vec3A;
1719 #[inline]
1720 fn mul(self, rhs: &f32) -> Vec3A {
1721 (*self).mul(*rhs)
1722 }
1723}
1724
1725impl Mul<f32> for &Vec3A {
1726 type Output = Vec3A;
1727 #[inline]
1728 fn mul(self, rhs: f32) -> Vec3A {
1729 (*self).mul(rhs)
1730 }
1731}
1732
1733impl MulAssign<f32> for Vec3A {
1734 #[inline]
1735 fn mul_assign(&mut self, rhs: f32) {
1736 self.0 = unsafe { vmulq_n_f32(self.0, rhs) };
1737 }
1738}
1739
1740impl MulAssign<&f32> for Vec3A {
1741 #[inline]
1742 fn mul_assign(&mut self, rhs: &f32) {
1743 self.mul_assign(*rhs);
1744 }
1745}
1746
1747impl Mul<Vec3A> for f32 {
1748 type Output = Vec3A;
1749 #[inline]
1750 fn mul(self, rhs: Vec3A) -> Vec3A {
1751 Vec3A(unsafe { vmulq_n_f32(rhs.0, self) })
1752 }
1753}
1754
1755impl Mul<&Vec3A> for f32 {
1756 type Output = Vec3A;
1757 #[inline]
1758 fn mul(self, rhs: &Vec3A) -> Vec3A {
1759 self.mul(*rhs)
1760 }
1761}
1762
1763impl Mul<&Vec3A> for &f32 {
1764 type Output = Vec3A;
1765 #[inline]
1766 fn mul(self, rhs: &Vec3A) -> Vec3A {
1767 (*self).mul(*rhs)
1768 }
1769}
1770
1771impl Mul<Vec3A> for &f32 {
1772 type Output = Vec3A;
1773 #[inline]
1774 fn mul(self, rhs: Vec3A) -> Vec3A {
1775 (*self).mul(rhs)
1776 }
1777}
1778
1779impl Add for Vec3A {
1780 type Output = Self;
1781 #[inline]
1782 fn add(self, rhs: Self) -> Self {
1783 Self(unsafe { vaddq_f32(self.0, rhs.0) })
1784 }
1785}
1786
1787impl Add<&Self> for Vec3A {
1788 type Output = Self;
1789 #[inline]
1790 fn add(self, rhs: &Self) -> Self {
1791 self.add(*rhs)
1792 }
1793}
1794
1795impl Add<&Vec3A> for &Vec3A {
1796 type Output = Vec3A;
1797 #[inline]
1798 fn add(self, rhs: &Vec3A) -> Vec3A {
1799 (*self).add(*rhs)
1800 }
1801}
1802
1803impl Add<Vec3A> for &Vec3A {
1804 type Output = Vec3A;
1805 #[inline]
1806 fn add(self, rhs: Vec3A) -> Vec3A {
1807 (*self).add(rhs)
1808 }
1809}
1810
1811impl AddAssign for Vec3A {
1812 #[inline]
1813 fn add_assign(&mut self, rhs: Self) {
1814 self.0 = unsafe { vaddq_f32(self.0, rhs.0) };
1815 }
1816}
1817
1818impl AddAssign<&Self> for Vec3A {
1819 #[inline]
1820 fn add_assign(&mut self, rhs: &Self) {
1821 self.add_assign(*rhs);
1822 }
1823}
1824
1825impl Add<f32> for Vec3A {
1826 type Output = Self;
1827 #[inline]
1828 fn add(self, rhs: f32) -> Self {
1829 Self(unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) })
1830 }
1831}
1832
1833impl Add<&f32> for Vec3A {
1834 type Output = Self;
1835 #[inline]
1836 fn add(self, rhs: &f32) -> Self {
1837 self.add(*rhs)
1838 }
1839}
1840
1841impl Add<&f32> for &Vec3A {
1842 type Output = Vec3A;
1843 #[inline]
1844 fn add(self, rhs: &f32) -> Vec3A {
1845 (*self).add(*rhs)
1846 }
1847}
1848
1849impl Add<f32> for &Vec3A {
1850 type Output = Vec3A;
1851 #[inline]
1852 fn add(self, rhs: f32) -> Vec3A {
1853 (*self).add(rhs)
1854 }
1855}
1856
1857impl AddAssign<f32> for Vec3A {
1858 #[inline]
1859 fn add_assign(&mut self, rhs: f32) {
1860 self.0 = unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) };
1861 }
1862}
1863
1864impl AddAssign<&f32> for Vec3A {
1865 #[inline]
1866 fn add_assign(&mut self, rhs: &f32) {
1867 self.add_assign(*rhs);
1868 }
1869}
1870
1871impl Add<Vec3A> for f32 {
1872 type Output = Vec3A;
1873 #[inline]
1874 fn add(self, rhs: Vec3A) -> Vec3A {
1875 Vec3A(unsafe { vaddq_f32(vld1q_dup_f32(&self), rhs.0) })
1876 }
1877}
1878
1879impl Add<&Vec3A> for f32 {
1880 type Output = Vec3A;
1881 #[inline]
1882 fn add(self, rhs: &Vec3A) -> Vec3A {
1883 self.add(*rhs)
1884 }
1885}
1886
1887impl Add<&Vec3A> for &f32 {
1888 type Output = Vec3A;
1889 #[inline]
1890 fn add(self, rhs: &Vec3A) -> Vec3A {
1891 (*self).add(*rhs)
1892 }
1893}
1894
1895impl Add<Vec3A> for &f32 {
1896 type Output = Vec3A;
1897 #[inline]
1898 fn add(self, rhs: Vec3A) -> Vec3A {
1899 (*self).add(rhs)
1900 }
1901}
1902
1903impl Sub for Vec3A {
1904 type Output = Self;
1905 #[inline]
1906 fn sub(self, rhs: Self) -> Self {
1907 Self(unsafe { vsubq_f32(self.0, rhs.0) })
1908 }
1909}
1910
1911impl Sub<&Self> for Vec3A {
1912 type Output = Self;
1913 #[inline]
1914 fn sub(self, rhs: &Self) -> Self {
1915 self.sub(*rhs)
1916 }
1917}
1918
1919impl Sub<&Vec3A> for &Vec3A {
1920 type Output = Vec3A;
1921 #[inline]
1922 fn sub(self, rhs: &Vec3A) -> Vec3A {
1923 (*self).sub(*rhs)
1924 }
1925}
1926
1927impl Sub<Vec3A> for &Vec3A {
1928 type Output = Vec3A;
1929 #[inline]
1930 fn sub(self, rhs: Vec3A) -> Vec3A {
1931 (*self).sub(rhs)
1932 }
1933}
1934
1935impl SubAssign for Vec3A {
1936 #[inline]
1937 fn sub_assign(&mut self, rhs: Self) {
1938 self.0 = unsafe { vsubq_f32(self.0, rhs.0) };
1939 }
1940}
1941
1942impl SubAssign<&Self> for Vec3A {
1943 #[inline]
1944 fn sub_assign(&mut self, rhs: &Self) {
1945 self.sub_assign(*rhs);
1946 }
1947}
1948
1949impl Sub<f32> for Vec3A {
1950 type Output = Self;
1951 #[inline]
1952 fn sub(self, rhs: f32) -> Self {
1953 Self(unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) })
1954 }
1955}
1956
1957impl Sub<&f32> for Vec3A {
1958 type Output = Self;
1959 #[inline]
1960 fn sub(self, rhs: &f32) -> Self {
1961 self.sub(*rhs)
1962 }
1963}
1964
1965impl Sub<&f32> for &Vec3A {
1966 type Output = Vec3A;
1967 #[inline]
1968 fn sub(self, rhs: &f32) -> Vec3A {
1969 (*self).sub(*rhs)
1970 }
1971}
1972
1973impl Sub<f32> for &Vec3A {
1974 type Output = Vec3A;
1975 #[inline]
1976 fn sub(self, rhs: f32) -> Vec3A {
1977 (*self).sub(rhs)
1978 }
1979}
1980
1981impl SubAssign<f32> for Vec3A {
1982 #[inline]
1983 fn sub_assign(&mut self, rhs: f32) {
1984 self.0 = unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) };
1985 }
1986}
1987
1988impl SubAssign<&f32> for Vec3A {
1989 #[inline]
1990 fn sub_assign(&mut self, rhs: &f32) {
1991 self.sub_assign(*rhs);
1992 }
1993}
1994
1995impl Sub<Vec3A> for f32 {
1996 type Output = Vec3A;
1997 #[inline]
1998 fn sub(self, rhs: Vec3A) -> Vec3A {
1999 Vec3A(unsafe { vsubq_f32(vld1q_dup_f32(&self), rhs.0) })
2000 }
2001}
2002
2003impl Sub<&Vec3A> for f32 {
2004 type Output = Vec3A;
2005 #[inline]
2006 fn sub(self, rhs: &Vec3A) -> Vec3A {
2007 self.sub(*rhs)
2008 }
2009}
2010
2011impl Sub<&Vec3A> for &f32 {
2012 type Output = Vec3A;
2013 #[inline]
2014 fn sub(self, rhs: &Vec3A) -> Vec3A {
2015 (*self).sub(*rhs)
2016 }
2017}
2018
2019impl Sub<Vec3A> for &f32 {
2020 type Output = Vec3A;
2021 #[inline]
2022 fn sub(self, rhs: Vec3A) -> Vec3A {
2023 (*self).sub(rhs)
2024 }
2025}
2026
2027impl Rem for Vec3A {
2028 type Output = Self;
2029 #[inline]
2030 fn rem(self, rhs: Self) -> Self {
2031 unsafe {
2032 let n = vrndmq_f32(vdivq_f32(self.0, rhs.0));
2033 Self(vsubq_f32(self.0, vmulq_f32(n, rhs.0)))
2034 }
2035 }
2036}
2037
2038impl Rem<&Self> for Vec3A {
2039 type Output = Self;
2040 #[inline]
2041 fn rem(self, rhs: &Self) -> Self {
2042 self.rem(*rhs)
2043 }
2044}
2045
2046impl Rem<&Vec3A> for &Vec3A {
2047 type Output = Vec3A;
2048 #[inline]
2049 fn rem(self, rhs: &Vec3A) -> Vec3A {
2050 (*self).rem(*rhs)
2051 }
2052}
2053
2054impl Rem<Vec3A> for &Vec3A {
2055 type Output = Vec3A;
2056 #[inline]
2057 fn rem(self, rhs: Vec3A) -> Vec3A {
2058 (*self).rem(rhs)
2059 }
2060}
2061
2062impl RemAssign for Vec3A {
2063 #[inline]
2064 fn rem_assign(&mut self, rhs: Self) {
2065 *self = self.rem(rhs);
2066 }
2067}
2068
2069impl RemAssign<&Self> for Vec3A {
2070 #[inline]
2071 fn rem_assign(&mut self, rhs: &Self) {
2072 self.rem_assign(*rhs);
2073 }
2074}
2075
2076impl Rem<f32> for Vec3A {
2077 type Output = Self;
2078 #[inline]
2079 fn rem(self, rhs: f32) -> Self {
2080 self.rem(Self::splat(rhs))
2081 }
2082}
2083
2084impl Rem<&f32> for Vec3A {
2085 type Output = Self;
2086 #[inline]
2087 fn rem(self, rhs: &f32) -> Self {
2088 self.rem(*rhs)
2089 }
2090}
2091
2092impl Rem<&f32> for &Vec3A {
2093 type Output = Vec3A;
2094 #[inline]
2095 fn rem(self, rhs: &f32) -> Vec3A {
2096 (*self).rem(*rhs)
2097 }
2098}
2099
2100impl Rem<f32> for &Vec3A {
2101 type Output = Vec3A;
2102 #[inline]
2103 fn rem(self, rhs: f32) -> Vec3A {
2104 (*self).rem(rhs)
2105 }
2106}
2107
2108impl RemAssign<f32> for Vec3A {
2109 #[inline]
2110 fn rem_assign(&mut self, rhs: f32) {
2111 *self = self.rem(Self::splat(rhs));
2112 }
2113}
2114
2115impl RemAssign<&f32> for Vec3A {
2116 #[inline]
2117 fn rem_assign(&mut self, rhs: &f32) {
2118 self.rem_assign(*rhs);
2119 }
2120}
2121
2122impl Rem<Vec3A> for f32 {
2123 type Output = Vec3A;
2124 #[inline]
2125 fn rem(self, rhs: Vec3A) -> Vec3A {
2126 Vec3A::splat(self).rem(rhs)
2127 }
2128}
2129
2130impl Rem<&Vec3A> for f32 {
2131 type Output = Vec3A;
2132 #[inline]
2133 fn rem(self, rhs: &Vec3A) -> Vec3A {
2134 self.rem(*rhs)
2135 }
2136}
2137
2138impl Rem<&Vec3A> for &f32 {
2139 type Output = Vec3A;
2140 #[inline]
2141 fn rem(self, rhs: &Vec3A) -> Vec3A {
2142 (*self).rem(*rhs)
2143 }
2144}
2145
2146impl Rem<Vec3A> for &f32 {
2147 type Output = Vec3A;
2148 #[inline]
2149 fn rem(self, rhs: Vec3A) -> Vec3A {
2150 (*self).rem(rhs)
2151 }
2152}
2153
2154impl AsRef<[f32; 3]> for Vec3A {
2155 #[inline]
2156 fn as_ref(&self) -> &[f32; 3] {
2157 unsafe { &*(self as *const Self as *const [f32; 3]) }
2158 }
2159}
2160
2161impl AsMut<[f32; 3]> for Vec3A {
2162 #[inline]
2163 fn as_mut(&mut self) -> &mut [f32; 3] {
2164 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2165 }
2166}
2167
2168impl Sum for Vec3A {
2169 #[inline]
2170 fn sum<I>(iter: I) -> Self
2171 where
2172 I: Iterator<Item = Self>,
2173 {
2174 iter.fold(Self::ZERO, Self::add)
2175 }
2176}
2177
2178impl<'a> Sum<&'a Self> for Vec3A {
2179 #[inline]
2180 fn sum<I>(iter: I) -> Self
2181 where
2182 I: Iterator<Item = &'a Self>,
2183 {
2184 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2185 }
2186}
2187
2188impl Product for Vec3A {
2189 #[inline]
2190 fn product<I>(iter: I) -> Self
2191 where
2192 I: Iterator<Item = Self>,
2193 {
2194 iter.fold(Self::ONE, Self::mul)
2195 }
2196}
2197
2198impl<'a> Product<&'a Self> for Vec3A {
2199 #[inline]
2200 fn product<I>(iter: I) -> Self
2201 where
2202 I: Iterator<Item = &'a Self>,
2203 {
2204 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2205 }
2206}
2207
2208impl Neg for Vec3A {
2209 type Output = Self;
2210 #[inline]
2211 fn neg(self) -> Self {
2212 Self(unsafe { vnegq_f32(self.0) })
2213 }
2214}
2215
2216impl Neg for &Vec3A {
2217 type Output = Vec3A;
2218 #[inline]
2219 fn neg(self) -> Vec3A {
2220 (*self).neg()
2221 }
2222}
2223
2224impl Index<usize> for Vec3A {
2225 type Output = f32;
2226 #[inline]
2227 #[track_caller]
2228 fn index(&self, index: usize) -> &Self::Output {
2229 match index {
2230 0 => &self.x,
2231 1 => &self.y,
2232 2 => &self.z,
2233 _ => panic!("index out of bounds"),
2234 }
2235 }
2236}
2237
2238impl IndexMut<usize> for Vec3A {
2239 #[inline]
2240 #[track_caller]
2241 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2242 match index {
2243 0 => &mut self.x,
2244 1 => &mut self.y,
2245 2 => &mut self.z,
2246 _ => panic!("index out of bounds"),
2247 }
2248 }
2249}
2250
2251impl fmt::Display for Vec3A {
2252 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2253 if let Some(p) = f.precision() {
2254 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2255 } else {
2256 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2257 }
2258 }
2259}
2260
2261impl fmt::Debug for Vec3A {
2262 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2263 fmt.debug_tuple(stringify!(Vec3A))
2264 .field(&self.x)
2265 .field(&self.y)
2266 .field(&self.z)
2267 .finish()
2268 }
2269}
2270
2271impl From<Vec3A> for float32x4_t {
2272 #[inline(always)]
2273 fn from(t: Vec3A) -> Self {
2274 t.0
2275 }
2276}
2277
2278impl From<float32x4_t> for Vec3A {
2279 #[inline(always)]
2280 fn from(t: float32x4_t) -> Self {
2281 Self(t)
2282 }
2283}
2284
2285impl From<[f32; 3]> for Vec3A {
2286 #[inline]
2287 fn from(a: [f32; 3]) -> Self {
2288 Self::new(a[0], a[1], a[2])
2289 }
2290}
2291
2292impl From<Vec3A> for [f32; 3] {
2293 #[inline]
2294 fn from(v: Vec3A) -> Self {
2295 use crate::align16::Align16;
2296 use core::mem::MaybeUninit;
2297 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2298 unsafe {
2299 vst1q_f32(out.as_mut_ptr().cast(), v.0);
2300 out.assume_init().0
2301 }
2302 }
2303}
2304
2305impl From<(f32, f32, f32)> for Vec3A {
2306 #[inline]
2307 fn from(t: (f32, f32, f32)) -> Self {
2308 Self::new(t.0, t.1, t.2)
2309 }
2310}
2311
2312impl From<Vec3A> for (f32, f32, f32) {
2313 #[inline]
2314 fn from(v: Vec3A) -> Self {
2315 (v.x, v.y, v.z)
2316 }
2317}
2318
2319impl From<Vec3> for Vec3A {
2320 #[inline]
2321 fn from(v: Vec3) -> Self {
2322 Self::new(v.x, v.y, v.z)
2323 }
2324}
2325
2326impl From<Vec3A> for Vec3 {
2327 #[inline]
2328 fn from(v: Vec3A) -> Self {
2329 use crate::align16::Align16;
2330 use core::mem::MaybeUninit;
2331 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2332 unsafe {
2333 vst1q_f32(out.as_mut_ptr().cast(), v.0);
2334 out.assume_init().0
2335 }
2336 }
2337}
2338
2339impl From<(Vec2, f32)> for Vec3A {
2340 #[inline]
2341 fn from((v, z): (Vec2, f32)) -> Self {
2342 Self::new(v.x, v.y, z)
2343 }
2344}
2345
2346impl Deref for Vec3A {
2347 type Target = crate::deref::Vec3<f32>;
2348 #[inline]
2349 fn deref(&self) -> &Self::Target {
2350 unsafe { &*(self as *const Self).cast() }
2351 }
2352}
2353
2354impl DerefMut for Vec3A {
2355 #[inline]
2356 fn deref_mut(&mut self) -> &mut Self::Target {
2357 unsafe { &mut *(self as *mut Self).cast() }
2358 }
2359}
2360
2361impl From<BVec3> for Vec3A {
2362 #[inline]
2363 fn from(v: BVec3) -> Self {
2364 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2365 }
2366}
2367
2368impl From<BVec3A> for Vec3A {
2369 #[inline]
2370 fn from(v: BVec3A) -> Self {
2371 let bool_array: [bool; 3] = v.into();
2372 Self::new(
2373 f32::from(bool_array[0]),
2374 f32::from(bool_array[1]),
2375 f32::from(bool_array[2]),
2376 )
2377 }
2378}