Debian Codegraph @

debian/missing-sources/glam-0.19.0/src/core/wasm32/quaternion.rs

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