Debian Codegraph @

debian/missing-sources/glam-0.20.5/src/core/wasm32/float.rs

raw
1 use core::arch::wasm32::*;
2
3 macro_rules! const_u32x4 {
4 ($ux4:expr) => {
5 unsafe { $crate::cast::UVec4Cast { ux4: $ux4 }.v128 }
6 };
7 }
8
9 const PS_NEGATIVE_ZERO: v128 = const_u32x4!([0x8000_0000; 4]);
10 const PS_PI: v128 = const_f32x4!([core::f32::consts::PI; 4]);
11 const PS_HALF_PI: v128 = const_f32x4!([core::f32::consts::FRAC_PI_2; 4]);
12 const PS_SIN_COEFFICIENTS0: v128 =
13 const_f32x4!([-0.16666667, 0.008_333_331, -0.00019840874, 2.752_556_2e-6]);
14 const PS_SIN_COEFFICIENTS1: v128 = const_f32x4!([
15 -2.388_985_9e-8,
16 -0.16665852, /*Est1*/
17 0.008_313_95, /*Est2*/
18 -0.000_185_246_7 /*Est3*/
19 ]);
20 const PS_ONE: v128 = const_f32x4!([1.0; 4]);
21 const PS_TWO_PI: v128 = const_f32x4!([core::f32::consts::TAU; 4]);
22 const PS_RECIPROCAL_TWO_PI: v128 = const_f32x4!([0.159_154_94; 4]);
23
24 #[inline(always)]
25 pub(crate) fn v128_mul_add(a: v128, b: v128, c: v128) -> v128 {
26 f32x4_add(f32x4_mul(a, b), c)
27 }
28
29 #[inline(always)]
30 pub(crate) fn v128_neg_mul_sub(a: v128, b: v128, c: v128) -> v128 {
31 f32x4_sub(c, f32x4_mul(a, b))
32 }
33
34 /// Returns a vector whose components are the corresponding components of Angles modulo 2PI.
35 #[inline]
36 pub(crate) fn v128_mod_angles(angles: v128) -> v128 {
37 // Based on https://github.com/microsoft/DirectXMath `XMVectorModAngles`
38 let v = f32x4_mul(angles, PS_RECIPROCAL_TWO_PI);
39 let v = f32x4_nearest(v);
40 v128_neg_mul_sub(PS_TWO_PI, v, angles)
41 }
42
43 /// Computes the sine of the angle in each lane of `v`. Values outside
44 /// the bounds of PI may produce an increasing error as the input angle
45 /// drifts from `[-PI, PI]`.
46 #[inline]
47 pub(crate) fn v128_sin(v: v128) -> v128 {
48 // Based on https://github.com/microsoft/DirectXMath `XMVectorSin`
49
50 // 11-degree minimax approximation
51
52 // Force the value within the bounds of pi
53 let mut x = v128_mod_angles(v);
54
55 // Map in [-pi/2,pi/2] with sin(y) = sin(x).
56 let sign = v128_and(x, PS_NEGATIVE_ZERO);
57 // pi when x >= 0, -pi when x < 0
58 let c = v128_or(PS_PI, sign);
59 // |x|
60 let absx = v128_andnot(sign, x);
61 let rflx = f32x4_sub(c, x);
62 let comp = f32x4_le(absx, PS_HALF_PI);
63 let select0 = v128_and(comp, x);
64 let select1 = v128_andnot(comp, rflx);
65 x = v128_or(select0, select1);
66
67 let x2 = f32x4_mul(x, x);
68
69 // Compute polynomial approximation
70 const SC1: v128 = PS_SIN_COEFFICIENTS1;
71 let v_constants_b = i32x4_shuffle::<0, 0, 4, 4>(SC1, SC1);
72
73 const SC0: v128 = PS_SIN_COEFFICIENTS0;
74 let mut v_constants = i32x4_shuffle::<3, 3, 7, 7>(SC0, SC0);
75 let mut result = v128_mul_add(v_constants_b, x2, v_constants);
76
77 v_constants = i32x4_shuffle::<2, 2, 6, 6>(SC0, SC0);
78 result = v128_mul_add(result, x2, v_constants);
79
80 v_constants = i32x4_shuffle::<1, 1, 5, 5>(SC0, SC0);
81 result = v128_mul_add(result, x2, v_constants);
82
83 v_constants = i32x4_shuffle::<0, 0, 4, 4>(SC0, SC0);
84 result = v128_mul_add(result, x2, v_constants);
85
86 result = v128_mul_add(result, x2, PS_ONE);
87 result = f32x4_mul(result, x);
88
89 result
90 }
91
92 #[test]
93 fn test_wasm32_v128_sin() {
94 use crate::core::traits::vector::*;
95 use core::f32::consts::PI;
96
97 fn test_wasm32_v128_sin_angle(a: f32) {
98 let v = v128_sin(f32x4_splat(a));
99 let v = v.as_ref_xyzw();
100 let a_sin = a.sin();
101 // dbg!((a, a_sin, v));
102 assert!(v.abs_diff_eq(Vector::splat(a_sin), 1e-6));
103 }
104
105 let mut a = -PI;
106 let end = PI;
107 let step = PI / 8192.0;
108
109 while a <= end {
110 test_wasm32_v128_sin_angle(a);
111 a += step;
112 }
113 }