Debian Codegraph @

debian/missing-sources/glam-0.20.5/src/core/sse2/float.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 macro_rules! const_u32x4 {
7 ($ux4:expr) => {
8 unsafe { $crate::cast::UVec4Cast { ux4: $ux4 }.m128 }
9 };
10 }
11
12 const PS_INV_SIGN_MASK: __m128 = const_u32x4!([!0x8000_0000; 4]);
13 const PS_SIGN_MASK: __m128 = const_u32x4!([0x8000_0000; 4]);
14 const PS_NO_FRACTION: __m128 = const_f32x4!([8388608.0; 4]);
15 const PS_NEGATIVE_ZERO: __m128 = const_u32x4!([0x8000_0000; 4]);
16 const PS_PI: __m128 = const_f32x4!([core::f32::consts::PI; 4]);
17 const PS_HALF_PI: __m128 = const_f32x4!([core::f32::consts::FRAC_PI_2; 4]);
18 const PS_SIN_COEFFICIENTS0: __m128 =
19 const_f32x4!([-0.16666667, 0.008_333_331, -0.00019840874, 2.752_556_2e-6]);
20 const PS_SIN_COEFFICIENTS1: __m128 = const_f32x4!([
21 -2.388_985_9e-8,
22 -0.16665852, /*Est1*/
23 0.008_313_95, /*Est2*/
24 -0.000_185_246_7 /*Est3*/
25 ]);
26 const PS_ONE: __m128 = const_f32x4!([1.0; 4]);
27 const PS_TWO_PI: __m128 = const_f32x4!([core::f32::consts::TAU; 4]);
28 const PS_RECIPROCAL_TWO_PI: __m128 = const_f32x4!([0.159_154_94; 4]);
29
30 #[inline]
31 pub(crate) unsafe fn m128_abs(v: __m128) -> __m128 {
32 _mm_and_ps(v, _mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff)))
33 }
34
35 #[inline]
36 pub(crate) unsafe fn m128_round(v: __m128) -> __m128 {
37 // Based on https://github.com/microsoft/DirectXMath `XMVectorRound`
38 let sign = _mm_and_ps(v, PS_SIGN_MASK);
39 let s_magic = _mm_or_ps(PS_NO_FRACTION, sign);
40 let r1 = _mm_add_ps(v, s_magic);
41 let r1 = _mm_sub_ps(r1, s_magic);
42 let r2 = _mm_and_ps(v, PS_INV_SIGN_MASK);
43 let mask = _mm_cmple_ps(r2, PS_NO_FRACTION);
44 let r2 = _mm_andnot_ps(mask, v);
45 let r1 = _mm_and_ps(r1, mask);
46 _mm_xor_ps(r1, r2)
47 }
48
49 #[inline]
50 pub(crate) unsafe fn m128_floor(v: __m128) -> __m128 {
51 // Based on https://github.com/microsoft/DirectXMath `XMVectorFloor`
52 // To handle NAN, INF and numbers greater than 8388608, use masking
53 let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK));
54 let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION));
55 // Truncate
56 let vint = _mm_cvttps_epi32(v);
57 let result = _mm_cvtepi32_ps(vint);
58 let larger = _mm_cmpgt_ps(result, v);
59 // 0 -> 0, 0xffffffff -> -1.0f
60 let larger = _mm_cvtepi32_ps(_mm_castps_si128(larger));
61 let result = _mm_add_ps(result, larger);
62 // All numbers less than 8388608 will use the round to int
63 let result = _mm_and_ps(result, _mm_castsi128_ps(test));
64 // All others, use the ORIGINAL value
65 let test = _mm_andnot_si128(test, _mm_castps_si128(v));
66 _mm_or_ps(result, _mm_castsi128_ps(test))
67 }
68
69 #[inline]
70 pub(crate) unsafe fn m128_ceil(v: __m128) -> __m128 {
71 // Based on https://github.com/microsoft/DirectXMath `XMVectorCeil`
72 // To handle NAN, INF and numbers greater than 8388608, use masking
73 let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK));
74 let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION));
75 // Truncate
76 let vint = _mm_cvttps_epi32(v);
77 let result = _mm_cvtepi32_ps(vint);
78 let smaller = _mm_cmplt_ps(result, v);
79 // 0 -> 0, 0xffffffff -> -1.0f
80 let smaller = _mm_cvtepi32_ps(_mm_castps_si128(smaller));
81 let result = _mm_sub_ps(result, smaller);
82 // All numbers less than 8388608 will use the round to int
83 let result = _mm_and_ps(result, _mm_castsi128_ps(test));
84 // All others, use the ORIGINAL value
85 let test = _mm_andnot_si128(test, _mm_castps_si128(v));
86 _mm_or_ps(result, _mm_castsi128_ps(test))
87 }
88
89 #[inline(always)]
90 pub(crate) unsafe fn m128_mul_add(a: __m128, b: __m128, c: __m128) -> __m128 {
91 // Only enable fused multiply-adds here if "fast-math" is enabled and the
92 // platform supports it. Otherwise this may break cross-platform determinism.
93 #[cfg(all(feature = "fast-math", target_feature = "fma"))]
94 {
95 _mm_fmadd_ps(a, b, c)
96 }
97
98 #[cfg(any(not(feature = "fast-math"), not(target_feature = "fma")))]
99 {
100 _mm_add_ps(_mm_mul_ps(a, b), c)
101 }
102 }
103
104 #[inline(always)]
105 pub(crate) unsafe fn m128_neg_mul_sub(a: __m128, b: __m128, c: __m128) -> __m128 {
106 _mm_sub_ps(c, _mm_mul_ps(a, b))
107 }
108
109 /// Returns a vector whose components are the corresponding components of Angles modulo 2PI.
110 #[inline]
111 pub(crate) unsafe fn m128_mod_angles(angles: __m128) -> __m128 {
112 // Based on https://github.com/microsoft/DirectXMath `XMVectorModAngles`
113 let v = _mm_mul_ps(angles, PS_RECIPROCAL_TWO_PI);
114 let v = m128_round(v);
115 m128_neg_mul_sub(PS_TWO_PI, v, angles)
116 }
117
118 /// Computes the sine of the angle in each lane of `v`. Values outside
119 /// the bounds of PI may produce an increasing error as the input angle
120 /// drifts from `[-PI, PI]`.
121 #[inline]
122 pub(crate) unsafe fn m128_sin(v: __m128) -> __m128 {
123 // Based on https://github.com/microsoft/DirectXMath `XMVectorSin`
124
125 // 11-degree minimax approximation
126
127 // Force the value within the bounds of pi
128 let mut x = m128_mod_angles(v);
129
130 // Map in [-pi/2,pi/2] with sin(y) = sin(x).
131 let sign = _mm_and_ps(x, PS_NEGATIVE_ZERO);
132 // pi when x >= 0, -pi when x < 0
133 let c = _mm_or_ps(PS_PI, sign);
134 // |x|
135 let absx = _mm_andnot_ps(sign, x);
136 let rflx = _mm_sub_ps(c, x);
137 let comp = _mm_cmple_ps(absx, PS_HALF_PI);
138 let select0 = _mm_and_ps(comp, x);
139 let select1 = _mm_andnot_ps(comp, rflx);
140 x = _mm_or_ps(select0, select1);
141
142 let x2 = _mm_mul_ps(x, x);
143
144 // Compute polynomial approximation
145 const SC1: __m128 = PS_SIN_COEFFICIENTS1;
146 let v_constants_b = _mm_shuffle_ps(SC1, SC1, 0b00_00_00_00);
147
148 const SC0: __m128 = PS_SIN_COEFFICIENTS0;
149 let mut v_constants = _mm_shuffle_ps(SC0, SC0, 0b11_11_11_11);
150 let mut result = m128_mul_add(v_constants_b, x2, v_constants);
151
152 v_constants = _mm_shuffle_ps(SC0, SC0, 0b10_10_10_10);
153 result = m128_mul_add(result, x2, v_constants);
154
155 v_constants = _mm_shuffle_ps(SC0, SC0, 0b01_01_01_01);
156 result = m128_mul_add(result, x2, v_constants);
157
158 v_constants = _mm_shuffle_ps(SC0, SC0, 0b00_00_00_00);
159 result = m128_mul_add(result, x2, v_constants);
160
161 result = m128_mul_add(result, x2, PS_ONE);
162 result = _mm_mul_ps(result, x);
163
164 result
165 }
166
167 // Based on http://gruntthepeon.free.fr/ssemath/sse_mathfun.h
168 // #[cfg(target_feature = "sse2")]
169 // unsafe fn sin_cos_sse2(x: __m128) -> (__m128, __m128) {
170 // let mut sign_bit_sin = x;
171 // // take the absolute value
172 // let mut x = _mm_and_ps(x, PS_INV_SIGN_MASK.m128);
173 // // extract the sign bit (upper one)
174 // sign_bit_sin = _mm_and_ps(sign_bit_sin, PS_SIGN_MASK.m128);
175
176 // // scale by 4/Pi
177 // let mut y = _mm_mul_ps(x, PS_CEPHES_FOPI.m128);
178
179 // // store the integer part of y in emm2
180 // let mut emm2 = _mm_cvttps_epi32(y);
181
182 // // j=(j+1) & (~1) (see the cephes sources)
183 // emm2 = _mm_add_epi32(emm2, PI32_1.m128i);
184 // emm2 = _mm_and_si128(emm2, PI32_INV_1.m128i);
185 // y = _mm_cvtepi32_ps(emm2);
186
187 // let mut emm4 = emm2;
188
189 // /* get the swap sign flag for the sine */
190 // let mut emm0 = _mm_and_si128(emm2, PI32_4.m128i);
191 // emm0 = _mm_slli_epi32(emm0, 29);
192 // let swap_sign_bit_sin = _mm_castsi128_ps(emm0);
193
194 // /* get the polynom selection mask for the sine*/
195 // emm2 = _mm_and_si128(emm2, PI32_2.m128i);
196 // emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
197 // let poly_mask = _mm_castsi128_ps(emm2);
198
199 // /* The magic pass: "Extended precision modular arithmetic"
200 // x = ((x - y * DP1) - y * DP2) - y * DP3; */
201 // let mut xmm1 = PS_MINUS_CEPHES_DP1.m128;
202 // let mut xmm2 = PS_MINUS_CEPHES_DP2.m128;
203 // let mut xmm3 = PS_MINUS_CEPHES_DP3.m128;
204 // xmm1 = _mm_mul_ps(y, xmm1);
205 // xmm2 = _mm_mul_ps(y, xmm2);
206 // xmm3 = _mm_mul_ps(y, xmm3);
207 // x = _mm_add_ps(x, xmm1);
208 // x = _mm_add_ps(x, xmm2);
209 // x = _mm_add_ps(x, xmm3);
210
211 // emm4 = _mm_sub_epi32(emm4, PI32_2.m128i);
212 // emm4 = _mm_andnot_si128(emm4, PI32_4.m128i);
213 // emm4 = _mm_slli_epi32(emm4, 29);
214 // let sign_bit_cos = _mm_castsi128_ps(emm4);
215
216 // sign_bit_sin = _mm_xor_ps(sign_bit_sin, swap_sign_bit_sin);
217
218 // // Evaluate the first polynom (0 <= x <= Pi/4)
219 // let z = _mm_mul_ps(x, x);
220 // y = PS_COSCOF_P0.m128;
221
222 // y = _mm_mul_ps(y, z);
223 // y = _mm_add_ps(y, PS_COSCOF_P1.m128);
224 // y = _mm_mul_ps(y, z);
225 // y = _mm_add_ps(y, PS_COSCOF_P2.m128);
226 // y = _mm_mul_ps(y, z);
227 // y = _mm_mul_ps(y, z);
228 // let tmp = _mm_mul_ps(z, PS_0_5.m128);
229 // y = _mm_sub_ps(y, tmp);
230 // y = _mm_add_ps(y, PS_1_0.m128);
231
232 // // Evaluate the second polynom (Pi/4 <= x <= 0)
233 // let mut y2 = PS_SINCOF_P0.m128;
234 // y2 = _mm_mul_ps(y2, z);
235 // y2 = _mm_add_ps(y2, PS_SINCOF_P1.m128);
236 // y2 = _mm_mul_ps(y2, z);
237 // y2 = _mm_add_ps(y2, PS_SINCOF_P2.m128);
238 // y2 = _mm_mul_ps(y2, z);
239 // y2 = _mm_mul_ps(y2, x);
240 // y2 = _mm_add_ps(y2, x);
241
242 // // select the correct result from the two polynoms
243 // xmm3 = poly_mask;
244 // let ysin2 = _mm_and_ps(xmm3, y2);
245 // let ysin1 = _mm_andnot_ps(xmm3, y);
246 // y2 = _mm_sub_ps(y2, ysin2);
247 // y = _mm_sub_ps(y, ysin1);
248
249 // xmm1 = _mm_add_ps(ysin1, ysin2);
250 // xmm2 = _mm_add_ps(y, y2);
251
252 // // update the sign
253 // (
254 // _mm_xor_ps(xmm1, sign_bit_sin),
255 // _mm_xor_ps(xmm2, sign_bit_cos),
256 // )
257 // }
258
259 #[test]
260 fn test_sse2_m128_sin() {
261 use crate::core::traits::vector::*;
262 use core::f32::consts::PI;
263
264 fn test_sse2_m128_sin_angle(a: f32) {
265 let v = unsafe { m128_sin(_mm_set_ps1(a)) };
266 let v = v.as_ref_xyzw();
267 let a_sin = a.sin();
268 // dbg!((a, a_sin, v));
269 assert!(v.abs_diff_eq(Vector::splat(a_sin), 1e-6));
270 }
271
272 let mut a = -PI;
273 let end = PI;
274 let step = PI / 8192.0;
275
276 while a <= end {
277 test_sse2_m128_sin_angle(a);
278 a += step;
279 }
280 }