Debian Codegraph @

debian/missing-sources/glam-0.15.2/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 unsafe {
80 // Based on https://github.com/nfrechette/rtm `rtm::quat_mul`
81 let lhs = self;
82 let rhs = other;
83
84 const CONTROL_WZYX: __m128 = const_m128!([1.0, -1.0, 1.0, -1.0]);
85 const CONTROL_ZWXY: __m128 = const_m128!([1.0, 1.0, -1.0, -1.0]);
86 const CONTROL_YXWZ: __m128 = const_m128!([-1.0, 1.0, 1.0, -1.0]);
87
88 let r_xxxx = _mm_shuffle_ps(lhs, lhs, 0b00_00_00_00);
89 let r_yyyy = _mm_shuffle_ps(lhs, lhs, 0b01_01_01_01);
90 let r_zzzz = _mm_shuffle_ps(lhs, lhs, 0b10_10_10_10);
91 let r_wwww = _mm_shuffle_ps(lhs, lhs, 0b11_11_11_11);
92
93 let lxrw_lyrw_lzrw_lwrw = _mm_mul_ps(r_wwww, rhs);
94 let l_wzyx = _mm_shuffle_ps(rhs, rhs, 0b00_01_10_11);
95
96 let lwrx_lzrx_lyrx_lxrx = _mm_mul_ps(r_xxxx, l_wzyx);
97 let l_zwxy = _mm_shuffle_ps(l_wzyx, l_wzyx, 0b10_11_00_01);
98
99 let lwrx_nlzrx_lyrx_nlxrx = _mm_mul_ps(lwrx_lzrx_lyrx_lxrx, CONTROL_WZYX);
100
101 let lzry_lwry_lxry_lyry = _mm_mul_ps(r_yyyy, l_zwxy);
102 let l_yxwz = _mm_shuffle_ps(l_zwxy, l_zwxy, 0b00_01_10_11);
103
104 let lzry_lwry_nlxry_nlyry = _mm_mul_ps(lzry_lwry_lxry_lyry, CONTROL_ZWXY);
105
106 let lyrz_lxrz_lwrz_lzrz = _mm_mul_ps(r_zzzz, l_yxwz);
107 let result0 = _mm_add_ps(lxrw_lyrw_lzrw_lwrw, lwrx_nlzrx_lyrx_nlxrx);
108
109 let nlyrz_lxrz_lwrz_wlzrz = _mm_mul_ps(lyrz_lxrz_lwrz_lzrz, CONTROL_YXWZ);
110 let result1 = _mm_add_ps(lzry_lwry_nlxry_nlyry, nlyrz_lxrz_lwrz_wlzrz);
111 _mm_add_ps(result0, result1)
112 }
113 }
114
115 #[inline]
116 fn mul_vector3(self, other: XYZ<f32>) -> XYZ<f32> {
117 self.mul_float4_as_vector3(other.into()).into()
118 }
119
120 #[inline]
121 fn mul_float4_as_vector3(self, other: __m128) -> __m128 {
122 glam_assert!(FloatVector4::is_normalized(self));
123 unsafe {
124 const TWO: __m128 = const_m128!([2.0; 4]);
125 let w = _mm_shuffle_ps(self, self, 0b11_11_11_11);
126 let b = self;
127 let b2 = Vector3::dot_into_vec(b, b);
128 other
129 .mul(w.mul(w).sub(b2))
130 .add(b.mul(Vector3::dot_into_vec(other, b).mul(TWO)))
131 .add(b.cross(other).mul(w.mul(TWO)))
132 }
133 }
134 }