vuint4_sse2.h 22 KB

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