1use crate::{BVec4, BVec4A, Vec2, Vec3, Vec3A, 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: Vec4,
18}
19
20#[inline(always)]
22#[must_use]
23pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
24 Vec4::new(x, y, z, w)
25}
26
27#[derive(Clone, Copy)]
33#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
34#[cfg_attr(
35 feature = "zerocopy-08",
36 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
37)]
38#[repr(transparent)]
39pub struct Vec4(pub(crate) float32x4_t);
40
41impl Vec4 {
42 pub const ZERO: Self = Self::splat(0.0);
44
45 pub const ONE: Self = Self::splat(1.0);
47
48 pub const NEG_ONE: Self = Self::splat(-1.0);
50
51 pub const MIN: Self = Self::splat(f32::MIN);
53
54 pub const MAX: Self = Self::splat(f32::MAX);
56
57 pub const NAN: Self = Self::splat(f32::NAN);
59
60 pub const INFINITY: Self = Self::splat(f32::INFINITY);
62
63 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
65
66 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
68
69 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
71
72 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
74
75 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
77
78 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
80
81 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
83
84 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
86
87 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
89
90 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
92
93 pub const USES_CORE_SIMD: bool = false;
95 pub const USES_NEON: bool = true;
97 pub const USES_SCALAR_MATH: bool = false;
99 pub const USES_SSE2: bool = false;
101 pub const USES_WASM_SIMD: bool = false;
103
104 #[inline(always)]
106 #[must_use]
107 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
108 unsafe { UnionCast { a: [x, y, z, w] }.v }
109 }
110
111 #[inline]
113 #[must_use]
114 pub const fn splat(v: f32) -> Self {
115 unsafe { UnionCast { a: [v; 4] }.v }
116 }
117
118 #[inline]
120 #[must_use]
121 pub fn map<F>(self, mut f: F) -> Self
122 where
123 F: FnMut(f32) -> f32,
124 {
125 Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
126 }
127
128 #[inline]
134 #[must_use]
135 pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
136 Self(unsafe { vbslq_f32(mask.0, if_true.0, if_false.0) })
137 }
138
139 #[inline]
141 #[must_use]
142 pub const fn from_array(a: [f32; 4]) -> Self {
143 Self::new(a[0], a[1], a[2], a[3])
144 }
145
146 #[inline]
148 #[must_use]
149 pub const fn to_array(&self) -> [f32; 4] {
150 unsafe { *(self as *const Self as *const [f32; 4]) }
151 }
152
153 #[inline]
159 #[must_use]
160 #[track_caller]
161 pub const fn from_slice(slice: &[f32]) -> Self {
162 assert!(slice.len() >= 4);
163 Self::new(slice[0], slice[1], slice[2], slice[3])
164 }
165
166 #[inline]
172 #[track_caller]
173 pub fn write_to_slice(self, slice: &mut [f32]) {
174 assert!(slice.len() >= 4);
175 unsafe {
176 vst1q_f32(slice.as_mut_ptr(), self.0);
177 }
178 }
179
180 #[inline]
186 #[must_use]
187 pub fn truncate(self) -> Vec3 {
188 use crate::swizzles::Vec4Swizzles;
189 self.xyz()
190 }
191
192 #[inline]
200 #[must_use]
201 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
202 pub fn project(self) -> Vec3 {
203 Vec3::from_homogeneous(self)
204 }
205
206 #[inline]
208 #[must_use]
209 pub fn with_x(mut self, x: f32) -> Self {
210 self.x = x;
211 self
212 }
213
214 #[inline]
216 #[must_use]
217 pub fn with_y(mut self, y: f32) -> Self {
218 self.y = y;
219 self
220 }
221
222 #[inline]
224 #[must_use]
225 pub fn with_z(mut self, z: f32) -> Self {
226 self.z = z;
227 self
228 }
229
230 #[inline]
232 #[must_use]
233 pub fn with_w(mut self, w: f32) -> Self {
234 self.w = w;
235 self
236 }
237
238 #[inline]
240 #[must_use]
241 pub fn dot(self, rhs: Self) -> f32 {
242 unsafe { dot4(self.0, rhs.0) }
243 }
244
245 #[inline]
247 #[must_use]
248 pub fn dot_into_vec(self, rhs: Self) -> Self {
249 Self(unsafe { dot4_into_f32x4(self.0, rhs.0) })
250 }
251
252 #[inline]
259 #[must_use]
260 pub fn min(self, rhs: Self) -> Self {
261 Self(unsafe { vminq_f32(self.0, rhs.0) })
262 }
263
264 #[inline]
271 #[must_use]
272 pub fn max(self, rhs: Self) -> Self {
273 Self(unsafe { vmaxq_f32(self.0, rhs.0) })
274 }
275
276 #[inline]
287 #[must_use]
288 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
289 pub fn clamp(self, min: Self, max: Self) -> Self {
290 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
291 self.max(min).min(max)
292 }
293
294 #[inline]
301 #[must_use]
302 pub fn min_element(self) -> f32 {
303 unsafe { vminnmvq_f32(self.0) }
304 }
305
306 #[inline]
313 #[must_use]
314 pub fn max_element(self) -> f32 {
315 unsafe { vmaxnmvq_f32(self.0) }
316 }
317
318 #[doc(alias = "argmin")]
320 #[inline]
321 #[must_use]
322 pub fn min_position(self) -> usize {
323 let mut min = self.x;
324 let mut index = 0;
325 if self.y < min {
326 min = self.y;
327 index = 1;
328 }
329 if self.z < min {
330 min = self.z;
331 index = 2;
332 }
333 if self.w < min {
334 index = 3;
335 }
336 index
337 }
338
339 #[doc(alias = "argmax")]
341 #[inline]
342 #[must_use]
343 pub fn max_position(self) -> usize {
344 let mut max = self.x;
345 let mut index = 0;
346 if self.y > max {
347 max = self.y;
348 index = 1;
349 }
350 if self.z > max {
351 max = self.z;
352 index = 2;
353 }
354 if self.w > max {
355 index = 3;
356 }
357 index
358 }
359
360 #[inline]
364 #[must_use]
365 pub fn element_sum(self) -> f32 {
366 unsafe { vaddvq_f32(self.0) }
367 }
368
369 #[inline]
373 #[must_use]
374 pub fn element_product(self) -> f32 {
375 unsafe {
376 let s = vmuls_laneq_f32(vgetq_lane_f32(self.0, 0), self.0, 1);
377 let s = vmuls_laneq_f32(s, self.0, 2);
378 vmuls_laneq_f32(s, self.0, 3)
379 }
380 }
381
382 #[inline]
388 #[must_use]
389 pub fn cmpeq(self, rhs: Self) -> BVec4A {
390 BVec4A(unsafe { vceqq_f32(self.0, rhs.0) })
391 }
392
393 #[inline]
399 #[must_use]
400 pub fn cmpne(self, rhs: Self) -> BVec4A {
401 BVec4A(unsafe { vmvnq_u32(vceqq_f32(self.0, rhs.0)) })
402 }
403
404 #[inline]
410 #[must_use]
411 pub fn cmpge(self, rhs: Self) -> BVec4A {
412 BVec4A(unsafe { vcgeq_f32(self.0, rhs.0) })
413 }
414
415 #[inline]
421 #[must_use]
422 pub fn cmpgt(self, rhs: Self) -> BVec4A {
423 BVec4A(unsafe { vcgtq_f32(self.0, rhs.0) })
424 }
425
426 #[inline]
432 #[must_use]
433 pub fn cmple(self, rhs: Self) -> BVec4A {
434 BVec4A(unsafe { vcleq_f32(self.0, rhs.0) })
435 }
436
437 #[inline]
443 #[must_use]
444 pub fn cmplt(self, rhs: Self) -> BVec4A {
445 BVec4A(unsafe { vcltq_f32(self.0, rhs.0) })
446 }
447
448 #[inline]
450 #[must_use]
451 pub fn abs(self) -> Self {
452 Self(unsafe { vabsq_f32(self.0) })
453 }
454
455 #[inline]
461 #[must_use]
462 pub fn signum(self) -> Self {
463 let result = Self(unsafe {
464 vreinterpretq_f32_u32(vorrq_u32(
465 vandq_u32(
466 vreinterpretq_u32_f32(self.0),
467 vreinterpretq_u32_f32(Self::NEG_ONE.0),
468 ),
469 vreinterpretq_u32_f32(Self::ONE.0),
470 ))
471 });
472 let mask = self.is_nan_mask();
473 Self::select(mask, self, result)
474 }
475
476 #[inline]
478 #[must_use]
479 pub fn copysign(self, rhs: Self) -> Self {
480 let mask = Self::splat(-0.0);
481 Self(unsafe {
482 vreinterpretq_f32_u32(vorrq_u32(
483 vandq_u32(vreinterpretq_u32_f32(rhs.0), vreinterpretq_u32_f32(mask.0)),
484 vandq_u32(
485 vreinterpretq_u32_f32(self.0),
486 vmvnq_u32(vreinterpretq_u32_f32(mask.0)),
487 ),
488 ))
489 })
490 }
491
492 #[inline]
500 #[must_use]
501 pub fn is_negative_bitmask(self) -> u32 {
502 unsafe {
503 let nmask = vreinterpretq_u32_f32(vdupq_n_f32(-0.0));
504 let m = vandq_u32(vreinterpretq_u32_f32(self.0), nmask);
505 let x = vgetq_lane_u32(m, 0) >> 31;
506 let y = vgetq_lane_u32(m, 1) >> 31;
507 let z = vgetq_lane_u32(m, 2) >> 31;
508
509 let w = vgetq_lane_u32(m, 3) >> 31;
510 x | y << 1 | z << 2 | w << 3
511 }
512 }
513
514 #[inline]
519 #[must_use]
520 pub fn is_negative_mask(self) -> BVec4A {
521 BVec4A(unsafe { vcltq_s32(vreinterpretq_s32_f32(self.0), vdupq_n_s32(0)) })
522 }
523
524 #[inline]
527 #[must_use]
528 pub fn is_finite(self) -> bool {
529 self.is_finite_mask().all()
530 }
531
532 #[inline]
536 #[must_use]
537 pub fn is_finite_mask(self) -> BVec4A {
538 BVec4A(unsafe { vcltq_f32(vabsq_f32(self.0), Self::INFINITY.0) })
539 }
540
541 #[inline]
543 #[must_use]
544 pub fn is_nan(self) -> bool {
545 self.is_nan_mask().any()
546 }
547
548 #[inline]
552 #[must_use]
553 pub fn is_nan_mask(self) -> BVec4A {
554 BVec4A(unsafe { vmvnq_u32(vceqq_f32(self.0, self.0)) })
555 }
556
557 #[doc(alias = "magnitude")]
559 #[inline]
560 #[must_use]
561 pub fn length(self) -> f32 {
562 math::sqrt(self.dot(self))
563 }
564
565 #[allow(dead_code)]
567 fn is_non_zero(self) -> bool {
568 self.length_squared() > 0.0
569 }
570
571 #[doc(alias = "magnitude2")]
575 #[inline]
576 #[must_use]
577 pub fn length_squared(self) -> f32 {
578 self.dot(self)
579 }
580
581 #[inline]
585 #[must_use]
586 pub fn length_recip(self) -> f32 {
587 1.0 / self.length()
588 }
589
590 #[inline]
592 #[must_use]
593 pub fn distance(self, rhs: Self) -> f32 {
594 (self - rhs).length()
595 }
596
597 #[inline]
599 #[must_use]
600 pub fn distance_squared(self, rhs: Self) -> f32 {
601 (self - rhs).length_squared()
602 }
603
604 #[inline]
606 #[must_use]
607 pub fn div_euclid(self, rhs: Self) -> Self {
608 Self::new(
609 math::div_euclid(self.x, rhs.x),
610 math::div_euclid(self.y, rhs.y),
611 math::div_euclid(self.z, rhs.z),
612 math::div_euclid(self.w, rhs.w),
613 )
614 }
615
616 #[inline]
620 #[must_use]
621 pub fn rem_euclid(self, rhs: Self) -> Self {
622 Self::new(
623 math::rem_euclid(self.x, rhs.x),
624 math::rem_euclid(self.y, rhs.y),
625 math::rem_euclid(self.z, rhs.z),
626 math::rem_euclid(self.w, rhs.w),
627 )
628 }
629
630 #[inline]
640 #[must_use]
641 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
642 pub fn normalize(self) -> Self {
643 #[allow(clippy::let_and_return)]
644 let normalized = self.mul(self.length_recip());
645 glam_assert!(normalized.is_finite());
646 normalized
647 }
648
649 #[inline]
656 #[must_use]
657 pub fn try_normalize(self) -> Option<Self> {
658 let rcp = self.length_recip();
659 if rcp.is_finite() && rcp > 0.0 {
660 Some(self * rcp)
661 } else {
662 None
663 }
664 }
665
666 #[inline]
674 #[must_use]
675 pub fn normalize_or(self, fallback: Self) -> Self {
676 let rcp = self.length_recip();
677 if rcp.is_finite() && rcp > 0.0 {
678 self * rcp
679 } else {
680 fallback
681 }
682 }
683
684 #[inline]
691 #[must_use]
692 pub fn normalize_or_zero(self) -> Self {
693 self.normalize_or(Self::ZERO)
694 }
695
696 #[inline]
700 #[must_use]
701 pub fn normalize_and_length(self) -> (Self, f32) {
702 let length = self.length();
703 let rcp = 1.0 / length;
704 if rcp.is_finite() && rcp > 0.0 {
705 (self * rcp, length)
706 } else {
707 (Self::X, 0.0)
708 }
709 }
710
711 #[inline]
715 #[must_use]
716 pub fn is_normalized(self) -> bool {
717 (self.length_squared() - 1.0).abs() <= 2e-4
718 }
719
720 #[inline]
728 #[must_use]
729 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
730 pub fn project_onto(self, rhs: Self) -> Self {
731 let rhs_len_sq = rhs.dot(rhs);
732 glam_assert!(rhs_len_sq != 0.0);
733 rhs * (self.dot(rhs) / rhs_len_sq)
734 }
735
736 #[doc(alias("plane"))]
747 #[inline]
748 #[must_use]
749 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
750 pub fn reject_from(self, rhs: Self) -> Self {
751 self - self.project_onto(rhs)
752 }
753
754 #[inline]
762 #[must_use]
763 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
764 pub fn project_onto_normalized(self, rhs: Self) -> Self {
765 glam_assert!(rhs.is_normalized());
766 rhs * self.dot(rhs)
767 }
768
769 #[doc(alias("plane"))]
780 #[inline]
781 #[must_use]
782 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
783 pub fn reject_from_normalized(self, rhs: Self) -> Self {
784 self - self.project_onto_normalized(rhs)
785 }
786
787 #[inline]
790 #[must_use]
791 pub fn round(self) -> Self {
792 Self(unsafe { vrndaq_f32(self.0) })
793 }
794
795 #[inline]
798 #[must_use]
799 pub fn floor(self) -> Self {
800 Self(unsafe { vrndmq_f32(self.0) })
801 }
802
803 #[inline]
806 #[must_use]
807 pub fn ceil(self) -> Self {
808 Self(unsafe { vrndpq_f32(self.0) })
809 }
810
811 #[inline]
814 #[must_use]
815 pub fn trunc(self) -> Self {
816 Self(unsafe { vrndq_f32(self.0) })
817 }
818
819 #[inline]
823 #[must_use]
824 pub fn step(self, rhs: Self) -> Self {
825 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
826 }
827
828 #[inline]
839 #[must_use]
840 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
841 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
842 glam_assert!(edge0.cmplt(edge1).all());
843 let t = ((self - edge0) / (edge1 - edge0)).saturate();
844 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
845 }
846
847 #[inline]
849 #[must_use]
850 pub fn saturate(self) -> Self {
851 self.clamp(Self::ZERO, Self::ONE)
852 }
853
854 #[inline]
861 #[must_use]
862 pub fn fract(self) -> Self {
863 self - self.trunc()
864 }
865
866 #[inline]
873 #[must_use]
874 pub fn fract_gl(self) -> Self {
875 self - self.floor()
876 }
877
878 #[inline]
881 #[must_use]
882 pub fn exp(self) -> Self {
883 Self::new(
884 math::exp(self.x),
885 math::exp(self.y),
886 math::exp(self.z),
887 math::exp(self.w),
888 )
889 }
890
891 #[inline]
893 #[must_use]
894 pub fn exp2(self) -> Self {
895 Self::new(
896 math::exp2(self.x),
897 math::exp2(self.y),
898 math::exp2(self.z),
899 math::exp2(self.w),
900 )
901 }
902
903 #[inline]
906 #[must_use]
907 pub fn ln(self) -> Self {
908 Self::new(
909 math::ln(self.x),
910 math::ln(self.y),
911 math::ln(self.z),
912 math::ln(self.w),
913 )
914 }
915
916 #[inline]
919 #[must_use]
920 pub fn log2(self) -> Self {
921 Self::new(
922 math::log2(self.x),
923 math::log2(self.y),
924 math::log2(self.z),
925 math::log2(self.w),
926 )
927 }
928
929 #[inline]
931 #[must_use]
932 pub fn powf(self, n: f32) -> Self {
933 Self::new(
934 math::powf(self.x, n),
935 math::powf(self.y, n),
936 math::powf(self.z, n),
937 math::powf(self.w, n),
938 )
939 }
940
941 #[inline]
944 #[must_use]
945 pub fn sqrt(self) -> Self {
946 Self::new(
947 math::sqrt(self.x),
948 math::sqrt(self.y),
949 math::sqrt(self.z),
950 math::sqrt(self.w),
951 )
952 }
953
954 #[inline]
956 #[must_use]
957 pub fn cos(self) -> Self {
958 Self::new(
959 math::cos(self.x),
960 math::cos(self.y),
961 math::cos(self.z),
962 math::cos(self.w),
963 )
964 }
965
966 #[inline]
968 #[must_use]
969 pub fn sin(self) -> Self {
970 Self::new(
971 math::sin(self.x),
972 math::sin(self.y),
973 math::sin(self.z),
974 math::sin(self.w),
975 )
976 }
977
978 #[inline]
980 #[must_use]
981 pub fn sin_cos(self) -> (Self, Self) {
982 let (sin_x, cos_x) = math::sin_cos(self.x);
983 let (sin_y, cos_y) = math::sin_cos(self.y);
984 let (sin_z, cos_z) = math::sin_cos(self.z);
985 let (sin_w, cos_w) = math::sin_cos(self.w);
986
987 (
988 Self::new(sin_x, sin_y, sin_z, sin_w),
989 Self::new(cos_x, cos_y, cos_z, cos_w),
990 )
991 }
992
993 #[inline]
995 #[must_use]
996 pub fn recip(self) -> Self {
997 Self(unsafe { vdivq_f32(Self::ONE.0, self.0) })
998 }
999
1000 #[doc(alias = "mix")]
1012 #[inline]
1013 #[must_use]
1014 pub fn lerp(self, rhs: Self, s: f32) -> Self {
1015 self * (1.0 - s) + rhs * s
1016 }
1017
1018 #[doc(alias = "mix")]
1035 #[inline]
1036 #[must_use]
1037 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
1038 Self(unsafe { vfmaq_f32(self.0, vsubq_f32(rhs.0, self.0), vdupq_n_f32(s)) })
1039 }
1040
1041 #[inline]
1046 #[must_use]
1047 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
1048 let a = rhs - self;
1049 let len = a.length();
1050 if len <= d || len <= 1e-4 {
1051 return rhs;
1052 }
1053 self + a / len * d
1054 }
1055
1056 #[inline]
1062 pub fn midpoint(self, rhs: Self) -> Self {
1063 (self + rhs) * 0.5
1064 }
1065
1066 #[inline]
1076 #[must_use]
1077 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1078 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1079 }
1080
1081 #[inline]
1087 #[must_use]
1088 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1089 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1090 glam_assert!(0.0 <= min);
1091 glam_assert!(min <= max);
1092 let length_sq = self.length_squared();
1093 if length_sq < min * min {
1094 min * (self / math::sqrt(length_sq))
1095 } else if length_sq > max * max {
1096 max * (self / math::sqrt(length_sq))
1097 } else {
1098 self
1099 }
1100 }
1101
1102 #[inline]
1108 #[must_use]
1109 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1110 pub fn clamp_length_max(self, max: f32) -> Self {
1111 glam_assert!(0.0 <= max);
1112 let length_sq = self.length_squared();
1113 if length_sq > max * max {
1114 max * (self / math::sqrt(length_sq))
1115 } else {
1116 self
1117 }
1118 }
1119
1120 #[inline]
1126 #[must_use]
1127 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1128 pub fn clamp_length_min(self, min: f32) -> Self {
1129 glam_assert!(0.0 <= min);
1130 let length_sq = self.length_squared();
1131 if length_sq < min * min {
1132 min * (self / math::sqrt(length_sq))
1133 } else {
1134 self
1135 }
1136 }
1137
1138 #[inline]
1146 #[must_use]
1147 pub fn mul_add(self, a: Self, b: Self) -> Self {
1148 Self(unsafe { vfmaq_f32(b.0, self.0, a.0) })
1149 }
1150
1151 #[inline]
1160 #[must_use]
1161 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1162 pub fn reflect(self, normal: Self) -> Self {
1163 glam_assert!(normal.is_normalized());
1164 self - 2.0 * self.dot(normal) * normal
1165 }
1166
1167 #[inline]
1177 #[must_use]
1178 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1179 pub fn refract(self, normal: Self, eta: f32) -> Self {
1180 glam_assert!(self.is_normalized());
1181 glam_assert!(normal.is_normalized());
1182 let n_dot_i = normal.dot(self);
1183 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1184 if k >= 0.0 {
1185 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1186 } else {
1187 Self::ZERO
1188 }
1189 }
1190
1191 #[cfg(feature = "f64")]
1193 #[inline]
1194 #[must_use]
1195 pub fn as_dvec4(self) -> crate::DVec4 {
1196 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
1197 }
1198
1199 #[cfg(feature = "i8")]
1201 #[inline]
1202 #[must_use]
1203 pub fn as_i8vec4(self) -> crate::I8Vec4 {
1204 crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1205 }
1206
1207 #[cfg(feature = "u8")]
1209 #[inline]
1210 #[must_use]
1211 pub fn as_u8vec4(self) -> crate::U8Vec4 {
1212 crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1213 }
1214
1215 #[cfg(feature = "i16")]
1217 #[inline]
1218 #[must_use]
1219 pub fn as_i16vec4(self) -> crate::I16Vec4 {
1220 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1221 }
1222
1223 #[cfg(feature = "u16")]
1225 #[inline]
1226 #[must_use]
1227 pub fn as_u16vec4(self) -> crate::U16Vec4 {
1228 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1229 }
1230
1231 #[cfg(feature = "i32")]
1233 #[inline]
1234 #[must_use]
1235 pub fn as_ivec4(self) -> crate::IVec4 {
1236 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1237 }
1238
1239 #[cfg(feature = "u32")]
1241 #[inline]
1242 #[must_use]
1243 pub fn as_uvec4(self) -> crate::UVec4 {
1244 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1245 }
1246
1247 #[cfg(feature = "i64")]
1249 #[inline]
1250 #[must_use]
1251 pub fn as_i64vec4(self) -> crate::I64Vec4 {
1252 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1253 }
1254
1255 #[cfg(feature = "u64")]
1257 #[inline]
1258 #[must_use]
1259 pub fn as_u64vec4(self) -> crate::U64Vec4 {
1260 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1261 }
1262
1263 #[cfg(feature = "isize")]
1265 #[inline]
1266 #[must_use]
1267 pub fn as_isizevec4(self) -> crate::ISizeVec4 {
1268 crate::ISizeVec4::new(
1269 self.x as isize,
1270 self.y as isize,
1271 self.z as isize,
1272 self.w as isize,
1273 )
1274 }
1275
1276 #[cfg(feature = "usize")]
1278 #[inline]
1279 #[must_use]
1280 pub fn as_usizevec4(self) -> crate::USizeVec4 {
1281 crate::USizeVec4::new(
1282 self.x as usize,
1283 self.y as usize,
1284 self.z as usize,
1285 self.w as usize,
1286 )
1287 }
1288}
1289
1290impl Default for Vec4 {
1291 #[inline(always)]
1292 fn default() -> Self {
1293 Self::ZERO
1294 }
1295}
1296
1297impl PartialEq for Vec4 {
1298 #[inline]
1299 fn eq(&self, rhs: &Self) -> bool {
1300 self.cmpeq(*rhs).all()
1301 }
1302}
1303
1304impl Div for Vec4 {
1305 type Output = Self;
1306 #[inline]
1307 fn div(self, rhs: Self) -> Self {
1308 Self(unsafe { vdivq_f32(self.0, rhs.0) })
1309 }
1310}
1311
1312impl Div<&Self> for Vec4 {
1313 type Output = Self;
1314 #[inline]
1315 fn div(self, rhs: &Self) -> Self {
1316 self.div(*rhs)
1317 }
1318}
1319
1320impl Div<&Vec4> for &Vec4 {
1321 type Output = Vec4;
1322 #[inline]
1323 fn div(self, rhs: &Vec4) -> Vec4 {
1324 (*self).div(*rhs)
1325 }
1326}
1327
1328impl Div<Vec4> for &Vec4 {
1329 type Output = Vec4;
1330 #[inline]
1331 fn div(self, rhs: Vec4) -> Vec4 {
1332 (*self).div(rhs)
1333 }
1334}
1335
1336impl DivAssign for Vec4 {
1337 #[inline]
1338 fn div_assign(&mut self, rhs: Self) {
1339 self.0 = unsafe { vdivq_f32(self.0, rhs.0) };
1340 }
1341}
1342
1343impl DivAssign<&Self> for Vec4 {
1344 #[inline]
1345 fn div_assign(&mut self, rhs: &Self) {
1346 self.div_assign(*rhs);
1347 }
1348}
1349
1350impl Div<f32> for Vec4 {
1351 type Output = Self;
1352 #[inline]
1353 fn div(self, rhs: f32) -> Self {
1354 Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) })
1355 }
1356}
1357
1358impl Div<&f32> for Vec4 {
1359 type Output = Self;
1360 #[inline]
1361 fn div(self, rhs: &f32) -> Self {
1362 self.div(*rhs)
1363 }
1364}
1365
1366impl Div<&f32> for &Vec4 {
1367 type Output = Vec4;
1368 #[inline]
1369 fn div(self, rhs: &f32) -> Vec4 {
1370 (*self).div(*rhs)
1371 }
1372}
1373
1374impl Div<f32> for &Vec4 {
1375 type Output = Vec4;
1376 #[inline]
1377 fn div(self, rhs: f32) -> Vec4 {
1378 (*self).div(rhs)
1379 }
1380}
1381
1382impl DivAssign<f32> for Vec4 {
1383 #[inline]
1384 fn div_assign(&mut self, rhs: f32) {
1385 self.0 = unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) };
1386 }
1387}
1388
1389impl DivAssign<&f32> for Vec4 {
1390 #[inline]
1391 fn div_assign(&mut self, rhs: &f32) {
1392 self.div_assign(*rhs);
1393 }
1394}
1395
1396impl Div<Vec4> for f32 {
1397 type Output = Vec4;
1398 #[inline]
1399 fn div(self, rhs: Vec4) -> Vec4 {
1400 Vec4(unsafe { vdivq_f32(vld1q_dup_f32(&self), rhs.0) })
1401 }
1402}
1403
1404impl Div<&Vec4> for f32 {
1405 type Output = Vec4;
1406 #[inline]
1407 fn div(self, rhs: &Vec4) -> Vec4 {
1408 self.div(*rhs)
1409 }
1410}
1411
1412impl Div<&Vec4> for &f32 {
1413 type Output = Vec4;
1414 #[inline]
1415 fn div(self, rhs: &Vec4) -> Vec4 {
1416 (*self).div(*rhs)
1417 }
1418}
1419
1420impl Div<Vec4> for &f32 {
1421 type Output = Vec4;
1422 #[inline]
1423 fn div(self, rhs: Vec4) -> Vec4 {
1424 (*self).div(rhs)
1425 }
1426}
1427
1428impl Mul for Vec4 {
1429 type Output = Self;
1430 #[inline]
1431 fn mul(self, rhs: Self) -> Self {
1432 Self(unsafe { vmulq_f32(self.0, rhs.0) })
1433 }
1434}
1435
1436impl Mul<&Self> for Vec4 {
1437 type Output = Self;
1438 #[inline]
1439 fn mul(self, rhs: &Self) -> Self {
1440 self.mul(*rhs)
1441 }
1442}
1443
1444impl Mul<&Vec4> for &Vec4 {
1445 type Output = Vec4;
1446 #[inline]
1447 fn mul(self, rhs: &Vec4) -> Vec4 {
1448 (*self).mul(*rhs)
1449 }
1450}
1451
1452impl Mul<Vec4> for &Vec4 {
1453 type Output = Vec4;
1454 #[inline]
1455 fn mul(self, rhs: Vec4) -> Vec4 {
1456 (*self).mul(rhs)
1457 }
1458}
1459
1460impl MulAssign for Vec4 {
1461 #[inline]
1462 fn mul_assign(&mut self, rhs: Self) {
1463 self.0 = unsafe { vmulq_f32(self.0, rhs.0) };
1464 }
1465}
1466
1467impl MulAssign<&Self> for Vec4 {
1468 #[inline]
1469 fn mul_assign(&mut self, rhs: &Self) {
1470 self.mul_assign(*rhs);
1471 }
1472}
1473
1474impl Mul<f32> for Vec4 {
1475 type Output = Self;
1476 #[inline]
1477 fn mul(self, rhs: f32) -> Self {
1478 Self(unsafe { vmulq_n_f32(self.0, rhs) })
1479 }
1480}
1481
1482impl Mul<&f32> for Vec4 {
1483 type Output = Self;
1484 #[inline]
1485 fn mul(self, rhs: &f32) -> Self {
1486 self.mul(*rhs)
1487 }
1488}
1489
1490impl Mul<&f32> for &Vec4 {
1491 type Output = Vec4;
1492 #[inline]
1493 fn mul(self, rhs: &f32) -> Vec4 {
1494 (*self).mul(*rhs)
1495 }
1496}
1497
1498impl Mul<f32> for &Vec4 {
1499 type Output = Vec4;
1500 #[inline]
1501 fn mul(self, rhs: f32) -> Vec4 {
1502 (*self).mul(rhs)
1503 }
1504}
1505
1506impl MulAssign<f32> for Vec4 {
1507 #[inline]
1508 fn mul_assign(&mut self, rhs: f32) {
1509 self.0 = unsafe { vmulq_n_f32(self.0, rhs) };
1510 }
1511}
1512
1513impl MulAssign<&f32> for Vec4 {
1514 #[inline]
1515 fn mul_assign(&mut self, rhs: &f32) {
1516 self.mul_assign(*rhs);
1517 }
1518}
1519
1520impl Mul<Vec4> for f32 {
1521 type Output = Vec4;
1522 #[inline]
1523 fn mul(self, rhs: Vec4) -> Vec4 {
1524 Vec4(unsafe { vmulq_n_f32(rhs.0, self) })
1525 }
1526}
1527
1528impl Mul<&Vec4> for f32 {
1529 type Output = Vec4;
1530 #[inline]
1531 fn mul(self, rhs: &Vec4) -> Vec4 {
1532 self.mul(*rhs)
1533 }
1534}
1535
1536impl Mul<&Vec4> for &f32 {
1537 type Output = Vec4;
1538 #[inline]
1539 fn mul(self, rhs: &Vec4) -> Vec4 {
1540 (*self).mul(*rhs)
1541 }
1542}
1543
1544impl Mul<Vec4> for &f32 {
1545 type Output = Vec4;
1546 #[inline]
1547 fn mul(self, rhs: Vec4) -> Vec4 {
1548 (*self).mul(rhs)
1549 }
1550}
1551
1552impl Add for Vec4 {
1553 type Output = Self;
1554 #[inline]
1555 fn add(self, rhs: Self) -> Self {
1556 Self(unsafe { vaddq_f32(self.0, rhs.0) })
1557 }
1558}
1559
1560impl Add<&Self> for Vec4 {
1561 type Output = Self;
1562 #[inline]
1563 fn add(self, rhs: &Self) -> Self {
1564 self.add(*rhs)
1565 }
1566}
1567
1568impl Add<&Vec4> for &Vec4 {
1569 type Output = Vec4;
1570 #[inline]
1571 fn add(self, rhs: &Vec4) -> Vec4 {
1572 (*self).add(*rhs)
1573 }
1574}
1575
1576impl Add<Vec4> for &Vec4 {
1577 type Output = Vec4;
1578 #[inline]
1579 fn add(self, rhs: Vec4) -> Vec4 {
1580 (*self).add(rhs)
1581 }
1582}
1583
1584impl AddAssign for Vec4 {
1585 #[inline]
1586 fn add_assign(&mut self, rhs: Self) {
1587 self.0 = unsafe { vaddq_f32(self.0, rhs.0) };
1588 }
1589}
1590
1591impl AddAssign<&Self> for Vec4 {
1592 #[inline]
1593 fn add_assign(&mut self, rhs: &Self) {
1594 self.add_assign(*rhs);
1595 }
1596}
1597
1598impl Add<f32> for Vec4 {
1599 type Output = Self;
1600 #[inline]
1601 fn add(self, rhs: f32) -> Self {
1602 Self(unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) })
1603 }
1604}
1605
1606impl Add<&f32> for Vec4 {
1607 type Output = Self;
1608 #[inline]
1609 fn add(self, rhs: &f32) -> Self {
1610 self.add(*rhs)
1611 }
1612}
1613
1614impl Add<&f32> for &Vec4 {
1615 type Output = Vec4;
1616 #[inline]
1617 fn add(self, rhs: &f32) -> Vec4 {
1618 (*self).add(*rhs)
1619 }
1620}
1621
1622impl Add<f32> for &Vec4 {
1623 type Output = Vec4;
1624 #[inline]
1625 fn add(self, rhs: f32) -> Vec4 {
1626 (*self).add(rhs)
1627 }
1628}
1629
1630impl AddAssign<f32> for Vec4 {
1631 #[inline]
1632 fn add_assign(&mut self, rhs: f32) {
1633 self.0 = unsafe { vaddq_f32(self.0, vld1q_dup_f32(&rhs)) };
1634 }
1635}
1636
1637impl AddAssign<&f32> for Vec4 {
1638 #[inline]
1639 fn add_assign(&mut self, rhs: &f32) {
1640 self.add_assign(*rhs);
1641 }
1642}
1643
1644impl Add<Vec4> for f32 {
1645 type Output = Vec4;
1646 #[inline]
1647 fn add(self, rhs: Vec4) -> Vec4 {
1648 Vec4(unsafe { vaddq_f32(vld1q_dup_f32(&self), rhs.0) })
1649 }
1650}
1651
1652impl Add<&Vec4> for f32 {
1653 type Output = Vec4;
1654 #[inline]
1655 fn add(self, rhs: &Vec4) -> Vec4 {
1656 self.add(*rhs)
1657 }
1658}
1659
1660impl Add<&Vec4> for &f32 {
1661 type Output = Vec4;
1662 #[inline]
1663 fn add(self, rhs: &Vec4) -> Vec4 {
1664 (*self).add(*rhs)
1665 }
1666}
1667
1668impl Add<Vec4> for &f32 {
1669 type Output = Vec4;
1670 #[inline]
1671 fn add(self, rhs: Vec4) -> Vec4 {
1672 (*self).add(rhs)
1673 }
1674}
1675
1676impl Sub for Vec4 {
1677 type Output = Self;
1678 #[inline]
1679 fn sub(self, rhs: Self) -> Self {
1680 Self(unsafe { vsubq_f32(self.0, rhs.0) })
1681 }
1682}
1683
1684impl Sub<&Self> for Vec4 {
1685 type Output = Self;
1686 #[inline]
1687 fn sub(self, rhs: &Self) -> Self {
1688 self.sub(*rhs)
1689 }
1690}
1691
1692impl Sub<&Vec4> for &Vec4 {
1693 type Output = Vec4;
1694 #[inline]
1695 fn sub(self, rhs: &Vec4) -> Vec4 {
1696 (*self).sub(*rhs)
1697 }
1698}
1699
1700impl Sub<Vec4> for &Vec4 {
1701 type Output = Vec4;
1702 #[inline]
1703 fn sub(self, rhs: Vec4) -> Vec4 {
1704 (*self).sub(rhs)
1705 }
1706}
1707
1708impl SubAssign for Vec4 {
1709 #[inline]
1710 fn sub_assign(&mut self, rhs: Self) {
1711 self.0 = unsafe { vsubq_f32(self.0, rhs.0) };
1712 }
1713}
1714
1715impl SubAssign<&Self> for Vec4 {
1716 #[inline]
1717 fn sub_assign(&mut self, rhs: &Self) {
1718 self.sub_assign(*rhs);
1719 }
1720}
1721
1722impl Sub<f32> for Vec4 {
1723 type Output = Self;
1724 #[inline]
1725 fn sub(self, rhs: f32) -> Self {
1726 Self(unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) })
1727 }
1728}
1729
1730impl Sub<&f32> for Vec4 {
1731 type Output = Self;
1732 #[inline]
1733 fn sub(self, rhs: &f32) -> Self {
1734 self.sub(*rhs)
1735 }
1736}
1737
1738impl Sub<&f32> for &Vec4 {
1739 type Output = Vec4;
1740 #[inline]
1741 fn sub(self, rhs: &f32) -> Vec4 {
1742 (*self).sub(*rhs)
1743 }
1744}
1745
1746impl Sub<f32> for &Vec4 {
1747 type Output = Vec4;
1748 #[inline]
1749 fn sub(self, rhs: f32) -> Vec4 {
1750 (*self).sub(rhs)
1751 }
1752}
1753
1754impl SubAssign<f32> for Vec4 {
1755 #[inline]
1756 fn sub_assign(&mut self, rhs: f32) {
1757 self.0 = unsafe { vsubq_f32(self.0, vld1q_dup_f32(&rhs)) };
1758 }
1759}
1760
1761impl SubAssign<&f32> for Vec4 {
1762 #[inline]
1763 fn sub_assign(&mut self, rhs: &f32) {
1764 self.sub_assign(*rhs);
1765 }
1766}
1767
1768impl Sub<Vec4> for f32 {
1769 type Output = Vec4;
1770 #[inline]
1771 fn sub(self, rhs: Vec4) -> Vec4 {
1772 Vec4(unsafe { vsubq_f32(vld1q_dup_f32(&self), rhs.0) })
1773 }
1774}
1775
1776impl Sub<&Vec4> for f32 {
1777 type Output = Vec4;
1778 #[inline]
1779 fn sub(self, rhs: &Vec4) -> Vec4 {
1780 self.sub(*rhs)
1781 }
1782}
1783
1784impl Sub<&Vec4> for &f32 {
1785 type Output = Vec4;
1786 #[inline]
1787 fn sub(self, rhs: &Vec4) -> Vec4 {
1788 (*self).sub(*rhs)
1789 }
1790}
1791
1792impl Sub<Vec4> for &f32 {
1793 type Output = Vec4;
1794 #[inline]
1795 fn sub(self, rhs: Vec4) -> Vec4 {
1796 (*self).sub(rhs)
1797 }
1798}
1799
1800impl Rem for Vec4 {
1801 type Output = Self;
1802 #[inline]
1803 fn rem(self, rhs: Self) -> Self {
1804 unsafe {
1805 let n = vrndmq_f32(vdivq_f32(self.0, rhs.0));
1806 Self(vsubq_f32(self.0, vmulq_f32(n, rhs.0)))
1807 }
1808 }
1809}
1810
1811impl Rem<&Self> for Vec4 {
1812 type Output = Self;
1813 #[inline]
1814 fn rem(self, rhs: &Self) -> Self {
1815 self.rem(*rhs)
1816 }
1817}
1818
1819impl Rem<&Vec4> for &Vec4 {
1820 type Output = Vec4;
1821 #[inline]
1822 fn rem(self, rhs: &Vec4) -> Vec4 {
1823 (*self).rem(*rhs)
1824 }
1825}
1826
1827impl Rem<Vec4> for &Vec4 {
1828 type Output = Vec4;
1829 #[inline]
1830 fn rem(self, rhs: Vec4) -> Vec4 {
1831 (*self).rem(rhs)
1832 }
1833}
1834
1835impl RemAssign for Vec4 {
1836 #[inline]
1837 fn rem_assign(&mut self, rhs: Self) {
1838 *self = self.rem(rhs);
1839 }
1840}
1841
1842impl RemAssign<&Self> for Vec4 {
1843 #[inline]
1844 fn rem_assign(&mut self, rhs: &Self) {
1845 self.rem_assign(*rhs);
1846 }
1847}
1848
1849impl Rem<f32> for Vec4 {
1850 type Output = Self;
1851 #[inline]
1852 fn rem(self, rhs: f32) -> Self {
1853 self.rem(Self::splat(rhs))
1854 }
1855}
1856
1857impl Rem<&f32> for Vec4 {
1858 type Output = Self;
1859 #[inline]
1860 fn rem(self, rhs: &f32) -> Self {
1861 self.rem(*rhs)
1862 }
1863}
1864
1865impl Rem<&f32> for &Vec4 {
1866 type Output = Vec4;
1867 #[inline]
1868 fn rem(self, rhs: &f32) -> Vec4 {
1869 (*self).rem(*rhs)
1870 }
1871}
1872
1873impl Rem<f32> for &Vec4 {
1874 type Output = Vec4;
1875 #[inline]
1876 fn rem(self, rhs: f32) -> Vec4 {
1877 (*self).rem(rhs)
1878 }
1879}
1880
1881impl RemAssign<f32> for Vec4 {
1882 #[inline]
1883 fn rem_assign(&mut self, rhs: f32) {
1884 *self = self.rem(Self::splat(rhs));
1885 }
1886}
1887
1888impl RemAssign<&f32> for Vec4 {
1889 #[inline]
1890 fn rem_assign(&mut self, rhs: &f32) {
1891 self.rem_assign(*rhs);
1892 }
1893}
1894
1895impl Rem<Vec4> for f32 {
1896 type Output = Vec4;
1897 #[inline]
1898 fn rem(self, rhs: Vec4) -> Vec4 {
1899 Vec4::splat(self).rem(rhs)
1900 }
1901}
1902
1903impl Rem<&Vec4> for f32 {
1904 type Output = Vec4;
1905 #[inline]
1906 fn rem(self, rhs: &Vec4) -> Vec4 {
1907 self.rem(*rhs)
1908 }
1909}
1910
1911impl Rem<&Vec4> for &f32 {
1912 type Output = Vec4;
1913 #[inline]
1914 fn rem(self, rhs: &Vec4) -> Vec4 {
1915 (*self).rem(*rhs)
1916 }
1917}
1918
1919impl Rem<Vec4> for &f32 {
1920 type Output = Vec4;
1921 #[inline]
1922 fn rem(self, rhs: Vec4) -> Vec4 {
1923 (*self).rem(rhs)
1924 }
1925}
1926
1927impl AsRef<[f32; 4]> for Vec4 {
1928 #[inline]
1929 fn as_ref(&self) -> &[f32; 4] {
1930 unsafe { &*(self as *const Self as *const [f32; 4]) }
1931 }
1932}
1933
1934impl AsMut<[f32; 4]> for Vec4 {
1935 #[inline]
1936 fn as_mut(&mut self) -> &mut [f32; 4] {
1937 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
1938 }
1939}
1940
1941impl Sum for Vec4 {
1942 #[inline]
1943 fn sum<I>(iter: I) -> Self
1944 where
1945 I: Iterator<Item = Self>,
1946 {
1947 iter.fold(Self::ZERO, Self::add)
1948 }
1949}
1950
1951impl<'a> Sum<&'a Self> for Vec4 {
1952 #[inline]
1953 fn sum<I>(iter: I) -> Self
1954 where
1955 I: Iterator<Item = &'a Self>,
1956 {
1957 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1958 }
1959}
1960
1961impl Product for Vec4 {
1962 #[inline]
1963 fn product<I>(iter: I) -> Self
1964 where
1965 I: Iterator<Item = Self>,
1966 {
1967 iter.fold(Self::ONE, Self::mul)
1968 }
1969}
1970
1971impl<'a> Product<&'a Self> for Vec4 {
1972 #[inline]
1973 fn product<I>(iter: I) -> Self
1974 where
1975 I: Iterator<Item = &'a Self>,
1976 {
1977 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1978 }
1979}
1980
1981impl Neg for Vec4 {
1982 type Output = Self;
1983 #[inline]
1984 fn neg(self) -> Self {
1985 Self(unsafe { vnegq_f32(self.0) })
1986 }
1987}
1988
1989impl Neg for &Vec4 {
1990 type Output = Vec4;
1991 #[inline]
1992 fn neg(self) -> Vec4 {
1993 (*self).neg()
1994 }
1995}
1996
1997impl Index<usize> for Vec4 {
1998 type Output = f32;
1999 #[inline]
2000 #[track_caller]
2001 fn index(&self, index: usize) -> &Self::Output {
2002 match index {
2003 0 => &self.x,
2004 1 => &self.y,
2005 2 => &self.z,
2006 3 => &self.w,
2007 _ => panic!("index out of bounds"),
2008 }
2009 }
2010}
2011
2012impl IndexMut<usize> for Vec4 {
2013 #[inline]
2014 #[track_caller]
2015 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2016 match index {
2017 0 => &mut self.x,
2018 1 => &mut self.y,
2019 2 => &mut self.z,
2020 3 => &mut self.w,
2021 _ => panic!("index out of bounds"),
2022 }
2023 }
2024}
2025
2026impl fmt::Display for Vec4 {
2027 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2028 if let Some(p) = f.precision() {
2029 write!(
2030 f,
2031 "[{:.*}, {:.*}, {:.*}, {:.*}]",
2032 p, self.x, p, self.y, p, self.z, p, self.w
2033 )
2034 } else {
2035 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
2036 }
2037 }
2038}
2039
2040impl fmt::Debug for Vec4 {
2041 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2042 fmt.debug_tuple(stringify!(Vec4))
2043 .field(&self.x)
2044 .field(&self.y)
2045 .field(&self.z)
2046 .field(&self.w)
2047 .finish()
2048 }
2049}
2050
2051impl From<Vec4> for float32x4_t {
2052 #[inline(always)]
2053 fn from(t: Vec4) -> Self {
2054 t.0
2055 }
2056}
2057
2058impl From<float32x4_t> for Vec4 {
2059 #[inline(always)]
2060 fn from(t: float32x4_t) -> Self {
2061 Self(t)
2062 }
2063}
2064
2065impl From<[f32; 4]> for Vec4 {
2066 #[inline]
2067 fn from(a: [f32; 4]) -> Self {
2068 Self(unsafe { vld1q_f32(a.as_ptr()) })
2069 }
2070}
2071
2072impl From<Vec4> for [f32; 4] {
2073 #[inline]
2074 fn from(v: Vec4) -> Self {
2075 use crate::align16::Align16;
2076 use core::mem::MaybeUninit;
2077 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2078 unsafe {
2079 vst1q_f32(out.as_mut_ptr().cast(), v.0);
2080 out.assume_init().0
2081 }
2082 }
2083}
2084
2085impl From<(f32, f32, f32, f32)> for Vec4 {
2086 #[inline]
2087 fn from(t: (f32, f32, f32, f32)) -> Self {
2088 Self::new(t.0, t.1, t.2, t.3)
2089 }
2090}
2091
2092impl From<Vec4> for (f32, f32, f32, f32) {
2093 #[inline]
2094 fn from(v: Vec4) -> Self {
2095 (v.x, v.y, v.z, v.w)
2096 }
2097}
2098
2099impl From<(Vec3A, f32)> for Vec4 {
2100 #[inline]
2101 fn from((v, w): (Vec3A, f32)) -> Self {
2102 v.extend(w)
2103 }
2104}
2105
2106impl From<(f32, Vec3A)> for Vec4 {
2107 #[inline]
2108 fn from((x, v): (f32, Vec3A)) -> Self {
2109 Self::new(x, v.x, v.y, v.z)
2110 }
2111}
2112
2113impl From<(Vec3, f32)> for Vec4 {
2114 #[inline]
2115 fn from((v, w): (Vec3, f32)) -> Self {
2116 Self::new(v.x, v.y, v.z, w)
2117 }
2118}
2119
2120impl From<(f32, Vec3)> for Vec4 {
2121 #[inline]
2122 fn from((x, v): (f32, Vec3)) -> Self {
2123 Self::new(x, v.x, v.y, v.z)
2124 }
2125}
2126
2127impl From<(Vec2, f32, f32)> for Vec4 {
2128 #[inline]
2129 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
2130 Self::new(v.x, v.y, z, w)
2131 }
2132}
2133
2134impl From<(Vec2, Vec2)> for Vec4 {
2135 #[inline]
2136 fn from((v, u): (Vec2, Vec2)) -> Self {
2137 Self::new(v.x, v.y, u.x, u.y)
2138 }
2139}
2140
2141impl Deref for Vec4 {
2142 type Target = crate::deref::Vec4<f32>;
2143 #[inline]
2144 fn deref(&self) -> &Self::Target {
2145 unsafe { &*(self as *const Self).cast() }
2146 }
2147}
2148
2149impl DerefMut for Vec4 {
2150 #[inline]
2151 fn deref_mut(&mut self) -> &mut Self::Target {
2152 unsafe { &mut *(self as *mut Self).cast() }
2153 }
2154}
2155
2156impl From<BVec4> for Vec4 {
2157 #[inline]
2158 fn from(v: BVec4) -> Self {
2159 Self::new(
2160 f32::from(v.x),
2161 f32::from(v.y),
2162 f32::from(v.z),
2163 f32::from(v.w),
2164 )
2165 }
2166}
2167
2168#[cfg(not(feature = "scalar-math"))]
2169impl From<BVec4A> for Vec4 {
2170 #[inline]
2171 fn from(v: BVec4A) -> Self {
2172 let bool_array: [bool; 4] = v.into();
2173 Self::new(
2174 f32::from(bool_array[0]),
2175 f32::from(bool_array[1]),
2176 f32::from(bool_array[2]),
2177 f32::from(bool_array[3]),
2178 )
2179 }
2180}