1 #ifndef SSE2NEON_H
2 #define SSE2NEON_H
3 
4 // This header file provides a simple API translation layer
5 // between SSE intrinsics to their corresponding ARM NEON versions
6 //
7 // This header file does not (yet) translate *all* of the SSE intrinsics.
8 // Since this is in support of a specific porting effort, I have only
9 // included the intrinsics I needed to get my port to work.
10 //
11 // Questions/Comments/Feedback send to: jratcliffscarab@gmail.com
12 //
13 // If you want to improve or add to this project, send me an
14 // email and I will probably approve your access to the depot.
15 //
16 // Project is located here:
17 //
18 //	https://github.com/jratcliff63367/sse2neon
19 //
20 // Show your appreciation for open source by sending me a bitcoin tip to the following
21 // address.
22 //
23 // TipJar: 1PzgWDSyq4pmdAXRH8SPUtta4SWGrt4B1p :
24 // https://blockchain.info/address/1PzgWDSyq4pmdAXRH8SPUtta4SWGrt4B1p
25 //
26 //
27 // Contributors to this project are:
28 //
29 // John W. Ratcliff : jratcliffscarab@gmail.com
30 // Brandon Rowlett : browlett@nvidia.com
31 // Ken Fast : kfast@gdeb.com
32 // yomoma : iterentiev@nvidia.com
33 //
34 //
35 /*
36 ** The MIT license:
37 **
38 ** Permission is hereby granted, MEMALLOC_FREE of charge, to any person obtaining a copy
39 ** of this software and associated documentation files (the "Software"), to deal
40 ** in the Software without restriction, including without limitation the rights
41 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42 ** copies of the Software, and to permit persons to whom the Software is furnished
43 ** to do so, subject to the following conditions:
44 **
45 ** The above copyright notice and this permission notice shall be included in all
46 ** copies or substantial portions of the Software.
47 
48 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
52 ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
53 ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 */
55 
56 #define GCC 1
57 #define ENABLE_CPP_VERSION 0
58 
59 //yomoma: disable templated shuffle. need to find a solution for pure C
60 #ifndef __cplusplus
61 #define DISABLE_SHUFFLE 1
62 #endif
63 
64 #if GCC
65 #define FORCE_INLINE					inline __attribute__((always_inline))
66 #else
67 #define FORCE_INLINE					inline
68 #endif
69 
70 #include "arm_neon.h"
71 
72 /*******************************************************/
73 /* MACRO for shuffle parameter for _mm_shuffle_ps().   */
74 /* Argument fp3 is a digit[0123] that represents the fp*/
75 /* from argument "b" of mm_shuffle_ps that will be     */
76 /* placed in fp3 of result. fp2 is the same for fp2 in */
77 /* result. fp1 is a digit[0123] that represents the fp */
78 /* from argument "a" of mm_shuffle_ps that will be     */
79 /* places in fp1 of result. fp0 is the same for fp0 of */
80 /* result                                              */
81 /*******************************************************/
82 #define _MM_SHUFFLE(fp3,fp2,fp1,fp0) (((fp3) << 6) | ((fp2) << 4) | \
83 	((fp1) << 2) | ((fp0)))
84 
85 typedef float32x4_t __m128;
86 typedef int32x4_t __m128i;
87 
88 // ******************************************
89 // Set/get methods
90 // ******************************************
91 
92 // Extracts the lower order floating point value from the parameter https://msdn.microsoft.com/en-us/library/bb514059(v=vs.120).aspx
_mm_cvtss_f32(__m128 a)93 FORCE_INLINE float _mm_cvtss_f32(__m128 a)
94 {
95 	return vgetq_lane_f32(a, 0);
96 }
97 
98 // Sets the 128-bit value to zero https://msdn.microsoft.com/en-us/library/vstudio/ys7dw0kh(v=vs.100).aspx
_mm_setzero_si128()99 FORCE_INLINE __m128i _mm_setzero_si128()
100 {
101 	return vdupq_n_s32(0);
102 }
103 
104 // Clears the four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/tk1t2tbz(v=vs.100).aspx
_mm_setzero_ps(void)105 FORCE_INLINE __m128 _mm_setzero_ps(void)
106 {
107 	return vdupq_n_f32(0);
108 }
109 
110 // Sets the four single-precision, floating-point values to w. https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx
_mm_set1_ps(float _w)111 FORCE_INLINE __m128 _mm_set1_ps(float _w)
112 {
113 	return vdupq_n_f32(_w);
114 }
115 
116 // Sets the four single-precision, floating-point values to w. https://msdn.microsoft.com/en-us/library/vstudio/2x1se8ha(v=vs.100).aspx
_mm_set_ps1(float _w)117 FORCE_INLINE __m128 _mm_set_ps1(float _w)
118 {
119 	return vdupq_n_f32(_w);
120 }
121 
122 // Sets the four single-precision, floating-point values to the four inputs. https://msdn.microsoft.com/en-us/library/vstudio/afh0zf75(v=vs.100).aspx
_mm_set_ps(float w,float z,float y,float x)123 FORCE_INLINE __m128 _mm_set_ps(float w, float z, float y, float x)
124 {
125 	float __attribute__((aligned(16))) data[4] = { x, y, z, w };
126 	return vld1q_f32(data);
127 }
128 
129 // Sets the four single-precision, floating-point values to the four inputs in reverse order. https://msdn.microsoft.com/en-us/library/vstudio/d2172ct3(v=vs.100).aspx
_mm_setr_ps(float w,float z,float y,float x)130 FORCE_INLINE __m128 _mm_setr_ps(float w, float z , float y , float x )
131 {
132 	float __attribute__ ((aligned (16))) data[4] = { w, z, y, x };
133 	return vld1q_f32(data);
134 }
135 
136 // Sets the 4 signed 32-bit integer values to i. https://msdn.microsoft.com/en-us/library/vstudio/h4xscxat(v=vs.100).aspx
_mm_set1_epi32(int _i)137 FORCE_INLINE __m128i _mm_set1_epi32(int _i)
138 {
139 	return vdupq_n_s32(_i);
140 }
141 
_mm_set1_epi16(short _i)142 FORCE_INLINE __m128i _mm_set1_epi16(short _i)
143 {
144 	return (__m128i)vdupq_n_s16(_i);
145 }
146 
147 // Sets the 4 signed 32-bit integer values. https://msdn.microsoft.com/en-us/library/vstudio/019beekt(v=vs.100).aspx
_mm_set_epi32(int i3,int i2,int i1,int i0)148 FORCE_INLINE __m128i _mm_set_epi32(int i3, int i2, int i1, int i0)
149 {
150 	int32_t __attribute__((aligned(16))) data[4] = { i0, i1, i2, i3 };
151 	return vld1q_s32(data);
152 }
153 
154 // Stores four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/s3h4ay6y(v=vs.100).aspx
_mm_store_ps(float * p,__m128 a)155 FORCE_INLINE void _mm_store_ps(float *p, __m128 a)
156 {
157 	vst1q_f32(p, a);
158 }
159 
160 // Stores four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/44e30x22(v=vs.100).aspx
_mm_storeu_ps(float * p,__m128 a)161 FORCE_INLINE void _mm_storeu_ps(float *p, __m128 a)
162 {
163 	vst1q_f32(p, a);
164 }
165 
166 // Stores four 32-bit integer values as (as a __m128i value) at the address p. https://msdn.microsoft.com/en-us/library/vstudio/edk11s13(v=vs.100).aspx
_mm_store_si128(__m128i * p,__m128i a)167 FORCE_INLINE void _mm_store_si128(__m128i *p, __m128i a )
168 {
169 	vst1q_s32((int32_t*) p,a);
170 }
171 
172 // Stores the lower single - precision, floating - point value. https://msdn.microsoft.com/en-us/library/tzz10fbx(v=vs.100).aspx
_mm_store_ss(float * p,__m128 a)173 FORCE_INLINE void _mm_store_ss(float *p, __m128 a)
174 {
175 	vst1q_lane_f32(p, a, 0);
176 }
177 
178 // Reads the lower 64 bits of b and stores them into the lower 64 bits of a.  https://msdn.microsoft.com/en-us/library/hhwf428f%28v=vs.90%29.aspx
_mm_storel_epi64(__m128i * a,__m128i b)179 FORCE_INLINE void _mm_storel_epi64(__m128i* a, __m128i b)
180 {
181 	*a = (__m128i)vsetq_lane_s64((int64_t)vget_low_s32(b), *(int64x2_t*)a, 0);
182 }
183 
184 // Loads a single single-precision, floating-point value, copying it into all four words https://msdn.microsoft.com/en-us/library/vstudio/5cdkf716(v=vs.100).aspx
_mm_load1_ps(const float * p)185 FORCE_INLINE __m128 _mm_load1_ps(const float * p)
186 {
187 	return vld1q_dup_f32(p);
188 }
189 
190 // Loads four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/zzd50xxt(v=vs.100).aspx
_mm_load_ps(const float * p)191 FORCE_INLINE __m128 _mm_load_ps(const float * p)
192 {
193 	return vld1q_f32(p);
194 }
195 
196 // Loads four single-precision, floating-point values.  https://msdn.microsoft.com/en-us/library/x1b16s7z%28v=vs.90%29.aspx
_mm_loadu_ps(const float * p)197 FORCE_INLINE __m128 _mm_loadu_ps(const float * p)
198 {
199 	// for neon, alignment doesn't matter, so _mm_load_ps and _mm_loadu_ps are equivalent for neon
200 	return vld1q_f32(p);
201 }
202 
203 // Loads an single - precision, floating - point value into the low word and clears the upper three words.  https://msdn.microsoft.com/en-us/library/548bb9h4%28v=vs.90%29.aspx
_mm_load_ss(const float * p)204 FORCE_INLINE __m128 _mm_load_ss(const float * p)
205 {
206 	__m128 result = vdupq_n_f32(0);
207 	return vsetq_lane_f32(*p, result, 0);
208 }
209 
210 
211 // ******************************************
212 // Logic/Binary operations
213 // ******************************************
214 
215 // Compares for inequality.  https://msdn.microsoft.com/en-us/library/sf44thbx(v=vs.100).aspx
_mm_cmpneq_ps(__m128 a,__m128 b)216 FORCE_INLINE __m128 _mm_cmpneq_ps(__m128 a, __m128 b)
217 {
218 	return (__m128)vmvnq_s32((__m128i)vceqq_f32(a, b));
219 }
220 
221 // Computes the bitwise AND-NOT of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/68h7wd02(v=vs.100).aspx
_mm_andnot_ps(__m128 a,__m128 b)222 FORCE_INLINE __m128 _mm_andnot_ps(__m128 a, __m128 b)
223 {
224 	return (__m128)vbicq_s32((__m128i)b, (__m128i)a); // *NOTE* argument swap
225 }
226 
227 // Computes the bitwise AND of the 128-bit value in b and the bitwise NOT of the 128-bit value in a. https://msdn.microsoft.com/en-us/library/vstudio/1beaceh8(v=vs.100).aspx
_mm_andnot_si128(__m128i a,__m128i b)228 FORCE_INLINE __m128i _mm_andnot_si128(__m128i a, __m128i b)
229 {
230 	return (__m128i)vbicq_s32(b, a); // *NOTE* argument swap
231 }
232 
233 // Computes the bitwise AND of the 128-bit value in a and the 128-bit value in b. https://msdn.microsoft.com/en-us/library/vstudio/6d1txsa8(v=vs.100).aspx
_mm_and_si128(__m128i a,__m128i b)234 FORCE_INLINE __m128i _mm_and_si128(__m128i a, __m128i b)
235 {
236 	return (__m128i)vandq_s32(a, b);
237 }
238 
239 // Computes the bitwise AND of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/73ck1xc5(v=vs.100).aspx
_mm_and_ps(__m128 a,__m128 b)240 FORCE_INLINE __m128 _mm_and_ps(__m128 a, __m128 b)
241 {
242 	return (__m128)vandq_s32((__m128i)a, (__m128i)b);
243 }
244 
245 // Computes the bitwise OR of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/7ctdsyy0(v=vs.100).aspx
_mm_or_ps(__m128 a,__m128 b)246 FORCE_INLINE __m128 _mm_or_ps(__m128 a, __m128 b)
247 {
248 	return (__m128)vorrq_s32((__m128i)a, (__m128i)b);
249 }
250 
251 // Computes bitwise EXOR (exclusive-or) of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/ss6k3wk8(v=vs.100).aspx
_mm_xor_ps(__m128 a,__m128 b)252 FORCE_INLINE __m128 _mm_xor_ps(__m128 a, __m128 b)
253 {
254 	return (__m128)veorq_s32((__m128i)a, (__m128i)b);
255 }
256 
257 // Computes the bitwise OR of the 128-bit value in a and the 128-bit value in b. https://msdn.microsoft.com/en-us/library/vstudio/ew8ty0db(v=vs.100).aspx
_mm_or_si128(__m128i a,__m128i b)258 FORCE_INLINE __m128i _mm_or_si128(__m128i a, __m128i b)
259 {
260 	return (__m128i)vorrq_s32(a, b);
261 }
262 
263 // Computes the bitwise XOR of the 128-bit value in a and the 128-bit value in b.  https://msdn.microsoft.com/en-us/library/fzt08www(v=vs.100).aspx
_mm_xor_si128(__m128i a,__m128i b)264 FORCE_INLINE __m128i _mm_xor_si128(__m128i a, __m128i b)
265 {
266 	return veorq_s32(a, b);
267 }
268 
269 // NEON does not provide this method
270 // Creates a 4-bit mask from the most significant bits of the four single-precision, floating-point values. https://msdn.microsoft.com/en-us/library/vstudio/4490ys29(v=vs.100).aspx
_mm_movemask_ps(__m128 a)271 FORCE_INLINE int _mm_movemask_ps(__m128 a)
272 {
273 #if ENABLE_CPP_VERSION // I am not yet convinced that the NEON version is faster than the C version of this
274 	uint32x4_t &ia = *(uint32x4_t *)&a;
275 	return (ia[0] >> 31) | ((ia[1] >> 30) & 2) | ((ia[2] >> 29) & 4) | ((ia[3] >> 28) & 8);
276 #else
277 	static const uint32x4_t movemask = { 1, 2, 4, 8 };
278 	static const uint32x4_t highbit = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
279 	uint32x4_t t0 = vreinterpretq_u32_f32(a);
280 	uint32x4_t t1 = vtstq_u32(t0, highbit);
281 	uint32x4_t t2 = vandq_u32(t1, movemask);
282 	uint32x2_t t3 = vorr_u32(vget_low_u32(t2), vget_high_u32(t2));
283 	return vget_lane_u32(t3, 0) | vget_lane_u32(t3, 1);
284 #endif
285 }
286 
287 #ifndef DISABLE_SHUFFLE
288 // Takes the upper 64 bits of a and places it in the low end of the result
289 // Takes the lower 64 bits of b and places it into the high end of the result.
_mm_shuffle_ps_1032(__m128 a,__m128 b)290 FORCE_INLINE __m128 _mm_shuffle_ps_1032(__m128 a, __m128 b)
291 {
292 	return vcombine_f32(vget_high_f32(a), vget_low_f32(b));
293 }
294 
295 // takes the lower two 32-bit values from a and swaps them and places in high end of result
296 // takes the higher two 32 bit values from b and swaps them and places in low end of result.
_mm_shuffle_ps_2301(__m128 a,__m128 b)297 FORCE_INLINE __m128 _mm_shuffle_ps_2301(__m128 a, __m128 b)
298 {
299 	return vcombine_f32(vrev64_f32(vget_low_f32(a)), vrev64_f32(vget_high_f32(b)));
300 }
301 
302 // keeps the low 64 bits of b in the low and puts the high 64 bits of a in the high
_mm_shuffle_ps_3210(__m128 a,__m128 b)303 FORCE_INLINE __m128 _mm_shuffle_ps_3210(__m128 a, __m128 b)
304 {
305 	return vcombine_f32(vget_low_f32(a), vget_high_f32(b));
306 }
307 
_mm_shuffle_ps_0011(__m128 a,__m128 b)308 FORCE_INLINE __m128 _mm_shuffle_ps_0011(__m128 a, __m128 b)
309 {
310 	return vcombine_f32(vdup_n_f32(vgetq_lane_f32(a, 1)), vdup_n_f32(vgetq_lane_f32(b, 0)));
311 }
312 
_mm_shuffle_ps_0022(__m128 a,__m128 b)313 FORCE_INLINE __m128 _mm_shuffle_ps_0022(__m128 a, __m128 b)
314 {
315 	return vcombine_f32(vdup_n_f32(vgetq_lane_f32(a, 2)), vdup_n_f32(vgetq_lane_f32(b, 0)));
316 }
317 
_mm_shuffle_ps_2200(__m128 a,__m128 b)318 FORCE_INLINE __m128 _mm_shuffle_ps_2200(__m128 a, __m128 b)
319 {
320 	return vcombine_f32(vdup_n_f32(vgetq_lane_f32(a, 0)), vdup_n_f32(vgetq_lane_f32(b, 2)));
321 }
322 
_mm_shuffle_ps_3202(__m128 a,__m128 b)323 FORCE_INLINE __m128 _mm_shuffle_ps_3202(__m128 a, __m128 b)
324 {
325 	float32_t a0 = vgetq_lane_f32(a, 0);
326 	float32_t a2 = vgetq_lane_f32(a, 2);
327 	float32x2_t aVal = vdup_n_f32(a2);
328 	aVal = vset_lane_f32(a0, aVal, 1);
329 	return vcombine_f32(aVal, vget_high_f32(b));
330 }
331 
_mm_shuffle_ps_1133(__m128 a,__m128 b)332 FORCE_INLINE __m128 _mm_shuffle_ps_1133(__m128 a, __m128 b)
333 {
334 	return vcombine_f32(vdup_n_f32(vgetq_lane_f32(a, 3)), vdup_n_f32(vgetq_lane_f32(b, 1)));
335 }
336 
_mm_shuffle_ps_2010(__m128 a,__m128 b)337 FORCE_INLINE __m128 _mm_shuffle_ps_2010(__m128 a, __m128 b)
338 {
339 	float32_t b0 = vgetq_lane_f32(b, 0);
340 	float32_t b2 = vgetq_lane_f32(b, 2);
341 	float32x2_t bVal = vdup_n_f32(b0);
342 	bVal = vset_lane_f32(b2, bVal, 1);
343 	return vcombine_f32(vget_low_f32(a), bVal);
344 }
345 
_mm_shuffle_ps_2001(__m128 a,__m128 b)346 FORCE_INLINE __m128 _mm_shuffle_ps_2001(__m128 a, __m128 b)
347 {
348 	float32_t b0 = vgetq_lane_f32(b, 0);
349 	float32_t b2 = vgetq_lane_f32(b, 2);
350 	float32x2_t bVal = vdup_n_f32(b0);
351 	bVal = vset_lane_f32(b2, bVal, 1);
352 	return vcombine_f32(vrev64_f32(vget_low_f32(a)), bVal);
353 }
354 
_mm_shuffle_ps_2032(__m128 a,__m128 b)355 FORCE_INLINE __m128 _mm_shuffle_ps_2032(__m128 a, __m128 b)
356 {
357 	float32_t b0 = vgetq_lane_f32(b, 0);
358 	float32_t b2 = vgetq_lane_f32(b, 2);
359 	float32x2_t bVal = vdup_n_f32(b0);
360 	bVal = vset_lane_f32(b2, bVal, 1);
361 	return vcombine_f32(vget_high_f32(a), bVal);
362 }
363 
364 
365 // NEON does not support a general purpose permute intrinsic
366 // Currently I am not sure whether the C implementation is faster or slower than the NEON version.
367 // Note, this has to be expanded as a template because the shuffle value must be an immediate value.
368 // The same is true on SSE as well.
369 // Selects four specific single-precision, floating-point values from a and b, based on the mask i. https://msdn.microsoft.com/en-us/library/vstudio/5f0858x0(v=vs.100).aspx
370 template <int i>
_mm_shuffle_ps_default(__m128 a,__m128 b)371 FORCE_INLINE __m128 _mm_shuffle_ps_default(__m128 a, __m128 b)
372 {
373 #if ENABLE_CPP_VERSION // I am not convinced that the NEON version is faster than the C version yet.
374 	__m128 ret;
375 	ret[0] = a[i & 0x3];
376 	ret[1] = a[(i >> 2) & 0x3];
377 	ret[2] = b[(i >> 4) & 0x03];
378 	ret[3] = b[(i >> 6) & 0x03];
379 	return ret;
380 #else
381 	__m128 ret = vmovq_n_f32(vgetq_lane_f32(a, i & 0x3));
382 	ret = vsetq_lane_f32(vgetq_lane_f32(a, (i >> 2) & 0x3), ret, 1);
383 	ret = vsetq_lane_f32(vgetq_lane_f32(b, (i >> 4) & 0x3), ret, 2);
384 	ret = vsetq_lane_f32(vgetq_lane_f32(b, (i >> 6) & 0x3), ret, 3);
385 	return ret;
386 #endif
387 }
388 
389 template <int i >
_mm_shuffle_ps_function(__m128 a,__m128 b)390 FORCE_INLINE __m128 _mm_shuffle_ps_function(__m128 a, __m128 b)
391 {
392 	switch (i)
393 	{
394 		case _MM_SHUFFLE(1, 0, 3, 2): return _mm_shuffle_ps_1032(a, b); break;
395 		case _MM_SHUFFLE(2, 3, 0, 1): return _mm_shuffle_ps_2301(a, b); break;
396 		case _MM_SHUFFLE(3, 2, 1, 0): return _mm_shuffle_ps_3210(a, b); break;
397 		case _MM_SHUFFLE(0, 0, 1, 1): return _mm_shuffle_ps_0011(a, b); break;
398 		case _MM_SHUFFLE(0, 0, 2, 2): return _mm_shuffle_ps_0022(a, b); break;
399 		case _MM_SHUFFLE(2, 2, 0, 0): return _mm_shuffle_ps_2200(a, b); break;
400 		case _MM_SHUFFLE(3, 2, 0, 2): return _mm_shuffle_ps_3202(a, b); break;
401 		case _MM_SHUFFLE(1, 1, 3, 3): return _mm_shuffle_ps_1133(a, b); break;
402 		case _MM_SHUFFLE(2, 0, 1, 0): return _mm_shuffle_ps_2010(a, b); break;
403 		case _MM_SHUFFLE(2, 0, 0, 1): return _mm_shuffle_ps_2001(a, b); break;
404 		case _MM_SHUFFLE(2, 0, 3, 2): return _mm_shuffle_ps_2032(a, b); break;
405 		default: return _mm_shuffle_ps_default<i>(a, b);
406 	}
407 }
408 
409 #define _mm_shuffle_ps(a,b,i) _mm_shuffle_ps_function<i>(a,b)
410 
411 // Takes the upper 64 bits of a and places it in the low end of the result
412 // Takes the lower 64 bits of b and places it into the high end of the result.
_mm_shuffle_epi_1032(__m128i a,__m128i b)413 FORCE_INLINE __m128i _mm_shuffle_epi_1032(__m128i a, __m128i b)
414 {
415 	return vcombine_s32(vget_high_s32(a), vget_low_s32(b));
416 }
417 
418 // takes the lower two 32-bit values from a and swaps them and places in low end of result
419 // takes the higher two 32 bit values from b and swaps them and places in high end of result.
_mm_shuffle_epi_2301(__m128i a,__m128i b)420 FORCE_INLINE __m128i _mm_shuffle_epi_2301(__m128i a, __m128i b)
421 {
422 	return vcombine_s32(vrev64_s32(vget_low_s32(a)), vrev64_s32(vget_high_s32(b)));
423 }
424 
425 // shift a right by 32 bits, and put the lower 32 bits of a into the upper 32 bits of b
426 // when a and b are the same, rotates the least significant 32 bits into the most signficant 32 bits, and shifts the rest down
_mm_shuffle_epi_0321(__m128i a,__m128i b)427 FORCE_INLINE __m128i _mm_shuffle_epi_0321(__m128i a, __m128i b)
428 {
429 	return vextq_s32(a, b, 1);
430 }
431 
432 // shift a left by 32 bits, and put the upper 32 bits of b into the lower 32 bits of a
433 // when a and b are the same, rotates the most significant 32 bits into the least signficant 32 bits, and shifts the rest up
_mm_shuffle_epi_2103(__m128i a,__m128i b)434 FORCE_INLINE __m128i _mm_shuffle_epi_2103(__m128i a, __m128i b)
435 {
436 	return vextq_s32(a, b, 3);
437 }
438 
439 // gets the lower 64 bits of a, and places it in the upper 64 bits
440 // gets the lower 64 bits of b and places it in the lower 64 bits
_mm_shuffle_epi_1010(__m128i a,__m128i b)441 FORCE_INLINE __m128i _mm_shuffle_epi_1010(__m128i a, __m128i b)
442 {
443 	return vcombine_s32(vget_low_s32(a), vget_low_s32(a));
444 }
445 
446 // gets the lower 64 bits of a, and places it in the upper 64 bits
447 // gets the lower 64 bits of b, swaps the 0 and 1 elements, and places it in the lower 64 bits
_mm_shuffle_epi_1001(__m128i a,__m128i b)448 FORCE_INLINE __m128i _mm_shuffle_epi_1001(__m128i a, __m128i b)
449 {
450 	return vcombine_s32(vrev64_s32(vget_low_s32(a)), vget_low_s32(b));
451 }
452 
453 // gets the lower 64 bits of a, swaps the 0 and 1 elements and places it in the upper 64 bits
454 // gets the lower 64 bits of b, swaps the 0 and 1 elements, and places it in the lower 64 bits
_mm_shuffle_epi_0101(__m128i a,__m128i b)455 FORCE_INLINE __m128i _mm_shuffle_epi_0101(__m128i a, __m128i b)
456 {
457 	return vcombine_s32(vrev64_s32(vget_low_s32(a)), vrev64_s32(vget_low_s32(b)));
458 }
459 
_mm_shuffle_epi_2211(__m128i a,__m128i b)460 FORCE_INLINE __m128i _mm_shuffle_epi_2211(__m128i a, __m128i b)
461 {
462 	return vcombine_s32(vdup_n_s32(vgetq_lane_s32(a, 1)), vdup_n_s32(vgetq_lane_s32(b, 2)));
463 }
464 
_mm_shuffle_epi_0122(__m128i a,__m128i b)465 FORCE_INLINE __m128i _mm_shuffle_epi_0122(__m128i a, __m128i b)
466 {
467 	return vcombine_s32(vdup_n_s32(vgetq_lane_s32(a, 2)), vrev64_s32(vget_low_s32(b)));
468 }
469 
_mm_shuffle_epi_3332(__m128i a,__m128i b)470 FORCE_INLINE __m128i _mm_shuffle_epi_3332(__m128i a, __m128i b)
471 {
472 	return vcombine_s32(vget_high_s32(a), vdup_n_s32(vgetq_lane_s32(b, 3)));
473 }
474 
475 template <int i >
_mm_shuffle_epi32_default(__m128i a,__m128i b)476 FORCE_INLINE __m128i _mm_shuffle_epi32_default(__m128i a, __m128i b)
477 {
478 #if ENABLE_CPP_VERSION
479 	__m128i ret;
480 	ret[0] = a[i & 0x3];
481 	ret[1] = a[(i >> 2) & 0x3];
482 	ret[2] = b[(i >> 4) & 0x03];
483 	ret[3] = b[(i >> 6) & 0x03];
484 	return ret;
485 #else
486 	__m128i ret = vmovq_n_s32(vgetq_lane_s32(a, i & 0x3));
487 	ret = vsetq_lane_s32(vgetq_lane_s32(a, (i >> 2) & 0x3), ret, 1);
488 	ret = vsetq_lane_s32(vgetq_lane_s32(b, (i >> 4) & 0x3), ret, 2);
489 	ret = vsetq_lane_s32(vgetq_lane_s32(b, (i >> 6) & 0x3), ret, 3);
490 	return ret;
491 #endif
492 }
493 
494 template <int i >
_mm_shuffle_epi32_function(__m128i a,__m128i b)495 FORCE_INLINE __m128i _mm_shuffle_epi32_function(__m128i a, __m128i b)
496 {
497 	switch (i)
498 	{
499 		case _MM_SHUFFLE(1, 0, 3, 2): return _mm_shuffle_epi_1032(a, b); break;
500 		case _MM_SHUFFLE(2, 3, 0, 1): return _mm_shuffle_epi_2301(a, b); break;
501 		case _MM_SHUFFLE(0, 3, 2, 1): return _mm_shuffle_epi_0321(a, b); break;
502 		case _MM_SHUFFLE(2, 1, 0, 3): return _mm_shuffle_epi_2103(a, b); break;
503 		case _MM_SHUFFLE(1, 0, 1, 0): return _mm_shuffle_epi_1010(a, b); break;
504 		case _MM_SHUFFLE(1, 0, 0, 1): return _mm_shuffle_epi_1001(a, b); break;
505 		case _MM_SHUFFLE(0, 1, 0, 1): return _mm_shuffle_epi_0101(a, b); break;
506 		case _MM_SHUFFLE(2, 2, 1, 1): return _mm_shuffle_epi_2211(a, b); break;
507 		case _MM_SHUFFLE(0, 1, 2, 2): return _mm_shuffle_epi_0122(a, b); break;
508 		case _MM_SHUFFLE(3, 3, 3, 2): return _mm_shuffle_epi_3332(a, b); break;
509 		default: return _mm_shuffle_epi32_default<i>(a, b);
510 	}
511 }
512 
513 template <int i >
_mm_shuffle_epi32_splat(__m128i a)514 FORCE_INLINE __m128i _mm_shuffle_epi32_splat(__m128i a)
515 {
516 	return vdupq_n_s32(vgetq_lane_s32(a, i));
517 }
518 
519 template <int i>
_mm_shuffle_epi32_single(__m128i a)520 FORCE_INLINE __m128i _mm_shuffle_epi32_single(__m128i a)
521 {
522 	switch (i)
523 	{
524 		case _MM_SHUFFLE(0, 0, 0, 0): return _mm_shuffle_epi32_splat<0>(a); break;
525 		case _MM_SHUFFLE(1, 1, 1, 1): return _mm_shuffle_epi32_splat<1>(a); break;
526 		case _MM_SHUFFLE(2, 2, 2, 2): return _mm_shuffle_epi32_splat<2>(a); break;
527 		case _MM_SHUFFLE(3, 3, 3, 3): return _mm_shuffle_epi32_splat<3>(a); break;
528 		default: return _mm_shuffle_epi32_function<i>(a, a);
529 	}
530 }
531 
532 // Shuffles the 4 signed or unsigned 32-bit integers in a as specified by imm.	https://msdn.microsoft.com/en-us/library/56f67xbk%28v=vs.90%29.aspx
533 #define _mm_shuffle_epi32(a,i) _mm_shuffle_epi32_single<i>(a)
534 #endif
535 
536 #define _mm_shufflehi_epi16(a,i) \
537 ({\
538 	int16x8_t ret = (int16x8_t)a;\
539 	int16x4_t highBits = vget_high_s16(ret);\
540 	ret = vsetq_lane_s16(vget_lane_s16(highBits, i & 0x3), ret, 4);\
541 	ret = vsetq_lane_s16(vget_lane_s16(highBits, (i >> 2) & 0x3), ret, 5);\
542 	ret = vsetq_lane_s16(vget_lane_s16(highBits, (i >> 4) & 0x3), ret, 6);\
543 	ret = vsetq_lane_s16(vget_lane_s16(highBits, (i >> 6) & 0x3), ret, 7);\
544 	(__m128i)ret;\
545 })
546 
547 // Shuffles the upper 4 signed or unsigned 16 - bit integers in a as specified by imm.  https://msdn.microsoft.com/en-us/library/13ywktbs(v=vs.100).aspx
548 #define _mm_shufflelo_epi16(a,i)\
549 ({\
550 	int16x8_t ret = (int16x8_t)a;\
551 	int16x4_t lowBits = vget_low_s16(ret);\
552 	ret = vsetq_lane_s16(vget_lane_s16(lowBits, i & 0x3), ret, 0);\
553 	ret = vsetq_lane_s16(vget_lane_s16(lowBits, (i >> 2) & 0x3), ret, 1);\
554 	ret = vsetq_lane_s16(vget_lane_s16(lowBits, (i >> 4) & 0x3), ret, 2);\
555 	ret = vsetq_lane_s16(vget_lane_s16(lowBits, (i >> 6) & 0x3), ret, 3);\
556 	(__m128i)ret;\
557 })
558 
559 // Shifts the 4 signed or unsigned 32-bit integers in a left by count bits while shifting in zeros. : https://msdn.microsoft.com/en-us/library/z2k3bbtb%28v=vs.90%29.aspx
560 #define _mm_slli_epi32(a, imm) (__m128i)vshlq_n_s32(a,imm)
561 
562 // Shifts the 8 signed or unsigned 16-bit integers in a left by count bits while shifting in zeros.  https://msdn.microsoft.com/en-us/library/es73bcsy(v=vs.90).aspx
563 #define _mm_slli_epi16(a, imm) (__m128i)vshlq_n_s16((int16x8_t)a,imm)
564 
565 //Shifts the 4 signed or unsigned 32-bit integers in a right by count bits while shifting in zeros.  https://msdn.microsoft.com/en-us/library/w486zcfa(v=vs.100).aspx
566 #define _mm_srli_epi32( a, imm ) (__m128i)vshrq_n_u32((uint32x4_t)a, imm)
567 
568 //Shifts the 8 signed or unsigned 16-bit integers in a right by count bits while shifting in zeros.  https://msdn.microsoft.com/en-us/library/6tcwd38t(v=vs.90).aspx
569 #define _mm_srli_epi16( a, imm ) (__m128i)vshrq_n_u16((uint16x8_t)a, imm)
570 
571 // Shifts the 4 signed 32 - bit integers in a right by count bits while shifting in the sign bit.  https://msdn.microsoft.com/en-us/library/z1939387(v=vs.100).aspx
572 #define _mm_srai_epi32( a, imm ) vshrq_n_s32(a, imm)
573 
574 // Shifts the 8 signed 16 - bit integers in a right by count bits while shifting in the sign bit.  https://msdn.microsoft.com/en-us/library/5x22s2fb(v=vs.90).aspx
575 #define _mm_srai_epi16( a, imm ) (__m128i)vshrq_n_s16((int16x8_t)a, imm)
576 
577 // Shifts the 128 - bit value in a right by imm bytes while shifting in zeros.imm must be an immediate. https://msdn.microsoft.com/en-us/library/305w28yz(v=vs.100).aspx
578 //#define _mm_srli_si128( a, imm ) (__m128i)vmaxq_s8((int8x16_t)a, vextq_s8((int8x16_t)a, vdupq_n_s8(0), imm))
579 #define _mm_srli_si128( a, imm ) (__m128i)vextq_s8((int8x16_t)a, vdupq_n_s8(0), (imm))
580 
581 // Shifts the 128-bit value in a left by imm bytes while shifting in zeros. imm must be an immediate.  https://msdn.microsoft.com/en-us/library/34d3k2kt(v=vs.100).aspx
582 #define _mm_slli_si128( a, imm ) (__m128i)vextq_s8(vdupq_n_s8(0), (int8x16_t)a, 16 - (imm))
583 
584 // NEON does not provide a version of this function, here is an article about some ways to repro the results.
585 // http://stackoverflow.com/questions/11870910/sse-mm-movemask-epi8-equivalent-method-for-arm-neon
586 // Creates a 16-bit mask from the most significant bits of the 16 signed or unsigned 8-bit integers in a and zero extends the upper bits. https://msdn.microsoft.com/en-us/library/vstudio/s090c8fk(v=vs.100).aspx
_mm_movemask_epi8(__m128i _a)587 FORCE_INLINE int _mm_movemask_epi8(__m128i _a)
588 {
589 	uint8x16_t input = (uint8x16_t)_a;
590 	const int8_t __attribute__((aligned(16))) xr[8] = { -7, -6, -5, -4, -3, -2, -1, 0 };
591 	uint8x8_t mask_and = vdup_n_u8(0x80);
592 	int8x8_t mask_shift = vld1_s8(xr);
593 
594 	uint8x8_t lo = vget_low_u8(input);
595 	uint8x8_t hi = vget_high_u8(input);
596 
597 	lo = vand_u8(lo, mask_and);
598 	lo = vshl_u8(lo, mask_shift);
599 
600 	hi = vand_u8(hi, mask_and);
601 	hi = vshl_u8(hi, mask_shift);
602 
603 	lo = vpadd_u8(lo, lo);
604 	lo = vpadd_u8(lo, lo);
605 	lo = vpadd_u8(lo, lo);
606 
607 	hi = vpadd_u8(hi, hi);
608 	hi = vpadd_u8(hi, hi);
609 	hi = vpadd_u8(hi, hi);
610 
611 	return ((hi[0] << 8) | (lo[0] & 0xFF));
612 }
613 
614 
615 // ******************************************
616 // Math operations
617 // ******************************************
618 
619 // Subtracts the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/1zad2k61(v=vs.100).aspx
_mm_sub_ps(__m128 a,__m128 b)620 FORCE_INLINE __m128 _mm_sub_ps(__m128 a, __m128 b)
621 {
622 	return vsubq_f32(a, b);
623 }
624 
625 // Subtracts the 4 signed or unsigned 32-bit integers of b from the 4 signed or unsigned 32-bit integers of a. https://msdn.microsoft.com/en-us/library/vstudio/fhh866h0(v=vs.100).aspx
_mm_sub_epi32(__m128i a,__m128i b)626 FORCE_INLINE __m128i _mm_sub_epi32(__m128i a, __m128i b)
627 {
628 	return vsubq_s32(a, b);
629 }
630 
_mm_sub_epi16(__m128i a,__m128i b)631 FORCE_INLINE __m128i _mm_sub_epi16(__m128i a, __m128i b)
632 {
633 	return (__m128i)vsubq_s16((int16x8_t)a, (int16x8_t)b);
634 }
635 
_mm_subs_epi16(__m128i a,__m128i b)636 FORCE_INLINE __m128i _mm_subs_epi16(__m128i a, __m128i b)
637 {
638 	return (__m128i)vqsubq_s16((int16x8_t)a, (int16x8_t)b);
639 }
640 
_mm_subs_epu16(__m128i a,__m128i b)641 FORCE_INLINE __m128i _mm_subs_epu16(__m128i a, __m128i b)
642 {
643 	return (__m128i)vqsubq_u16((uint16x8_t)a, (uint16x8_t)b);
644 }
645 
646 // Adds the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/c9848chc(v=vs.100).aspx
_mm_add_ps(__m128 a,__m128 b)647 FORCE_INLINE __m128 _mm_add_ps(__m128 a, __m128 b)
648 {
649 	return vaddq_f32(a, b);
650 }
651 
652 // Adds the 4 signed or unsigned 32-bit integers in a to the 4 signed or unsigned 32-bit integers in b. https://msdn.microsoft.com/en-us/library/vstudio/09xs4fkk(v=vs.100).aspx
_mm_add_epi32(__m128i a,__m128i b)653 FORCE_INLINE __m128i _mm_add_epi32(__m128i a, __m128i b)
654 {
655 	return vaddq_s32(a, b);
656 }
657 
658 // Adds the 8 signed or unsigned 16-bit integers in a to the 8 signed or unsigned 16-bit integers in b. https://msdn.microsoft.com/en-us/library/fceha5k4(v=vs.100).aspx
_mm_add_epi16(__m128i a,__m128i b)659 FORCE_INLINE __m128i _mm_add_epi16(__m128i a, __m128i b)
660 {
661 	return (__m128i)vaddq_s16((int16x8_t)a, (int16x8_t)b);
662 }
663 
_mm_adds_epi16(__m128i a,__m128i b)664 FORCE_INLINE __m128i _mm_adds_epi16(__m128i a, __m128i b)
665 {
666 	return (__m128i)vqaddq_s16((int16x8_t)a, (int16x8_t)b);
667 }
668 
669 // Multiplies the 8 signed or unsigned 16-bit integers from a by the 8 signed or unsigned 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/9ks1472s(v=vs.100).aspx
_mm_mullo_epi16(__m128i a,__m128i b)670 FORCE_INLINE __m128i _mm_mullo_epi16(__m128i a, __m128i b)
671 {
672 	return (__m128i)vmulq_s16((int16x8_t)a, (int16x8_t)b);
673 }
674 
675 // Multiplies the 4 signed or unsigned 32-bit integers from a by the 4 signed or unsigned 32-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/bb531409(v=vs.100).aspx
_mm_mullo_epi32(__m128i a,__m128i b)676 FORCE_INLINE __m128i _mm_mullo_epi32 (__m128i a, __m128i b)
677 {
678 	return (__m128i)vmulq_s32((int32x4_t)a,(int32x4_t)b);
679 }
680 
681 // Multiplies the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/22kbk6t9(v=vs.100).aspx
_mm_mul_ps(__m128 a,__m128 b)682 FORCE_INLINE __m128 _mm_mul_ps(__m128 a, __m128 b)
683 {
684 	return vmulq_f32(a, b);
685 }
686 
687 // Divides the four single-precision, floating-point values of a and b.
_mm_div_ps(__m128 a,__m128 b)688 FORCE_INLINE __m128 _mm_div_ps(__m128 a, __m128 b)
689 {
690 	__m128 recip = vrecpeq_f32(b);
691 	recip = vmulq_f32(recip, vrecpsq_f32(recip, b));
692 	return vmulq_f32(a, recip);
693 }
694 
695 // This version does additional iterations to improve accuracy.  Between 1 and 4 recommended.
696 // Computes the approximations of reciprocals of the four single-precision, floating-point values of a. https://msdn.microsoft.com/en-us/library/vstudio/796k1tty(v=vs.100).aspx
recipq_newton(__m128 in,int n)697 FORCE_INLINE __m128 recipq_newton(__m128 in, int n)
698 {
699 	int i;
700 	__m128 recip = vrecpeq_f32(in);
701 	for (i = 0; i<n; ++i)
702 	{
703 		recip = vmulq_f32(recip, vrecpsq_f32(recip, in));
704 	}
705 	return recip;
706 }
707 
708 // Computes the approximations of reciprocals of the four single-precision, floating-point values of a. https://msdn.microsoft.com/en-us/library/vstudio/796k1tty(v=vs.100).aspx
_mm_rcp_ps(__m128 in)709 FORCE_INLINE __m128 _mm_rcp_ps(__m128 in)
710 {
711 	__m128 recip = vrecpeq_f32(in);
712 	recip = vmulq_f32(recip, vrecpsq_f32(recip, in));
713 	return recip;
714 }
715 
716 
717 // Computes the approximations of square roots of the four single-precision, floating-point values of a. First computes reciprocal square roots and then reciprocals of the four values. https://msdn.microsoft.com/en-us/library/vstudio/8z67bwwk(v=vs.100).aspx
_mm_sqrt_ps(__m128 in)718 FORCE_INLINE __m128 _mm_sqrt_ps(__m128 in)
719 {
720 	__m128 recipsq = vrsqrteq_f32(in);
721 	__m128 sq = vrecpeq_f32(recipsq);
722 	// ??? use step versions of both sqrt and recip for better accuracy?
723 	return sq;
724 }
725 
726 
727 // Computes the maximums of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/ff5d607a(v=vs.100).aspx
_mm_max_ps(__m128 a,__m128 b)728 FORCE_INLINE __m128 _mm_max_ps(__m128 a, __m128 b)
729 {
730 	return vmaxq_f32(a, b);
731 }
732 
733 // Computes the minima of the four single-precision, floating-point values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/wh13kadz(v=vs.100).aspx
_mm_min_ps(__m128 a,__m128 b)734 FORCE_INLINE __m128 _mm_min_ps(__m128 a, __m128 b)
735 {
736 	return vminq_f32(a, b);
737 }
738 
739 // Computes the pairwise minima of the 8 signed 16-bit integers from a and the 8 signed 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/6te997ew(v=vs.100).aspx
_mm_min_epi16(__m128i a,__m128i b)740 FORCE_INLINE __m128i _mm_min_epi16(__m128i a, __m128i b)
741 {
742 	return (__m128i)vminq_s16((int16x8_t)a, (int16x8_t)b);
743 }
744 
_mm_max_epi16(__m128i a,__m128i b)745 FORCE_INLINE __m128i _mm_max_epi16(__m128i a, __m128i b)
746 {
747 	return (__m128i)vmaxq_s16((int16x8_t)a, (int16x8_t)b);
748 }
749 
750 // epi versions of min/max
751 // Computes the pariwise maximums of the four signed 32-bit integer values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/bb514055(v=vs.100).aspx
_mm_max_epi32(__m128i a,__m128i b)752 FORCE_INLINE __m128i _mm_max_epi32(__m128i a, __m128i b )
753 {
754 	return vmaxq_s32(a,b);
755 }
756 
757 // Computes the pariwise minima of the four signed 32-bit integer values of a and b. https://msdn.microsoft.com/en-us/library/vstudio/bb531476(v=vs.100).aspx
_mm_min_epi32(__m128i a,__m128i b)758 FORCE_INLINE __m128i _mm_min_epi32(__m128i a, __m128i b )
759 {
760 	return vminq_s32(a,b);
761 }
762 
763 // Multiplies the 8 signed 16-bit integers from a by the 8 signed 16-bit integers from b. https://msdn.microsoft.com/en-us/library/vstudio/59hddw1d(v=vs.100).aspx
_mm_mulhi_epi16(__m128i a,__m128i b)764 FORCE_INLINE __m128i _mm_mulhi_epi16(__m128i a, __m128i b)
765 {
766 	const int32x4_t lo = vmull_s16(vget_low_s16((int16x8_t)a),  vget_low_s16((int16x8_t)b));
767 	const int32x4_t hi = vmull_s16(vget_high_s16((int16x8_t)a), vget_high_s16((int16x8_t)b));
768 
769 	return (__m128i)vcombine_s16(vshrn_n_s32(lo, 16), vshrn_n_s32(hi, 16));
770 }
771 
_mm_mulhi_epu16(__m128i a,__m128i b)772 FORCE_INLINE __m128i _mm_mulhi_epu16(__m128i a, __m128i b)
773 {
774 	const uint32x4_t lo = vmull_u16(vget_low_u16((uint16x8_t)a),  vget_low_u16((uint16x8_t)b));
775 	const uint32x4_t hi = vmull_u16(vget_high_u16((uint16x8_t)a), vget_high_u16((uint16x8_t)b));
776 
777 	return (__m128i)vcombine_u16(vshrn_n_u32(lo, 16), vshrn_n_u32(hi, 16));
778 }
779 
780 // Computes pairwise add of each argument as single-precision, floating-point values a and b.
781 //https://msdn.microsoft.com/en-us/library/yd9wecaa.aspx
_mm_hadd_ps(__m128 a,__m128 b)782 FORCE_INLINE __m128 _mm_hadd_ps(__m128 a, __m128 b )
783 {
784 // This does not work, no vpaddq...
785 //	return (__m128) vpaddq_f32(a,b);
786         //
787         // get two f32x2_t values from a
788         // do vpadd
789         // put result in low half of f32x4 result
790         //
791         // get two f32x2_t values from b
792         // do vpadd
793         // put result in high half of f32x4 result
794         //
795         // combine
796         return vcombine_f32( vpadd_f32( vget_low_f32(a), vget_high_f32(a) ), vpadd_f32( vget_low_f32(b), vget_high_f32(b) ) );
797 }
798 
799 // ******************************************
800 // Compare operations
801 // ******************************************
802 
803 // Compares for less than https://msdn.microsoft.com/en-us/library/vstudio/f330yhc8(v=vs.100).aspx
_mm_cmplt_ps(__m128 a,__m128 b)804 FORCE_INLINE __m128 _mm_cmplt_ps(__m128 a, __m128 b)
805 {
806 	return (__m128)vcltq_f32(a, b);
807 }
808 
809 // Compares for greater than. https://msdn.microsoft.com/en-us/library/vstudio/11dy102s(v=vs.100).aspx
_mm_cmpgt_ps(__m128 a,__m128 b)810 FORCE_INLINE __m128 _mm_cmpgt_ps(__m128 a, __m128 b)
811 {
812 	return (__m128)vcgtq_f32(a, b);
813 }
814 
815 // Compares for greater than or equal. https://msdn.microsoft.com/en-us/library/vstudio/fs813y2t(v=vs.100).aspx
_mm_cmpge_ps(__m128 a,__m128 b)816 FORCE_INLINE __m128 _mm_cmpge_ps(__m128 a, __m128 b)
817 {
818 	return (__m128)vcgeq_f32(a, b);
819 }
820 
821 // Compares for less than or equal. https://msdn.microsoft.com/en-us/library/vstudio/1s75w83z(v=vs.100).aspx
_mm_cmple_ps(__m128 a,__m128 b)822 FORCE_INLINE __m128 _mm_cmple_ps(__m128 a, __m128 b)
823 {
824 	return (__m128)vcleq_f32(a, b);
825 }
826 
827 // Compares for equality. https://msdn.microsoft.com/en-us/library/vstudio/36aectz5(v=vs.100).aspx
_mm_cmpeq_ps(__m128 a,__m128 b)828 FORCE_INLINE __m128 _mm_cmpeq_ps(__m128 a, __m128 b)
829 {
830 	return (__m128)vceqq_f32(a, b);
831 }
832 
_mm_cmpeq_epi16(__m128i a,__m128i b)833 FORCE_INLINE __m128i _mm_cmpeq_epi16(__m128i a, __m128i b)
834 {
835 	return (__m128i)vceqq_s16((int16x8_t)a, (int16x8_t)b);
836 }
837 
838 // Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers in b for less than. https://msdn.microsoft.com/en-us/library/vstudio/4ak0bf5d(v=vs.100).aspx
_mm_cmplt_epi32(__m128i a,__m128i b)839 FORCE_INLINE __m128i _mm_cmplt_epi32(__m128i a, __m128i b)
840 {
841 	return (__m128i)vcltq_s32(a, b);
842 }
843 
_mm_cmplt_epu32(__m128i a,__m128i b)844 FORCE_INLINE __m128i _mm_cmplt_epu32(__m128i a, __m128i b)
845 {
846 	return (__m128i)vcltq_u32((uint32x4_t)a, (uint32x4_t)b);
847 }
848 
_mm_cmplt_epu16(__m128i a,__m128i b)849 FORCE_INLINE __m128i _mm_cmplt_epu16(__m128i a, __m128i b)
850 {
851 	return (__m128i)vcltq_u16((uint16x8_t)a, (uint16x8_t)b);
852 }
853 
854 // Compares the 4 signed 32-bit integers in a and the 4 signed 32-bit integers in b for greater than. https://msdn.microsoft.com/en-us/library/vstudio/1s9f2z0y(v=vs.100).aspx
_mm_cmpgt_epi32(__m128i a,__m128i b)855 FORCE_INLINE __m128i _mm_cmpgt_epi32(__m128i a, __m128i b)
856 {
857 	return (__m128i)vcgtq_s32(a, b);
858 }
859 
_mm_cmpgt_epi16(__m128i a,__m128i b)860 FORCE_INLINE __m128i _mm_cmpgt_epi16(__m128i a, __m128i b)
861 {
862 	return (__m128i)vcgtq_s16((int16x8_t)a, (int16x8_t)b);
863 }
864 
_mm_cmpgt_epu16(__m128i a,__m128i b)865 FORCE_INLINE __m128i _mm_cmpgt_epu16(__m128i a, __m128i b)
866 {
867 	return (__m128i)vcgtq_u16((uint16x8_t)a, (uint16x8_t)b);
868 }
869 
870 // Compares the four 32-bit floats in a and b to check if any values are NaN. Ordered compare between each value returns true for "orderable" and false for "not orderable" (NaN). https://msdn.microsoft.com/en-us/library/vstudio/0h9w00fx(v=vs.100).aspx
871 // see also:
872 // http://stackoverflow.com/questions/8627331/what-does-ordered-unordered-comparison-mean
873 // http://stackoverflow.com/questions/29349621/neon-isnanval-intrinsics
_mm_cmpord_ps(__m128 a,__m128 b)874 FORCE_INLINE __m128 _mm_cmpord_ps(__m128 a, __m128 b )
875 {
876         // Note: NEON does not have ordered compare builtin
877         // Need to compare a eq a and b eq b to check for NaN
878         // Do AND of results to get final
879 	return (__m128) vreinterpretq_f32_u32( vandq_u32( vceqq_f32(a,a), vceqq_f32(b,b) ) );
880 }
881 
882 // ******************************************
883 // Conversions
884 // ******************************************
885 
886 // Converts the four single-precision, floating-point values of a to signed 32-bit integer values using truncate. https://msdn.microsoft.com/en-us/library/vstudio/1h005y6x(v=vs.100).aspx
_mm_cvttps_epi32(__m128 a)887 FORCE_INLINE __m128i _mm_cvttps_epi32(__m128 a)
888 {
889 	return vcvtq_s32_f32(a);
890 }
891 
892 // Converts the four signed 32-bit integer values of a to single-precision, floating-point values https://msdn.microsoft.com/en-us/library/vstudio/36bwxcx5(v=vs.100).aspx
_mm_cvtepi32_ps(__m128i a)893 FORCE_INLINE __m128 _mm_cvtepi32_ps(__m128i a)
894 {
895 	return vcvtq_f32_s32(a);
896 }
897 
898 // Converts the four single-precision, floating-point values of a to signed 32-bit integer values. https://msdn.microsoft.com/en-us/library/vstudio/xdc42k5e(v=vs.100).aspx
_mm_cvtps_epi32(__m128 a)899 FORCE_INLINE __m128i _mm_cvtps_epi32(__m128 a)
900 {
901 #if __aarch64__
902 	return vcvtaq_s32_f32(a);
903 #else
904 	__m128 half = vdupq_n_f32(0.5f);
905 	const __m128 sign = vcvtq_f32_u32((vshrq_n_u32(vreinterpretq_u32_f32(a), 31)));
906 	const __m128 aPlusHalf = vaddq_f32(a, half);
907 	const __m128 aRound = vsubq_f32(aPlusHalf, sign);
908 	return vcvtq_s32_f32(aRound);
909 #endif
910 }
911 
912 // Moves the least significant 32 bits of a to a 32-bit integer. https://msdn.microsoft.com/en-us/library/5z7a9642%28v=vs.90%29.aspx
_mm_cvtsi128_si32(__m128i a)913 FORCE_INLINE int _mm_cvtsi128_si32(__m128i a)
914 {
915 	return vgetq_lane_s32(a, 0);
916 }
917 
918 // Moves 32-bit integer a to the least significant 32 bits of an __m128 object, zero extending the upper bits. https://msdn.microsoft.com/en-us/library/ct3539ha%28v=vs.90%29.aspx
_mm_cvtsi32_si128(int a)919 FORCE_INLINE __m128i _mm_cvtsi32_si128(int a)
920 {
921 	__m128i result = vdupq_n_s32(0);
922 	return vsetq_lane_s32(a, result, 0);
923 }
924 
925 
926 // Applies a type cast to reinterpret four 32-bit floating point values passed in as a 128-bit parameter as packed 32-bit integers. https://msdn.microsoft.com/en-us/library/bb514099.aspx
_mm_castps_si128(__m128 a)927 FORCE_INLINE __m128i _mm_castps_si128(__m128 a)
928 {
929 	return *(const __m128i *)&a;
930 }
931 
932 // Applies a type cast to reinterpret four 32-bit integers passed in as a 128-bit parameter as packed 32-bit floating point values. https://msdn.microsoft.com/en-us/library/bb514029.aspx
_mm_castsi128_ps(__m128i a)933 FORCE_INLINE __m128 _mm_castsi128_ps(__m128i a)
934 {
935 	return *(const __m128 *)&a;
936 }
937 
938 // Loads 128-bit value. : https://msdn.microsoft.com/en-us/library/atzzad1h(v=vs.80).aspx
_mm_load_si128(const __m128i * p)939 FORCE_INLINE __m128i _mm_load_si128(const __m128i *p)
940 {
941 	return vld1q_s32((int32_t *)p);
942 }
943 
944 // ******************************************
945 // Miscellaneous Operations
946 // ******************************************
947 
948 // Packs the 16 signed 16-bit integers from a and b into 8-bit integers and saturates. https://msdn.microsoft.com/en-us/library/k4y4f7w5%28v=vs.90%29.aspx
_mm_packs_epi16(__m128i a,__m128i b)949 FORCE_INLINE __m128i _mm_packs_epi16(__m128i a, __m128i b)
950 {
951 	return (__m128i)vcombine_s8(vqmovn_s16((int16x8_t)a), vqmovn_s16((int16x8_t)b));
952 }
953 
954 // Packs the 16 signed 16 - bit integers from a and b into 8 - bit unsigned integers and saturates. https://msdn.microsoft.com/en-us/library/07ad1wx4(v=vs.100).aspx
_mm_packus_epi16(const __m128i a,const __m128i b)955 FORCE_INLINE __m128i _mm_packus_epi16(const __m128i a, const __m128i b)
956 {
957 	return (__m128i)vcombine_u8(vqmovun_s16((int16x8_t)a), vqmovun_s16((int16x8_t)b));
958 }
959 
960 // Packs the 8 signed 32-bit integers from a and b into signed 16-bit integers and saturates. https://msdn.microsoft.com/en-us/library/393t56f9%28v=vs.90%29.aspx
_mm_packs_epi32(__m128i a,__m128i b)961 FORCE_INLINE __m128i _mm_packs_epi32(__m128i a, __m128i b)
962 {
963 	return (__m128i)vcombine_s16(vqmovn_s32(a), vqmovn_s32(b));
964 }
965 
966 // Interleaves the lower 8 signed or unsigned 8-bit integers in a with the lower 8 signed or unsigned 8-bit integers in b.  https://msdn.microsoft.com/en-us/library/xf7k860c%28v=vs.90%29.aspx
_mm_unpacklo_epi8(__m128i a,__m128i b)967 FORCE_INLINE __m128i _mm_unpacklo_epi8(__m128i a, __m128i b)
968 {
969 	int8x8_t a1 = (int8x8_t)vget_low_s16((int16x8_t)a);
970 	int8x8_t b1 = (int8x8_t)vget_low_s16((int16x8_t)b);
971 
972 	int8x8x2_t result = vzip_s8(a1, b1);
973 
974 	return (__m128i)vcombine_s8(result.val[0], result.val[1]);
975 }
976 
977 // Interleaves the lower 4 signed or unsigned 16-bit integers in a with the lower 4 signed or unsigned 16-bit integers in b.  https://msdn.microsoft.com/en-us/library/btxb17bw%28v=vs.90%29.aspx
_mm_unpacklo_epi16(__m128i a,__m128i b)978 FORCE_INLINE __m128i _mm_unpacklo_epi16(__m128i a, __m128i b)
979 {
980 	int16x4_t a1 = vget_low_s16((int16x8_t)a);
981 	int16x4_t b1 = vget_low_s16((int16x8_t)b);
982 
983 	int16x4x2_t result = vzip_s16(a1, b1);
984 
985 	return (__m128i)vcombine_s16(result.val[0], result.val[1]);
986 }
987 
988 // Interleaves the lower 2 signed or unsigned 32 - bit integers in a with the lower 2 signed or unsigned 32 - bit integers in b.  https://msdn.microsoft.com/en-us/library/x8atst9d(v=vs.100).aspx
_mm_unpacklo_epi32(__m128i a,__m128i b)989 FORCE_INLINE __m128i _mm_unpacklo_epi32(__m128i a, __m128i b)
990 {
991 	int32x2_t a1 = vget_low_s32(a);
992 	int32x2_t b1 = vget_low_s32(b);
993 
994 	int32x2x2_t result = vzip_s32(a1, b1);
995 
996 	return vcombine_s32(result.val[0], result.val[1]);
997 }
998 
999 // Selects and interleaves the lower two single-precision, floating-point values from a and b. https://msdn.microsoft.com/en-us/library/25st103b%28v=vs.90%29.aspx
_mm_unpacklo_ps(__m128 a,__m128 b)1000 FORCE_INLINE __m128 _mm_unpacklo_ps(__m128 a, __m128 b)
1001 {
1002 	float32x2x2_t result = vzip_f32(vget_low_f32(a), vget_low_f32(b));
1003 	return vcombine_f32(result.val[0], result.val[1]);
1004 }
1005 
1006 // Selects and interleaves the upper two single-precision, floating-point values from a and b. https://msdn.microsoft.com/en-us/library/skccxx7d%28v=vs.90%29.aspx
_mm_unpackhi_ps(__m128 a,__m128 b)1007 FORCE_INLINE __m128 _mm_unpackhi_ps(__m128 a, __m128 b)
1008 {
1009 	float32x2x2_t result = vzip_f32(vget_high_f32(a), vget_high_f32(b));
1010 	return vcombine_f32(result.val[0], result.val[1]);
1011 }
1012 
1013 // Interleaves the upper 2 signed or unsigned 32-bit integers in a with the upper 2 signed or unsigned 32-bit integers in b.  https://msdn.microsoft.com/en-us/library/65sa7cbs(v=vs.100).aspx
_mm_unpackhi_epi32(__m128i a,__m128i b)1014 FORCE_INLINE __m128i _mm_unpackhi_epi32(__m128i a, __m128i b)
1015 {
1016 	int32x2_t a1 = vget_high_s32(a);
1017 	int32x2_t b1 = vget_high_s32(b);
1018 
1019 	int32x2x2_t result = vzip_s32(a1, b1);
1020 
1021 	return vcombine_s32(result.val[0], result.val[1]);
1022 }
1023 
_mm_unpackhi_epi16(__m128i a,__m128i b)1024 FORCE_INLINE __m128i _mm_unpackhi_epi16(__m128i a, __m128i b)
1025 {
1026 	int16x4_t a1 = vget_high_s16((int16x8_t)a);
1027 	int16x4_t b1 = vget_high_s16((int16x8_t)b);
1028 
1029 	int16x4x2_t result = vzip_s16(a1, b1);
1030 
1031 	return (__m128i)vcombine_s16(result.val[0], result.val[1]);
1032 }
1033 
1034 // Extracts the selected signed or unsigned 16-bit integer from a and zero extends.  https://msdn.microsoft.com/en-us/library/6dceta0c(v=vs.100).aspx
1035 #define _mm_extract_epi16( a, imm ) vgetq_lane_s16((int16x8_t)a, imm)
1036 
1037 #define _mm_insert_epi16( a, b, imm ) (__m128i)vsetq_lane_s16(b, (int16x8_t)a, imm)
1038 
1039 // ******************************************
1040 // Streaming Extensions
1041 // ******************************************
1042 
1043 // Guarantees that every preceding store is globally visible before any subsequent store.  https://msdn.microsoft.com/en-us/library/5h2w73d1%28v=vs.90%29.aspx
_mm_sfence(void)1044 FORCE_INLINE void _mm_sfence(void)
1045 {
1046 	__sync_synchronize();
1047 }
1048 
1049 // Stores the data in a to the address p without polluting the caches.  If the cache line containing address p is already in the cache, the cache will be updated.Address p must be 16 - byte aligned.  https://msdn.microsoft.com/en-us/library/ba08y07y%28v=vs.90%29.aspx
_mm_stream_si128(__m128i * p,__m128i a)1050 FORCE_INLINE void _mm_stream_si128(__m128i *p, __m128i a)
1051 {
1052 	*p = a;
1053 }
1054 
1055 // Cache line containing p is flushed and invalidated from all caches in the coherency domain.
_mm_clflush(void const * p)1056 FORCE_INLINE void _mm_clflush(void const*p)
1057 {
1058 	// no corollary for Neon?
1059 }
1060 
_mm_empty()1061 FORCE_INLINE void _mm_empty()
1062 {
1063 }
1064 
1065 #define SSE2NEON (1)
1066 
1067 #endif
1068