| 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 | #[cfg(target_feature = "fma")] |
| 92 | { |
| 93 | _mm_fmadd_ps(a, b, c) |
| 94 | } |
| 95 | |
| 96 | #[cfg(not(target_feature = "fma"))] |
| 97 | { |
| 98 | _mm_add_ps(_mm_mul_ps(a, b), c) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[inline(always)] |
| 103 | pub(crate) unsafe fn m128_neg_mul_sub(a: __m128, b: __m128, c: __m128) -> __m128 { |
| 104 | _mm_sub_ps(c, _mm_mul_ps(a, b)) |
| 105 | } |
| 106 | |
| 107 | /// Returns a vector whose components are the corresponding components of Angles modulo 2PI. |
| 108 | #[inline] |
| 109 | pub(crate) unsafe fn m128_mod_angles(angles: __m128) -> __m128 { |
| 110 | // Based on https://github.com/microsoft/DirectXMath `XMVectorModAngles` |
| 111 | let v = _mm_mul_ps(angles, PS_RECIPROCAL_TWO_PI); |
| 112 | let v = m128_round(v); |
| 113 | m128_neg_mul_sub(PS_TWO_PI, v, angles) |
| 114 | } |
| 115 | |
| 116 | /// Computes the sine of the angle in each lane of `v`. Values outside |
| 117 | /// the bounds of PI may produce an increasing error as the input angle |
| 118 | /// drifts from `[-PI, PI]`. |
| 119 | #[inline] |
| 120 | pub(crate) unsafe fn m128_sin(v: __m128) -> __m128 { |
| 121 | // Based on https://github.com/microsoft/DirectXMath `XMVectorSin` |
| 122 | |
| 123 | // 11-degree minimax approximation |
| 124 | |
| 125 | // Force the value within the bounds of pi |
| 126 | let mut x = m128_mod_angles(v); |
| 127 | |
| 128 | // Map in [-pi/2,pi/2] with sin(y) = sin(x). |
| 129 | let sign = _mm_and_ps(x, PS_NEGATIVE_ZERO); |
| 130 | // pi when x >= 0, -pi when x < 0 |
| 131 | let c = _mm_or_ps(PS_PI, sign); |
| 132 | // |x| |
| 133 | let absx = _mm_andnot_ps(sign, x); |
| 134 | let rflx = _mm_sub_ps(c, x); |
| 135 | let comp = _mm_cmple_ps(absx, PS_HALF_PI); |
| 136 | let select0 = _mm_and_ps(comp, x); |
| 137 | let select1 = _mm_andnot_ps(comp, rflx); |
| 138 | x = _mm_or_ps(select0, select1); |
| 139 | |
| 140 | let x2 = _mm_mul_ps(x, x); |
| 141 | |
| 142 | // Compute polynomial approximation |
| 143 | const SC1: __m128 = PS_SIN_COEFFICIENTS1; |
| 144 | let v_constants_b = _mm_shuffle_ps(SC1, SC1, 0b00_00_00_00); |
| 145 | |
| 146 | const SC0: __m128 = PS_SIN_COEFFICIENTS0; |
| 147 | let mut v_constants = _mm_shuffle_ps(SC0, SC0, 0b11_11_11_11); |
| 148 | let mut result = m128_mul_add(v_constants_b, x2, v_constants); |
| 149 | |
| 150 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b10_10_10_10); |
| 151 | result = m128_mul_add(result, x2, v_constants); |
| 152 | |
| 153 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b01_01_01_01); |
| 154 | result = m128_mul_add(result, x2, v_constants); |
| 155 | |
| 156 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b00_00_00_00); |
| 157 | result = m128_mul_add(result, x2, v_constants); |
| 158 | |
| 159 | result = m128_mul_add(result, x2, PS_ONE); |
| 160 | result = _mm_mul_ps(result, x); |
| 161 | |
| 162 | result |
| 163 | } |
| 164 | |
| 165 | // Based on http://gruntthepeon.free.fr/ssemath/sse_mathfun.h |
| 166 | // #[cfg(target_feature = "sse2")] |
| 167 | // unsafe fn sin_cos_sse2(x: __m128) -> (__m128, __m128) { |
| 168 | // let mut sign_bit_sin = x; |
| 169 | // // take the absolute value |
| 170 | // let mut x = _mm_and_ps(x, PS_INV_SIGN_MASK.m128); |
| 171 | // // extract the sign bit (upper one) |
| 172 | // sign_bit_sin = _mm_and_ps(sign_bit_sin, PS_SIGN_MASK.m128); |
| 173 | |
| 174 | // // scale by 4/Pi |
| 175 | // let mut y = _mm_mul_ps(x, PS_CEPHES_FOPI.m128); |
| 176 | |
| 177 | // // store the integer part of y in emm2 |
| 178 | // let mut emm2 = _mm_cvttps_epi32(y); |
| 179 | |
| 180 | // // j=(j+1) & (~1) (see the cephes sources) |
| 181 | // emm2 = _mm_add_epi32(emm2, PI32_1.m128i); |
| 182 | // emm2 = _mm_and_si128(emm2, PI32_INV_1.m128i); |
| 183 | // y = _mm_cvtepi32_ps(emm2); |
| 184 | |
| 185 | // let mut emm4 = emm2; |
| 186 | |
| 187 | // /* get the swap sign flag for the sine */ |
| 188 | // let mut emm0 = _mm_and_si128(emm2, PI32_4.m128i); |
| 189 | // emm0 = _mm_slli_epi32(emm0, 29); |
| 190 | // let swap_sign_bit_sin = _mm_castsi128_ps(emm0); |
| 191 | |
| 192 | // /* get the polynom selection mask for the sine*/ |
| 193 | // emm2 = _mm_and_si128(emm2, PI32_2.m128i); |
| 194 | // emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128()); |
| 195 | // let poly_mask = _mm_castsi128_ps(emm2); |
| 196 | |
| 197 | // /* The magic pass: "Extended precision modular arithmetic" |
| 198 | // x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 199 | // let mut xmm1 = PS_MINUS_CEPHES_DP1.m128; |
| 200 | // let mut xmm2 = PS_MINUS_CEPHES_DP2.m128; |
| 201 | // let mut xmm3 = PS_MINUS_CEPHES_DP3.m128; |
| 202 | // xmm1 = _mm_mul_ps(y, xmm1); |
| 203 | // xmm2 = _mm_mul_ps(y, xmm2); |
| 204 | // xmm3 = _mm_mul_ps(y, xmm3); |
| 205 | // x = _mm_add_ps(x, xmm1); |
| 206 | // x = _mm_add_ps(x, xmm2); |
| 207 | // x = _mm_add_ps(x, xmm3); |
| 208 | |
| 209 | // emm4 = _mm_sub_epi32(emm4, PI32_2.m128i); |
| 210 | // emm4 = _mm_andnot_si128(emm4, PI32_4.m128i); |
| 211 | // emm4 = _mm_slli_epi32(emm4, 29); |
| 212 | // let sign_bit_cos = _mm_castsi128_ps(emm4); |
| 213 | |
| 214 | // sign_bit_sin = _mm_xor_ps(sign_bit_sin, swap_sign_bit_sin); |
| 215 | |
| 216 | // // Evaluate the first polynom (0 <= x <= Pi/4) |
| 217 | // let z = _mm_mul_ps(x, x); |
| 218 | // y = PS_COSCOF_P0.m128; |
| 219 | |
| 220 | // y = _mm_mul_ps(y, z); |
| 221 | // y = _mm_add_ps(y, PS_COSCOF_P1.m128); |
| 222 | // y = _mm_mul_ps(y, z); |
| 223 | // y = _mm_add_ps(y, PS_COSCOF_P2.m128); |
| 224 | // y = _mm_mul_ps(y, z); |
| 225 | // y = _mm_mul_ps(y, z); |
| 226 | // let tmp = _mm_mul_ps(z, PS_0_5.m128); |
| 227 | // y = _mm_sub_ps(y, tmp); |
| 228 | // y = _mm_add_ps(y, PS_1_0.m128); |
| 229 | |
| 230 | // // Evaluate the second polynom (Pi/4 <= x <= 0) |
| 231 | // let mut y2 = PS_SINCOF_P0.m128; |
| 232 | // y2 = _mm_mul_ps(y2, z); |
| 233 | // y2 = _mm_add_ps(y2, PS_SINCOF_P1.m128); |
| 234 | // y2 = _mm_mul_ps(y2, z); |
| 235 | // y2 = _mm_add_ps(y2, PS_SINCOF_P2.m128); |
| 236 | // y2 = _mm_mul_ps(y2, z); |
| 237 | // y2 = _mm_mul_ps(y2, x); |
| 238 | // y2 = _mm_add_ps(y2, x); |
| 239 | |
| 240 | // // select the correct result from the two polynoms |
| 241 | // xmm3 = poly_mask; |
| 242 | // let ysin2 = _mm_and_ps(xmm3, y2); |
| 243 | // let ysin1 = _mm_andnot_ps(xmm3, y); |
| 244 | // y2 = _mm_sub_ps(y2, ysin2); |
| 245 | // y = _mm_sub_ps(y, ysin1); |
| 246 | |
| 247 | // xmm1 = _mm_add_ps(ysin1, ysin2); |
| 248 | // xmm2 = _mm_add_ps(y, y2); |
| 249 | |
| 250 | // // update the sign |
| 251 | // ( |
| 252 | // _mm_xor_ps(xmm1, sign_bit_sin), |
| 253 | // _mm_xor_ps(xmm2, sign_bit_cos), |
| 254 | // ) |
| 255 | // } |
| 256 | |
| 257 | #[test] |
| 258 | fn test_sse2_m128_sin() { |
| 259 | use crate::core::traits::vector::*; |
| 260 | use core::f32::consts::PI; |
| 261 | |
| 262 | fn test_sse2_m128_sin_angle(a: f32) { |
| 263 | let v = unsafe { m128_sin(_mm_set_ps1(a)) }; |
| 264 | let v = v.as_ref_xyzw(); |
| 265 | let a_sin = a.sin(); |
| 266 | // dbg!((a, a_sin, v)); |
| 267 | assert!(v.abs_diff_eq(Vector::splat(a_sin), 1e-6)); |
| 268 | } |
| 269 | |
| 270 | let mut a = -PI; |
| 271 | let end = PI; |
| 272 | let step = PI / 8192.0; |
| 273 | |
| 274 | while a <= end { |
| 275 | test_sse2_m128_sin_angle(a); |
| 276 | a += step; |
| 277 | } |
| 278 | } |