1#[cfg(target_arch = "x86")]
2use core::arch::x86::*;
3#[cfg(target_arch = "x86_64")]
4use core::arch::x86_64::*;
5
6#[repr(C)]
7union UnionCast {
8 u32x4: [u32; 4],
9 f32x4: [f32; 4],
10 m128: __m128,
11}
12
13pub const fn m128_from_f32x4(f32x4: [f32; 4]) -> __m128 {
14 unsafe { UnionCast { f32x4 }.m128 }
15}
16
17const fn m128_from_u32x4(u32x4: [u32; 4]) -> __m128 {
18 unsafe { UnionCast { u32x4 }.m128 }
19}
20
21const PS_ABS_MASK: __m128 = m128_from_u32x4([0x7fffffff; 4]);
22const PS_INV_SIGN_MASK: __m128 = m128_from_u32x4([!0x8000_0000; 4]);
23const PS_SIGN_MASK: __m128 = m128_from_u32x4([0x8000_0000; 4]);
24const PS_NO_FRACTION: __m128 = m128_from_f32x4([8388608.0; 4]);
25const PS_NEGATIVE_ZERO: __m128 = m128_from_u32x4([0x8000_0000; 4]);
26const PS_PI: __m128 = m128_from_f32x4([core::f32::consts::PI; 4]);
27const PS_HALF_PI: __m128 = m128_from_f32x4([core::f32::consts::FRAC_PI_2; 4]);
28const PS_SIN_COEFFICIENTS0: __m128 =
29 m128_from_f32x4([-0.16666667, 0.008_333_331, -0.00019840874, 2.752_556_2e-6]);
30const PS_SIN_COEFFICIENTS1: __m128 = m128_from_f32x4([
31 -2.388_985_9e-8,
32 -0.16665852, 0.008_313_95, -0.000_185_246_7, ]);
36const PS_ONE: __m128 = m128_from_f32x4([1.0; 4]);
37const PS_TWO_PI: __m128 = m128_from_f32x4([core::f32::consts::TAU; 4]);
38const PS_RECIPROCAL_TWO_PI: __m128 = m128_from_f32x4([0.159_154_94; 4]);
39
40#[inline(always)]
42pub(crate) unsafe fn dot3_in_x(lhs: __m128, rhs: __m128) -> __m128 {
43 let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs);
44 let y2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_01);
45 let z2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_10);
46 let x2y2_0_0_0 = _mm_add_ss(x2_y2_z2_w2, y2_0_0_0);
47 _mm_add_ss(x2y2_0_0_0, z2_0_0_0)
48}
49
50#[inline(always)]
52pub(crate) unsafe fn dot4_in_x(lhs: __m128, rhs: __m128) -> __m128 {
53 let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs);
54 let z2_w2_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_11_10);
55 let x2z2_y2w2_0_0 = _mm_add_ps(x2_y2_z2_w2, z2_w2_0_0);
56 let y2w2_0_0_0 = _mm_shuffle_ps(x2z2_y2w2_0_0, x2z2_y2w2_0_0, 0b00_00_00_01);
57 _mm_add_ps(x2z2_y2w2_0_0, y2w2_0_0_0)
58}
59
60#[inline]
61pub(crate) unsafe fn dot3(lhs: __m128, rhs: __m128) -> f32 {
62 _mm_cvtss_f32(dot3_in_x(lhs, rhs))
63}
64
65#[inline]
66pub(crate) unsafe fn dot3_into_m128(lhs: __m128, rhs: __m128) -> __m128 {
67 let dot_in_x = dot3_in_x(lhs, rhs);
68 _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00)
69}
70
71#[inline]
72pub(crate) unsafe fn dot4(lhs: __m128, rhs: __m128) -> f32 {
73 _mm_cvtss_f32(dot4_in_x(lhs, rhs))
74}
75
76#[inline]
77pub(crate) unsafe fn dot4_into_m128(lhs: __m128, rhs: __m128) -> __m128 {
78 let dot_in_x = dot4_in_x(lhs, rhs);
79 _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00)
80}
81
82#[inline]
83pub(crate) unsafe fn m128_floor(v: __m128) -> __m128 {
84 let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK));
87 let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION));
88 let vint = _mm_cvttps_epi32(v);
90 let result = _mm_cvtepi32_ps(vint);
91 let larger = _mm_cmpgt_ps(result, v);
92 let larger = _mm_cvtepi32_ps(_mm_castps_si128(larger));
94 let result = _mm_add_ps(result, larger);
95 let result = _mm_and_ps(result, _mm_castsi128_ps(test));
97 let test = _mm_andnot_si128(test, _mm_castps_si128(v));
99 _mm_or_ps(result, _mm_castsi128_ps(test))
100}
101
102#[inline]
103pub(crate) unsafe fn m128_ceil(v: __m128) -> __m128 {
104 let test = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_INV_SIGN_MASK));
107 let test = _mm_cmplt_epi32(test, _mm_castps_si128(PS_NO_FRACTION));
108 let vint = _mm_cvttps_epi32(v);
110 let result = _mm_cvtepi32_ps(vint);
111 let smaller = _mm_cmplt_ps(result, v);
112 let smaller = _mm_cvtepi32_ps(_mm_castps_si128(smaller));
114 let result = _mm_sub_ps(result, smaller);
115 let result = _mm_and_ps(result, _mm_castsi128_ps(test));
117 let test = _mm_andnot_si128(test, _mm_castps_si128(v));
119 _mm_or_ps(result, _mm_castsi128_ps(test))
120}
121
122#[inline]
123pub(crate) unsafe fn m128_abs(v: __m128) -> __m128 {
124 _mm_and_ps(v, _mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff)))
125}
126
127#[inline(always)]
128pub(crate) unsafe fn m128_mul_add(a: __m128, b: __m128, c: __m128) -> __m128 {
129 #[cfg(all(feature = "fast-math", target_feature = "fma"))]
132 {
133 _mm_fmadd_ps(a, b, c)
134 }
135
136 #[cfg(any(not(feature = "fast-math"), not(target_feature = "fma")))]
137 {
138 _mm_add_ps(_mm_mul_ps(a, b), c)
139 }
140}
141
142#[inline(always)]
143pub(crate) unsafe fn m128_neg_mul_sub(a: __m128, b: __m128, c: __m128) -> __m128 {
144 _mm_sub_ps(c, _mm_mul_ps(a, b))
145}
146
147#[inline]
148pub(crate) unsafe fn m128_round(v: __m128) -> __m128 {
149 let sign = _mm_and_ps(v, PS_SIGN_MASK);
151 let s_magic = _mm_or_ps(PS_NO_FRACTION, sign);
152 let r1 = _mm_add_ps(v, s_magic);
153 let r1 = _mm_sub_ps(r1, s_magic);
154 let r2 = _mm_and_ps(v, PS_INV_SIGN_MASK);
155 let mask = _mm_cmple_ps(r2, PS_NO_FRACTION);
156 let r2 = _mm_andnot_ps(mask, v);
157 let r1 = _mm_and_ps(r1, mask);
158 _mm_xor_ps(r1, r2)
159}
160
161#[inline]
162pub(crate) unsafe fn m128_trunc(v: __m128) -> __m128 {
163 let mut vtest = _mm_and_si128(_mm_castps_si128(v), _mm_castps_si128(PS_ABS_MASK));
167 vtest = _mm_cmplt_epi32(vtest, _mm_castps_si128(PS_NO_FRACTION));
169 let vint = _mm_cvttps_epi32(v);
171 let mut vresult = _mm_cvtepi32_ps(vint);
173 vresult = _mm_and_ps(vresult, _mm_castsi128_ps(vtest));
175 vtest = _mm_andnot_si128(vtest, _mm_castps_si128(v));
177 _mm_or_ps(vresult, _mm_castsi128_ps(vtest))
178}
179
180#[inline]
182pub(crate) unsafe fn m128_mod_angles(angles: __m128) -> __m128 {
183 let v = _mm_mul_ps(angles, PS_RECIPROCAL_TWO_PI);
185 let v = m128_round(v);
186 m128_neg_mul_sub(PS_TWO_PI, v, angles)
187}
188
189#[inline]
193pub(crate) unsafe fn m128_sin(v: __m128) -> __m128 {
194 let mut x = m128_mod_angles(v);
200
201 let sign = _mm_and_ps(x, PS_NEGATIVE_ZERO);
203 let c = _mm_or_ps(PS_PI, sign);
205 let absx = _mm_andnot_ps(sign, x);
207 let rflx = _mm_sub_ps(c, x);
208 let comp = _mm_cmple_ps(absx, PS_HALF_PI);
209 let select0 = _mm_and_ps(comp, x);
210 let select1 = _mm_andnot_ps(comp, rflx);
211 x = _mm_or_ps(select0, select1);
212
213 let x2 = _mm_mul_ps(x, x);
214
215 const SC1: __m128 = PS_SIN_COEFFICIENTS1;
217 let v_constants_b = _mm_shuffle_ps(SC1, SC1, 0b00_00_00_00);
218
219 const SC0: __m128 = PS_SIN_COEFFICIENTS0;
220 let mut v_constants = _mm_shuffle_ps(SC0, SC0, 0b11_11_11_11);
221 let mut result = m128_mul_add(v_constants_b, x2, v_constants);
222
223 v_constants = _mm_shuffle_ps(SC0, SC0, 0b10_10_10_10);
224 result = m128_mul_add(result, x2, v_constants);
225
226 v_constants = _mm_shuffle_ps(SC0, SC0, 0b01_01_01_01);
227 result = m128_mul_add(result, x2, v_constants);
228
229 v_constants = _mm_shuffle_ps(SC0, SC0, 0b00_00_00_00);
230 result = m128_mul_add(result, x2, v_constants);
231
232 result = m128_mul_add(result, x2, PS_ONE);
233 result = _mm_mul_ps(result, x);
234
235 result
236}
237
238#[test]
239fn test_sse2_m128_sin() {
240 use crate::Vec4;
241 use core::f32::consts::PI;
242
243 fn test_sse2_m128_sin_angle(a: f32) {
244 let v = unsafe { m128_sin(_mm_set_ps1(a)) };
245 let v = Vec4(v);
246 let a_sin = a.sin();
247 assert!(v.abs_diff_eq(Vec4::splat(a_sin), 1e-6));
249 }
250
251 let mut a = -PI;
252 let end = PI;
253 let step = PI / 8192.0;
254
255 while a <= end {
256 test_sse2_m128_sin_angle(a);
257 a += step;
258 }
259}