| 1 | #[cfg(target_arch = "x86")] |
| 2 | use core::arch::x86::*; |
| 3 | #[cfg(target_arch = "x86_64")] |
| 4 | use core::arch::x86_64::*; |
| 5 | |
| 6 | union UnionCast { |
| 7 | u32x4: [u32; 4], |
| 8 | f32x4: [f32; 4], |
| 9 | m128: __m128, |
| 10 | } |
| 11 | |
| 12 | pub const fn m128_from_f32x4(f32x4: [f32; 4]) -> __m128 { |
| 13 | unsafe { UnionCast { f32x4 }.m128 } |
| 14 | } |
| 15 | |
| 16 | const fn m128_from_u32x4(u32x4: [u32; 4]) -> __m128 { |
| 17 | unsafe { UnionCast { u32x4 }.m128 } |
| 18 | } |
| 19 | |
| 20 | const PS_INV_SIGN_MASK: __m128 = m128_from_u32x4([!0x8000_0000; 4]); |
| 21 | const PS_SIGN_MASK: __m128 = m128_from_u32x4([0x8000_0000; 4]); |
| 22 | const PS_NO_FRACTION: __m128 = m128_from_f32x4([8388608.0; 4]); |
| 23 | const PS_NEGATIVE_ZERO: __m128 = m128_from_u32x4([0x8000_0000; 4]); |
| 24 | const PS_PI: __m128 = m128_from_f32x4([core::f32::consts::PI; 4]); |
| 25 | const PS_HALF_PI: __m128 = m128_from_f32x4([core::f32::consts::FRAC_PI_2; 4]); |
| 26 | const PS_SIN_COEFFICIENTS0: __m128 = |
| 27 | m128_from_f32x4([-0.16666667, 0.008_333_331, -0.00019840874, 2.752_556_2e-6]); |
| 28 | const PS_SIN_COEFFICIENTS1: __m128 = m128_from_f32x4([ |
| 29 | -2.388_985_9e-8, |
| 30 | -0.16665852, /*Est1*/ |
| 31 | 0.008_313_95, /*Est2*/ |
| 32 | -0.000_185_246_7, /*Est3*/ |
| 33 | ]); |
| 34 | const PS_ONE: __m128 = m128_from_f32x4([1.0; 4]); |
| 35 | const PS_TWO_PI: __m128 = m128_from_f32x4([core::f32::consts::TAU; 4]); |
| 36 | const PS_RECIPROCAL_TWO_PI: __m128 = m128_from_f32x4([0.159_154_94; 4]); |
| 37 | |
| 38 | /// Calculates the vector 3 dot product and returns answer in x lane of __m128. |
| 39 | #[inline(always)] |
| 40 | pub(crate) unsafe fn dot3_in_x(lhs: __m128, rhs: __m128) -> __m128 { |
| 41 | let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs); |
| 42 | let y2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_01); |
| 43 | let z2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_10); |
| 44 | let x2y2_0_0_0 = _mm_add_ss(x2_y2_z2_w2, y2_0_0_0); |
| 45 | _mm_add_ss(x2y2_0_0_0, z2_0_0_0) |
| 46 | } |
| 47 | |
| 48 | /// Calculates the vector 4 dot product and returns answer in x lane of __m128. |
| 49 | #[inline(always)] |
| 50 | pub(crate) unsafe fn dot4_in_x(lhs: __m128, rhs: __m128) -> __m128 { |
| 51 | let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs); |
| 52 | let z2_w2_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_11_10); |
| 53 | let x2z2_y2w2_0_0 = _mm_add_ps(x2_y2_z2_w2, z2_w2_0_0); |
| 54 | let y2w2_0_0_0 = _mm_shuffle_ps(x2z2_y2w2_0_0, x2z2_y2w2_0_0, 0b00_00_00_01); |
| 55 | _mm_add_ps(x2z2_y2w2_0_0, y2w2_0_0_0) |
| 56 | } |
| 57 | |
| 58 | #[inline] |
| 59 | pub(crate) unsafe fn dot3(lhs: __m128, rhs: __m128) -> f32 { |
| 60 | _mm_cvtss_f32(dot3_in_x(lhs, rhs)) |
| 61 | } |
| 62 | |
| 63 | #[inline] |
| 64 | pub(crate) unsafe fn dot3_into_m128(lhs: __m128, rhs: __m128) -> __m128 { |
| 65 | let dot_in_x = dot3_in_x(lhs, rhs); |
| 66 | _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00) |
| 67 | } |
| 68 | |
| 69 | #[inline] |
| 70 | pub(crate) unsafe fn dot4(lhs: __m128, rhs: __m128) -> f32 { |
| 71 | _mm_cvtss_f32(dot4_in_x(lhs, rhs)) |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | pub(crate) unsafe fn dot4_into_m128(lhs: __m128, rhs: __m128) -> __m128 { |
| 76 | let dot_in_x = dot4_in_x(lhs, rhs); |
| 77 | _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00) |
| 78 | } |
| 79 | |
| 80 | #[inline] |
| 81 | pub(crate) unsafe fn m128_floor(v: __m128) -> __m128 { |
| 82 | // Based on https://github.com/microsoft/DirectXMath `XMVectorFloor` |
| 83 | // To handle NAN, INF and numbers greater than 8388608, use masking |
| 84 | let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK)); |
| 85 | let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION)); |
| 86 | // Truncate |
| 87 | let vint = _mm_cvttps_epi32(v); |
| 88 | let result = _mm_cvtepi32_ps(vint); |
| 89 | let larger = _mm_cmpgt_ps(result, v); |
| 90 | // 0 -> 0, 0xffffffff -> -1.0f |
| 91 | let larger = _mm_cvtepi32_ps(_mm_castps_si128(larger)); |
| 92 | let result = _mm_add_ps(result, larger); |
| 93 | // All numbers less than 8388608 will use the round to int |
| 94 | let result = _mm_and_ps(result, _mm_castsi128_ps(test)); |
| 95 | // All others, use the ORIGINAL value |
| 96 | let test = _mm_andnot_si128(test, _mm_castps_si128(v)); |
| 97 | _mm_or_ps(result, _mm_castsi128_ps(test)) |
| 98 | } |
| 99 | |
| 100 | #[inline] |
| 101 | pub(crate) unsafe fn m128_ceil(v: __m128) -> __m128 { |
| 102 | // Based on https://github.com/microsoft/DirectXMath `XMVectorCeil` |
| 103 | // To handle NAN, INF and numbers greater than 8388608, use masking |
| 104 | let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK)); |
| 105 | let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION)); |
| 106 | // Truncate |
| 107 | let vint = _mm_cvttps_epi32(v); |
| 108 | let result = _mm_cvtepi32_ps(vint); |
| 109 | let smaller = _mm_cmplt_ps(result, v); |
| 110 | // 0 -> 0, 0xffffffff -> -1.0f |
| 111 | let smaller = _mm_cvtepi32_ps(_mm_castps_si128(smaller)); |
| 112 | let result = _mm_sub_ps(result, smaller); |
| 113 | // All numbers less than 8388608 will use the round to int |
| 114 | let result = _mm_and_ps(result, _mm_castsi128_ps(test)); |
| 115 | // All others, use the ORIGINAL value |
| 116 | let test = _mm_andnot_si128(test, _mm_castps_si128(v)); |
| 117 | _mm_or_ps(result, _mm_castsi128_ps(test)) |
| 118 | } |
| 119 | |
| 120 | #[inline] |
| 121 | pub(crate) unsafe fn m128_abs(v: __m128) -> __m128 { |
| 122 | _mm_and_ps(v, _mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff))) |
| 123 | } |
| 124 | |
| 125 | #[inline(always)] |
| 126 | pub(crate) unsafe fn m128_mul_add(a: __m128, b: __m128, c: __m128) -> __m128 { |
| 127 | // Only enable fused multiply-adds here if "fast-math" is enabled and the |
| 128 | // platform supports it. Otherwise this may break cross-platform determinism. |
| 129 | #[cfg(all(feature = "fast-math", target_feature = "fma"))] |
| 130 | { |
| 131 | _mm_fmadd_ps(a, b, c) |
| 132 | } |
| 133 | |
| 134 | #[cfg(any(not(feature = "fast-math"), not(target_feature = "fma")))] |
| 135 | { |
| 136 | _mm_add_ps(_mm_mul_ps(a, b), c) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[inline(always)] |
| 141 | pub(crate) unsafe fn m128_neg_mul_sub(a: __m128, b: __m128, c: __m128) -> __m128 { |
| 142 | _mm_sub_ps(c, _mm_mul_ps(a, b)) |
| 143 | } |
| 144 | |
| 145 | #[inline] |
| 146 | pub(crate) unsafe fn m128_round(v: __m128) -> __m128 { |
| 147 | // Based on https://github.com/microsoft/DirectXMath `XMVectorRound` |
| 148 | let sign = _mm_and_ps(v, PS_SIGN_MASK); |
| 149 | let s_magic = _mm_or_ps(PS_NO_FRACTION, sign); |
| 150 | let r1 = _mm_add_ps(v, s_magic); |
| 151 | let r1 = _mm_sub_ps(r1, s_magic); |
| 152 | let r2 = _mm_and_ps(v, PS_INV_SIGN_MASK); |
| 153 | let mask = _mm_cmple_ps(r2, PS_NO_FRACTION); |
| 154 | let r2 = _mm_andnot_ps(mask, v); |
| 155 | let r1 = _mm_and_ps(r1, mask); |
| 156 | _mm_xor_ps(r1, r2) |
| 157 | } |
| 158 | |
| 159 | /// Returns a vector whose components are the corresponding components of Angles modulo 2PI. |
| 160 | #[inline] |
| 161 | pub(crate) unsafe fn m128_mod_angles(angles: __m128) -> __m128 { |
| 162 | // Based on https://github.com/microsoft/DirectXMath `XMVectorModAngles` |
| 163 | let v = _mm_mul_ps(angles, PS_RECIPROCAL_TWO_PI); |
| 164 | let v = m128_round(v); |
| 165 | m128_neg_mul_sub(PS_TWO_PI, v, angles) |
| 166 | } |
| 167 | |
| 168 | /// Computes the sine of the angle in each lane of `v`. Values outside |
| 169 | /// the bounds of PI may produce an increasing error as the input angle |
| 170 | /// drifts from `[-PI, PI]`. |
| 171 | #[inline] |
| 172 | pub(crate) unsafe fn m128_sin(v: __m128) -> __m128 { |
| 173 | // Based on https://github.com/microsoft/DirectXMath `XMVectorSin` |
| 174 | |
| 175 | // 11-degree minimax approximation |
| 176 | |
| 177 | // Force the value within the bounds of pi |
| 178 | let mut x = m128_mod_angles(v); |
| 179 | |
| 180 | // Map in [-pi/2,pi/2] with sin(y) = sin(x). |
| 181 | let sign = _mm_and_ps(x, PS_NEGATIVE_ZERO); |
| 182 | // pi when x >= 0, -pi when x < 0 |
| 183 | let c = _mm_or_ps(PS_PI, sign); |
| 184 | // |x| |
| 185 | let absx = _mm_andnot_ps(sign, x); |
| 186 | let rflx = _mm_sub_ps(c, x); |
| 187 | let comp = _mm_cmple_ps(absx, PS_HALF_PI); |
| 188 | let select0 = _mm_and_ps(comp, x); |
| 189 | let select1 = _mm_andnot_ps(comp, rflx); |
| 190 | x = _mm_or_ps(select0, select1); |
| 191 | |
| 192 | let x2 = _mm_mul_ps(x, x); |
| 193 | |
| 194 | // Compute polynomial approximation |
| 195 | const SC1: __m128 = PS_SIN_COEFFICIENTS1; |
| 196 | let v_constants_b = _mm_shuffle_ps(SC1, SC1, 0b00_00_00_00); |
| 197 | |
| 198 | const SC0: __m128 = PS_SIN_COEFFICIENTS0; |
| 199 | let mut v_constants = _mm_shuffle_ps(SC0, SC0, 0b11_11_11_11); |
| 200 | let mut result = m128_mul_add(v_constants_b, x2, v_constants); |
| 201 | |
| 202 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b10_10_10_10); |
| 203 | result = m128_mul_add(result, x2, v_constants); |
| 204 | |
| 205 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b01_01_01_01); |
| 206 | result = m128_mul_add(result, x2, v_constants); |
| 207 | |
| 208 | v_constants = _mm_shuffle_ps(SC0, SC0, 0b00_00_00_00); |
| 209 | result = m128_mul_add(result, x2, v_constants); |
| 210 | |
| 211 | result = m128_mul_add(result, x2, PS_ONE); |
| 212 | result = _mm_mul_ps(result, x); |
| 213 | |
| 214 | result |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn test_sse2_m128_sin() { |
| 219 | use crate::Vec4; |
| 220 | use core::f32::consts::PI; |
| 221 | |
| 222 | fn test_sse2_m128_sin_angle(a: f32) { |
| 223 | let v = unsafe { m128_sin(_mm_set_ps1(a)) }; |
| 224 | let v = Vec4(v); |
| 225 | let a_sin = a.sin(); |
| 226 | // dbg!((a, a_sin, v)); |
| 227 | assert!(v.abs_diff_eq(Vec4::splat(a_sin), 1e-6)); |
| 228 | } |
| 229 | |
| 230 | let mut a = -PI; |
| 231 | let end = PI; |
| 232 | let step = PI / 8192.0; |
| 233 | |
| 234 | while a <= end { |
| 235 | test_sse2_m128_sin_angle(a); |
| 236 | a += step; |
| 237 | } |
| 238 | } |