1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
#[repr(C)]
union UnionCast {
pub m128: __m128,
pub m128i: __m128i,
pub f32x4: [f32; 4],
pub i32x4: [i32; 4],
pub u32x4: [u32; 4],
}
macro_rules! _ps_const_ty {
($name:ident, $field:ident, $x:expr) => {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
const $name: UnionCast = UnionCast {
$field: [$x, $x, $x, $x],
};
};
($name:ident, $field:ident, $x:expr, $y:expr, $z:expr, $w:expr) => {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
const $name: UnionCast = UnionCast {
$field: [$x, $y, $z, $w],
};
};
}
_ps_const_ty!(PS_INV_SIGN_MASK, u32x4, !0x8000_0000);
_ps_const_ty!(PS_SIGN_MASK, u32x4, 0x8000_0000);
_ps_const_ty!(PS_NO_FRACTION, f32x4, 8388608.0);
_ps_const_ty!(PS_NEGATIVE_ZERO, u32x4, 0x80000000);
_ps_const_ty!(PS_PI, f32x4, core::f32::consts::PI);
_ps_const_ty!(PS_HALF_PI, f32x4, core::f32::consts::FRAC_PI_2);
_ps_const_ty!(
PS_SIN_COEFFICIENTS0,
f32x4,
-0.16666667,
0.008_333_331,
-0.00019840874,
2.752_556_2e-6
);
_ps_const_ty!(
PS_SIN_COEFFICIENTS1,
f32x4,
-2.388_985_9e-8,
-0.16665852,
0.008_313_95,
-0.000_185_246_7
);
_ps_const_ty!(PS_ONE, f32x4, 1.0);
_ps_const_ty!(PS_TWO_PI, f32x4, core::f32::consts::PI * 2.0);
_ps_const_ty!(PS_RECIPROCAL_TWO_PI, f32x4, 0.159_154_94);
#[inline]
pub(crate) unsafe fn m128_abs(v: __m128) -> __m128 {
_mm_and_ps(v, _mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff)))
}
#[inline]
pub(crate) unsafe fn m128_round(v: __m128) -> __m128 {
let sign = _mm_and_ps(v, PS_SIGN_MASK.m128);
let s_magic = _mm_or_ps(PS_NO_FRACTION.m128, sign);
let r1 = _mm_add_ps(v, s_magic);
let r1 = _mm_sub_ps(r1, s_magic);
let r2 = _mm_and_ps(v, PS_INV_SIGN_MASK.m128);
let mask = _mm_cmple_ps(r2, PS_NO_FRACTION.m128);
let r2 = _mm_andnot_ps(mask, v);
let r1 = _mm_and_ps(r1, mask);
_mm_xor_ps(r1, r2)
}
#[inline]
pub(crate) unsafe fn m128_floor(v: __m128) -> __m128 {
let test = _mm_and_si128(_mm_castps_si128(v), PS_INV_SIGN_MASK.m128i);
let test = _mm_cmplt_epi32(test, PS_NO_FRACTION.m128i);
let vint = _mm_cvttps_epi32(v);
let result = _mm_cvtepi32_ps(vint);
let larger = _mm_cmpgt_ps(result, v);
let larger = _mm_cvtepi32_ps(_mm_castps_si128(larger));
let result = _mm_add_ps(result, larger);
let result = _mm_and_ps(result, _mm_castsi128_ps(test));
let test = _mm_andnot_si128(test, _mm_castps_si128(v));
_mm_or_ps(result, _mm_castsi128_ps(test))
}
#[inline]
pub(crate) unsafe fn m128_ceil(v: __m128) -> __m128 {
let test = _mm_and_si128(_mm_castps_si128(v), PS_INV_SIGN_MASK.m128i);
let test = _mm_cmplt_epi32(test, PS_NO_FRACTION.m128i);
let vint = _mm_cvttps_epi32(v);
let result = _mm_cvtepi32_ps(vint);
let smaller = _mm_cmplt_ps(result, v);
let smaller = _mm_cvtepi32_ps(_mm_castps_si128(smaller));
let result = _mm_sub_ps(result, smaller);
let result = _mm_and_ps(result, _mm_castsi128_ps(test));
let test = _mm_andnot_si128(test, _mm_castps_si128(v));
_mm_or_ps(result, _mm_castsi128_ps(test))
}
#[inline(always)]
pub(crate) unsafe fn m128_mul_add(a: __m128, b: __m128, c: __m128) -> __m128 {
#[cfg(target_feature = "fma")]
{
_mm_fmadd_ps(a, b, c)
}
#[cfg(not(target_feature = "fma"))]
{
_mm_add_ps(_mm_mul_ps(a, b), c)
}
}
#[inline(always)]
pub(crate) unsafe fn m128_neg_mul_sub(a: __m128, b: __m128, c: __m128) -> __m128 {
_mm_sub_ps(c, _mm_mul_ps(a, b))
}
#[inline]
pub(crate) unsafe fn m128_mod_angles(angles: __m128) -> __m128 {
let v = _mm_mul_ps(angles, PS_RECIPROCAL_TWO_PI.m128);
let v = m128_round(v);
m128_neg_mul_sub(PS_TWO_PI.m128, v, angles)
}
#[inline]
pub(crate) unsafe fn m128_sin(v: __m128) -> __m128 {
let mut x = m128_mod_angles(v);
let sign = _mm_and_ps(x, PS_NEGATIVE_ZERO.m128);
let c = _mm_or_ps(PS_PI.m128, sign);
let absx = _mm_andnot_ps(sign, x);
let rflx = _mm_sub_ps(c, x);
let comp = _mm_cmple_ps(absx, PS_HALF_PI.m128);
let select0 = _mm_and_ps(comp, x);
let select1 = _mm_andnot_ps(comp, rflx);
x = _mm_or_ps(select0, select1);
let x2 = _mm_mul_ps(x, x);
const SC1: __m128 = unsafe { PS_SIN_COEFFICIENTS1.m128 };
let v_constants_b = _mm_shuffle_ps(SC1, SC1, 0b00_00_00_00);
const SC0: __m128 = unsafe { PS_SIN_COEFFICIENTS0.m128 };
let mut v_constants = _mm_shuffle_ps(SC0, SC0, 0b11_11_11_11);
let mut result = m128_mul_add(v_constants_b, x2, v_constants);
v_constants = _mm_shuffle_ps(SC0, SC0, 0b10_10_10_10);
result = m128_mul_add(result, x2, v_constants);
v_constants = _mm_shuffle_ps(SC0, SC0, 0b01_01_01_01);
result = m128_mul_add(result, x2, v_constants);
v_constants = _mm_shuffle_ps(SC0, SC0, 0b00_00_00_00);
result = m128_mul_add(result, x2, v_constants);
result = m128_mul_add(result, x2, PS_ONE.m128);
result = _mm_mul_ps(result, x);
result
}
#[test]
fn test_sse2_m128_sin() {
use crate::core::traits::vector::*;
use core::f32::consts::PI;
fn test_sse2_m128_sin_angle(a: f32) {
let v = unsafe { m128_sin(_mm_set_ps1(a)) };
let v = v.as_ref_xyzw();
let a_sin = a.sin();
assert!(v.abs_diff_eq(Vector::splat(a_sin), 1e-6));
}
let mut a = -PI;
let end = PI;
let step = PI / 8192.0;
while a <= end {
test_sse2_m128_sin_angle(a);
a += step;
}
}