vint8_avx2.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #define vboolf vboolf_impl
  5. #define vboold vboold_impl
  6. #define vint vint_impl
  7. #define vuint vuint_impl
  8. #define vllong vllong_impl
  9. #define vfloat vfloat_impl
  10. #define vdouble vdouble_impl
  11. namespace embree
  12. {
  13. /* 8-wide AVX integer type */
  14. template<>
  15. struct vint<8>
  16. {
  17. ALIGNED_STRUCT_(32);
  18. typedef vboolf8 Bool;
  19. typedef vint8 Int;
  20. typedef vfloat8 Float;
  21. enum { size = 8 }; // number of SIMD elements
  22. union { // data
  23. __m256i v;
  24. int i[8];
  25. };
  26. ////////////////////////////////////////////////////////////////////////////////
  27. /// Constructors, Assignment & Cast Operators
  28. ////////////////////////////////////////////////////////////////////////////////
  29. __forceinline vint() {}
  30. __forceinline vint(const vint8& a) { v = a.v; }
  31. __forceinline vint8& operator =(const vint8& a) { v = a.v; return *this; }
  32. __forceinline vint(__m256i a) : v(a) {}
  33. __forceinline operator const __m256i&() const { return v; }
  34. __forceinline operator __m256i&() { return v; }
  35. __forceinline explicit vint(const vint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}
  36. __forceinline vint(const vint4& a, const vint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
  37. __forceinline vint(const __m128i& a, const __m128i& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
  38. __forceinline explicit vint(const int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}
  39. __forceinline vint(int a) : v(_mm256_set1_epi32(a)) {}
  40. __forceinline vint(int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}
  41. __forceinline vint(int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}
  42. __forceinline vint(int a, int b, int c, int d, int e, int f, int g, int h) : v(_mm256_set_epi32(h, g, f, e, d, c, b, a)) {}
  43. __forceinline explicit vint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}
  44. #if defined(__AVX512VL__)
  45. __forceinline explicit vint(const vboolf8& a) : v(_mm256_movm_epi32(a)) {}
  46. #else
  47. __forceinline explicit vint(const vboolf8& a) : v(_mm256_castps_si256((__m256)a)) {}
  48. #endif
  49. ////////////////////////////////////////////////////////////////////////////////
  50. /// Constants
  51. ////////////////////////////////////////////////////////////////////////////////
  52. __forceinline vint(ZeroTy) : v(_mm256_setzero_si256()) {}
  53. __forceinline vint(OneTy) : v(_mm256_set1_epi32(1)) {}
  54. __forceinline vint(PosInfTy) : v(_mm256_set1_epi32(pos_inf)) {}
  55. __forceinline vint(NegInfTy) : v(_mm256_set1_epi32(neg_inf)) {}
  56. __forceinline vint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}
  57. __forceinline vint(ReverseStepTy) : v(_mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7)) {}
  58. __forceinline vint(UndefinedTy) : v(_mm256_undefined_si256()) {}
  59. ////////////////////////////////////////////////////////////////////////////////
  60. /// Loads and Stores
  61. ////////////////////////////////////////////////////////////////////////////////
  62. static __forceinline vint8 load(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }
  63. static __forceinline vint8 loadu(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }
  64. static __forceinline vint8 load(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_load_si128((__m128i*)ptr)); }
  65. static __forceinline vint8 loadu(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr)); }
  66. static __forceinline vint8 load(const void* ptr) { return _mm256_load_si256((__m256i*)ptr); }
  67. static __forceinline vint8 loadu(const void* ptr) { return _mm256_loadu_si256((__m256i*)ptr); }
  68. static __forceinline void store (void* ptr, const vint8& v) { _mm256_store_si256((__m256i*)ptr,v); }
  69. static __forceinline void storeu(void* ptr, const vint8& v) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(v)); }
  70. #if defined(__AVX512VL__)
  71. static __forceinline vint8 compact(const vboolf8& mask, vint8 &v) {
  72. return _mm256_mask_compress_epi32(v, mask, v);
  73. }
  74. static __forceinline vint8 compact(const vboolf8& mask, vint8 &a, const vint8& b) {
  75. return _mm256_mask_compress_epi32(a, mask, b);
  76. }
  77. static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_mask_load_epi32 (_mm256_setzero_si256(),mask,ptr); }
  78. static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_mask_loadu_epi32(_mm256_setzero_si256(),mask,ptr); }
  79. static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_store_epi32 (ptr,mask,v); }
  80. static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_storeu_epi32(ptr,mask,v); }
  81. #else
  82. static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
  83. static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
  84. static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
  85. static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
  86. #endif
  87. static __forceinline vint8 load_nt(void* ptr) {
  88. return _mm256_stream_load_si256((__m256i*)ptr);
  89. }
  90. static __forceinline void store_nt(void* ptr, const vint8& v) {
  91. _mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));
  92. }
  93. static __forceinline void store(unsigned char* ptr, const vint8& i)
  94. {
  95. for (size_t j=0; j<8; j++)
  96. ptr[j] = i[j];
  97. }
  98. static __forceinline void store(unsigned short* ptr, const vint8& v) {
  99. for (size_t i=0;i<8;i++)
  100. ptr[i] = (unsigned short)v[i];
  101. }
  102. template<int scale = 4>
  103. static __forceinline vint8 gather(const int *const ptr, const vint8& index) {
  104. return _mm256_i32gather_epi32(ptr, index, scale);
  105. }
  106. template<int scale = 4>
  107. static __forceinline vint8 gather(const vboolf8& mask, const int *const ptr, const vint8& index) {
  108. vint8 r = zero;
  109. #if defined(__AVX512VL__)
  110. return _mm256_mmask_i32gather_epi32(r, mask, index, ptr, scale);
  111. #else
  112. return _mm256_mask_i32gather_epi32(r, ptr, index, mask, scale);
  113. #endif
  114. }
  115. template<int scale = 4>
  116. static __forceinline void scatter(void* ptr, const vint8& ofs, const vint8& v)
  117. {
  118. #if defined(__AVX512VL__)
  119. _mm256_i32scatter_epi32((int*)ptr, ofs, v, scale);
  120. #else
  121. *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
  122. *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
  123. *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
  124. *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
  125. *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
  126. *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
  127. *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
  128. *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
  129. #endif
  130. }
  131. template<int scale = 4>
  132. static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vint8& v)
  133. {
  134. #if defined(__AVX512VL__)
  135. _mm256_mask_i32scatter_epi32((int*)ptr, mask, ofs, v, scale);
  136. #else
  137. if (likely(mask[0])) *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
  138. if (likely(mask[1])) *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
  139. if (likely(mask[2])) *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
  140. if (likely(mask[3])) *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
  141. if (likely(mask[4])) *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
  142. if (likely(mask[5])) *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
  143. if (likely(mask[6])) *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
  144. if (likely(mask[7])) *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
  145. #endif
  146. }
  147. static __forceinline vint8 broadcast64(const long long &a) { return _mm256_set1_epi64x(a); }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. /// Array Access
  150. ////////////////////////////////////////////////////////////////////////////////
  151. __forceinline const int& operator [](size_t index) const { assert(index < 8); return i[index]; }
  152. __forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }
  153. };
  154. ////////////////////////////////////////////////////////////////////////////////
  155. /// Unary Operators
  156. ////////////////////////////////////////////////////////////////////////////////
  157. #if defined(__AVX512VL__)
  158. static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_movepi32_mask(a); }
  159. #else
  160. static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_castsi256_ps(a); }
  161. #endif
  162. __forceinline vint8 operator +(const vint8& a) { return a; }
  163. __forceinline vint8 operator -(const vint8& a) { return _mm256_sub_epi32(_mm256_setzero_si256(), a); }
  164. __forceinline vint8 abs (const vint8& a) { return _mm256_abs_epi32(a); }
  165. ////////////////////////////////////////////////////////////////////////////////
  166. /// Binary Operators
  167. ////////////////////////////////////////////////////////////////////////////////
  168. __forceinline vint8 operator +(const vint8& a, const vint8& b) { return _mm256_add_epi32(a, b); }
  169. __forceinline vint8 operator +(const vint8& a, int b) { return a + vint8(b); }
  170. __forceinline vint8 operator +(int a, const vint8& b) { return vint8(a) + b; }
  171. __forceinline vint8 operator -(const vint8& a, const vint8& b) { return _mm256_sub_epi32(a, b); }
  172. __forceinline vint8 operator -(const vint8& a, int b) { return a - vint8(b); }
  173. __forceinline vint8 operator -(int a, const vint8& b) { return vint8(a) - b; }
  174. __forceinline vint8 operator *(const vint8& a, const vint8& b) { return _mm256_mullo_epi32(a, b); }
  175. __forceinline vint8 operator *(const vint8& a, int b) { return a * vint8(b); }
  176. __forceinline vint8 operator *(int a, const vint8& b) { return vint8(a) * b; }
  177. __forceinline vint8 operator &(const vint8& a, const vint8& b) { return _mm256_and_si256(a, b); }
  178. __forceinline vint8 operator &(const vint8& a, int b) { return a & vint8(b); }
  179. __forceinline vint8 operator &(int a, const vint8& b) { return vint8(a) & b; }
  180. __forceinline vint8 operator |(const vint8& a, const vint8& b) { return _mm256_or_si256(a, b); }
  181. __forceinline vint8 operator |(const vint8& a, int b) { return a | vint8(b); }
  182. __forceinline vint8 operator |(int a, const vint8& b) { return vint8(a) | b; }
  183. __forceinline vint8 operator ^(const vint8& a, const vint8& b) { return _mm256_xor_si256(a, b); }
  184. __forceinline vint8 operator ^(const vint8& a, int b) { return a ^ vint8(b); }
  185. __forceinline vint8 operator ^(int a, const vint8& b) { return vint8(a) ^ b; }
  186. __forceinline vint8 operator <<(const vint8& a, int n) { return _mm256_slli_epi32(a, n); }
  187. __forceinline vint8 operator >>(const vint8& a, int n) { return _mm256_srai_epi32(a, n); }
  188. __forceinline vint8 operator <<(const vint8& a, const vint8& n) { return _mm256_sllv_epi32(a, n); }
  189. __forceinline vint8 operator >>(const vint8& a, const vint8& n) { return _mm256_srav_epi32(a, n); }
  190. __forceinline vint8 sll(const vint8& a, int b) { return _mm256_slli_epi32(a, b); }
  191. __forceinline vint8 sra(const vint8& a, int b) { return _mm256_srai_epi32(a, b); }
  192. __forceinline vint8 srl(const vint8& a, int b) { return _mm256_srli_epi32(a, b); }
  193. __forceinline vint8 sll(const vint8& a, const vint8& b) { return _mm256_sllv_epi32(a, b); }
  194. __forceinline vint8 sra(const vint8& a, const vint8& b) { return _mm256_srav_epi32(a, b); }
  195. __forceinline vint8 srl(const vint8& a, const vint8& b) { return _mm256_srlv_epi32(a, b); }
  196. __forceinline vint8 min(const vint8& a, const vint8& b) { return _mm256_min_epi32(a, b); }
  197. __forceinline vint8 min(const vint8& a, int b) { return min(a,vint8(b)); }
  198. __forceinline vint8 min(int a, const vint8& b) { return min(vint8(a),b); }
  199. __forceinline vint8 max(const vint8& a, const vint8& b) { return _mm256_max_epi32(a, b); }
  200. __forceinline vint8 max(const vint8& a, int b) { return max(a,vint8(b)); }
  201. __forceinline vint8 max(int a, const vint8& b) { return max(vint8(a),b); }
  202. __forceinline vint8 umin(const vint8& a, const vint8& b) { return _mm256_min_epu32(a, b); }
  203. __forceinline vint8 umax(const vint8& a, const vint8& b) { return _mm256_max_epu32(a, b); }
  204. ////////////////////////////////////////////////////////////////////////////////
  205. /// Assignment Operators
  206. ////////////////////////////////////////////////////////////////////////////////
  207. __forceinline vint8& operator +=(vint8& a, const vint8& b) { return a = a + b; }
  208. __forceinline vint8& operator +=(vint8& a, int b) { return a = a + b; }
  209. __forceinline vint8& operator -=(vint8& a, const vint8& b) { return a = a - b; }
  210. __forceinline vint8& operator -=(vint8& a, int b) { return a = a - b; }
  211. __forceinline vint8& operator *=(vint8& a, const vint8& b) { return a = a * b; }
  212. __forceinline vint8& operator *=(vint8& a, int b) { return a = a * b; }
  213. __forceinline vint8& operator &=(vint8& a, const vint8& b) { return a = a & b; }
  214. __forceinline vint8& operator &=(vint8& a, int b) { return a = a & b; }
  215. __forceinline vint8& operator |=(vint8& a, const vint8& b) { return a = a | b; }
  216. __forceinline vint8& operator |=(vint8& a, int b) { return a = a | b; }
  217. __forceinline vint8& operator <<=(vint8& a, const int b) { return a = a << b; }
  218. __forceinline vint8& operator >>=(vint8& a, const int b) { return a = a >> b; }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. /// Comparison Operators + Select
  221. ////////////////////////////////////////////////////////////////////////////////
  222. #if defined(__AVX512VL__)
  223. static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  224. static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  225. static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  226. static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  227. static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  228. static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
  229. static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {
  230. return _mm256_mask_blend_epi32(m, (__m256i)f, (__m256i)t);
  231. }
  232. #else
  233. static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b)); }
  234. static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return !(a == b); }
  235. static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(b, a)); }
  236. static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return !(a < b); }
  237. static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(a, b)); }
  238. static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return !(a > b); }
  239. static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {
  240. return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));
  241. }
  242. #endif
  243. template<int mask>
  244. __forceinline vint8 select(const vint8& t, const vint8& f) {
  245. return _mm256_blend_epi32(f, t, mask);
  246. }
  247. __forceinline vboolf8 operator ==(const vint8& a, int b) { return a == vint8(b); }
  248. __forceinline vboolf8 operator ==(int a, const vint8& b) { return vint8(a) == b; }
  249. __forceinline vboolf8 operator !=(const vint8& a, int b) { return a != vint8(b); }
  250. __forceinline vboolf8 operator !=(int a, const vint8& b) { return vint8(a) != b; }
  251. __forceinline vboolf8 operator < (const vint8& a, int b) { return a < vint8(b); }
  252. __forceinline vboolf8 operator < (int a, const vint8& b) { return vint8(a) < b; }
  253. __forceinline vboolf8 operator >=(const vint8& a, int b) { return a >= vint8(b); }
  254. __forceinline vboolf8 operator >=(int a, const vint8& b) { return vint8(a) >= b; }
  255. __forceinline vboolf8 operator > (const vint8& a, int b) { return a > vint8(b); }
  256. __forceinline vboolf8 operator > (int a, const vint8& b) { return vint8(a) > b; }
  257. __forceinline vboolf8 operator <=(const vint8& a, int b) { return a <= vint8(b); }
  258. __forceinline vboolf8 operator <=(int a, const vint8& b) { return vint8(a) <= b; }
  259. __forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }
  260. __forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }
  261. __forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }
  262. __forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }
  263. __forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }
  264. __forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }
  265. #if defined(__AVX512VL__)
  266. static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  267. static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  268. static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  269. static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  270. static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  271. static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
  272. #else
  273. static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }
  274. static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }
  275. static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }
  276. static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }
  277. static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }
  278. static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }
  279. #endif
  280. ////////////////////////////////////////////////////////////////////////////////
  281. /// Movement/Shifting/Shuffling Functions
  282. ////////////////////////////////////////////////////////////////////////////////
  283. __forceinline vint8 unpacklo(const vint8& a, const vint8& b) { return _mm256_unpacklo_epi32(a, b); }
  284. __forceinline vint8 unpackhi(const vint8& a, const vint8& b) { return _mm256_unpackhi_epi32(a, b); }
  285. template<int i>
  286. __forceinline vint8 shuffle(const vint8& v) {
  287. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));
  288. }
  289. template<int i0, int i1>
  290. __forceinline vint8 shuffle4(const vint8& v) {
  291. return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));
  292. }
  293. template<int i0, int i1>
  294. __forceinline vint8 shuffle4(const vint8& a, const vint8& b) {
  295. return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));
  296. }
  297. template<int i0, int i1, int i2, int i3>
  298. __forceinline vint8 shuffle(const vint8& v) {
  299. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));
  300. }
  301. template<int i0, int i1, int i2, int i3>
  302. __forceinline vint8 shuffle(const vint8& a, const vint8& b) {
  303. return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  304. }
  305. template<> __forceinline vint8 shuffle<0, 0, 2, 2>(const vint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }
  306. template<> __forceinline vint8 shuffle<1, 1, 3, 3>(const vint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }
  307. template<> __forceinline vint8 shuffle<0, 1, 0, 1>(const vint8& v) { return _mm256_castps_si256(_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(v))))); }
  308. __forceinline vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }
  309. template<int i> __forceinline vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }
  310. template<int i> __forceinline vint4 extract4(const vint8& a) { return _mm256_extractf128_si256(a, i); }
  311. template<> __forceinline vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }
  312. __forceinline int toScalar(const vint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }
  313. #if !defined(__aarch64__)
  314. __forceinline vint8 permute(const vint8& v, const __m256i& index) {
  315. return _mm256_permutevar8x32_epi32(v, index);
  316. }
  317. __forceinline vint8 shuffle(const vint8& v, const __m256i& index) {
  318. return _mm256_castps_si256(_mm256_permutevar_ps(_mm256_castsi256_ps(v), index));
  319. }
  320. template<int i>
  321. static __forceinline vint8 align_shift_right(const vint8& a, const vint8& b) {
  322. #if defined(__AVX512VL__)
  323. return _mm256_alignr_epi32(a, b, i);
  324. #else
  325. return _mm256_alignr_epi8(a, b, 4*i);
  326. #endif
  327. }
  328. #endif
  329. ////////////////////////////////////////////////////////////////////////////////
  330. /// Reductions
  331. ////////////////////////////////////////////////////////////////////////////////
  332. __forceinline vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }
  333. __forceinline vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
  334. __forceinline vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
  335. __forceinline vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }
  336. __forceinline vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
  337. __forceinline vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
  338. __forceinline vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }
  339. __forceinline vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
  340. __forceinline vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
  341. __forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }
  342. __forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }
  343. __forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }
  344. __forceinline size_t select_min(const vint8& v) { return bsf(movemask(v == vreduce_min(v))); }
  345. __forceinline size_t select_max(const vint8& v) { return bsf(movemask(v == vreduce_max(v))); }
  346. __forceinline size_t select_min(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); }
  347. __forceinline size_t select_max(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); }
  348. ////////////////////////////////////////////////////////////////////////////////
  349. /// Sorting networks
  350. ////////////////////////////////////////////////////////////////////////////////
  351. __forceinline vint8 usort_ascending(const vint8& v)
  352. {
  353. const vint8 a0 = v;
  354. const vint8 b0 = shuffle<1,0,3,2>(a0);
  355. const vint8 c0 = umin(a0,b0);
  356. const vint8 d0 = umax(a0,b0);
  357. const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  358. const vint8 b1 = shuffle<2,3,0,1>(a1);
  359. const vint8 c1 = umin(a1,b1);
  360. const vint8 d1 = umax(a1,b1);
  361. const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  362. const vint8 b2 = shuffle<1,0,3,2>(a2);
  363. const vint8 c2 = umin(a2,b2);
  364. const vint8 d2 = umax(a2,b2);
  365. const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  366. const vint8 b3 = shuffle4<1,0>(a3);
  367. const vint8 c3 = umin(a3,b3);
  368. const vint8 d3 = umax(a3,b3);
  369. const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  370. const vint8 b4 = shuffle<2,3,0,1>(a4);
  371. const vint8 c4 = umin(a4,b4);
  372. const vint8 d4 = umax(a4,b4);
  373. const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  374. const vint8 b5 = shuffle<1,0,3,2>(a5);
  375. const vint8 c5 = umin(a5,b5);
  376. const vint8 d5 = umax(a5,b5);
  377. const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  378. return a6;
  379. }
  380. __forceinline vint8 usort_descending(const vint8& v)
  381. {
  382. const vint8 a0 = v;
  383. const vint8 b0 = shuffle<1,0,3,2>(a0);
  384. const vint8 c0 = umax(a0,b0);
  385. const vint8 d0 = umin(a0,b0);
  386. const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  387. const vint8 b1 = shuffle<2,3,0,1>(a1);
  388. const vint8 c1 = umax(a1,b1);
  389. const vint8 d1 = umin(a1,b1);
  390. const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  391. const vint8 b2 = shuffle<1,0,3,2>(a2);
  392. const vint8 c2 = umax(a2,b2);
  393. const vint8 d2 = umin(a2,b2);
  394. const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  395. const vint8 b3 = shuffle4<1,0>(a3);
  396. const vint8 c3 = umax(a3,b3);
  397. const vint8 d3 = umin(a3,b3);
  398. const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  399. const vint8 b4 = shuffle<2,3,0,1>(a4);
  400. const vint8 c4 = umax(a4,b4);
  401. const vint8 d4 = umin(a4,b4);
  402. const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  403. const vint8 b5 = shuffle<1,0,3,2>(a5);
  404. const vint8 c5 = umax(a5,b5);
  405. const vint8 d5 = umin(a5,b5);
  406. const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  407. return a6;
  408. }
  409. ////////////////////////////////////////////////////////////////////////////////
  410. /// Output Operators
  411. ////////////////////////////////////////////////////////////////////////////////
  412. __forceinline embree_ostream operator <<(embree_ostream cout, const vint8& a) {
  413. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  414. }
  415. }
  416. #undef vboolf
  417. #undef vboold
  418. #undef vint
  419. #undef vuint
  420. #undef vllong
  421. #undef vfloat
  422. #undef vdouble