Debian Codegraph @

debian/missing-sources/glam-0.18.0/src/core/sse2/quaternion.rs

raw symbols SCIP
1 #[cfg(target_arch = "x86")]
2 use core::arch::x86::*;
3 #[cfg(target_arch = "x86_64")]
4 use core::arch::x86_64::*;
5
6 use super::float::*;
7 use crate::core::{
8 storage::XYZ,
9 traits::{quaternion::Quaternion, scalar::*, vector::*},
10 };
11
12 impl Quaternion<f32> for __m128 {
13 type SIMDVector3 = __m128;
14
15 #[inline(always)]
16 fn conjugate(self) -> Self {
17 const SIGN: __m128 = const_f32x4!([-0.0, -0.0, -0.0, 0.0]);
18 unsafe { _mm_xor_ps(self, SIGN) }
19 }
20
21 #[inline]
22 fn lerp(self, end: Self, s: f32) -> Self {
23 glam_assert!(FloatVector4::is_normalized(self));
24 glam_assert!(FloatVector4::is_normalized(end));
25
26 unsafe {
27 const NEG_ZERO: __m128 = const_f32x4!([-0.0; 4]);
28 let start = self;
29 let end = end;
30 let dot = Vector4::dot_into_vec(start, end);
31 // Calculate the bias, if the dot product is positive or zero, there is no bias
32 // but if it is negative, we want to flip the 'end' rotation XYZW components
33 let bias = _mm_and_ps(dot, NEG_ZERO);
34 let interpolated = _mm_add_ps(
35 _mm_mul_ps(_mm_sub_ps(_mm_xor_ps(end, bias), start), _mm_set_ps1(s)),
36 start,
37 );
38 FloatVector4::normalize(interpolated)
39 }
40 }
41
42 #[inline]
43 fn slerp(self, end: Self, s: f32) -> Self {
44 // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
45 glam_assert!(FloatVector4::is_normalized(self));
46 glam_assert!(FloatVector4::is_normalized(end));
47
48 const DOT_THRESHOLD: f32 = 0.9995;
49
50 let dot = Vector4::dot(self, end);
51
52 if dot > DOT_THRESHOLD {
53 // assumes lerp returns a normalized quaternion
54 self.lerp(end, s)
55 } else {
56 // assumes scalar_acos clamps the input to [-1.0, 1.0]
57 let theta = dot.acos_approx();
58
59 let x = 1.0 - s;
60 let y = s;
61 let z = 1.0;
62
63 unsafe {
64 let tmp = _mm_mul_ps(_mm_set_ps1(theta), _mm_set_ps(0.0, z, y, x));
65 let tmp = m128_sin(tmp);
66
67 let scale1 = _mm_shuffle_ps(tmp, tmp, 0b00_00_00_00);
68 let scale2 = _mm_shuffle_ps(tmp, tmp, 0b01_01_01_01);
69 let theta_sin = _mm_shuffle_ps(tmp, tmp, 0b10_10_10_10);
70
71 self.mul(scale1).add(end.mul(scale2)).div(theta_sin)
72 }
73 }
74 }
75
76 #[inline]
77 fn mul_quaternion(self, other: Self) -> Self {
78 glam_assert!(FloatVector4::is_normalized(self));
79 glam_assert!(FloatVector4::is_normalized(other));
80 unsafe {
81 // Based on https://github.com/nfrechette/rtm `rtm::quat_mul`
82 let lhs = self;
83 let rhs = other;
84
85 const CONTROL_WZYX: __m128 = const_f32x4!([1.0, -1.0, 1.0, -1.0]);
86 const CONTROL_ZWXY: __m128 = const_f32x4!([1.0, 1.0, -1.0, -1.0]);
87 const CONTROL_YXWZ: __m128 = const_f32x4!([-1.0, 1.0, 1.0, -1.0]);
88
89 let r_xxxx = _mm_shuffle_ps(lhs, lhs, 0b00_00_00_00);
90 let r_yyyy = _mm_shuffle_ps(lhs, lhs, 0b01_01_01_01);
91 let r_zzzz = _mm_shuffle_ps(lhs, lhs, 0b10_10_10_10);
92 let r_wwww = _mm_shuffle_ps(lhs, lhs, 0b11_11_11_11);
93
94 let lxrw_lyrw_lzrw_lwrw = _mm_mul_ps(r_wwww, rhs);
95 let l_wzyx = _mm_shuffle_ps(rhs, rhs, 0b00_01_10_11);
96
97 let lwrx_lzrx_lyrx_lxrx = _mm_mul_ps(r_xxxx, l_wzyx);
98 let l_zwxy = _mm_shuffle_ps(l_wzyx, l_wzyx, 0b10_11_00_01);
99
100 let lwrx_nlzrx_lyrx_nlxrx = _mm_mul_ps(lwrx_lzrx_lyrx_lxrx, CONTROL_WZYX);
101
102 let lzry_lwry_lxry_lyry = _mm_mul_ps(r_yyyy, l_zwxy);
103 let l_yxwz = _mm_shuffle_ps(l_zwxy, l_zwxy, 0b00_01_10_11);
104
105 let lzry_lwry_nlxry_nlyry = _mm_mul_ps(lzry_lwry_lxry_lyry, CONTROL_ZWXY);
106
107 let lyrz_lxrz_lwrz_lzrz = _mm_mul_ps(r_zzzz, l_yxwz);
108 let result0 = _mm_add_ps(lxrw_lyrw_lzrw_lwrw, lwrx_nlzrx_lyrx_nlxrx);
109
110 let nlyrz_lxrz_lwrz_wlzrz = _mm_mul_ps(lyrz_lxrz_lwrz_lzrz, CONTROL_YXWZ);
111 let result1 = _mm_add_ps(lzry_lwry_nlxry_nlyry, nlyrz_lxrz_lwrz_wlzrz);
112 _mm_add_ps(result0, result1)
113 }
114 }
115
116 #[inline]
117 fn mul_vector3(self, other: XYZ<f32>) -> XYZ<f32> {
118 self.mul_float4_as_vector3(other.into()).into()
119 }
120
121 #[inline]
122 fn mul_float4_as_vector3(self, other: __m128) -> __m128 {
123 glam_assert!(FloatVector4::is_normalized(self));
124 unsafe {
125 const TWO: __m128 = const_f32x4!([2.0; 4]);
126 let w = _mm_shuffle_ps(self, self, 0b11_11_11_11);
127 let b = self;
128 let b2 = Vector3::dot_into_vec(b, b);
129 other
130 .mul(w.mul(w).sub(b2))
131 .add(b.mul(Vector3::dot_into_vec(other, b).mul(TWO)))
132 .add(b.cross(other).mul(w.mul(TWO)))
133 }
134 }
135 }