1 /****************************************************************************
2 * Copyright (C) 2017 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ****************************************************************************/
23 #if !defined(__SIMD_LIB_AVX_HPP__)
24 #error Do not include this file directly, use "simdlib.hpp" instead.
27 using SIMD128T = SIMD128Impl::AVXImpl;
29 //============================================================================
30 // SIMD256 AVX (1) implementation
31 //============================================================================
33 #define SIMD_WRAPPER_1(op) \
34 static SIMDINLINE Float SIMDCALL op(Float a) \
36 return _mm256_##op(a);\
39 #define SIMD_WRAPPER_2(op) \
40 static SIMDINLINE Float SIMDCALL op(Float a, Float b) \
42 return _mm256_##op(a, b);\
45 #define SIMD_DWRAPPER_2(op) \
46 static SIMDINLINE Double SIMDCALL op(Double a, Double b) \
48 return _mm256_##op(a, b);\
51 #define SIMD_WRAPPER_2I(op) \
53 static SIMDINLINE Float SIMDCALL op(Float a, Float b) \
55 return _mm256_##op(a, b, ImmT);\
58 #define SIMD_DWRAPPER_2I(op) \
60 static SIMDINLINE Double SIMDCALL op(Double a, Double b) \
62 return _mm256_##op(a, b, ImmT);\
65 #define SIMD_WRAPPER_3(op) \
66 static SIMDINLINE Float SIMDCALL op(Float a, Float b, Float c) \
68 return _mm256_##op(a, b, c);\
71 #define SIMD_IWRAPPER_1(op) \
72 static SIMDINLINE Integer SIMDCALL op(Integer a) \
74 return _mm256_##op(a);\
77 #define SIMD_IWRAPPER_2(op) \
78 static SIMDINLINE Integer SIMDCALL op(Integer a, Integer b) \
80 return _mm256_##op(a, b);\
83 #define SIMD_IFWRAPPER_2(op, intrin) \
84 static SIMDINLINE Integer SIMDCALL op(Integer a, Integer b) \
86 return castps_si( intrin(castsi_ps(a), castsi_ps(b)) );\
89 #define SIMD_IFWRAPPER_2I(op, intrin) \
91 static SIMDINLINE Integer SIMDCALL op(Integer a, Integer b) \
93 return castps_si( intrin(castsi_ps(a), castsi_ps(b), ImmT) );\
96 #define SIMD_IWRAPPER_2I_(op, intrin) \
98 static SIMDINLINE Integer SIMDCALL op(Integer a, Integer b) \
100 return _mm256_##intrin(a, b, ImmT);\
102 #define SIMD_IWRAPPER_2I(op) SIMD_IWRAPPER_2I_(op, op)
104 #define SIMD_IWRAPPER_3(op) \
105 static SIMDINLINE Integer SIMDCALL op(Integer a, Integer b, Integer c) \
107 return _mm256_##op(a, b, c);\
110 // emulated integer simd
111 #define SIMD_EMU_IWRAPPER_1(op) \
113 Integer SIMDCALL op(Integer a)\
117 SIMD128T::op(a.v4[0]),\
118 SIMD128T::op(a.v4[1]),\
121 #define SIMD_EMU_IWRAPPER_1L(op, shift) \
123 Integer SIMDCALL op(Integer a)\
127 SIMD128T::op(a.v4[0]), \
128 SIMD128T::op(SIMD128T::template srli_si<shift>(a.v4[0])), \
132 Integer SIMDCALL op(SIMD128Impl::Integer a)\
137 SIMD128T::op(SIMD128T::template srli_si<shift>(a)), \
141 #define SIMD_EMU_IWRAPPER_1I(op) \
142 template <int ImmT> static SIMDINLINE \
143 Integer SIMDCALL op(Integer a)\
147 SIMD128T::template op<ImmT>(a.v4[0]),\
148 SIMD128T::template op<ImmT>(a.v4[1]),\
152 #define SIMD_EMU_IWRAPPER_2(op) \
154 Integer SIMDCALL op(Integer a, Integer b)\
158 SIMD128T::op(a.v4[0], b.v4[0]),\
159 SIMD128T::op(a.v4[1], b.v4[1]),\
163 #define SIMD_EMU_IWRAPPER_2I(op) \
164 template <int ImmT> static SIMDINLINE \
165 Integer SIMDCALL op(Integer a, Integer b)\
169 SIMD128T::template op<ImmT>(a.v4[0], b.v[0]),\
170 SIMD128T::template op<ImmT>(a.v4[1], b.v[1]),\
174 //-----------------------------------------------------------------------
175 // Single precision floating point arithmetic operations
176 //-----------------------------------------------------------------------
177 SIMD_WRAPPER_2(add_ps); // return a + b
178 SIMD_WRAPPER_2(div_ps); // return a / b
180 static SIMDINLINE Float SIMDCALL fmadd_ps(Float a, Float b, Float c) // return (a * b) + c
182 return add_ps(mul_ps(a, b), c);
185 static SIMDINLINE Float SIMDCALL fmsub_ps(Float a, Float b, Float c) // return (a * b) - c
187 return sub_ps(mul_ps(a, b), c);
190 SIMD_WRAPPER_2(max_ps); // return (a > b) ? a : b
191 SIMD_WRAPPER_2(min_ps); // return (a < b) ? a : b
192 SIMD_WRAPPER_2(mul_ps); // return a * b
193 SIMD_WRAPPER_1(rcp_ps); // return 1.0f / a
194 SIMD_WRAPPER_1(rsqrt_ps); // return 1.0f / sqrt(a)
195 SIMD_WRAPPER_2(sub_ps); // return a - b
197 template <RoundMode RMT>
198 static SIMDINLINE Float SIMDCALL round_ps(Float a)
200 return _mm256_round_ps(a, static_cast<int>(RMT));
203 static SIMDINLINE Float SIMDCALL ceil_ps(Float a) { return round_ps<RoundMode::CEIL_NOEXC>(a); }
204 static SIMDINLINE Float SIMDCALL floor_ps(Float a) { return round_ps<RoundMode::FLOOR_NOEXC>(a); }
206 //-----------------------------------------------------------------------
207 // Integer (various width) arithmetic operations
208 //-----------------------------------------------------------------------
209 SIMD_EMU_IWRAPPER_1(abs_epi32); // return absolute_value(a) (int32)
210 SIMD_EMU_IWRAPPER_2(add_epi32); // return a + b (int32)
211 SIMD_EMU_IWRAPPER_2(add_epi8); // return a + b (int8)
212 SIMD_EMU_IWRAPPER_2(adds_epu8); // return ((a + b) > 0xff) ? 0xff : (a + b) (uint8)
213 SIMD_EMU_IWRAPPER_2(max_epi32); // return (a > b) ? a : b (int32)
214 SIMD_EMU_IWRAPPER_2(max_epu32); // return (a > b) ? a : b (uint32)
215 SIMD_EMU_IWRAPPER_2(min_epi32); // return (a < b) ? a : b (int32)
216 SIMD_EMU_IWRAPPER_2(min_epu32); // return (a < b) ? a : b (uint32)
217 SIMD_EMU_IWRAPPER_2(mul_epi32); // return a * b (int32)
219 // return (a * b) & 0xFFFFFFFF
221 // Multiply the packed 32-bit integers in a and b, producing intermediate 64-bit integers,
222 // and store the low 32 bits of the intermediate integers in dst.
223 SIMD_EMU_IWRAPPER_2(mullo_epi32);
224 SIMD_EMU_IWRAPPER_2(sub_epi32); // return a - b (int32)
225 SIMD_EMU_IWRAPPER_2(sub_epi64); // return a - b (int64)
226 SIMD_EMU_IWRAPPER_2(subs_epu8); // return (b > a) ? 0 : (a - b) (uint8)
228 //-----------------------------------------------------------------------
229 // Logical operations
230 //-----------------------------------------------------------------------
231 SIMD_WRAPPER_2(and_ps); // return a & b (float treated as int)
232 SIMD_EMU_IWRAPPER_2(and_si); // return a & b (int)
233 SIMD_WRAPPER_2(andnot_ps); // return (~a) & b (float treated as int)
234 SIMD_EMU_IWRAPPER_2(andnot_si); // return (~a) & b (int)
235 SIMD_WRAPPER_2(or_ps); // return a | b (float treated as int)
236 SIMD_EMU_IWRAPPER_2(or_si); // return a | b (int)
237 SIMD_WRAPPER_2(xor_ps); // return a ^ b (float treated as int)
238 SIMD_EMU_IWRAPPER_2(xor_si); // return a ^ b (int)
241 //-----------------------------------------------------------------------
243 //-----------------------------------------------------------------------
244 SIMD_EMU_IWRAPPER_1I(slli_epi32); // return a << ImmT
246 static SIMDINLINE Integer SIMDCALL sllv_epi32(Integer vA, Integer vCount) // return a << b (uint32)
248 int32_t aHi, aLow, countHi, countLow;
249 __m128i vAHi = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vA), 1));
250 __m128i vALow = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vA), 0));
251 __m128i vCountHi = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vCount), 1));
252 __m128i vCountLow = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vCount), 0));
254 aHi = _mm_extract_epi32(vAHi, 0);
255 countHi = _mm_extract_epi32(vCountHi, 0);
257 vAHi = _mm_insert_epi32(vAHi, aHi, 0);
259 aLow = _mm_extract_epi32(vALow, 0);
260 countLow = _mm_extract_epi32(vCountLow, 0);
262 vALow = _mm_insert_epi32(vALow, aLow, 0);
264 aHi = _mm_extract_epi32(vAHi, 1);
265 countHi = _mm_extract_epi32(vCountHi, 1);
267 vAHi = _mm_insert_epi32(vAHi, aHi, 1);
269 aLow = _mm_extract_epi32(vALow, 1);
270 countLow = _mm_extract_epi32(vCountLow, 1);
272 vALow = _mm_insert_epi32(vALow, aLow, 1);
274 aHi = _mm_extract_epi32(vAHi, 2);
275 countHi = _mm_extract_epi32(vCountHi, 2);
277 vAHi = _mm_insert_epi32(vAHi, aHi, 2);
279 aLow = _mm_extract_epi32(vALow, 2);
280 countLow = _mm_extract_epi32(vCountLow, 2);
282 vALow = _mm_insert_epi32(vALow, aLow, 2);
284 aHi = _mm_extract_epi32(vAHi, 3);
285 countHi = _mm_extract_epi32(vCountHi, 3);
287 vAHi = _mm_insert_epi32(vAHi, aHi, 3);
289 aLow = _mm_extract_epi32(vALow, 3);
290 countLow = _mm_extract_epi32(vCountLow, 3);
292 vALow = _mm_insert_epi32(vALow, aLow, 3);
294 __m256i ret = _mm256_set1_epi32(0);
295 ret = _mm256_insertf128_si256(ret, vAHi, 1);
296 ret = _mm256_insertf128_si256(ret, vALow, 0);
300 SIMD_EMU_IWRAPPER_1I(srai_epi32); // return a >> ImmT (int32)
301 SIMD_EMU_IWRAPPER_1I(srli_epi32); // return a >> ImmT (uint32)
302 SIMD_EMU_IWRAPPER_1I(srli_si); // return a >> (ImmT*8) (uint)
304 template<int ImmT> // same as srli_si, but with Float cast to int
305 static SIMDINLINE Float SIMDCALL srlisi_ps(Float a)
307 return castsi_ps(srli_si<ImmT>(castps_si(a)));
310 static SIMDINLINE Integer SIMDCALL srlv_epi32(Integer vA, Integer vCount) // return a >> b (uint32)
312 int32_t aHi, aLow, countHi, countLow;
313 __m128i vAHi = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vA), 1));
314 __m128i vALow = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vA), 0));
315 __m128i vCountHi = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vCount), 1));
316 __m128i vCountLow = _mm_castps_si128(_mm256_extractf128_ps(_mm256_castsi256_ps(vCount), 0));
318 aHi = _mm_extract_epi32(vAHi, 0);
319 countHi = _mm_extract_epi32(vCountHi, 0);
321 vAHi = _mm_insert_epi32(vAHi, aHi, 0);
323 aLow = _mm_extract_epi32(vALow, 0);
324 countLow = _mm_extract_epi32(vCountLow, 0);
326 vALow = _mm_insert_epi32(vALow, aLow, 0);
328 aHi = _mm_extract_epi32(vAHi, 1);
329 countHi = _mm_extract_epi32(vCountHi, 1);
331 vAHi = _mm_insert_epi32(vAHi, aHi, 1);
333 aLow = _mm_extract_epi32(vALow, 1);
334 countLow = _mm_extract_epi32(vCountLow, 1);
336 vALow = _mm_insert_epi32(vALow, aLow, 1);
338 aHi = _mm_extract_epi32(vAHi, 2);
339 countHi = _mm_extract_epi32(vCountHi, 2);
341 vAHi = _mm_insert_epi32(vAHi, aHi, 2);
343 aLow = _mm_extract_epi32(vALow, 2);
344 countLow = _mm_extract_epi32(vCountLow, 2);
346 vALow = _mm_insert_epi32(vALow, aLow, 2);
348 aHi = _mm_extract_epi32(vAHi, 3);
349 countHi = _mm_extract_epi32(vCountHi, 3);
351 vAHi = _mm_insert_epi32(vAHi, aHi, 3);
353 aLow = _mm_extract_epi32(vALow, 3);
354 countLow = _mm_extract_epi32(vCountLow, 3);
356 vALow = _mm_insert_epi32(vALow, aLow, 3);
358 __m256i ret = _mm256_set1_epi32(0);
359 ret = _mm256_insertf128_si256(ret, vAHi, 1);
360 ret = _mm256_insertf128_si256(ret, vALow, 0);
366 //-----------------------------------------------------------------------
367 // Conversion operations
368 //-----------------------------------------------------------------------
369 static SIMDINLINE Float SIMDCALL castpd_ps(Double a) // return *(Float*)(&a)
371 return _mm256_castpd_ps(a);
374 static SIMDINLINE Integer SIMDCALL castps_si(Float a) // return *(Integer*)(&a)
376 return _mm256_castps_si256(a);
379 static SIMDINLINE Double SIMDCALL castsi_pd(Integer a) // return *(Double*)(&a)
381 return _mm256_castsi256_pd(a);
384 static SIMDINLINE Double SIMDCALL castps_pd(Float a) // return *(Double*)(&a)
386 return _mm256_castps_pd(a);
389 static SIMDINLINE Integer SIMDCALL castpd_si(Double a) // return *(Integer*)(&a)
391 return _mm256_castpd_si256(a);
394 static SIMDINLINE Float SIMDCALL castsi_ps(Integer a) // return *(Float*)(&a)
396 return _mm256_castsi256_ps(a);
399 static SIMDINLINE Float SIMDCALL cvtepi32_ps(Integer a) // return (float)a (int32 --> float)
401 return _mm256_cvtepi32_ps(a);
404 SIMD_EMU_IWRAPPER_1L(cvtepu8_epi16, 8); // return (int16)a (uint8 --> int16)
405 SIMD_EMU_IWRAPPER_1L(cvtepu8_epi32, 4); // return (int32)a (uint8 --> int32)
406 SIMD_EMU_IWRAPPER_1L(cvtepu16_epi32, 8); // return (int32)a (uint16 --> int32)
407 SIMD_EMU_IWRAPPER_1L(cvtepu16_epi64, 4); // return (int64)a (uint16 --> int64)
408 SIMD_EMU_IWRAPPER_1L(cvtepu32_epi64, 8); // return (int64)a (uint32 --> int64)
410 static SIMDINLINE Integer SIMDCALL cvtps_epi32(Float a) // return (int32)a (float --> int32)
412 return _mm256_cvtps_epi32(a);
415 static SIMDINLINE Integer SIMDCALL cvttps_epi32(Float a) // return (int32)a (rnd_to_zero(float) --> int32)
417 return _mm256_cvttps_epi32(a);
420 //-----------------------------------------------------------------------
421 // Comparison operations
422 //-----------------------------------------------------------------------
423 template<CompareType CmpTypeT>
424 static SIMDINLINE Float SIMDCALL cmp_ps(Float a, Float b) // return a (CmpTypeT) b
426 return _mm256_cmp_ps(a, b, static_cast<const int>(CmpTypeT));
428 static SIMDINLINE Float SIMDCALL cmplt_ps(Float a, Float b) { return cmp_ps<CompareType::LT_OQ>(a, b); }
429 static SIMDINLINE Float SIMDCALL cmpgt_ps(Float a, Float b) { return cmp_ps<CompareType::GT_OQ>(a, b); }
430 static SIMDINLINE Float SIMDCALL cmpneq_ps(Float a, Float b) { return cmp_ps<CompareType::NEQ_OQ>(a, b); }
431 static SIMDINLINE Float SIMDCALL cmpeq_ps(Float a, Float b) { return cmp_ps<CompareType::EQ_OQ>(a, b); }
432 static SIMDINLINE Float SIMDCALL cmpge_ps(Float a, Float b) { return cmp_ps<CompareType::GE_OQ>(a, b); }
433 static SIMDINLINE Float SIMDCALL cmple_ps(Float a, Float b) { return cmp_ps<CompareType::LE_OQ>(a, b); }
435 SIMD_EMU_IWRAPPER_2(cmpeq_epi8); // return a == b (int8)
436 SIMD_EMU_IWRAPPER_2(cmpeq_epi16); // return a == b (int16)
437 SIMD_EMU_IWRAPPER_2(cmpeq_epi32); // return a == b (int32)
438 SIMD_EMU_IWRAPPER_2(cmpeq_epi64); // return a == b (int64)
439 SIMD_EMU_IWRAPPER_2(cmpgt_epi8); // return a > b (int8)
440 SIMD_EMU_IWRAPPER_2(cmpgt_epi16); // return a > b (int16)
441 SIMD_EMU_IWRAPPER_2(cmpgt_epi32); // return a > b (int32)
442 SIMD_EMU_IWRAPPER_2(cmpgt_epi64); // return a > b (int64)
443 SIMD_EMU_IWRAPPER_2(cmplt_epi32); // return a < b (int32)
445 static SIMDINLINE bool SIMDCALL testz_ps(Float a, Float b) // return all_lanes_zero(a & b) ? 1 : 0 (float)
447 return 0 != _mm256_testz_ps(a, b);
450 static SIMDINLINE bool SIMDCALL testz_si(Integer a, Integer b) // return all_lanes_zero(a & b) ? 1 : 0 (int)
452 return 0 != _mm256_testz_si256(a, b);
455 //-----------------------------------------------------------------------
456 // Blend / shuffle / permute operations
457 //-----------------------------------------------------------------------
458 SIMD_WRAPPER_2I(blend_ps); // return ImmT ? b : a (float)
459 SIMD_IFWRAPPER_2I(blend_epi32, _mm256_blend_ps); // return ImmT ? b : a (int32)
460 SIMD_WRAPPER_3(blendv_ps); // return mask ? b : a (float)
462 static SIMDINLINE Integer SIMDCALL blendv_epi32(Integer a, Integer b, Float mask) // return mask ? b : a (int)
464 return castps_si(blendv_ps(castsi_ps(a), castsi_ps(b), mask));
467 static SIMDINLINE Integer SIMDCALL blendv_epi32(Integer a, Integer b, Integer mask) // return mask ? b : a (int)
469 return castps_si(blendv_ps(castsi_ps(a), castsi_ps(b), castsi_ps(mask)));
472 static SIMDINLINE Float SIMDCALL broadcast_ss(float const *p) // return *p (all elements in vector get same value)
474 return _mm256_broadcast_ss(p);
477 SIMD_EMU_IWRAPPER_2(packs_epi16); // See documentation for _mm256_packs_epi16 and _mm512_packs_epi16
478 SIMD_EMU_IWRAPPER_2(packs_epi32); // See documentation for _mm256_packs_epi32 and _mm512_packs_epi32
479 SIMD_EMU_IWRAPPER_2(packus_epi16); // See documentation for _mm256_packus_epi16 and _mm512_packus_epi16
480 SIMD_EMU_IWRAPPER_2(packus_epi32); // See documentation for _mm256_packus_epi32 and _mm512_packus_epi32
482 static SIMDINLINE Integer SIMDCALL permute_epi32(Integer a, Integer swiz) // return a[swiz[i]] for each 32-bit lane i (int32)
486 // Ugly slow implementation
487 uint32_t const *pA = reinterpret_cast<uint32_t const*>(&a);
488 uint32_t const *pSwiz = reinterpret_cast<uint32_t const*>(&swiz);
489 uint32_t *pResult = reinterpret_cast<uint32_t *>(&result);
491 for (uint32_t i = 0; i < SIMD_WIDTH; ++i)
493 pResult[i] = pA[0xF & pSwiz[i]];
499 static SIMDINLINE Float SIMDCALL permute_ps(Float a, Integer swiz) // return a[swiz[i]] for each 32-bit lane i (float)
503 // Ugly slow implementation
504 float const *pA = reinterpret_cast<float const*>(&a);
505 uint32_t const *pSwiz = reinterpret_cast<uint32_t const*>(&swiz);
506 float *pResult = reinterpret_cast<float *>(&result);
508 for (uint32_t i = 0; i < SIMD_WIDTH; ++i)
510 pResult[i] = pA[0xF & pSwiz[i]];
516 SIMD_WRAPPER_2I(permute2f128_ps);
517 SIMD_DWRAPPER_2I(permute2f128_pd);
518 SIMD_IWRAPPER_2I_(permute2f128_si, permute2f128_si256);
521 SIMD_EMU_IWRAPPER_1I(shuffle_epi32);
524 static SIMDINLINE Integer SIMDCALL shuffle_epi64(Integer a, Integer b)
526 return castpd_si(shuffle_pd<ImmT>(castsi_pd(a), castsi_pd(b)));
528 SIMD_EMU_IWRAPPER_2(shuffle_epi8);
529 SIMD_DWRAPPER_2I(shuffle_pd);
530 SIMD_WRAPPER_2I(shuffle_ps);
531 SIMD_EMU_IWRAPPER_2(unpackhi_epi16);
532 SIMD_IFWRAPPER_2(unpackhi_epi32, _mm256_unpackhi_ps);
533 SIMD_EMU_IWRAPPER_2(unpackhi_epi64);
534 SIMD_EMU_IWRAPPER_2(unpackhi_epi8);
535 SIMD_DWRAPPER_2(unpackhi_pd);
536 SIMD_WRAPPER_2(unpackhi_ps);
537 SIMD_EMU_IWRAPPER_2(unpacklo_epi16);
538 SIMD_IFWRAPPER_2(unpacklo_epi32, _mm256_unpacklo_ps);
539 SIMD_EMU_IWRAPPER_2(unpacklo_epi64);
540 SIMD_EMU_IWRAPPER_2(unpacklo_epi8);
541 SIMD_DWRAPPER_2(unpacklo_pd);
542 SIMD_WRAPPER_2(unpacklo_ps);
544 //-----------------------------------------------------------------------
545 // Load / store operations
546 //-----------------------------------------------------------------------
547 template<ScaleFactor ScaleT>
548 static SIMDINLINE Float SIMDCALL i32gather_ps(float const* p, Integer idx) // return *(float*)(((int8*)p) + (idx * ScaleT))
550 uint32_t *pOffsets = (uint32_t*)&idx;
552 float* pResult = (float*)&vResult;
553 for (uint32_t i = 0; i < SIMD_WIDTH; ++i)
555 uint32_t offset = pOffsets[i];
556 offset = offset * static_cast<uint32_t>(ScaleT);
557 pResult[i] = *(float const*)(((uint8_t const*)p + offset));
563 static SIMDINLINE Float SIMDCALL load1_ps(float const *p) // return *p (broadcast 1 value to all elements)
565 return broadcast_ss(p);
568 static SIMDINLINE Float SIMDCALL load_ps(float const *p) // return *p (loads SIMD width elements from memory)
570 return _mm256_load_ps(p);
573 static SIMDINLINE Integer SIMDCALL load_si(Integer const *p) // return *p
575 return _mm256_load_si256(&p->v);
578 static SIMDINLINE Float SIMDCALL loadu_ps(float const *p) // return *p (same as load_ps but allows for unaligned mem)
580 return _mm256_loadu_ps(p);
583 static SIMDINLINE Integer SIMDCALL loadu_si(Integer const *p) // return *p (same as load_si but allows for unaligned mem)
585 return _mm256_lddqu_si256(&p->v);
588 // for each element: (mask & (1 << 31)) ? (i32gather_ps<ScaleT>(p, idx), mask = 0) : old
589 template<ScaleFactor ScaleT>
590 static SIMDINLINE Float SIMDCALL mask_i32gather_ps(Float old, float const* p, Integer idx, Float mask)
592 uint32_t *pOffsets = (uint32_t*)&idx;
594 float* pResult = (float*)&vResult;
596 uint32_t umask = movemask_ps(mask);
597 while (_BitScanForward(&index, umask))
599 umask &= ~(1 << index);
600 uint32_t offset = pOffsets[index];
601 offset = offset * static_cast<uint32_t>(ScaleT);
602 pResult[index] = *(float const *)(((uint8_t const *)p + offset));
608 static SIMDINLINE void SIMDCALL maskstore_ps(float *p, Integer mask, Float src)
610 _mm256_maskstore_ps(p, mask, src);
613 static SIMDINLINE uint32_t SIMDCALL movemask_epi8(Integer a)
615 return SIMD128T::movemask_epi8(a.v4[0]) |
616 (SIMD128T::movemask_epi8(a.v4[1]) << 16);
619 static SIMDINLINE uint32_t SIMDCALL movemask_pd(Double a)
621 return static_cast<uint32_t>(_mm256_movemask_pd(a));
623 static SIMDINLINE uint32_t SIMDCALL movemask_ps(Float a)
625 return static_cast<uint32_t>(_mm256_movemask_ps(a));
628 static SIMDINLINE Integer SIMDCALL set1_epi32(int i) // return i (all elements are same value)
630 return _mm256_set1_epi32(i);
633 static SIMDINLINE Integer SIMDCALL set1_epi8(char i) // return i (all elements are same value)
635 return _mm256_set1_epi8(i);
638 static SIMDINLINE Float SIMDCALL set1_ps(float f) // return f (all elements are same value)
640 return _mm256_set1_ps(f);
643 static SIMDINLINE Float SIMDCALL setzero_ps() // return 0 (float)
645 return _mm256_setzero_ps();
648 static SIMDINLINE Integer SIMDCALL setzero_si() // return 0 (integer)
650 return _mm256_setzero_si256();
653 static SIMDINLINE void SIMDCALL store_ps(float *p, Float a) // *p = a (stores all elements contiguously in memory)
655 _mm256_store_ps(p, a);
658 static SIMDINLINE void SIMDCALL store_si(Integer *p, Integer a) // *p = a
660 _mm256_store_si256(&p->v, a);
663 static SIMDINLINE void SIMDCALL stream_ps(float *p, Float a) // *p = a (same as store_ps, but doesn't keep memory in cache)
665 _mm256_stream_ps(p, a);
668 //=======================================================================
669 // Legacy interface (available only in SIMD256 width)
670 //=======================================================================
672 static SIMDINLINE Float SIMDCALL broadcast_ps(SIMD128Impl::Float const *p)
674 return _mm256_broadcast_ps(&p->v);
678 static SIMDINLINE SIMD128Impl::Double SIMDCALL extractf128_pd(Double a)
680 return _mm256_extractf128_pd(a, ImmT);
684 static SIMDINLINE SIMD128Impl::Float SIMDCALL extractf128_ps(Float a)
686 return _mm256_extractf128_ps(a, ImmT);
690 static SIMDINLINE SIMD128Impl::Integer SIMDCALL extractf128_si(Integer a)
692 return _mm256_extractf128_si256(a, ImmT);
696 static SIMDINLINE Double SIMDCALL insertf128_pd(Double a, SIMD128Impl::Double b)
698 return _mm256_insertf128_pd(a, b, ImmT);
702 static SIMDINLINE Float SIMDCALL insertf128_ps(Float a, SIMD128Impl::Float b)
704 return _mm256_insertf128_ps(a, b, ImmT);
708 static SIMDINLINE Integer SIMDCALL insertf128_si(Integer a, SIMD128Impl::Integer b)
710 return _mm256_insertf128_si256(a, b, ImmT);
713 #ifndef _mm256_set_m128i
714 #define _mm256_set_m128i(/* SIMD128Impl::Integer */ hi, /* SIMD128Impl::Integer */ lo) \
715 _mm256_insertf128_si256(_mm256_castsi128_si256(lo), (hi), 0x1)
718 #ifndef _mm256_loadu2_m128i
719 #define _mm256_loadu2_m128i(/* SIMD128Impl::Integer const* */ hiaddr, \
720 /* SIMD128Impl::Integer const* */ loaddr) \
721 _mm256_set_m128i(_mm_loadu_si128(hiaddr), _mm_loadu_si128(loaddr))
724 static SIMDINLINE Integer SIMDCALL loadu2_si(SIMD128Impl::Integer const* phi, SIMD128Impl::Integer const* plo)
726 return _mm256_loadu2_m128i(&phi->v, &plo->v);
729 static SIMDINLINE Integer SIMDCALL set_epi32(int i7, int i6, int i5, int i4, int i3, int i2, int i1, int i0)
731 return _mm256_set_epi32(i7, i6, i5, i4, i3, i2, i1, i0);
734 static SIMDINLINE Float SIMDCALL set_ps(float i7, float i6, float i5, float i4, float i3, float i2, float i1, float i0)
736 return _mm256_set_ps(i7, i6, i5, i4, i3, i2, i1, i0);
739 static SIMDINLINE void SIMDCALL storeu2_si(SIMD128Impl::Integer *phi, SIMD128Impl::Integer *plo, Integer src)
741 _mm256_storeu2_m128i(&phi->v, &plo->v, src);
744 static SIMDINLINE Float SIMDCALL vmask_ps(int32_t mask)
746 Integer vec = set1_epi32(mask);
747 const Integer bit = set_epi32(
748 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01);
749 vec = and_si(vec, bit);
750 vec = cmplt_epi32(setzero_si(), vec);
751 return castsi_ps(vec);
754 #undef SIMD_WRAPPER_1
755 #undef SIMD_WRAPPER_2
756 #undef SIMD_DWRAPPER_2
757 #undef SIMD_DWRAPPER_2I
758 #undef SIMD_WRAPPER_2I
759 #undef SIMD_WRAPPER_3
760 #undef SIMD_IWRAPPER_1
761 #undef SIMD_IWRAPPER_2
762 #undef SIMD_IFWRAPPER_2
763 #undef SIMD_IFWRAPPER_2I
764 #undef SIMD_IWRAPPER_2I
765 #undef SIMD_IWRAPPER_2I_
766 #undef SIMD_IWRAPPER_2_
767 #undef SIMD_IWRAPPER_3
768 #undef SIMD_EMU_IWRAPPER_1
769 #undef SIMD_EMU_IWRAPPER_1I
770 #undef SIMD_EMU_IWRAPPER_2
771 #undef SIMD_EMU_IWRAPPER_2I