Debian Codegraph @

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