vint8_avx.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. struct { __m128i vl,vh; };
  25. int i[8];
  26. };
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// Constructors, Assignment & Cast Operators
  29. ////////////////////////////////////////////////////////////////////////////////
  30. __forceinline vint() {}
  31. __forceinline vint(const vint8& a) { v = a.v; }
  32. __forceinline vint8& operator =(const vint8& a) { v = a.v; return *this; }
  33. __forceinline vint(__m256i a) : v(a) {}
  34. __forceinline operator const __m256i&() const { return v; }
  35. __forceinline operator __m256i&() { return v; }
  36. __forceinline explicit vint(const vint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}
  37. __forceinline vint(const vint4& a, const vint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
  38. __forceinline vint(const __m128i& a, const __m128i& b) : vl(a), vh(b) {}
  39. __forceinline explicit vint(const int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}
  40. __forceinline vint(int a) : v(_mm256_set1_epi32(a)) {}
  41. __forceinline vint(int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}
  42. __forceinline vint(int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}
  43. __forceinline vint(int a, int b, int c, int d, int e, int f, int g, int vh) : v(_mm256_set_epi32(vh, g, f, e, d, c, b, a)) {}
  44. __forceinline explicit vint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}
  45. ////////////////////////////////////////////////////////////////////////////////
  46. /// Constants
  47. ////////////////////////////////////////////////////////////////////////////////
  48. __forceinline vint(ZeroTy) : v(_mm256_setzero_si256()) {}
  49. __forceinline vint(OneTy) : v(_mm256_set_epi32(1,1,1,1,1,1,1,1)) {}
  50. __forceinline vint(PosInfTy) : v(_mm256_set_epi32(pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf)) {}
  51. __forceinline vint(NegInfTy) : v(_mm256_set_epi32(neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf)) {}
  52. __forceinline vint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}
  53. __forceinline vint(ReverseStepTy) : v(_mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7)) {}
  54. __forceinline vint(UndefinedTy) : v(_mm256_undefined_si256()) {}
  55. ////////////////////////////////////////////////////////////////////////////////
  56. /// Loads and Stores
  57. ////////////////////////////////////////////////////////////////////////////////
  58. static __forceinline vint8 load (const void* a) { return _mm256_castps_si256(_mm256_load_ps((float*)a)); }
  59. static __forceinline vint8 loadu(const void* a) { return _mm256_castps_si256(_mm256_loadu_ps((float*)a)); }
  60. static __forceinline vint8 load (const vboolf8& mask, const void* a) { return _mm256_castps_si256(_mm256_maskload_ps((float*)a,mask)); }
  61. static __forceinline vint8 loadu(const vboolf8& mask, const void* a) { return _mm256_castps_si256(_mm256_maskload_ps((float*)a,mask)); }
  62. static __forceinline void store (void* ptr, const vint8& f) { _mm256_store_ps((float*)ptr,_mm256_castsi256_ps(f)); }
  63. static __forceinline void storeu(void* ptr, const vint8& f) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(f)); }
  64. static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& f) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),_mm256_castsi256_ps(f)); }
  65. static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& f) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),_mm256_castsi256_ps(f)); }
  66. static __forceinline void store_nt(void* ptr, const vint8& v) {
  67. _mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));
  68. }
  69. static __forceinline vint8 load(const unsigned char* ptr) {
  70. vint4 il = vint4::load(ptr+0);
  71. vint4 ih = vint4::load(ptr+4);
  72. return vint8(il,ih);
  73. }
  74. static __forceinline vint8 loadu(const unsigned char* ptr) {
  75. vint4 il = vint4::loadu(ptr+0);
  76. vint4 ih = vint4::loadu(ptr+4);
  77. return vint8(il,ih);
  78. }
  79. static __forceinline vint8 load(const unsigned short* ptr) {
  80. vint4 il = vint4::load(ptr+0);
  81. vint4 ih = vint4::load(ptr+4);
  82. return vint8(il,ih);
  83. }
  84. static __forceinline vint8 loadu(const unsigned short* ptr) {
  85. vint4 il = vint4::loadu(ptr+0);
  86. vint4 ih = vint4::loadu(ptr+4);
  87. return vint8(il,ih);
  88. }
  89. static __forceinline void store(unsigned char* ptr, const vint8& i) {
  90. vint4 il(i.vl);
  91. vint4 ih(i.vh);
  92. vint4::store(ptr + 0,il);
  93. vint4::store(ptr + 4,ih);
  94. }
  95. static __forceinline void store(unsigned short* ptr, const vint8& v) {
  96. for (size_t i=0;i<8;i++)
  97. ptr[i] = (unsigned short)v[i];
  98. }
  99. template<int scale = 4>
  100. static __forceinline vint8 gather(const int* ptr, const vint8& index) {
  101. return vint8(
  102. *(int*)(((char*)ptr)+scale*index[0]),
  103. *(int*)(((char*)ptr)+scale*index[1]),
  104. *(int*)(((char*)ptr)+scale*index[2]),
  105. *(int*)(((char*)ptr)+scale*index[3]),
  106. *(int*)(((char*)ptr)+scale*index[4]),
  107. *(int*)(((char*)ptr)+scale*index[5]),
  108. *(int*)(((char*)ptr)+scale*index[6]),
  109. *(int*)(((char*)ptr)+scale*index[7]));
  110. }
  111. template<int scale = 4>
  112. static __forceinline vint8 gather(const vboolf8& mask, const int* ptr, const vint8& index) {
  113. vint8 r = zero;
  114. if (likely(mask[0])) r[0] = *(int*)(((char*)ptr)+scale*index[0]);
  115. if (likely(mask[1])) r[1] = *(int*)(((char*)ptr)+scale*index[1]);
  116. if (likely(mask[2])) r[2] = *(int*)(((char*)ptr)+scale*index[2]);
  117. if (likely(mask[3])) r[3] = *(int*)(((char*)ptr)+scale*index[3]);
  118. if (likely(mask[4])) r[4] = *(int*)(((char*)ptr)+scale*index[4]);
  119. if (likely(mask[5])) r[5] = *(int*)(((char*)ptr)+scale*index[5]);
  120. if (likely(mask[6])) r[6] = *(int*)(((char*)ptr)+scale*index[6]);
  121. if (likely(mask[7])) r[7] = *(int*)(((char*)ptr)+scale*index[7]);
  122. return r;
  123. }
  124. template<int scale = 4>
  125. static __forceinline void scatter(void* ptr, const vint8& ofs, const vint8& v)
  126. {
  127. *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
  128. *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
  129. *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
  130. *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
  131. *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
  132. *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
  133. *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
  134. *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
  135. }
  136. template<int scale = 4>
  137. static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vint8& v)
  138. {
  139. if (likely(mask[0])) *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
  140. if (likely(mask[1])) *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
  141. if (likely(mask[2])) *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
  142. if (likely(mask[3])) *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
  143. if (likely(mask[4])) *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
  144. if (likely(mask[5])) *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
  145. if (likely(mask[6])) *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
  146. if (likely(mask[7])) *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
  147. }
  148. static __forceinline vint8 broadcast64(const long long& a) { return _mm256_set1_epi64x(a); }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. /// Array Access
  151. ////////////////////////////////////////////////////////////////////////////////
  152. __forceinline const int& operator [](size_t index) const { assert(index < 8); return i[index]; }
  153. __forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }
  154. };
  155. ////////////////////////////////////////////////////////////////////////////////
  156. /// Unary Operators
  157. ////////////////////////////////////////////////////////////////////////////////
  158. __forceinline vboolf8 asBool(const vint8& a) { return _mm256_castsi256_ps(a); }
  159. __forceinline vint8 operator +(const vint8& a) { return a; }
  160. __forceinline vint8 operator -(const vint8& a) { return vint8(_mm_sub_epi32(_mm_setzero_si128(), a.vl), _mm_sub_epi32(_mm_setzero_si128(), a.vh)); }
  161. __forceinline vint8 abs (const vint8& a) { return vint8(_mm_abs_epi32(a.vl), _mm_abs_epi32(a.vh)); }
  162. ////////////////////////////////////////////////////////////////////////////////
  163. /// Binary Operators
  164. ////////////////////////////////////////////////////////////////////////////////
  165. __forceinline vint8 operator +(const vint8& a, const vint8& b) { return vint8(_mm_add_epi32(a.vl, b.vl), _mm_add_epi32(a.vh, b.vh)); }
  166. __forceinline vint8 operator +(const vint8& a, int b) { return a + vint8(b); }
  167. __forceinline vint8 operator +(int a, const vint8& b) { return vint8(a) + b; }
  168. __forceinline vint8 operator -(const vint8& a, const vint8& b) { return vint8(_mm_sub_epi32(a.vl, b.vl), _mm_sub_epi32(a.vh, b.vh)); }
  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 vint8(_mm_mullo_epi32(a.vl, b.vl), _mm_mullo_epi32(a.vh, b.vh)); }
  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_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(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_castps_si256(_mm256_or_ps (_mm256_castsi256_ps(a), _mm256_castsi256_ps(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_castps_si256(_mm256_xor_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(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, int n) { return vint8(_mm_slli_epi32(a.vl, n), _mm_slli_epi32(a.vh, n)); }
  184. __forceinline vint8 operator >>(const vint8& a, int n) { return vint8(_mm_srai_epi32(a.vl, n), _mm_srai_epi32(a.vh, n)); }
  185. __forceinline vint8 sll (const vint8& a, int b) { return vint8(_mm_slli_epi32(a.vl, b), _mm_slli_epi32(a.vh, b)); }
  186. __forceinline vint8 sra (const vint8& a, int b) { return vint8(_mm_srai_epi32(a.vl, b), _mm_srai_epi32(a.vh, b)); }
  187. __forceinline vint8 srl (const vint8& a, int b) { return vint8(_mm_srli_epi32(a.vl, b), _mm_srli_epi32(a.vh, b)); }
  188. __forceinline vint8 min(const vint8& a, const vint8& b) { return vint8(_mm_min_epi32(a.vl, b.vl), _mm_min_epi32(a.vh, b.vh)); }
  189. __forceinline vint8 min(const vint8& a, int b) { return min(a,vint8(b)); }
  190. __forceinline vint8 min(int a, const vint8& b) { return min(vint8(a),b); }
  191. __forceinline vint8 max(const vint8& a, const vint8& b) { return vint8(_mm_max_epi32(a.vl, b.vl), _mm_max_epi32(a.vh, b.vh)); }
  192. __forceinline vint8 max(const vint8& a, int b) { return max(a,vint8(b)); }
  193. __forceinline vint8 max(int a, const vint8& b) { return max(vint8(a),b); }
  194. __forceinline vint8 umin(const vint8& a, const vint8& b) { return vint8(_mm_min_epu32(a.vl, b.vl), _mm_min_epu32(a.vh, b.vh)); }
  195. __forceinline vint8 umin(const vint8& a, int b) { return umin(a,vint8(b)); }
  196. __forceinline vint8 umin(int a, const vint8& b) { return umin(vint8(a),b); }
  197. __forceinline vint8 umax(const vint8& a, const vint8& b) { return vint8(_mm_max_epu32(a.vl, b.vl), _mm_max_epu32(a.vh, b.vh)); }
  198. __forceinline vint8 umax(const vint8& a, int b) { return umax(a,vint8(b)); }
  199. __forceinline vint8 umax(int a, const vint8& b) { return umax(vint8(a),b); }
  200. ////////////////////////////////////////////////////////////////////////////////
  201. /// Assignment Operators
  202. ////////////////////////////////////////////////////////////////////////////////
  203. __forceinline vint8& operator +=(vint8& a, const vint8& b) { return a = a + b; }
  204. __forceinline vint8& operator +=(vint8& a, int b) { return a = a + b; }
  205. __forceinline vint8& operator -=(vint8& a, const vint8& b) { return a = a - b; }
  206. __forceinline vint8& operator -=(vint8& a, int b) { return a = a - b; }
  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, int b) { return a = a << b; }
  214. __forceinline vint8& operator >>=(vint8& a, int b) { return a = a >> b; }
  215. ////////////////////////////////////////////////////////////////////////////////
  216. /// Comparison Operators + Select
  217. ////////////////////////////////////////////////////////////////////////////////
  218. __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmpeq_epi32 (a.vl, b.vl)),
  219. _mm_castsi128_ps(_mm_cmpeq_epi32 (a.vh, b.vh))); }
  220. __forceinline vboolf8 operator ==(const vint8& a, int b) { return a == vint8(b); }
  221. __forceinline vboolf8 operator ==(int a, const vint8& b) { return vint8(a) == b; }
  222. __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return !(a == b); }
  223. __forceinline vboolf8 operator !=(const vint8& a, int b) { return a != vint8(b); }
  224. __forceinline vboolf8 operator !=(int a, const vint8& b) { return vint8(a) != b; }
  225. __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmplt_epi32 (a.vl, b.vl)),
  226. _mm_castsi128_ps(_mm_cmplt_epi32 (a.vh, b.vh))); }
  227. __forceinline vboolf8 operator < (const vint8& a, int b) { return a < vint8(b); }
  228. __forceinline vboolf8 operator < (int a, const vint8& b) { return vint8(a) < b; }
  229. __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return !(a < b); }
  230. __forceinline vboolf8 operator >=(const vint8& a, int b) { return a >= vint8(b); }
  231. __forceinline vboolf8 operator >=(int a, const vint8& b) { return vint8(a) >= b; }
  232. __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmpgt_epi32 (a.vl, b.vl)),
  233. _mm_castsi128_ps(_mm_cmpgt_epi32 (a.vh, b.vh))); }
  234. __forceinline vboolf8 operator > (const vint8& a, int b) { return a > vint8(b); }
  235. __forceinline vboolf8 operator > (int a, const vint8& b) { return vint8(a) > b; }
  236. __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return !(a > b); }
  237. __forceinline vboolf8 operator <=(const vint8& a, int b) { return a <= vint8(b); }
  238. __forceinline vboolf8 operator <=(int a, const vint8& b) { return vint8(a) <= b; }
  239. __forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }
  240. __forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }
  241. __forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }
  242. __forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }
  243. __forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }
  244. __forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }
  245. __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }
  246. __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }
  247. __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }
  248. __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }
  249. __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }
  250. __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }
  251. __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {
  252. return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));
  253. }
  254. ////////////////////////////////////////////////////////////////////////////////
  255. /// Movement/Shifting/Shuffling Functions
  256. ////////////////////////////////////////////////////////////////////////////////
  257. __forceinline vint8 unpacklo(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_unpacklo_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }
  258. __forceinline vint8 unpackhi(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_unpackhi_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }
  259. template<int i>
  260. __forceinline vint8 shuffle(const vint8& v) {
  261. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));
  262. }
  263. template<int i0, int i1>
  264. __forceinline vint8 shuffle4(const vint8& v) {
  265. return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));
  266. }
  267. template<int i0, int i1>
  268. __forceinline vint8 shuffle4(const vint8& a, const vint8& b) {
  269. return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));
  270. }
  271. template<int i0, int i1, int i2, int i3>
  272. __forceinline vint8 shuffle(const vint8& v) {
  273. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));
  274. }
  275. template<int i0, int i1, int i2, int i3>
  276. __forceinline vint8 shuffle(const vint8& a, const vint8& b) {
  277. return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  278. }
  279. template<> __forceinline vint8 shuffle<0, 0, 2, 2>(const vint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }
  280. template<> __forceinline vint8 shuffle<1, 1, 3, 3>(const vint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }
  281. 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))))); }
  282. __forceinline vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }
  283. template<int i> __forceinline vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }
  284. template<int i> __forceinline vint4 extract4(const vint8& a) { return _mm256_extractf128_si256(a, i); }
  285. template<> __forceinline vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }
  286. __forceinline int toScalar(const vint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }
  287. ////////////////////////////////////////////////////////////////////////////////
  288. /// Reductions
  289. ////////////////////////////////////////////////////////////////////////////////
  290. __forceinline vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }
  291. __forceinline vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
  292. __forceinline vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
  293. __forceinline vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }
  294. __forceinline vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
  295. __forceinline vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
  296. __forceinline vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }
  297. __forceinline vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
  298. __forceinline vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
  299. __forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }
  300. __forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }
  301. __forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }
  302. __forceinline size_t select_min(const vint8& v) { return bsf(movemask(v == vreduce_min(v))); }
  303. __forceinline size_t select_max(const vint8& v) { return bsf(movemask(v == vreduce_max(v))); }
  304. __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)))); }
  305. __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)))); }
  306. ////////////////////////////////////////////////////////////////////////////////
  307. /// Sorting networks
  308. ////////////////////////////////////////////////////////////////////////////////
  309. __forceinline vint8 usort_ascending(const vint8& v)
  310. {
  311. const vint8 a0 = v;
  312. const vint8 b0 = shuffle<1,0,3,2>(a0);
  313. const vint8 c0 = umin(a0,b0);
  314. const vint8 d0 = umax(a0,b0);
  315. const vint8 a1 = select(0x99 /* 0b10011001 */,c0,d0);
  316. const vint8 b1 = shuffle<2,3,0,1>(a1);
  317. const vint8 c1 = umin(a1,b1);
  318. const vint8 d1 = umax(a1,b1);
  319. const vint8 a2 = select(0xc3 /* 0b11000011 */,c1,d1);
  320. const vint8 b2 = shuffle<1,0,3,2>(a2);
  321. const vint8 c2 = umin(a2,b2);
  322. const vint8 d2 = umax(a2,b2);
  323. const vint8 a3 = select(0xa5 /* 0b10100101 */,c2,d2);
  324. const vint8 b3 = shuffle4<1,0>(a3);
  325. const vint8 c3 = umin(a3,b3);
  326. const vint8 d3 = umax(a3,b3);
  327. const vint8 a4 = select(0xf /* 0b00001111 */,c3,d3);
  328. const vint8 b4 = shuffle<2,3,0,1>(a4);
  329. const vint8 c4 = umin(a4,b4);
  330. const vint8 d4 = umax(a4,b4);
  331. const vint8 a5 = select(0x33 /* 0b00110011 */,c4,d4);
  332. const vint8 b5 = shuffle<1,0,3,2>(a5);
  333. const vint8 c5 = umin(a5,b5);
  334. const vint8 d5 = umax(a5,b5);
  335. const vint8 a6 = select(0x55 /* 0b01010101 */,c5,d5);
  336. return a6;
  337. }
  338. __forceinline vint8 usort_descending(const vint8& v)
  339. {
  340. const vint8 a0 = v;
  341. const vint8 b0 = shuffle<1,0,3,2>(a0);
  342. const vint8 c0 = umax(a0,b0);
  343. const vint8 d0 = umin(a0,b0);
  344. const vint8 a1 = select(0x99 /* 0b10011001 */,c0,d0);
  345. const vint8 b1 = shuffle<2,3,0,1>(a1);
  346. const vint8 c1 = umax(a1,b1);
  347. const vint8 d1 = umin(a1,b1);
  348. const vint8 a2 = select(0xc3 /* 0b11000011 */,c1,d1);
  349. const vint8 b2 = shuffle<1,0,3,2>(a2);
  350. const vint8 c2 = umax(a2,b2);
  351. const vint8 d2 = umin(a2,b2);
  352. const vint8 a3 = select(0xa5 /* 0b10100101 */,c2,d2);
  353. const vint8 b3 = shuffle4<1,0>(a3);
  354. const vint8 c3 = umax(a3,b3);
  355. const vint8 d3 = umin(a3,b3);
  356. const vint8 a4 = select(0xf /* 0b00001111 */,c3,d3);
  357. const vint8 b4 = shuffle<2,3,0,1>(a4);
  358. const vint8 c4 = umax(a4,b4);
  359. const vint8 d4 = umin(a4,b4);
  360. const vint8 a5 = select(0x33 /* 0b00110011 */,c4,d4);
  361. const vint8 b5 = shuffle<1,0,3,2>(a5);
  362. const vint8 c5 = umax(a5,b5);
  363. const vint8 d5 = umin(a5,b5);
  364. const vint8 a6 = select(0x55 /* 0b01010101 */,c5,d5);
  365. return a6;
  366. }
  367. ////////////////////////////////////////////////////////////////////////////////
  368. /// Output Operators
  369. ////////////////////////////////////////////////////////////////////////////////
  370. __forceinline embree_ostream operator <<(embree_ostream cout, const vint8& a) {
  371. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  372. }
  373. }
  374. #undef vboolf
  375. #undef vboold
  376. #undef vint
  377. #undef vuint
  378. #undef vllong
  379. #undef vfloat
  380. #undef vdouble