1use crate::{f32::math, sse2::*, BVec4, BVec4A, Vec2, Vec3, Vec3A};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9#[cfg(target_arch = "x86")]
10use core::arch::x86::*;
11#[cfg(target_arch = "x86_64")]
12use core::arch::x86_64::*;
13
14#[cfg(feature = "zerocopy-08")]
15use zerocopy_derive_08::*;
16
17#[repr(C)]
18union UnionCast {
19 a: [f32; 4],
20 v: Vec4,
21}
22
23#[inline(always)]
25#[must_use]
26pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
27 Vec4::new(x, y, z, w)
28}
29
30#[derive(Clone, Copy)]
36#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
37#[cfg_attr(
38 feature = "zerocopy-08",
39 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
40)]
41#[repr(transparent)]
42pub struct Vec4(pub(crate) __m128);
43
44impl Vec4 {
45 pub const ZERO: Self = Self::splat(0.0);
47
48 pub const ONE: Self = Self::splat(1.0);
50
51 pub const NEG_ONE: Self = Self::splat(-1.0);
53
54 pub const MIN: Self = Self::splat(f32::MIN);
56
57 pub const MAX: Self = Self::splat(f32::MAX);
59
60 pub const NAN: Self = Self::splat(f32::NAN);
62
63 pub const INFINITY: Self = Self::splat(f32::INFINITY);
65
66 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
68
69 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
71
72 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
74
75 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
77
78 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
80
81 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
83
84 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
86
87 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
89
90 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
92
93 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
95
96 pub const USES_CORE_SIMD: bool = false;
98 pub const USES_NEON: bool = false;
100 pub const USES_SCALAR_MATH: bool = false;
102 pub const USES_SSE2: bool = true;
104 pub const USES_WASM_SIMD: bool = false;
106 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
107 pub const USES_WASM32_SIMD: bool = false;
108
109 #[inline(always)]
111 #[must_use]
112 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
113 unsafe { UnionCast { a: [x, y, z, w] }.v }
114 }
115
116 #[inline]
118 #[must_use]
119 pub const fn splat(v: f32) -> Self {
120 unsafe { UnionCast { a: [v; 4] }.v }
121 }
122
123 #[inline]
125 #[must_use]
126 pub fn map<F>(self, mut f: F) -> Self
127 where
128 F: FnMut(f32) -> f32,
129 {
130 Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
131 }
132
133 #[inline]
139 #[must_use]
140 pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
141 Self(unsafe {
142 _mm_or_ps(
143 _mm_andnot_ps(mask.0, if_false.0),
144 _mm_and_ps(if_true.0, mask.0),
145 )
146 })
147 }
148
149 #[inline]
151 #[must_use]
152 pub const fn from_array(a: [f32; 4]) -> Self {
153 Self::new(a[0], a[1], a[2], a[3])
154 }
155
156 #[inline]
158 #[must_use]
159 pub const fn to_array(&self) -> [f32; 4] {
160 unsafe { *(self as *const Self as *const [f32; 4]) }
161 }
162
163 #[inline]
169 #[must_use]
170 pub const fn from_slice(slice: &[f32]) -> Self {
171 assert!(slice.len() >= 4);
172 Self::new(slice[0], slice[1], slice[2], slice[3])
173 }
174
175 #[inline]
181 pub fn write_to_slice(self, slice: &mut [f32]) {
182 assert!(slice.len() >= 4);
183 unsafe {
184 _mm_storeu_ps(slice.as_mut_ptr(), self.0);
185 }
186 }
187
188 #[inline]
194 #[must_use]
195 pub fn truncate(self) -> Vec3 {
196 use crate::swizzles::Vec4Swizzles;
197 self.xyz()
198 }
199
200 #[inline]
208 #[must_use]
209 pub fn project(self) -> Vec3 {
210 Vec3::from_homogeneous(self)
211 }
212
213 #[inline]
215 #[must_use]
216 pub fn with_x(mut self, x: f32) -> Self {
217 self.x = x;
218 self
219 }
220
221 #[inline]
223 #[must_use]
224 pub fn with_y(mut self, y: f32) -> Self {
225 self.y = y;
226 self
227 }
228
229 #[inline]
231 #[must_use]
232 pub fn with_z(mut self, z: f32) -> Self {
233 self.z = z;
234 self
235 }
236
237 #[inline]
239 #[must_use]
240 pub fn with_w(mut self, w: f32) -> Self {
241 self.w = w;
242 self
243 }
244
245 #[inline]
247 #[must_use]
248 pub fn dot(self, rhs: Self) -> f32 {
249 unsafe { dot4(self.0, rhs.0) }
250 }
251
252 #[inline]
254 #[must_use]
255 pub fn dot_into_vec(self, rhs: Self) -> Self {
256 Self(unsafe { dot4_into_m128(self.0, rhs.0) })
257 }
258
259 #[inline]
266 #[must_use]
267 pub fn min(self, rhs: Self) -> Self {
268 Self(unsafe { _mm_min_ps(self.0, rhs.0) })
269 }
270
271 #[inline]
278 #[must_use]
279 pub fn max(self, rhs: Self) -> Self {
280 Self(unsafe { _mm_max_ps(self.0, rhs.0) })
281 }
282
283 #[inline]
294 #[must_use]
295 pub fn clamp(self, min: Self, max: Self) -> Self {
296 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
297 self.max(min).min(max)
298 }
299
300 #[inline]
307 #[must_use]
308 pub fn min_element(self) -> f32 {
309 unsafe {
310 let v = self.0;
311 let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10));
312 let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01));
313 _mm_cvtss_f32(v)
314 }
315 }
316
317 #[inline]
324 #[must_use]
325 pub fn max_element(self) -> f32 {
326 unsafe {
327 let v = self.0;
328 let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10));
329 let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01));
330 _mm_cvtss_f32(v)
331 }
332 }
333
334 #[doc(alias = "argmin")]
336 #[inline]
337 #[must_use]
338 pub fn min_position(self) -> usize {
339 let mut min = self.x;
340 let mut index = 0;
341 if self.y < min {
342 min = self.y;
343 index = 1;
344 }
345 if self.z < min {
346 min = self.z;
347 index = 2;
348 }
349 if self.w < min {
350 index = 3;
351 }
352 index
353 }
354
355 #[doc(alias = "argmax")]
357 #[inline]
358 #[must_use]
359 pub fn max_position(self) -> usize {
360 let mut max = self.x;
361 let mut index = 0;
362 if self.y > max {
363 max = self.y;
364 index = 1;
365 }
366 if self.z > max {
367 max = self.z;
368 index = 2;
369 }
370 if self.w > max {
371 index = 3;
372 }
373 index
374 }
375
376 #[inline]
380 #[must_use]
381 pub fn element_sum(self) -> f32 {
382 unsafe {
383 let v = self.0;
384 let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01));
385 let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10));
386 _mm_cvtss_f32(v)
387 }
388 }
389
390 #[inline]
394 #[must_use]
395 pub fn element_product(self) -> f32 {
396 unsafe {
397 let v = self.0;
398 let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01));
399 let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10));
400 _mm_cvtss_f32(v)
401 }
402 }
403
404 #[inline]
410 #[must_use]
411 pub fn cmpeq(self, rhs: Self) -> BVec4A {
412 BVec4A(unsafe { _mm_cmpeq_ps(self.0, rhs.0) })
413 }
414
415 #[inline]
421 #[must_use]
422 pub fn cmpne(self, rhs: Self) -> BVec4A {
423 BVec4A(unsafe { _mm_cmpneq_ps(self.0, rhs.0) })
424 }
425
426 #[inline]
432 #[must_use]
433 pub fn cmpge(self, rhs: Self) -> BVec4A {
434 BVec4A(unsafe { _mm_cmpge_ps(self.0, rhs.0) })
435 }
436
437 #[inline]
443 #[must_use]
444 pub fn cmpgt(self, rhs: Self) -> BVec4A {
445 BVec4A(unsafe { _mm_cmpgt_ps(self.0, rhs.0) })
446 }
447
448 #[inline]
454 #[must_use]
455 pub fn cmple(self, rhs: Self) -> BVec4A {
456 BVec4A(unsafe { _mm_cmple_ps(self.0, rhs.0) })
457 }
458
459 #[inline]
465 #[must_use]
466 pub fn cmplt(self, rhs: Self) -> BVec4A {
467 BVec4A(unsafe { _mm_cmplt_ps(self.0, rhs.0) })
468 }
469
470 #[inline]
472 #[must_use]
473 pub fn abs(self) -> Self {
474 Self(unsafe { crate::sse2::m128_abs(self.0) })
475 }
476
477 #[inline]
483 #[must_use]
484 pub fn signum(self) -> Self {
485 let result = Self(unsafe { _mm_or_ps(_mm_and_ps(self.0, Self::NEG_ONE.0), Self::ONE.0) });
486 let mask = self.is_nan_mask();
487 Self::select(mask, self, result)
488 }
489
490 #[inline]
492 #[must_use]
493 pub fn copysign(self, rhs: Self) -> Self {
494 let mask = Self::splat(-0.0);
495 Self(unsafe { _mm_or_ps(_mm_and_ps(rhs.0, mask.0), _mm_andnot_ps(mask.0, self.0)) })
496 }
497
498 #[inline]
506 #[must_use]
507 pub fn is_negative_bitmask(self) -> u32 {
508 unsafe { _mm_movemask_ps(self.0) as u32 }
509 }
510
511 #[inline]
516 #[must_use]
517 pub fn is_negative_mask(self) -> BVec4A {
518 BVec4A(unsafe {
519 _mm_castsi128_ps(_mm_cmplt_epi32(
520 _mm_castps_si128(self.0),
521 _mm_setzero_si128(),
522 ))
523 })
524 }
525
526 #[inline]
529 #[must_use]
530 pub fn is_finite(self) -> bool {
531 self.is_finite_mask().all()
532 }
533
534 #[inline]
538 #[must_use]
539 pub fn is_finite_mask(self) -> BVec4A {
540 BVec4A(unsafe { _mm_cmplt_ps(crate::sse2::m128_abs(self.0), Self::INFINITY.0) })
541 }
542
543 #[inline]
545 #[must_use]
546 pub fn is_nan(self) -> bool {
547 self.is_nan_mask().any()
548 }
549
550 #[inline]
554 #[must_use]
555 pub fn is_nan_mask(self) -> BVec4A {
556 BVec4A(unsafe { _mm_cmpunord_ps(self.0, self.0) })
557 }
558
559 #[doc(alias = "magnitude")]
561 #[inline]
562 #[must_use]
563 pub fn length(self) -> f32 {
564 unsafe {
565 let dot = dot4_in_x(self.0, self.0);
566 _mm_cvtss_f32(_mm_sqrt_ps(dot))
567 }
568 }
569
570 #[allow(dead_code)]
572 fn is_non_zero(self) -> bool {
573 self.length_squared() > 0.0
574 }
575
576 #[doc(alias = "magnitude2")]
580 #[inline]
581 #[must_use]
582 pub fn length_squared(self) -> f32 {
583 self.dot(self)
584 }
585
586 #[inline]
590 #[must_use]
591 pub fn length_recip(self) -> f32 {
592 unsafe {
593 let dot = dot4_in_x(self.0, self.0);
594 _mm_cvtss_f32(_mm_div_ps(Self::ONE.0, _mm_sqrt_ps(dot)))
595 }
596 }
597
598 #[inline]
600 #[must_use]
601 pub fn distance(self, rhs: Self) -> f32 {
602 (self - rhs).length()
603 }
604
605 #[inline]
607 #[must_use]
608 pub fn distance_squared(self, rhs: Self) -> f32 {
609 (self - rhs).length_squared()
610 }
611
612 #[inline]
614 #[must_use]
615 pub fn div_euclid(self, rhs: Self) -> Self {
616 Self::new(
617 math::div_euclid(self.x, rhs.x),
618 math::div_euclid(self.y, rhs.y),
619 math::div_euclid(self.z, rhs.z),
620 math::div_euclid(self.w, rhs.w),
621 )
622 }
623
624 #[inline]
628 #[must_use]
629 pub fn rem_euclid(self, rhs: Self) -> Self {
630 Self::new(
631 math::rem_euclid(self.x, rhs.x),
632 math::rem_euclid(self.y, rhs.y),
633 math::rem_euclid(self.z, rhs.z),
634 math::rem_euclid(self.w, rhs.w),
635 )
636 }
637
638 #[inline]
648 #[must_use]
649 pub fn normalize(self) -> Self {
650 unsafe {
651 let length = _mm_sqrt_ps(dot4_into_m128(self.0, self.0));
652 #[allow(clippy::let_and_return)]
653 let normalized = Self(_mm_div_ps(self.0, length));
654 glam_assert!(normalized.is_finite());
655 normalized
656 }
657 }
658
659 #[inline]
666 #[must_use]
667 pub fn try_normalize(self) -> Option<Self> {
668 let rcp = self.length_recip();
669 if rcp.is_finite() && rcp > 0.0 {
670 Some(self * rcp)
671 } else {
672 None
673 }
674 }
675
676 #[inline]
684 #[must_use]
685 pub fn normalize_or(self, fallback: Self) -> Self {
686 let rcp = self.length_recip();
687 if rcp.is_finite() && rcp > 0.0 {
688 self * rcp
689 } else {
690 fallback
691 }
692 }
693
694 #[inline]
701 #[must_use]
702 pub fn normalize_or_zero(self) -> Self {
703 self.normalize_or(Self::ZERO)
704 }
705
706 #[inline]
710 #[must_use]
711 pub fn normalize_and_length(self) -> (Self, f32) {
712 let length = self.length();
713 let rcp = 1.0 / length;
714 if rcp.is_finite() && rcp > 0.0 {
715 (self * rcp, length)
716 } else {
717 (Self::X, 0.0)
718 }
719 }
720
721 #[inline]
725 #[must_use]
726 pub fn is_normalized(self) -> bool {
727 math::abs(self.length_squared() - 1.0) <= 2e-4
728 }
729
730 #[inline]
738 #[must_use]
739 pub fn project_onto(self, rhs: Self) -> Self {
740 let other_len_sq_rcp = 1.0 / rhs.dot(rhs);
741 glam_assert!(other_len_sq_rcp.is_finite());
742 rhs * self.dot(rhs) * other_len_sq_rcp
743 }
744
745 #[doc(alias("plane"))]
756 #[inline]
757 #[must_use]
758 pub fn reject_from(self, rhs: Self) -> Self {
759 self - self.project_onto(rhs)
760 }
761
762 #[inline]
770 #[must_use]
771 pub fn project_onto_normalized(self, rhs: Self) -> Self {
772 glam_assert!(rhs.is_normalized());
773 rhs * self.dot(rhs)
774 }
775
776 #[doc(alias("plane"))]
787 #[inline]
788 #[must_use]
789 pub fn reject_from_normalized(self, rhs: Self) -> Self {
790 self - self.project_onto_normalized(rhs)
791 }
792
793 #[inline]
796 #[must_use]
797 pub fn round(self) -> Self {
798 Self(unsafe { m128_round(self.0) })
799 }
800
801 #[inline]
804 #[must_use]
805 pub fn floor(self) -> Self {
806 Self(unsafe { m128_floor(self.0) })
807 }
808
809 #[inline]
812 #[must_use]
813 pub fn ceil(self) -> Self {
814 Self(unsafe { m128_ceil(self.0) })
815 }
816
817 #[inline]
820 #[must_use]
821 pub fn trunc(self) -> Self {
822 Self(unsafe { m128_trunc(self.0) })
823 }
824
825 #[inline]
829 #[must_use]
830 pub fn step(self, rhs: Self) -> Self {
831 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
832 }
833
834 #[inline]
845 #[must_use]
846 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
847 glam_assert!(edge0.cmplt(edge1).all());
848 let t = ((self - edge0) / (edge1 - edge0)).saturate();
849 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
850 }
851
852 #[inline]
854 #[must_use]
855 pub fn saturate(self) -> Self {
856 self.clamp(Self::ZERO, Self::ONE)
857 }
858
859 #[inline]
866 #[must_use]
867 pub fn fract(self) -> Self {
868 self - self.trunc()
869 }
870
871 #[inline]
878 #[must_use]
879 pub fn fract_gl(self) -> Self {
880 self - self.floor()
881 }
882
883 #[inline]
886 #[must_use]
887 pub fn exp(self) -> Self {
888 Self::new(
889 math::exp(self.x),
890 math::exp(self.y),
891 math::exp(self.z),
892 math::exp(self.w),
893 )
894 }
895
896 #[inline]
898 #[must_use]
899 pub fn exp2(self) -> Self {
900 Self::new(
901 math::exp2(self.x),
902 math::exp2(self.y),
903 math::exp2(self.z),
904 math::exp2(self.w),
905 )
906 }
907
908 #[inline]
911 #[must_use]
912 pub fn ln(self) -> Self {
913 Self::new(
914 math::ln(self.x),
915 math::ln(self.y),
916 math::ln(self.z),
917 math::ln(self.w),
918 )
919 }
920
921 #[inline]
924 #[must_use]
925 pub fn log2(self) -> Self {
926 Self::new(
927 math::log2(self.x),
928 math::log2(self.y),
929 math::log2(self.z),
930 math::log2(self.w),
931 )
932 }
933
934 #[inline]
936 #[must_use]
937 pub fn powf(self, n: f32) -> Self {
938 Self::new(
939 math::powf(self.x, n),
940 math::powf(self.y, n),
941 math::powf(self.z, n),
942 math::powf(self.w, n),
943 )
944 }
945
946 #[inline]
949 #[must_use]
950 pub fn sqrt(self) -> Self {
951 Self::new(
952 math::sqrt(self.x),
953 math::sqrt(self.y),
954 math::sqrt(self.z),
955 math::sqrt(self.w),
956 )
957 }
958
959 #[inline]
961 #[must_use]
962 pub fn cos(self) -> Self {
963 Self::new(
964 math::cos(self.x),
965 math::cos(self.y),
966 math::cos(self.z),
967 math::cos(self.w),
968 )
969 }
970
971 #[inline]
973 #[must_use]
974 pub fn sin(self) -> Self {
975 Self::new(
976 math::sin(self.x),
977 math::sin(self.y),
978 math::sin(self.z),
979 math::sin(self.w),
980 )
981 }
982
983 #[inline]
985 #[must_use]
986 pub fn sin_cos(self) -> (Self, Self) {
987 let (sin_x, cos_x) = math::sin_cos(self.x);
988 let (sin_y, cos_y) = math::sin_cos(self.y);
989 let (sin_z, cos_z) = math::sin_cos(self.z);
990 let (sin_w, cos_w) = math::sin_cos(self.w);
991
992 (
993 Self::new(sin_x, sin_y, sin_z, sin_w),
994 Self::new(cos_x, cos_y, cos_z, cos_w),
995 )
996 }
997
998 #[inline]
1000 #[must_use]
1001 pub fn recip(self) -> Self {
1002 Self(unsafe { _mm_div_ps(Self::ONE.0, self.0) })
1003 }
1004
1005 #[doc(alias = "mix")]
1017 #[inline]
1018 #[must_use]
1019 pub fn lerp(self, rhs: Self, s: f32) -> Self {
1020 self * (1.0 - s) + rhs * s
1021 }
1022
1023 #[doc(alias = "mix")]
1040 #[inline]
1041 #[must_use]
1042 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
1043 unsafe {
1044 Self(m128_mul_add(
1045 _mm_sub_ps(rhs.0, self.0),
1046 _mm_set_ps1(s),
1047 self.0,
1048 ))
1049 }
1050 }
1051
1052 #[inline]
1057 #[must_use]
1058 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
1059 let a = rhs - self;
1060 let len = a.length();
1061 if len <= d || len <= 1e-4 {
1062 return rhs;
1063 }
1064 self + a / len * d
1065 }
1066
1067 #[inline]
1073 pub fn midpoint(self, rhs: Self) -> Self {
1074 (self + rhs) * 0.5
1075 }
1076
1077 #[inline]
1087 #[must_use]
1088 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1089 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1090 }
1091
1092 #[inline]
1098 #[must_use]
1099 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1100 glam_assert!(0.0 <= min);
1101 glam_assert!(min <= max);
1102 let length_sq = self.length_squared();
1103 if length_sq < min * min {
1104 min * (self / math::sqrt(length_sq))
1105 } else if length_sq > max * max {
1106 max * (self / math::sqrt(length_sq))
1107 } else {
1108 self
1109 }
1110 }
1111
1112 #[inline]
1118 #[must_use]
1119 pub fn clamp_length_max(self, max: f32) -> Self {
1120 glam_assert!(0.0 <= max);
1121 let length_sq = self.length_squared();
1122 if length_sq > max * max {
1123 max * (self / math::sqrt(length_sq))
1124 } else {
1125 self
1126 }
1127 }
1128
1129 #[inline]
1135 #[must_use]
1136 pub fn clamp_length_min(self, min: f32) -> Self {
1137 glam_assert!(0.0 <= min);
1138 let length_sq = self.length_squared();
1139 if length_sq < min * min {
1140 min * (self / math::sqrt(length_sq))
1141 } else {
1142 self
1143 }
1144 }
1145
1146 #[inline]
1154 #[must_use]
1155 pub fn mul_add(self, a: Self, b: Self) -> Self {
1156 #[cfg(target_feature = "fma")]
1157 unsafe {
1158 Self(_mm_fmadd_ps(self.0, a.0, b.0))
1159 }
1160 #[cfg(not(target_feature = "fma"))]
1161 Self::new(
1162 math::mul_add(self.x, a.x, b.x),
1163 math::mul_add(self.y, a.y, b.y),
1164 math::mul_add(self.z, a.z, b.z),
1165 math::mul_add(self.w, a.w, b.w),
1166 )
1167 }
1168
1169 #[inline]
1178 #[must_use]
1179 pub fn reflect(self, normal: Self) -> Self {
1180 glam_assert!(normal.is_normalized());
1181 self - 2.0 * self.dot(normal) * normal
1182 }
1183
1184 #[inline]
1194 #[must_use]
1195 pub fn refract(self, normal: Self, eta: f32) -> Self {
1196 glam_assert!(self.is_normalized());
1197 glam_assert!(normal.is_normalized());
1198 let n_dot_i = normal.dot(self);
1199 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1200 if k >= 0.0 {
1201 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1202 } else {
1203 Self::ZERO
1204 }
1205 }
1206
1207 #[cfg(feature = "f64")]
1209 #[inline]
1210 #[must_use]
1211 pub fn as_dvec4(self) -> crate::DVec4 {
1212 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
1213 }
1214
1215 #[cfg(feature = "i8")]
1217 #[inline]
1218 #[must_use]
1219 pub fn as_i8vec4(self) -> crate::I8Vec4 {
1220 crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1221 }
1222
1223 #[cfg(feature = "u8")]
1225 #[inline]
1226 #[must_use]
1227 pub fn as_u8vec4(self) -> crate::U8Vec4 {
1228 crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1229 }
1230
1231 #[cfg(feature = "i16")]
1233 #[inline]
1234 #[must_use]
1235 pub fn as_i16vec4(self) -> crate::I16Vec4 {
1236 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1237 }
1238
1239 #[cfg(feature = "u16")]
1241 #[inline]
1242 #[must_use]
1243 pub fn as_u16vec4(self) -> crate::U16Vec4 {
1244 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1245 }
1246
1247 #[cfg(feature = "i32")]
1249 #[inline]
1250 #[must_use]
1251 pub fn as_ivec4(self) -> crate::IVec4 {
1252 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1253 }
1254
1255 #[cfg(feature = "u32")]
1257 #[inline]
1258 #[must_use]
1259 pub fn as_uvec4(self) -> crate::UVec4 {
1260 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1261 }
1262
1263 #[cfg(feature = "i64")]
1265 #[inline]
1266 #[must_use]
1267 pub fn as_i64vec4(self) -> crate::I64Vec4 {
1268 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1269 }
1270
1271 #[cfg(feature = "u64")]
1273 #[inline]
1274 #[must_use]
1275 pub fn as_u64vec4(self) -> crate::U64Vec4 {
1276 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1277 }
1278
1279 #[cfg(feature = "isize")]
1281 #[inline]
1282 #[must_use]
1283 pub fn as_isizevec4(self) -> crate::ISizeVec4 {
1284 crate::ISizeVec4::new(
1285 self.x as isize,
1286 self.y as isize,
1287 self.z as isize,
1288 self.w as isize,
1289 )
1290 }
1291
1292 #[cfg(feature = "usize")]
1294 #[inline]
1295 #[must_use]
1296 pub fn as_usizevec4(self) -> crate::USizeVec4 {
1297 crate::USizeVec4::new(
1298 self.x as usize,
1299 self.y as usize,
1300 self.z as usize,
1301 self.w as usize,
1302 )
1303 }
1304}
1305
1306impl Default for Vec4 {
1307 #[inline(always)]
1308 fn default() -> Self {
1309 Self::ZERO
1310 }
1311}
1312
1313impl PartialEq for Vec4 {
1314 #[inline]
1315 fn eq(&self, rhs: &Self) -> bool {
1316 self.cmpeq(*rhs).all()
1317 }
1318}
1319
1320impl Div for Vec4 {
1321 type Output = Self;
1322 #[inline]
1323 fn div(self, rhs: Self) -> Self {
1324 Self(unsafe { _mm_div_ps(self.0, rhs.0) })
1325 }
1326}
1327
1328impl Div<&Self> for Vec4 {
1329 type Output = Self;
1330 #[inline]
1331 fn div(self, rhs: &Self) -> Self {
1332 self.div(*rhs)
1333 }
1334}
1335
1336impl Div<&Vec4> for &Vec4 {
1337 type Output = Vec4;
1338 #[inline]
1339 fn div(self, rhs: &Vec4) -> Vec4 {
1340 (*self).div(*rhs)
1341 }
1342}
1343
1344impl Div<Vec4> for &Vec4 {
1345 type Output = Vec4;
1346 #[inline]
1347 fn div(self, rhs: Vec4) -> Vec4 {
1348 (*self).div(rhs)
1349 }
1350}
1351
1352impl DivAssign for Vec4 {
1353 #[inline]
1354 fn div_assign(&mut self, rhs: Self) {
1355 self.0 = unsafe { _mm_div_ps(self.0, rhs.0) };
1356 }
1357}
1358
1359impl DivAssign<&Self> for Vec4 {
1360 #[inline]
1361 fn div_assign(&mut self, rhs: &Self) {
1362 self.div_assign(*rhs);
1363 }
1364}
1365
1366impl Div<f32> for Vec4 {
1367 type Output = Self;
1368 #[inline]
1369 fn div(self, rhs: f32) -> Self {
1370 Self(unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) })
1371 }
1372}
1373
1374impl Div<&f32> for Vec4 {
1375 type Output = Self;
1376 #[inline]
1377 fn div(self, rhs: &f32) -> Self {
1378 self.div(*rhs)
1379 }
1380}
1381
1382impl Div<&f32> for &Vec4 {
1383 type Output = Vec4;
1384 #[inline]
1385 fn div(self, rhs: &f32) -> Vec4 {
1386 (*self).div(*rhs)
1387 }
1388}
1389
1390impl Div<f32> for &Vec4 {
1391 type Output = Vec4;
1392 #[inline]
1393 fn div(self, rhs: f32) -> Vec4 {
1394 (*self).div(rhs)
1395 }
1396}
1397
1398impl DivAssign<f32> for Vec4 {
1399 #[inline]
1400 fn div_assign(&mut self, rhs: f32) {
1401 self.0 = unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) };
1402 }
1403}
1404
1405impl DivAssign<&f32> for Vec4 {
1406 #[inline]
1407 fn div_assign(&mut self, rhs: &f32) {
1408 self.div_assign(*rhs);
1409 }
1410}
1411
1412impl Div<Vec4> for f32 {
1413 type Output = Vec4;
1414 #[inline]
1415 fn div(self, rhs: Vec4) -> Vec4 {
1416 Vec4(unsafe { _mm_div_ps(_mm_set1_ps(self), rhs.0) })
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 Div<&Vec4> for &f32 {
1429 type Output = Vec4;
1430 #[inline]
1431 fn div(self, rhs: &Vec4) -> Vec4 {
1432 (*self).div(*rhs)
1433 }
1434}
1435
1436impl Div<Vec4> for &f32 {
1437 type Output = Vec4;
1438 #[inline]
1439 fn div(self, rhs: Vec4) -> Vec4 {
1440 (*self).div(rhs)
1441 }
1442}
1443
1444impl Mul for Vec4 {
1445 type Output = Self;
1446 #[inline]
1447 fn mul(self, rhs: Self) -> Self {
1448 Self(unsafe { _mm_mul_ps(self.0, rhs.0) })
1449 }
1450}
1451
1452impl Mul<&Self> for Vec4 {
1453 type Output = Self;
1454 #[inline]
1455 fn mul(self, rhs: &Self) -> Self {
1456 self.mul(*rhs)
1457 }
1458}
1459
1460impl Mul<&Vec4> for &Vec4 {
1461 type Output = Vec4;
1462 #[inline]
1463 fn mul(self, rhs: &Vec4) -> Vec4 {
1464 (*self).mul(*rhs)
1465 }
1466}
1467
1468impl Mul<Vec4> for &Vec4 {
1469 type Output = Vec4;
1470 #[inline]
1471 fn mul(self, rhs: Vec4) -> Vec4 {
1472 (*self).mul(rhs)
1473 }
1474}
1475
1476impl MulAssign for Vec4 {
1477 #[inline]
1478 fn mul_assign(&mut self, rhs: Self) {
1479 self.0 = unsafe { _mm_mul_ps(self.0, rhs.0) };
1480 }
1481}
1482
1483impl MulAssign<&Self> for Vec4 {
1484 #[inline]
1485 fn mul_assign(&mut self, rhs: &Self) {
1486 self.mul_assign(*rhs);
1487 }
1488}
1489
1490impl Mul<f32> for Vec4 {
1491 type Output = Self;
1492 #[inline]
1493 fn mul(self, rhs: f32) -> Self {
1494 Self(unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) })
1495 }
1496}
1497
1498impl Mul<&f32> for Vec4 {
1499 type Output = Self;
1500 #[inline]
1501 fn mul(self, rhs: &f32) -> Self {
1502 self.mul(*rhs)
1503 }
1504}
1505
1506impl Mul<&f32> for &Vec4 {
1507 type Output = Vec4;
1508 #[inline]
1509 fn mul(self, rhs: &f32) -> Vec4 {
1510 (*self).mul(*rhs)
1511 }
1512}
1513
1514impl Mul<f32> for &Vec4 {
1515 type Output = Vec4;
1516 #[inline]
1517 fn mul(self, rhs: f32) -> Vec4 {
1518 (*self).mul(rhs)
1519 }
1520}
1521
1522impl MulAssign<f32> for Vec4 {
1523 #[inline]
1524 fn mul_assign(&mut self, rhs: f32) {
1525 self.0 = unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) };
1526 }
1527}
1528
1529impl MulAssign<&f32> for Vec4 {
1530 #[inline]
1531 fn mul_assign(&mut self, rhs: &f32) {
1532 self.mul_assign(*rhs);
1533 }
1534}
1535
1536impl Mul<Vec4> for f32 {
1537 type Output = Vec4;
1538 #[inline]
1539 fn mul(self, rhs: Vec4) -> Vec4 {
1540 Vec4(unsafe { _mm_mul_ps(_mm_set1_ps(self), rhs.0) })
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 Mul<&Vec4> for &f32 {
1553 type Output = Vec4;
1554 #[inline]
1555 fn mul(self, rhs: &Vec4) -> Vec4 {
1556 (*self).mul(*rhs)
1557 }
1558}
1559
1560impl Mul<Vec4> for &f32 {
1561 type Output = Vec4;
1562 #[inline]
1563 fn mul(self, rhs: Vec4) -> Vec4 {
1564 (*self).mul(rhs)
1565 }
1566}
1567
1568impl Add for Vec4 {
1569 type Output = Self;
1570 #[inline]
1571 fn add(self, rhs: Self) -> Self {
1572 Self(unsafe { _mm_add_ps(self.0, rhs.0) })
1573 }
1574}
1575
1576impl Add<&Self> for Vec4 {
1577 type Output = Self;
1578 #[inline]
1579 fn add(self, rhs: &Self) -> Self {
1580 self.add(*rhs)
1581 }
1582}
1583
1584impl Add<&Vec4> for &Vec4 {
1585 type Output = Vec4;
1586 #[inline]
1587 fn add(self, rhs: &Vec4) -> Vec4 {
1588 (*self).add(*rhs)
1589 }
1590}
1591
1592impl Add<Vec4> for &Vec4 {
1593 type Output = Vec4;
1594 #[inline]
1595 fn add(self, rhs: Vec4) -> Vec4 {
1596 (*self).add(rhs)
1597 }
1598}
1599
1600impl AddAssign for Vec4 {
1601 #[inline]
1602 fn add_assign(&mut self, rhs: Self) {
1603 self.0 = unsafe { _mm_add_ps(self.0, rhs.0) };
1604 }
1605}
1606
1607impl AddAssign<&Self> for Vec4 {
1608 #[inline]
1609 fn add_assign(&mut self, rhs: &Self) {
1610 self.add_assign(*rhs);
1611 }
1612}
1613
1614impl Add<f32> for Vec4 {
1615 type Output = Self;
1616 #[inline]
1617 fn add(self, rhs: f32) -> Self {
1618 Self(unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) })
1619 }
1620}
1621
1622impl Add<&f32> for Vec4 {
1623 type Output = Self;
1624 #[inline]
1625 fn add(self, rhs: &f32) -> Self {
1626 self.add(*rhs)
1627 }
1628}
1629
1630impl Add<&f32> for &Vec4 {
1631 type Output = Vec4;
1632 #[inline]
1633 fn add(self, rhs: &f32) -> Vec4 {
1634 (*self).add(*rhs)
1635 }
1636}
1637
1638impl Add<f32> for &Vec4 {
1639 type Output = Vec4;
1640 #[inline]
1641 fn add(self, rhs: f32) -> Vec4 {
1642 (*self).add(rhs)
1643 }
1644}
1645
1646impl AddAssign<f32> for Vec4 {
1647 #[inline]
1648 fn add_assign(&mut self, rhs: f32) {
1649 self.0 = unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) };
1650 }
1651}
1652
1653impl AddAssign<&f32> for Vec4 {
1654 #[inline]
1655 fn add_assign(&mut self, rhs: &f32) {
1656 self.add_assign(*rhs);
1657 }
1658}
1659
1660impl Add<Vec4> for f32 {
1661 type Output = Vec4;
1662 #[inline]
1663 fn add(self, rhs: Vec4) -> Vec4 {
1664 Vec4(unsafe { _mm_add_ps(_mm_set1_ps(self), rhs.0) })
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 Add<&Vec4> for &f32 {
1677 type Output = Vec4;
1678 #[inline]
1679 fn add(self, rhs: &Vec4) -> Vec4 {
1680 (*self).add(*rhs)
1681 }
1682}
1683
1684impl Add<Vec4> for &f32 {
1685 type Output = Vec4;
1686 #[inline]
1687 fn add(self, rhs: Vec4) -> Vec4 {
1688 (*self).add(rhs)
1689 }
1690}
1691
1692impl Sub for Vec4 {
1693 type Output = Self;
1694 #[inline]
1695 fn sub(self, rhs: Self) -> Self {
1696 Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
1697 }
1698}
1699
1700impl Sub<&Self> for Vec4 {
1701 type Output = Self;
1702 #[inline]
1703 fn sub(self, rhs: &Self) -> Self {
1704 self.sub(*rhs)
1705 }
1706}
1707
1708impl Sub<&Vec4> for &Vec4 {
1709 type Output = Vec4;
1710 #[inline]
1711 fn sub(self, rhs: &Vec4) -> Vec4 {
1712 (*self).sub(*rhs)
1713 }
1714}
1715
1716impl Sub<Vec4> for &Vec4 {
1717 type Output = Vec4;
1718 #[inline]
1719 fn sub(self, rhs: Vec4) -> Vec4 {
1720 (*self).sub(rhs)
1721 }
1722}
1723
1724impl SubAssign for Vec4 {
1725 #[inline]
1726 fn sub_assign(&mut self, rhs: Self) {
1727 self.0 = unsafe { _mm_sub_ps(self.0, rhs.0) };
1728 }
1729}
1730
1731impl SubAssign<&Self> for Vec4 {
1732 #[inline]
1733 fn sub_assign(&mut self, rhs: &Self) {
1734 self.sub_assign(*rhs);
1735 }
1736}
1737
1738impl Sub<f32> for Vec4 {
1739 type Output = Self;
1740 #[inline]
1741 fn sub(self, rhs: f32) -> Self {
1742 Self(unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) })
1743 }
1744}
1745
1746impl Sub<&f32> for Vec4 {
1747 type Output = Self;
1748 #[inline]
1749 fn sub(self, rhs: &f32) -> Self {
1750 self.sub(*rhs)
1751 }
1752}
1753
1754impl Sub<&f32> for &Vec4 {
1755 type Output = Vec4;
1756 #[inline]
1757 fn sub(self, rhs: &f32) -> Vec4 {
1758 (*self).sub(*rhs)
1759 }
1760}
1761
1762impl Sub<f32> for &Vec4 {
1763 type Output = Vec4;
1764 #[inline]
1765 fn sub(self, rhs: f32) -> Vec4 {
1766 (*self).sub(rhs)
1767 }
1768}
1769
1770impl SubAssign<f32> for Vec4 {
1771 #[inline]
1772 fn sub_assign(&mut self, rhs: f32) {
1773 self.0 = unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) };
1774 }
1775}
1776
1777impl SubAssign<&f32> for Vec4 {
1778 #[inline]
1779 fn sub_assign(&mut self, rhs: &f32) {
1780 self.sub_assign(*rhs);
1781 }
1782}
1783
1784impl Sub<Vec4> for f32 {
1785 type Output = Vec4;
1786 #[inline]
1787 fn sub(self, rhs: Vec4) -> Vec4 {
1788 Vec4(unsafe { _mm_sub_ps(_mm_set1_ps(self), rhs.0) })
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 Sub<&Vec4> for &f32 {
1801 type Output = Vec4;
1802 #[inline]
1803 fn sub(self, rhs: &Vec4) -> Vec4 {
1804 (*self).sub(*rhs)
1805 }
1806}
1807
1808impl Sub<Vec4> for &f32 {
1809 type Output = Vec4;
1810 #[inline]
1811 fn sub(self, rhs: Vec4) -> Vec4 {
1812 (*self).sub(rhs)
1813 }
1814}
1815
1816impl Rem for Vec4 {
1817 type Output = Self;
1818 #[inline]
1819 fn rem(self, rhs: Self) -> Self {
1820 unsafe {
1821 let n = m128_floor(_mm_div_ps(self.0, rhs.0));
1822 Self(_mm_sub_ps(self.0, _mm_mul_ps(n, rhs.0)))
1823 }
1824 }
1825}
1826
1827impl Rem<&Self> for Vec4 {
1828 type Output = Self;
1829 #[inline]
1830 fn rem(self, rhs: &Self) -> Self {
1831 self.rem(*rhs)
1832 }
1833}
1834
1835impl Rem<&Vec4> for &Vec4 {
1836 type Output = Vec4;
1837 #[inline]
1838 fn rem(self, rhs: &Vec4) -> Vec4 {
1839 (*self).rem(*rhs)
1840 }
1841}
1842
1843impl Rem<Vec4> for &Vec4 {
1844 type Output = Vec4;
1845 #[inline]
1846 fn rem(self, rhs: Vec4) -> Vec4 {
1847 (*self).rem(rhs)
1848 }
1849}
1850
1851impl RemAssign for Vec4 {
1852 #[inline]
1853 fn rem_assign(&mut self, rhs: Self) {
1854 *self = self.rem(rhs);
1855 }
1856}
1857
1858impl RemAssign<&Self> for Vec4 {
1859 #[inline]
1860 fn rem_assign(&mut self, rhs: &Self) {
1861 self.rem_assign(*rhs);
1862 }
1863}
1864
1865impl Rem<f32> for Vec4 {
1866 type Output = Self;
1867 #[inline]
1868 fn rem(self, rhs: f32) -> Self {
1869 self.rem(Self::splat(rhs))
1870 }
1871}
1872
1873impl Rem<&f32> for Vec4 {
1874 type Output = Self;
1875 #[inline]
1876 fn rem(self, rhs: &f32) -> Self {
1877 self.rem(*rhs)
1878 }
1879}
1880
1881impl Rem<&f32> for &Vec4 {
1882 type Output = Vec4;
1883 #[inline]
1884 fn rem(self, rhs: &f32) -> Vec4 {
1885 (*self).rem(*rhs)
1886 }
1887}
1888
1889impl Rem<f32> for &Vec4 {
1890 type Output = Vec4;
1891 #[inline]
1892 fn rem(self, rhs: f32) -> Vec4 {
1893 (*self).rem(rhs)
1894 }
1895}
1896
1897impl RemAssign<f32> for Vec4 {
1898 #[inline]
1899 fn rem_assign(&mut self, rhs: f32) {
1900 *self = self.rem(Self::splat(rhs));
1901 }
1902}
1903
1904impl RemAssign<&f32> for Vec4 {
1905 #[inline]
1906 fn rem_assign(&mut self, rhs: &f32) {
1907 self.rem_assign(*rhs);
1908 }
1909}
1910
1911impl Rem<Vec4> for f32 {
1912 type Output = Vec4;
1913 #[inline]
1914 fn rem(self, rhs: Vec4) -> Vec4 {
1915 Vec4::splat(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 Rem<&Vec4> for &f32 {
1928 type Output = Vec4;
1929 #[inline]
1930 fn rem(self, rhs: &Vec4) -> Vec4 {
1931 (*self).rem(*rhs)
1932 }
1933}
1934
1935impl Rem<Vec4> for &f32 {
1936 type Output = Vec4;
1937 #[inline]
1938 fn rem(self, rhs: Vec4) -> Vec4 {
1939 (*self).rem(rhs)
1940 }
1941}
1942
1943impl AsRef<[f32; 4]> for Vec4 {
1944 #[inline]
1945 fn as_ref(&self) -> &[f32; 4] {
1946 unsafe { &*(self as *const Self as *const [f32; 4]) }
1947 }
1948}
1949
1950impl AsMut<[f32; 4]> for Vec4 {
1951 #[inline]
1952 fn as_mut(&mut self) -> &mut [f32; 4] {
1953 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
1954 }
1955}
1956
1957impl Sum for Vec4 {
1958 #[inline]
1959 fn sum<I>(iter: I) -> Self
1960 where
1961 I: Iterator<Item = Self>,
1962 {
1963 iter.fold(Self::ZERO, Self::add)
1964 }
1965}
1966
1967impl<'a> Sum<&'a Self> for Vec4 {
1968 #[inline]
1969 fn sum<I>(iter: I) -> Self
1970 where
1971 I: Iterator<Item = &'a Self>,
1972 {
1973 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1974 }
1975}
1976
1977impl Product for Vec4 {
1978 #[inline]
1979 fn product<I>(iter: I) -> Self
1980 where
1981 I: Iterator<Item = Self>,
1982 {
1983 iter.fold(Self::ONE, Self::mul)
1984 }
1985}
1986
1987impl<'a> Product<&'a Self> for Vec4 {
1988 #[inline]
1989 fn product<I>(iter: I) -> Self
1990 where
1991 I: Iterator<Item = &'a Self>,
1992 {
1993 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1994 }
1995}
1996
1997impl Neg for Vec4 {
1998 type Output = Self;
1999 #[inline]
2000 fn neg(self) -> Self {
2001 Self(unsafe { _mm_xor_ps(_mm_set1_ps(-0.0), self.0) })
2002 }
2003}
2004
2005impl Neg for &Vec4 {
2006 type Output = Vec4;
2007 #[inline]
2008 fn neg(self) -> Vec4 {
2009 (*self).neg()
2010 }
2011}
2012
2013impl Index<usize> for Vec4 {
2014 type Output = f32;
2015 #[inline]
2016 fn index(&self, index: usize) -> &Self::Output {
2017 match index {
2018 0 => &self.x,
2019 1 => &self.y,
2020 2 => &self.z,
2021 3 => &self.w,
2022 _ => panic!("index out of bounds"),
2023 }
2024 }
2025}
2026
2027impl IndexMut<usize> for Vec4 {
2028 #[inline]
2029 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2030 match index {
2031 0 => &mut self.x,
2032 1 => &mut self.y,
2033 2 => &mut self.z,
2034 3 => &mut self.w,
2035 _ => panic!("index out of bounds"),
2036 }
2037 }
2038}
2039
2040impl fmt::Display for Vec4 {
2041 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2042 if let Some(p) = f.precision() {
2043 write!(
2044 f,
2045 "[{:.*}, {:.*}, {:.*}, {:.*}]",
2046 p, self.x, p, self.y, p, self.z, p, self.w
2047 )
2048 } else {
2049 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
2050 }
2051 }
2052}
2053
2054impl fmt::Debug for Vec4 {
2055 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2056 fmt.debug_tuple(stringify!(Vec4))
2057 .field(&self.x)
2058 .field(&self.y)
2059 .field(&self.z)
2060 .field(&self.w)
2061 .finish()
2062 }
2063}
2064
2065impl From<Vec4> for __m128 {
2066 #[inline(always)]
2067 fn from(t: Vec4) -> Self {
2068 t.0
2069 }
2070}
2071
2072impl From<__m128> for Vec4 {
2073 #[inline(always)]
2074 fn from(t: __m128) -> Self {
2075 Self(t)
2076 }
2077}
2078
2079impl From<[f32; 4]> for Vec4 {
2080 #[inline]
2081 fn from(a: [f32; 4]) -> Self {
2082 Self(unsafe { _mm_loadu_ps(a.as_ptr()) })
2083 }
2084}
2085
2086impl From<Vec4> for [f32; 4] {
2087 #[inline]
2088 fn from(v: Vec4) -> Self {
2089 use crate::Align16;
2090 use core::mem::MaybeUninit;
2091 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2092 unsafe {
2093 _mm_store_ps(out.as_mut_ptr().cast(), v.0);
2094 out.assume_init().0
2095 }
2096 }
2097}
2098
2099impl From<(f32, f32, f32, f32)> for Vec4 {
2100 #[inline]
2101 fn from(t: (f32, f32, f32, f32)) -> Self {
2102 Self::new(t.0, t.1, t.2, t.3)
2103 }
2104}
2105
2106impl From<Vec4> for (f32, f32, f32, f32) {
2107 #[inline]
2108 fn from(v: Vec4) -> Self {
2109 (v.x, v.y, v.z, v.w)
2110 }
2111}
2112
2113impl From<(Vec3A, f32)> for Vec4 {
2114 #[inline]
2115 fn from((v, w): (Vec3A, f32)) -> Self {
2116 v.extend(w)
2117 }
2118}
2119
2120impl From<(f32, Vec3A)> for Vec4 {
2121 #[inline]
2122 fn from((x, v): (f32, Vec3A)) -> Self {
2123 Self::new(x, v.x, v.y, v.z)
2124 }
2125}
2126
2127impl From<(Vec3, f32)> for Vec4 {
2128 #[inline]
2129 fn from((v, w): (Vec3, f32)) -> Self {
2130 Self::new(v.x, v.y, v.z, w)
2131 }
2132}
2133
2134impl From<(f32, Vec3)> for Vec4 {
2135 #[inline]
2136 fn from((x, v): (f32, Vec3)) -> Self {
2137 Self::new(x, v.x, v.y, v.z)
2138 }
2139}
2140
2141impl From<(Vec2, f32, f32)> for Vec4 {
2142 #[inline]
2143 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
2144 Self::new(v.x, v.y, z, w)
2145 }
2146}
2147
2148impl From<(Vec2, Vec2)> for Vec4 {
2149 #[inline]
2150 fn from((v, u): (Vec2, Vec2)) -> Self {
2151 Self::new(v.x, v.y, u.x, u.y)
2152 }
2153}
2154
2155impl Deref for Vec4 {
2156 type Target = crate::deref::Vec4<f32>;
2157 #[inline]
2158 fn deref(&self) -> &Self::Target {
2159 unsafe { &*(self as *const Self).cast() }
2160 }
2161}
2162
2163impl DerefMut for Vec4 {
2164 #[inline]
2165 fn deref_mut(&mut self) -> &mut Self::Target {
2166 unsafe { &mut *(self as *mut Self).cast() }
2167 }
2168}
2169
2170impl From<BVec4> for Vec4 {
2171 #[inline]
2172 fn from(v: BVec4) -> Self {
2173 Self::new(
2174 f32::from(v.x),
2175 f32::from(v.y),
2176 f32::from(v.z),
2177 f32::from(v.w),
2178 )
2179 }
2180}
2181
2182#[cfg(not(feature = "scalar-math"))]
2183impl From<BVec4A> for Vec4 {
2184 #[inline]
2185 fn from(v: BVec4A) -> Self {
2186 let bool_array: [bool; 4] = v.into();
2187 Self::new(
2188 f32::from(bool_array[0]),
2189 f32::from(bool_array[1]),
2190 f32::from(bool_array[2]),
2191 f32::from(bool_array[3]),
2192 )
2193 }
2194}