vint8_avx2.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. namespace embree
  18. {
  19. /* 8-wide AVX integer type */
  20. template<>
  21. struct vint<8>
  22. {
  23. typedef vboolf8 Bool;
  24. typedef vint8 Int;
  25. typedef vfloat8 Float;
  26. enum { size = 8 }; // number of SIMD elements
  27. union { // data
  28. __m256i v;
  29. int i[8];
  30. };
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// Constructors, Assignment & Cast Operators
  33. ////////////////////////////////////////////////////////////////////////////////
  34. __forceinline vint ( ) {}
  35. __forceinline vint ( const vint8& a ) { v = a.v; }
  36. __forceinline vint8& operator=( const vint8& a ) { v = a.v; return *this; }
  37. __forceinline vint( const __m256i a ) : v(a) {}
  38. __forceinline operator const __m256i&( void ) const { return v; }
  39. __forceinline operator __m256i&( void ) { return v; }
  40. __forceinline explicit vint( const vint4& a ) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}
  41. __forceinline vint( const vint4& a, const vint4& b ) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
  42. __forceinline vint( const __m128i& a, const __m128i& b ) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
  43. __forceinline explicit vint ( const int* const a ) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}
  44. __forceinline vint ( int a ) : v(_mm256_set1_epi32(a)) {}
  45. __forceinline vint ( int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}
  46. __forceinline vint ( int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}
  47. __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)) {}
  48. __forceinline explicit vint( const __m256 a ) : v(_mm256_cvtps_epi32(a)) {}
  49. #if defined(__AVX512VL__)
  50. __forceinline explicit vint( const vboolf8 &a ) : v(_mm256_movm_epi32(a)) {}
  51. #else
  52. __forceinline explicit vint( const vboolf8 &a ) : v(_mm256_castps_si256((__m256)a)) {}
  53. #endif
  54. ////////////////////////////////////////////////////////////////////////////////
  55. /// Constants
  56. ////////////////////////////////////////////////////////////////////////////////
  57. __forceinline vint( ZeroTy ) : v(_mm256_setzero_si256()) {}
  58. __forceinline vint( OneTy ) : v(_mm256_set1_epi32(1)) {}
  59. __forceinline vint( PosInfTy ) : v(_mm256_set1_epi32(pos_inf)) {}
  60. __forceinline vint( NegInfTy ) : v(_mm256_set1_epi32(neg_inf)) {}
  61. __forceinline vint( StepTy ) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}
  62. ////////////////////////////////////////////////////////////////////////////////
  63. /// Loads and Stores
  64. ////////////////////////////////////////////////////////////////////////////////
  65. static __forceinline const vint8 load( const unsigned char* const ptr ) { return _mm256_cvtepu8_epi32(_mm_load_si128((__m128i*)ptr)); }
  66. static __forceinline const vint8 loadu( const unsigned char* const ptr ) { return _mm256_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr)); }
  67. static __forceinline const vint8 load( const void* const ptr ) { return _mm256_load_si256((__m256i*)ptr); }
  68. static __forceinline const vint8 loadu(const void* const ptr ) { return _mm256_loadu_si256((__m256i*)ptr); }
  69. static __forceinline void store ( void* ptr, const vint8& v ) { _mm256_store_si256((__m256i*)ptr,v); }
  70. static __forceinline void storeu( void* ptr, const vint8& v ) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(v)); }
  71. #if defined(__AVX512VL__)
  72. static __forceinline vint8 load ( const vboolf8& mask, const void* const ptr ) { return _mm256_mask_load_epi32 (_mm256_setzero_si256(),mask,ptr); }
  73. static __forceinline vint8 loadu( const vboolf8& mask, const void* const ptr ) { return _mm256_mask_loadu_epi32(_mm256_setzero_si256(),mask,ptr); }
  74. static __forceinline void store ( const vboolf8& mask, void* ptr, const vint8& v ) { _mm256_mask_store_epi32 (ptr,mask,v); }
  75. static __forceinline void storeu( const vboolf8& mask, void* ptr, const vint8& v ) { _mm256_mask_storeu_epi32(ptr,mask,v); }
  76. #else
  77. static __forceinline vint8 load ( const vboolf8& mask, const void* const ptr ) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
  78. static __forceinline vint8 loadu( const vboolf8& mask, const void* const ptr ) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
  79. static __forceinline void store ( const vboolf8& mask, void* ptr, const vint8& v ) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
  80. static __forceinline void storeu( const vboolf8& mask, void* ptr, const vint8& v ) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
  81. #endif
  82. #if defined (__AVX2__)
  83. static __forceinline vint8 load_nt(void* ptr) {
  84. return _mm256_stream_load_si256((__m256i*)ptr);
  85. }
  86. #endif
  87. static __forceinline void store_nt(void* ptr, const vint8& v) {
  88. _mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));
  89. }
  90. static __forceinline void store_uchar( unsigned char* const ptr, const vint8& i ) {
  91. __m256i x = i;
  92. x = _mm256_packus_epi32(x, x);
  93. x = _mm256_packus_epi16(x, x);
  94. #if defined(__X86_64__)
  95. *(size_t*)ptr = _mm_cvtsi128_si64(_mm256_castsi256_si128(x));
  96. #else
  97. for (size_t i = 0; i < 8; i++)
  98. ptr[i] = ((unsigned char*)&x)[i];
  99. #endif
  100. }
  101. static __forceinline vint8 broadcast64(const long long &a) { return _mm256_set1_epi64x(a); }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. /// Array Access
  104. ////////////////////////////////////////////////////////////////////////////////
  105. __forceinline const int& operator []( const size_t index ) const { assert(index < 8); return i[index]; }
  106. __forceinline int& operator []( const size_t index ) { assert(index < 8); return i[index]; }
  107. };
  108. ////////////////////////////////////////////////////////////////////////////////
  109. /// Unary Operators
  110. ////////////////////////////////////////////////////////////////////////////////
  111. __forceinline const vint8 operator +( const vint8& a ) { return a; }
  112. __forceinline const vint8 operator -( const vint8& a ) { return _mm256_sub_epi32(_mm256_setzero_si256(), a.v); }
  113. __forceinline const vint8 abs ( const vint8& a ) { return _mm256_abs_epi32(a.v); }
  114. ////////////////////////////////////////////////////////////////////////////////
  115. /// Binary Operators
  116. ////////////////////////////////////////////////////////////////////////////////
  117. __forceinline const vint8 operator +( const vint8& a, const vint8& b ) { return _mm256_add_epi32(a.v, b.v); }
  118. __forceinline const vint8 operator +( const vint8& a, const int b ) { return a + vint8(b); }
  119. __forceinline const vint8 operator +( const int a, const vint8& b ) { return vint8(a) + b; }
  120. __forceinline const vint8 operator -( const vint8& a, const vint8& b ) { return _mm256_sub_epi32(a.v, b.v); }
  121. __forceinline const vint8 operator -( const vint8& a, const int b ) { return a - vint8(b); }
  122. __forceinline const vint8 operator -( const int a, const vint8& b ) { return vint8(a) - b; }
  123. __forceinline const vint8 operator *( const vint8& a, const vint8& b ) { return _mm256_mullo_epi32(a.v, b.v); }
  124. __forceinline const vint8 operator *( const vint8& a, const int b ) { return a * vint8(b); }
  125. __forceinline const vint8 operator *( const int a, const vint8& b ) { return vint8(a) * b; }
  126. __forceinline const vint8 operator &( const vint8& a, const vint8& b ) { return _mm256_and_si256(a.v, b.v); }
  127. __forceinline const vint8 operator &( const vint8& a, const int b ) { return a & vint8(b); }
  128. __forceinline const vint8 operator &( const int a, const vint8& b ) { return vint8(a) & b; }
  129. __forceinline const vint8 operator |( const vint8& a, const vint8& b ) { return _mm256_or_si256(a.v, b.v); }
  130. __forceinline const vint8 operator |( const vint8& a, const int b ) { return a | vint8(b); }
  131. __forceinline const vint8 operator |( const int a, const vint8& b ) { return vint8(a) | b; }
  132. __forceinline const vint8 operator ^( const vint8& a, const vint8& b ) { return _mm256_xor_si256(a.v, b.v); }
  133. __forceinline const vint8 operator ^( const vint8& a, const int b ) { return a ^ vint8(b); }
  134. __forceinline const vint8 operator ^( const int a, const vint8& b ) { return vint8(a) ^ b; }
  135. __forceinline const vint8 operator <<( const vint8& a, const int n ) { return _mm256_slli_epi32(a.v, n); }
  136. __forceinline const vint8 operator >>( const vint8& a, const int n ) { return _mm256_srai_epi32(a.v, n); }
  137. __forceinline const vint8 operator <<( const vint8& a, const vint8& n ) { return _mm256_sllv_epi32(a.v, n); }
  138. __forceinline const vint8 operator >>( const vint8& a, const vint8& n ) { return _mm256_srav_epi32(a.v, n); }
  139. __forceinline const vint8 sll ( const vint8& a, const int b ) { return _mm256_slli_epi32(a.v, b); }
  140. __forceinline const vint8 sra ( const vint8& a, const int b ) { return _mm256_srai_epi32(a.v, b); }
  141. __forceinline const vint8 srl ( const vint8& a, const int b ) { return _mm256_srli_epi32(a.v, b); }
  142. __forceinline const vint8 sll ( const vint8& a, const vint8 &b ) { return _mm256_sllv_epi32(a.v, b.v); }
  143. __forceinline const vint8 sra ( const vint8& a, const vint8 &b ) { return _mm256_srav_epi32(a.v, b.v); }
  144. __forceinline const vint8 srl ( const vint8& a, const vint8 &b ) { return _mm256_srlv_epi32(a.v, b.v); }
  145. __forceinline const vint8 min( const vint8& a, const vint8& b ) { return _mm256_min_epi32(a.v, b.v); }
  146. __forceinline const vint8 min( const vint8& a, const int b ) { return min(a,vint8(b)); }
  147. __forceinline const vint8 min( const int a, const vint8& b ) { return min(vint8(a),b); }
  148. __forceinline const vint8 max( const vint8& a, const vint8& b ) { return _mm256_max_epi32(a.v, b.v); }
  149. __forceinline const vint8 max( const vint8& a, const int b ) { return max(a,vint8(b)); }
  150. __forceinline const vint8 max( const int a, const vint8& b ) { return max(vint8(a),b); }
  151. __forceinline const vint8 umin( const vint8& a, const vint8& b ) { return _mm256_min_epu32(a.v, b.v); }
  152. __forceinline const vint8 umax( const vint8& a, const vint8& b ) { return _mm256_max_epu32(a.v, b.v); }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. /// Assignment Operators
  155. ////////////////////////////////////////////////////////////////////////////////
  156. __forceinline vint8& operator +=( vint8& a, const vint8& b ) { return a = a + b; }
  157. __forceinline vint8& operator +=( vint8& a, const int b ) { return a = a + b; }
  158. __forceinline vint8& operator -=( vint8& a, const vint8& b ) { return a = a - b; }
  159. __forceinline vint8& operator -=( vint8& a, const int b ) { return a = a - b; }
  160. __forceinline vint8& operator *=( vint8& a, const vint8& b ) { return a = a * b; }
  161. __forceinline vint8& operator *=( vint8& a, const int b ) { return a = a * b; }
  162. __forceinline vint8& operator &=( vint8& a, const vint8& b ) { return a = a & b; }
  163. __forceinline vint8& operator &=( vint8& a, const int b ) { return a = a & b; }
  164. __forceinline vint8& operator |=( vint8& a, const vint8& b ) { return a = a | b; }
  165. __forceinline vint8& operator |=( vint8& a, const int b ) { return a = a | b; }
  166. __forceinline vint8& operator <<=( vint8& a, const int b ) { return a = a << b; }
  167. __forceinline vint8& operator >>=( vint8& a, const int b ) { return a = a >> b; }
  168. ////////////////////////////////////////////////////////////////////////////////
  169. /// Comparison Operators + Select
  170. ////////////////////////////////////////////////////////////////////////////////
  171. #if defined(__AVX512VL__)
  172. __forceinline const vboolf8 operator ==( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  173. __forceinline const vboolf8 operator !=( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  174. __forceinline const vboolf8 operator < ( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  175. __forceinline const vboolf8 operator >=( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  176. __forceinline const vboolf8 operator > ( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  177. __forceinline const vboolf8 operator <=( const vint8& a, const vint8& b ) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
  178. __forceinline const vint8 select( const vboolf8& m, const vint8& t, const vint8& f ) {
  179. return _mm256_mask_blend_epi32(m, f, t);
  180. }
  181. #else
  182. __forceinline const vboolf8 operator ==( const vint8& a, const vint8& b ) { return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b)); }
  183. __forceinline const vboolf8 operator !=( const vint8& a, const vint8& b ) { return !(a == b); }
  184. __forceinline const vboolf8 operator < ( const vint8& a, const vint8& b ) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(b, a)); }
  185. __forceinline const vboolf8 operator >=( const vint8& a, const vint8& b ) { return !(a < b); }
  186. __forceinline const vboolf8 operator > ( const vint8& a, const vint8& b ) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(a, b)); }
  187. __forceinline const vboolf8 operator <=( const vint8& a, const vint8& b ) { return !(a > b); }
  188. __forceinline const vint8 select( const vboolf8& m, const vint8& t, const vint8& f ) {
  189. return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));
  190. }
  191. #endif
  192. template<int mask>
  193. __forceinline const vint8 select(const vint8& t, const vint8& f) {
  194. return _mm256_blend_epi32(f, t, mask);
  195. }
  196. __forceinline const vboolf8 operator ==( const vint8& a, const int b ) { return a == vint8(b); }
  197. __forceinline const vboolf8 operator ==( const int a, const vint8& b ) { return vint8(a) == b; }
  198. __forceinline const vboolf8 operator !=( const vint8& a, const int b ) { return a != vint8(b); }
  199. __forceinline const vboolf8 operator !=( const int a, const vint8& b ) { return vint8(a) != b; }
  200. __forceinline const vboolf8 operator < ( const vint8& a, const int b ) { return a < vint8(b); }
  201. __forceinline const vboolf8 operator < ( const int a, const vint8& b ) { return vint8(a) < b; }
  202. __forceinline const vboolf8 operator >=( const vint8& a, const int b ) { return a >= vint8(b); }
  203. __forceinline const vboolf8 operator >=( const int a, const vint8& b ) { return vint8(a) >= b; }
  204. __forceinline const vboolf8 operator > ( const vint8& a, const int b ) { return a > vint8(b); }
  205. __forceinline const vboolf8 operator > ( const int a, const vint8& b ) { return vint8(a) > b; }
  206. __forceinline const vboolf8 operator <=( const vint8& a, const int b ) { return a <= vint8(b); }
  207. __forceinline const vboolf8 operator <=( const int a, const vint8& b ) { return vint8(a) <= b; }
  208. __forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }
  209. __forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }
  210. __forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }
  211. __forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }
  212. __forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }
  213. __forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }
  214. #if defined(__AVX512VL__)
  215. __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  216. __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  217. __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  218. __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  219. __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  220. __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
  221. #else
  222. __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }
  223. __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }
  224. __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }
  225. __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }
  226. __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }
  227. __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }
  228. #endif
  229. ////////////////////////////////////////////////////////////////////////////////
  230. /// Movement/Shifting/Shuffling Functions
  231. ////////////////////////////////////////////////////////////////////////////////
  232. __forceinline vint8 unpacklo( const vint8& a, const vint8& b ) { return _mm256_unpacklo_epi32(a.v, b.v); }
  233. __forceinline vint8 unpackhi( const vint8& a, const vint8& b ) { return _mm256_unpackhi_epi32(a.v, b.v); }
  234. template<size_t i> __forceinline const vint8 shuffle( const vint8& a ) {
  235. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i, i, i, i)));
  236. }
  237. template<size_t i0, size_t i1> __forceinline const vint8 shuffle4( const vint8& a ) {
  238. return _mm256_permute2f128_si256(a, a, (i1 << 4) | (i0 << 0));
  239. }
  240. template<size_t i0, size_t i1> __forceinline const vint8 shuffle4( const vint8& a, const vint8& b) {
  241. return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));
  242. }
  243. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vint8 shuffle( const vint8& a ) {
  244. return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i3, i2, i1, i0)));
  245. }
  246. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vint8 shuffle( const vint8& a, const vint8& b ) {
  247. return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  248. }
  249. template<> __forceinline const vint8 shuffle<0, 0, 2, 2>( const vint8& b ) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(b))); }
  250. template<> __forceinline const vint8 shuffle<1, 1, 3, 3>( const vint8& b ) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(b))); }
  251. template<> __forceinline const vint8 shuffle<0, 1, 0, 1>( const vint8& b ) { return _mm256_castps_si256(_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(b))))); }
  252. __forceinline const vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }
  253. template<size_t i> __forceinline const vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }
  254. template<size_t i> __forceinline const vint4 extract4 (const vint8& a) { return _mm256_extractf128_si256(a, i); }
  255. template<> __forceinline const vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }
  256. __forceinline int toScalar(const vint8& a) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(a)); }
  257. __forceinline vint8 permute(const vint8& a, const __m256i& index) {
  258. return _mm256_permutevar8x32_epi32(a,index);
  259. }
  260. __forceinline vint8 shuffle(const vint8& a, const __m256i& index) {
  261. return _mm256_castps_si256(_mm256_permutevar_ps(_mm256_castsi256_ps(a),index));
  262. }
  263. template<int i>
  264. __forceinline vint8 align_shift_right(const vint8 &a, const vint8 &b) {
  265. return _mm256_alignr_epi8(a, b, i);
  266. }
  267. ////////////////////////////////////////////////////////////////////////////////
  268. /// Reductions
  269. ////////////////////////////////////////////////////////////////////////////////
  270. __forceinline const vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }
  271. __forceinline const vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
  272. __forceinline const vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
  273. __forceinline const vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }
  274. __forceinline const vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
  275. __forceinline const vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
  276. __forceinline const vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }
  277. __forceinline const vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
  278. __forceinline const vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
  279. __forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }
  280. __forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }
  281. __forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }
  282. __forceinline size_t select_min(const vint8& v) { return __bsf(movemask(v == vreduce_min(v))); }
  283. __forceinline size_t select_max(const vint8& v) { return __bsf(movemask(v == vreduce_max(v))); }
  284. __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)))); }
  285. __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)))); }
  286. __forceinline vint8 assign( const vint4& a ) { return _mm256_castsi128_si256(a); }
  287. ////////////////////////////////////////////////////////////////////////////////
  288. /// Sorting networks
  289. ////////////////////////////////////////////////////////////////////////////////
  290. __forceinline vint8 sortNetwork2x4(const vint8& v)
  291. {
  292. const vint8 a0 = v;
  293. const vint8 b0 = shuffle<1,0,3,2>(a0);
  294. const vint8 c0 = umin(a0,b0);
  295. const vint8 d0 = umax(a0,b0);
  296. const vint8 a1 = select<0x55 /* 0b01010101 */>(c0,d0);
  297. const vint8 b1 = shuffle<2,3,0,1>(a1);
  298. const vint8 c1 = umin(a1,b1);
  299. const vint8 d1 = umax(a1,b1);
  300. const vint8 a2 = select<0x33 /* 0b00110011 */>(c1,d1);
  301. const vint8 b2 = shuffle<0,2,1,3>(a2);
  302. const vint8 c2 = umin(a2,b2);
  303. const vint8 d2 = umax(a2,b2);
  304. const vint8 a3 = select<0x22 /* 0b00100010 */>(c2,d2);
  305. return a3;
  306. }
  307. __forceinline vint8 sortNetwork(const vint8& v)
  308. {
  309. const vint8 a0 = v;
  310. const vint8 b0 = shuffle<1,0,3,2>(a0);
  311. const vint8 c0 = umin(a0,b0);
  312. const vint8 d0 = umax(a0,b0);
  313. const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  314. const vint8 b1 = shuffle<2,3,0,1>(a1);
  315. const vint8 c1 = umin(a1,b1);
  316. const vint8 d1 = umax(a1,b1);
  317. const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  318. const vint8 b2 = shuffle<1,0,3,2>(a2);
  319. const vint8 c2 = umin(a2,b2);
  320. const vint8 d2 = umax(a2,b2);
  321. const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  322. const vint8 b3 = shuffle4<1,0>(a3);
  323. const vint8 c3 = umin(a3,b3);
  324. const vint8 d3 = umax(a3,b3);
  325. const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  326. const vint8 b4 = shuffle<2,3,0,1>(a4);
  327. const vint8 c4 = umin(a4,b4);
  328. const vint8 d4 = umax(a4,b4);
  329. const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  330. const vint8 b5 = shuffle<1,0,3,2>(a5);
  331. const vint8 c5 = umin(a5,b5);
  332. const vint8 d5 = umax(a5,b5);
  333. const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  334. return a6;
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////
  337. /// Output Operators
  338. ////////////////////////////////////////////////////////////////////////////////
  339. inline std::ostream& operator<<(std::ostream& cout, const vint8& a) {
  340. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  341. }
  342. }