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