vint4_sse2.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #include "../math/math.h"
  18. namespace embree
  19. {
  20. /* 4-wide SSE integer type */
  21. template<>
  22. struct vint<4>
  23. {
  24. typedef vboolf4 Bool;
  25. typedef vint4 Int;
  26. typedef vfloat4 Float;
  27. enum { size = 4 }; // number of SIMD elements
  28. union { __m128i v; int i[4]; }; // data
  29. ////////////////////////////////////////////////////////////////////////////////
  30. /// Constructors, Assignment & Cast Operators
  31. ////////////////////////////////////////////////////////////////////////////////
  32. __forceinline vint ( ) {}
  33. __forceinline vint ( const vint4& a ) { v = a.v; }
  34. __forceinline vint4& operator=( const vint4& a ) { v = a.v; return *this; }
  35. __forceinline vint( const __m128i a ) : v(a) {}
  36. __forceinline operator const __m128i&( void ) const { return v; }
  37. __forceinline operator __m128i&( void ) { return v; }
  38. __forceinline vint( const int& a ) : v(_mm_shuffle_epi32(_mm_castps_si128(_mm_load_ss((float*)&a)), _MM_SHUFFLE(0, 0, 0, 0))) {}
  39. __forceinline vint( const uint32_t& a ) : v(_mm_shuffle_epi32(_mm_castps_si128(_mm_load_ss((float*)&a)), _MM_SHUFFLE(0, 0, 0, 0))) {}
  40. #if defined(__X86_64__)
  41. __forceinline vint( const size_t a ) : v(_mm_set1_epi32((int)a)) {}
  42. #endif
  43. __forceinline vint( int a, int b, int c, int d) : v(_mm_set_epi32(d, c, b, a)) {}
  44. __forceinline explicit vint( const __m128 a ) : v(_mm_cvtps_epi32(a)) {}
  45. #if defined(__AVX512VL__)
  46. __forceinline explicit vint( const vboolf4 &a ) : v(_mm_movm_epi32(a)) {}
  47. #else
  48. __forceinline explicit vint( const vboolf4 &a ) : v(_mm_castps_si128((__m128)a)) {}
  49. #endif
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// Constants
  52. ////////////////////////////////////////////////////////////////////////////////
  53. __forceinline vint( ZeroTy ) : v(_mm_setzero_si128()) {}
  54. __forceinline vint( OneTy ) : v(_mm_set_epi32(1, 1, 1, 1)) {}
  55. __forceinline vint( PosInfTy ) : v(_mm_set_epi32(pos_inf, pos_inf, pos_inf, pos_inf)) {}
  56. __forceinline vint( NegInfTy ) : v(_mm_set_epi32(neg_inf, neg_inf, neg_inf, neg_inf)) {}
  57. __forceinline vint( StepTy ) : v(_mm_set_epi32(3, 2, 1, 0)) {}
  58. __forceinline vint( TrueTy ) { v = _mm_cmpeq_epi32(v,v); }
  59. ////////////////////////////////////////////////////////////////////////////////
  60. /// Loads and Stores
  61. ////////////////////////////////////////////////////////////////////////////////
  62. static __forceinline vint4 load ( const void* const a ) { return _mm_load_si128((__m128i*)a); }
  63. static __forceinline vint4 loadu( const void* const a ) { return _mm_loadu_si128((__m128i*)a); }
  64. static __forceinline void store (void* ptr, const vint4& v) { _mm_store_si128((__m128i*)ptr,v); }
  65. static __forceinline void storeu(void* ptr, const vint4& v) { _mm_storeu_si128((__m128i*)ptr,v); }
  66. #if defined(__AVX512VL__)
  67. static __forceinline vint4 load ( const vboolf4& mask, const void* const ptr ) { return _mm_mask_load_epi32 (_mm_setzero_si128(),mask,ptr); }
  68. static __forceinline vint4 loadu( const vboolf4& mask, const void* const ptr ) { return _mm_mask_loadu_epi32(_mm_setzero_si128(),mask,ptr); }
  69. static __forceinline void store ( const vboolf4& mask, void* ptr, const vint4& v ) { _mm_mask_store_epi32 (ptr,mask,v); }
  70. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vint4& v ) { _mm_mask_storeu_epi32(ptr,mask,v); }
  71. #elif defined(__AVX__)
  72. static __forceinline vint4 load ( const vbool4& mask, const void* const a ) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); }
  73. static __forceinline vint4 loadu( const vbool4& mask, const void* const a ) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); }
  74. static __forceinline void store ( const vboolf4& mask, void* ptr, const vint4& i ) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); }
  75. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vint4& i ) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); }
  76. #else
  77. static __forceinline vint4 load ( const vbool4& mask, const void* const a ) { return _mm_and_si128(_mm_load_si128 ((__m128i*)a),mask); }
  78. static __forceinline vint4 loadu( const vbool4& mask, const void* const a ) { return _mm_and_si128(_mm_loadu_si128((__m128i*)a),mask); }
  79. static __forceinline void store ( const vboolf4& mask, void* ptr, const vint4& i ) { store (ptr,select(mask,i,load (ptr))); }
  80. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vint4& i ) { storeu(ptr,select(mask,i,loadu(ptr))); }
  81. #endif
  82. #if defined(__SSE4_1__)
  83. static __forceinline vint4 load( const unsigned char* const ptr ) {
  84. return _mm_cvtepu8_epi32(_mm_load_si128((__m128i*)ptr));
  85. }
  86. static __forceinline vint4 loadu( const unsigned char* const ptr ) {
  87. return _mm_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr));
  88. }
  89. #endif
  90. static __forceinline vint4 load(const unsigned short* const ptr) {
  91. #if defined (__SSE4_1__)
  92. return _mm_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr));
  93. #else
  94. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  95. #endif
  96. }
  97. static __forceinline void store_uchar( unsigned char* const ptr, const vint4& v ) {
  98. #if defined(__SSE4_1__)
  99. __m128i x = v;
  100. x = _mm_packus_epi32(x, x);
  101. x = _mm_packus_epi16(x, x);
  102. *(int*)ptr = _mm_cvtsi128_si32(x);
  103. #else
  104. for (size_t i=0;i<4;i++)
  105. ptr[i] = (unsigned char)v[i];
  106. #endif
  107. }
  108. static __forceinline vint4 load_nt (void* ptr) {
  109. #if defined(__SSE4_1__)
  110. return _mm_stream_load_si128((__m128i*)ptr);
  111. #else
  112. return _mm_load_si128((__m128i*)ptr);
  113. #endif
  114. }
  115. static __forceinline void store_nt(void* ptr, const vint4& v) {
  116. #if defined(__SSE4_1__)
  117. _mm_stream_ps((float*)ptr,_mm_castsi128_ps(v));
  118. #else
  119. _mm_store_si128((__m128i*)ptr,v);
  120. #endif
  121. }
  122. #if defined(__x86_64__)
  123. static __forceinline vint4 broadcast64(const long long &a) { return _mm_set1_epi64x(a); }
  124. #endif
  125. ////////////////////////////////////////////////////////////////////////////////
  126. /// Array Access
  127. ////////////////////////////////////////////////////////////////////////////////
  128. __forceinline const int& operator []( const size_t index ) const { assert(index < 4); return i[index]; }
  129. __forceinline int& operator []( const size_t index ) { assert(index < 4); return i[index]; }
  130. friend __forceinline const vint4 select( const vboolf4& m, const vint4& t, const vint4& f ) {
  131. #if defined(__AVX512VL__)
  132. return _mm_mask_blend_epi32(m, f, t);
  133. #elif defined(__SSE4_1__)
  134. return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m));
  135. #else
  136. return _mm_or_si128(_mm_and_si128(m, t), _mm_andnot_si128(m, f));
  137. #endif
  138. }
  139. };
  140. ////////////////////////////////////////////////////////////////////////////////
  141. /// Unary Operators
  142. ////////////////////////////////////////////////////////////////////////////////
  143. __forceinline const vint4 asInt ( const __m128& a ) { return _mm_castps_si128(a); }
  144. __forceinline const vint4 operator +( const vint4& a ) { return a; }
  145. __forceinline const vint4 operator -( const vint4& a ) { return _mm_sub_epi32(_mm_setzero_si128(), a.v); }
  146. #if defined(__SSSE3__)
  147. __forceinline const vint4 abs ( const vint4& a ) { return _mm_abs_epi32(a.v); }
  148. #endif
  149. ////////////////////////////////////////////////////////////////////////////////
  150. /// Binary Operators
  151. ////////////////////////////////////////////////////////////////////////////////
  152. __forceinline const vint4 operator +( const vint4& a, const vint4& b ) { return _mm_add_epi32(a.v, b.v); }
  153. __forceinline const vint4 operator +( const vint4& a, const int& b ) { return a + vint4(b); }
  154. __forceinline const vint4 operator +( const int& a, const vint4& b ) { return vint4(a) + b; }
  155. __forceinline const vint4 operator -( const vint4& a, const vint4& b ) { return _mm_sub_epi32(a.v, b.v); }
  156. __forceinline const vint4 operator -( const vint4& a, const int& b ) { return a - vint4(b); }
  157. __forceinline const vint4 operator -( const int& a, const vint4& b ) { return vint4(a) - b; }
  158. #if defined(__SSE4_1__)
  159. __forceinline const vint4 operator *( const vint4& a, const vint4& b ) { return _mm_mullo_epi32(a.v, b.v); }
  160. #else
  161. __forceinline const vint4 operator *( const vint4& a, const vint4& b ) { return vint4(a[0]*b[0],a[1]*b[1],a[2]*b[2],a[3]*b[3]); }
  162. #endif
  163. __forceinline const vint4 operator *( const vint4& a, const int& b ) { return a * vint4(b); }
  164. __forceinline const vint4 operator *( const int& a, const vint4& b ) { return vint4(a) * b; }
  165. __forceinline const vint4 operator &( const vint4& a, const vint4& b ) { return _mm_and_si128(a.v, b.v); }
  166. __forceinline const vint4 operator &( const vint4& a, const int& b ) { return a & vint4(b); }
  167. __forceinline const vint4 operator &( const int& a, const vint4& b ) { return vint4(a) & b; }
  168. __forceinline const vint4 operator |( const vint4& a, const vint4& b ) { return _mm_or_si128(a.v, b.v); }
  169. __forceinline const vint4 operator |( const vint4& a, const int& b ) { return a | vint4(b); }
  170. __forceinline const vint4 operator |( const int& a, const vint4& b ) { return vint4(a) | b; }
  171. __forceinline const vint4 operator ^( const vint4& a, const vint4& b ) { return _mm_xor_si128(a.v, b.v); }
  172. __forceinline const vint4 operator ^( const vint4& a, const int& b ) { return a ^ vint4(b); }
  173. __forceinline const vint4 operator ^( const int& a, const vint4& b ) { return vint4(a) ^ b; }
  174. __forceinline const vint4 operator <<( const vint4& a, const int& n ) { return _mm_slli_epi32(a.v, n); }
  175. __forceinline const vint4 operator >>( const vint4& a, const int& n ) { return _mm_srai_epi32(a.v, n); }
  176. __forceinline const vint4 sll ( const vint4& a, const int& b ) { return _mm_slli_epi32(a.v, b); }
  177. __forceinline const vint4 sra ( const vint4& a, const int& b ) { return _mm_srai_epi32(a.v, b); }
  178. __forceinline const vint4 srl ( const vint4& a, const int& b ) { return _mm_srli_epi32(a.v, b); }
  179. #if defined(__SSE4_1__)
  180. __forceinline const vint4 min( const vint4& a, const vint4& b ) { return _mm_min_epi32(a.v, b.v); }
  181. __forceinline const vint4 max( const vint4& a, const vint4& b ) { return _mm_max_epi32(a.v, b.v); }
  182. __forceinline const vint4 umin( const vint4& a, const vint4& b ) { return _mm_min_epu32(a.v, b.v); }
  183. __forceinline const vint4 umax( const vint4& a, const vint4& b ) { return _mm_max_epu32(a.v, b.v); }
  184. #else
  185. __forceinline const vint4 min( const vint4& a, const vint4& b ) { return vint4(min(a[0],b[0]),min(a[1],b[1]),min(a[2],b[2]),min(a[3],b[3])); }
  186. __forceinline const vint4 max( const vint4& a, const vint4& b ) { return vint4(max(a[0],b[0]),max(a[1],b[1]),max(a[2],b[2]),max(a[3],b[3])); }
  187. #endif
  188. __forceinline const vint4 min( const vint4& a, const int& b ) { return min(a,vint4(b)); }
  189. __forceinline const vint4 min( const int& a, const vint4& b ) { return min(vint4(a),b); }
  190. __forceinline const vint4 max( const vint4& a, const int& b ) { return max(a,vint4(b)); }
  191. __forceinline const vint4 max( const int& a, const vint4& b ) { return max(vint4(a),b); }
  192. ////////////////////////////////////////////////////////////////////////////////
  193. /// Assignment Operators
  194. ////////////////////////////////////////////////////////////////////////////////
  195. __forceinline vint4& operator +=( vint4& a, const vint4& b ) { return a = a + b; }
  196. __forceinline vint4& operator +=( vint4& a, const int& b ) { return a = a + b; }
  197. __forceinline vint4& operator -=( vint4& a, const vint4& b ) { return a = a - b; }
  198. __forceinline vint4& operator -=( vint4& a, const int& b ) { return a = a - b; }
  199. #if defined(__SSE4_1__)
  200. __forceinline vint4& operator *=( vint4& a, const vint4& b ) { return a = a * b; }
  201. __forceinline vint4& operator *=( vint4& a, const int& b ) { return a = a * b; }
  202. #endif
  203. __forceinline vint4& operator &=( vint4& a, const vint4& b ) { return a = a & b; }
  204. __forceinline vint4& operator &=( vint4& a, const int& b ) { return a = a & b; }
  205. __forceinline vint4& operator |=( vint4& a, const vint4& b ) { return a = a | b; }
  206. __forceinline vint4& operator |=( vint4& a, const int& b ) { return a = a | b; }
  207. __forceinline vint4& operator <<=( vint4& a, const int& b ) { return a = a << b; }
  208. __forceinline vint4& operator >>=( vint4& a, const int& b ) { return a = a >> b; }
  209. ////////////////////////////////////////////////////////////////////////////////
  210. /// Comparison Operators + Select
  211. ////////////////////////////////////////////////////////////////////////////////
  212. #if defined(__AVX512VL__)
  213. __forceinline const vboolf4 operator ==( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  214. __forceinline const vboolf4 operator !=( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  215. __forceinline const vboolf4 operator < ( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  216. __forceinline const vboolf4 operator >=( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  217. __forceinline const vboolf4 operator > ( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  218. __forceinline const vboolf4 operator <=( const vint4& a, const vint4& b ) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
  219. #else
  220. __forceinline const vboolf4 operator ==( const vint4& a, const vint4& b ) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }
  221. __forceinline const vboolf4 operator !=( const vint4& a, const vint4& b ) { return !(a == b); }
  222. __forceinline const vboolf4 operator < ( const vint4& a, const vint4& b ) { return _mm_castsi128_ps(_mm_cmplt_epi32(a, b)); }
  223. __forceinline const vboolf4 operator >=( const vint4& a, const vint4& b ) { return !(a < b); }
  224. __forceinline const vboolf4 operator > ( const vint4& a, const vint4& b ) { return _mm_castsi128_ps(_mm_cmpgt_epi32(a, b)); }
  225. __forceinline const vboolf4 operator <=( const vint4& a, const vint4& b ) { return !(a > b); }
  226. #endif
  227. __forceinline const vboolf4 operator ==( const vint4& a, const int& b ) { return a == vint4(b); }
  228. __forceinline const vboolf4 operator ==( const int& a, const vint4& b ) { return vint4(a) == b; }
  229. __forceinline const vboolf4 operator !=( const vint4& a, const int& b ) { return a != vint4(b); }
  230. __forceinline const vboolf4 operator !=( const int& a, const vint4& b ) { return vint4(a) != b; }
  231. __forceinline const vboolf4 operator < ( const vint4& a, const int& b ) { return a < vint4(b); }
  232. __forceinline const vboolf4 operator < ( const int& a, const vint4& b ) { return vint4(a) < b; }
  233. __forceinline const vboolf4 operator >=( const vint4& a, const int& b ) { return a >= vint4(b); }
  234. __forceinline const vboolf4 operator >=( const int& a, const vint4& b ) { return vint4(a) >= b; }
  235. __forceinline const vboolf4 operator > ( const vint4& a, const int& b ) { return a > vint4(b); }
  236. __forceinline const vboolf4 operator > ( const int& a, const vint4& b ) { return vint4(a) > b; }
  237. __forceinline const vboolf4 operator <=( const vint4& a, const int& b ) { return a <= vint4(b); }
  238. __forceinline const vboolf4 operator <=( const int& a, const vint4& b ) { return vint4(a) <= b; }
  239. __forceinline vboolf4 eq(const vint4& a, const vint4& b) { return a == b; }
  240. __forceinline vboolf4 ne(const vint4& a, const vint4& b) { return a != b; }
  241. __forceinline vboolf4 lt(const vint4& a, const vint4& b) { return a < b; }
  242. __forceinline vboolf4 ge(const vint4& a, const vint4& b) { return a >= b; }
  243. __forceinline vboolf4 gt(const vint4& a, const vint4& b) { return a > b; }
  244. __forceinline vboolf4 le(const vint4& a, const vint4& b) { return a <= b; }
  245. #if defined(__AVX512VL__)
  246. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  247. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  248. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  249. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  250. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  251. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
  252. #else
  253. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a == b); }
  254. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a != b); }
  255. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a < b); }
  256. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a >= b); }
  257. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a > b); }
  258. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a <= b); }
  259. #endif
  260. template<int mask>
  261. __forceinline const vint4 select(const vint4& t, const vint4& f) {
  262. #if defined(__SSE4_1__)
  263. return _mm_castps_si128(_mm_blend_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), mask));
  264. #else
  265. return select(vboolf4(mask), t, f);
  266. #endif
  267. }
  268. ////////////////////////////////////////////////////////////////////////////////
  269. // Movement/Shifting/Shuffling Functions
  270. ////////////////////////////////////////////////////////////////////////////////
  271. __forceinline vint4 unpacklo( const vint4& a, const vint4& b ) { return _mm_castps_si128(_mm_unpacklo_ps(_mm_castsi128_ps(a.v), _mm_castsi128_ps(b.v))); }
  272. __forceinline vint4 unpackhi( const vint4& a, const vint4& b ) { return _mm_castps_si128(_mm_unpackhi_ps(_mm_castsi128_ps(a.v), _mm_castsi128_ps(b.v))); }
  273. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vint4 shuffle( const vint4& a ) {
  274. return _mm_shuffle_epi32(a, _MM_SHUFFLE(i3, i2, i1, i0));
  275. }
  276. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vint4 shuffle( const vint4& a, const vint4& b ) {
  277. return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  278. }
  279. #if defined(__SSE3__)
  280. template<> __forceinline const vint4 shuffle<0, 0, 2, 2>( const vint4& a ) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(a))); }
  281. template<> __forceinline const vint4 shuffle<1, 1, 3, 3>( const vint4& a ) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(a))); }
  282. template<> __forceinline const vint4 shuffle<0, 1, 0, 1>( const vint4& a ) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(a))); }
  283. #endif
  284. template<size_t i0> __forceinline const vint4 shuffle( const vint4& b ) {
  285. return shuffle<i0,i0,i0,i0>(b);
  286. }
  287. #if defined(__SSE4_1__)
  288. template<size_t src> __forceinline int extract( const vint4& b ) { return _mm_extract_epi32(b, src); }
  289. template<size_t dst> __forceinline const vint4 insert( const vint4& a, const int b ) { return _mm_insert_epi32(a, b, dst); }
  290. #else
  291. template<size_t src> __forceinline int extract( const vint4& b ) { return b[src]; }
  292. template<size_t dst> __forceinline const vint4 insert( const vint4& a, const int b ) { vint4 c = a; c[dst] = b; return c; }
  293. #endif
  294. template<> __forceinline int extract<0>( const vint4& b ) { return _mm_cvtsi128_si32(b); }
  295. __forceinline int toScalar(const vint4& a) { return _mm_cvtsi128_si32(a); }
  296. ////////////////////////////////////////////////////////////////////////////////
  297. /// Reductions
  298. ////////////////////////////////////////////////////////////////////////////////
  299. #if defined(__SSE4_1__)
  300. __forceinline const vint4 vreduce_min(const vint4& v) { vint4 h = min(shuffle<1,0,3,2>(v),v); return min(shuffle<2,3,0,1>(h),h); }
  301. __forceinline const vint4 vreduce_max(const vint4& v) { vint4 h = max(shuffle<1,0,3,2>(v),v); return max(shuffle<2,3,0,1>(h),h); }
  302. __forceinline const vint4 vreduce_add(const vint4& v) { vint4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }
  303. __forceinline int reduce_min(const vint4& v) { return toScalar(vreduce_min(v)); }
  304. __forceinline int reduce_max(const vint4& v) { return toScalar(vreduce_max(v)); }
  305. __forceinline int reduce_add(const vint4& v) { return toScalar(vreduce_add(v)); }
  306. __forceinline size_t select_min(const vint4& v) { return __bsf(movemask(v == vreduce_min(v))); }
  307. __forceinline size_t select_max(const vint4& v) { return __bsf(movemask(v == vreduce_max(v))); }
  308. __forceinline size_t select_min(const vboolf4& valid, const vint4& v) { const vint4 a = select(valid,v,vint4(pos_inf)); return __bsf(movemask(valid & (a == vreduce_min(a)))); }
  309. __forceinline size_t select_max(const vboolf4& valid, const vint4& v) { const vint4 a = select(valid,v,vint4(neg_inf)); return __bsf(movemask(valid & (a == vreduce_max(a)))); }
  310. #else
  311. __forceinline int reduce_min(const vint4& v) { return min(v[0],v[1],v[2],v[3]); }
  312. __forceinline int reduce_max(const vint4& v) { return max(v[0],v[1],v[2],v[3]); }
  313. __forceinline int reduce_add(const vint4& v) { return v[0]+v[1]+v[2]+v[3]; }
  314. #endif
  315. ////////////////////////////////////////////////////////////////////////////////
  316. /// Sorting networks
  317. ////////////////////////////////////////////////////////////////////////////////
  318. #if defined(__SSE4_1__)
  319. __forceinline vint4 sortNetwork(const vint4& v)
  320. {
  321. const vint4 a0 = v;
  322. const vint4 b0 = shuffle<1,0,3,2>(a0);
  323. const vint4 c0 = umin(a0,b0);
  324. const vint4 d0 = umax(a0,b0);
  325. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  326. const vint4 b1 = shuffle<2,3,0,1>(a1);
  327. const vint4 c1 = umin(a1,b1);
  328. const vint4 d1 = umax(a1,b1);
  329. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  330. const vint4 b2 = shuffle<0,2,1,3>(a2);
  331. const vint4 c2 = umin(a2,b2);
  332. const vint4 d2 = umax(a2,b2);
  333. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  334. return a3;
  335. }
  336. #endif
  337. ////////////////////////////////////////////////////////////////////////////////
  338. /// Output Operators
  339. ////////////////////////////////////////////////////////////////////////////////
  340. inline std::ostream& operator<<(std::ostream& cout, const vint4& a) {
  341. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  342. }
  343. }