vint4_sse2.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 vint<4>
  17. {
  18. ALIGNED_STRUCT_(16);
  19. typedef vboolf4 Bool;
  20. typedef vint4 Int;
  21. typedef vfloat4 Float;
  22. enum { size = 4 }; // number of SIMD elements
  23. union { __m128i v; int i[4]; }; // data
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// Constructors, Assignment & Cast Operators
  26. ////////////////////////////////////////////////////////////////////////////////
  27. __forceinline vint() {}
  28. __forceinline vint(const vint4& a) { v = a.v; }
  29. __forceinline vint4& operator =(const vint4& a) { v = a.v; return *this; }
  30. __forceinline vint(__m128i a) : v(a) {}
  31. __forceinline operator const __m128i&() const { return v; }
  32. __forceinline operator __m128i&() { return v; }
  33. __forceinline vint(int a) : v(_mm_set1_epi32(a)) {}
  34. __forceinline vint(int a, int b, int c, int d) : v(_mm_set_epi32(d, c, b, a)) {}
  35. __forceinline explicit vint(__m128 a) : v(_mm_cvtps_epi32(a)) {}
  36. #if defined(__AVX512VL__)
  37. __forceinline explicit vint(const vboolf4& a) : v(_mm_movm_epi32(a)) {}
  38. #else
  39. __forceinline explicit vint(const vboolf4& a) : v(_mm_castps_si128((__m128)a)) {}
  40. #endif
  41. __forceinline vint(long long a, long long b) : v(_mm_set_epi64x(b,a)) {}
  42. ////////////////////////////////////////////////////////////////////////////////
  43. /// Constants
  44. ////////////////////////////////////////////////////////////////////////////////
  45. __forceinline vint(ZeroTy) : v(_mm_setzero_si128()) {}
  46. __forceinline vint(OneTy) : v(_mm_set_epi32(1, 1, 1, 1)) {}
  47. __forceinline vint(PosInfTy) : v(_mm_set_epi32(pos_inf, pos_inf, pos_inf, pos_inf)) {}
  48. __forceinline vint(NegInfTy) : v(_mm_set_epi32(neg_inf, neg_inf, neg_inf, neg_inf)) {}
  49. __forceinline vint(StepTy) : v(_mm_set_epi32(3, 2, 1, 0)) {}
  50. __forceinline vint(ReverseStepTy) : v(_mm_set_epi32(0, 1, 2, 3)) {}
  51. __forceinline vint(TrueTy) { v = _mm_cmpeq_epi32(v,v); }
  52. __forceinline vint(UndefinedTy) : v(_mm_castps_si128(_mm_undefined_ps())) {}
  53. ////////////////////////////////////////////////////////////////////////////////
  54. /// Loads and Stores
  55. ////////////////////////////////////////////////////////////////////////////////
  56. static __forceinline vint4 load (const void* a) { return _mm_load_si128((__m128i*)a); }
  57. static __forceinline vint4 loadu(const void* a) { return _mm_loadu_si128((__m128i*)a); }
  58. static __forceinline void store (void* ptr, const vint4& v) { _mm_store_si128((__m128i*)ptr,v); }
  59. static __forceinline void storeu(void* ptr, const vint4& v) { _mm_storeu_si128((__m128i*)ptr,v); }
  60. #if defined(__AVX512VL__)
  61. static __forceinline vint4 compact(const vboolf4& mask, vint4 &v) {
  62. return _mm_mask_compress_epi32(v, mask, v);
  63. }
  64. static __forceinline vint4 compact(const vboolf4& mask, vint4 &a, const vint4& b) {
  65. return _mm_mask_compress_epi32(a, mask, b);
  66. }
  67. static __forceinline vint4 load (const vboolf4& mask, const void* ptr) { return _mm_mask_load_epi32 (_mm_setzero_si128(),mask,ptr); }
  68. static __forceinline vint4 loadu(const vboolf4& mask, const void* 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* a) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); }
  73. static __forceinline vint4 loadu(const vbool4& mask, const void* 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* a) { return _mm_and_si128(_mm_load_si128 ((__m128i*)a),mask); }
  78. static __forceinline vint4 loadu(const vbool4& mask, const void* 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* ptr) {
  84. return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));
  85. }
  86. static __forceinline vint4 loadu(const unsigned char* ptr) {
  87. return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));
  88. }
  89. #else
  90. static __forceinline vint4 load(const unsigned char* ptr) {
  91. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  92. }
  93. static __forceinline vint4 loadu(const unsigned char* ptr) {
  94. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  95. }
  96. #endif
  97. static __forceinline vint4 load(const unsigned short* ptr) {
  98. #if defined (__SSE4_1__)
  99. return _mm_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr));
  100. #else
  101. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  102. #endif
  103. }
  104. static __forceinline void store(unsigned char* ptr, const vint4& v) {
  105. #if defined(__SSE4_1__)
  106. __m128i x = v;
  107. x = _mm_packus_epi32(x, x);
  108. x = _mm_packus_epi16(x, x);
  109. *(int*)ptr = _mm_cvtsi128_si32(x);
  110. #else
  111. for (size_t i=0;i<4;i++)
  112. ptr[i] = (unsigned char)v[i];
  113. #endif
  114. }
  115. static __forceinline void store(unsigned short* ptr, const vint4& v) {
  116. for (size_t i=0;i<4;i++)
  117. ptr[i] = (unsigned short)v[i];
  118. }
  119. static __forceinline vint4 load_nt(void* ptr) {
  120. #if defined(__SSE4_1__)
  121. return _mm_stream_load_si128((__m128i*)ptr);
  122. #else
  123. return _mm_load_si128((__m128i*)ptr);
  124. #endif
  125. }
  126. static __forceinline void store_nt(void* ptr, const vint4& v) {
  127. #if defined(__SSE4_1__)
  128. _mm_stream_ps((float*)ptr, _mm_castsi128_ps(v));
  129. #else
  130. _mm_store_si128((__m128i*)ptr,v);
  131. #endif
  132. }
  133. template<int scale = 4>
  134. static __forceinline vint4 gather(const int* ptr, const vint4& index) {
  135. #if defined(__AVX2__)
  136. return _mm_i32gather_epi32(ptr, index, scale);
  137. #else
  138. return vint4(
  139. *(int*)(((char*)ptr)+scale*index[0]),
  140. *(int*)(((char*)ptr)+scale*index[1]),
  141. *(int*)(((char*)ptr)+scale*index[2]),
  142. *(int*)(((char*)ptr)+scale*index[3]));
  143. #endif
  144. }
  145. template<int scale = 4>
  146. static __forceinline vint4 gather(const vboolf4& mask, const int* ptr, const vint4& index) {
  147. vint4 r = zero;
  148. #if defined(__AVX512VL__)
  149. return _mm_mmask_i32gather_epi32(r, mask, index, ptr, scale);
  150. #elif defined(__AVX2__)
  151. return _mm_mask_i32gather_epi32(r, ptr, index, mask, scale);
  152. #else
  153. if (likely(mask[0])) r[0] = *(int*)(((char*)ptr)+scale*index[0]);
  154. if (likely(mask[1])) r[1] = *(int*)(((char*)ptr)+scale*index[1]);
  155. if (likely(mask[2])) r[2] = *(int*)(((char*)ptr)+scale*index[2]);
  156. if (likely(mask[3])) r[3] = *(int*)(((char*)ptr)+scale*index[3]);
  157. return r;
  158. #endif
  159. }
  160. template<int scale = 4>
  161. static __forceinline void scatter(void* ptr, const vint4& index, const vint4& v)
  162. {
  163. #if defined(__AVX512VL__)
  164. _mm_i32scatter_epi32((int*)ptr, index, v, scale);
  165. #else
  166. *(int*)(((char*)ptr)+scale*index[0]) = v[0];
  167. *(int*)(((char*)ptr)+scale*index[1]) = v[1];
  168. *(int*)(((char*)ptr)+scale*index[2]) = v[2];
  169. *(int*)(((char*)ptr)+scale*index[3]) = v[3];
  170. #endif
  171. }
  172. template<int scale = 4>
  173. static __forceinline void scatter(const vboolf4& mask, void* ptr, const vint4& index, const vint4& v)
  174. {
  175. #if defined(__AVX512VL__)
  176. _mm_mask_i32scatter_epi32((int*)ptr, mask, index, v, scale);
  177. #else
  178. if (likely(mask[0])) *(int*)(((char*)ptr)+scale*index[0]) = v[0];
  179. if (likely(mask[1])) *(int*)(((char*)ptr)+scale*index[1]) = v[1];
  180. if (likely(mask[2])) *(int*)(((char*)ptr)+scale*index[2]) = v[2];
  181. if (likely(mask[3])) *(int*)(((char*)ptr)+scale*index[3]) = v[3];
  182. #endif
  183. }
  184. #if defined(__x86_64__)
  185. static __forceinline vint4 broadcast64(long long a) { return _mm_set1_epi64x(a); }
  186. #endif
  187. ////////////////////////////////////////////////////////////////////////////////
  188. /// Array Access
  189. ////////////////////////////////////////////////////////////////////////////////
  190. __forceinline const int& operator [](size_t index) const { assert(index < 4); return i[index]; }
  191. __forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }
  192. friend __forceinline vint4 select(const vboolf4& m, const vint4& t, const vint4& f) {
  193. #if defined(__AVX512VL__)
  194. return _mm_mask_blend_epi32(m, (__m128i)f, (__m128i)t);
  195. #elif defined(__SSE4_1__)
  196. return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m));
  197. #else
  198. return _mm_or_si128(_mm_and_si128(m, t), _mm_andnot_si128(m, f));
  199. #endif
  200. }
  201. };
  202. ////////////////////////////////////////////////////////////////////////////////
  203. /// Unary Operators
  204. ////////////////////////////////////////////////////////////////////////////////
  205. #if defined(__AVX512VL__)
  206. __forceinline vboolf4 asBool(const vint4& a) { return _mm_movepi32_mask(a); }
  207. #else
  208. __forceinline vboolf4 asBool(const vint4& a) { return _mm_castsi128_ps(a); }
  209. #endif
  210. __forceinline vint4 operator +(const vint4& a) { return a; }
  211. __forceinline vint4 operator -(const vint4& a) { return _mm_sub_epi32(_mm_setzero_si128(), a); }
  212. #if defined(__SSSE3__)
  213. __forceinline vint4 abs(const vint4& a) { return _mm_abs_epi32(a); }
  214. #endif
  215. ////////////////////////////////////////////////////////////////////////////////
  216. /// Binary Operators
  217. ////////////////////////////////////////////////////////////////////////////////
  218. __forceinline vint4 operator +(const vint4& a, const vint4& b) { return _mm_add_epi32(a, b); }
  219. __forceinline vint4 operator +(const vint4& a, int b) { return a + vint4(b); }
  220. __forceinline vint4 operator +(int a, const vint4& b) { return vint4(a) + b; }
  221. __forceinline vint4 operator -(const vint4& a, const vint4& b) { return _mm_sub_epi32(a, b); }
  222. __forceinline vint4 operator -(const vint4& a, int b) { return a - vint4(b); }
  223. __forceinline vint4 operator -(int a, const vint4& b) { return vint4(a) - b; }
  224. #if defined(__SSE4_1__)
  225. __forceinline vint4 operator *(const vint4& a, const vint4& b) { return _mm_mullo_epi32(a, b); }
  226. #else
  227. __forceinline 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]); }
  228. #endif
  229. __forceinline vint4 operator *(const vint4& a, int b) { return a * vint4(b); }
  230. __forceinline vint4 operator *(int a, const vint4& b) { return vint4(a) * b; }
  231. __forceinline vint4 operator &(const vint4& a, const vint4& b) { return _mm_and_si128(a, b); }
  232. __forceinline vint4 operator &(const vint4& a, int b) { return a & vint4(b); }
  233. __forceinline vint4 operator &(int a, const vint4& b) { return vint4(a) & b; }
  234. __forceinline vint4 operator |(const vint4& a, const vint4& b) { return _mm_or_si128(a, b); }
  235. __forceinline vint4 operator |(const vint4& a, int b) { return a | vint4(b); }
  236. __forceinline vint4 operator |(int a, const vint4& b) { return vint4(a) | b; }
  237. __forceinline vint4 operator ^(const vint4& a, const vint4& b) { return _mm_xor_si128(a, b); }
  238. __forceinline vint4 operator ^(const vint4& a, int b) { return a ^ vint4(b); }
  239. __forceinline vint4 operator ^(int a, const vint4& b) { return vint4(a) ^ b; }
  240. __forceinline vint4 operator <<(const vint4& a, int n) { return _mm_slli_epi32(a, n); }
  241. __forceinline vint4 operator >>(const vint4& a, int n) { return _mm_srai_epi32(a, n); }
  242. __forceinline vint4 sll (const vint4& a, int b) { return _mm_slli_epi32(a, b); }
  243. __forceinline vint4 sra (const vint4& a, int b) { return _mm_srai_epi32(a, b); }
  244. __forceinline vint4 srl (const vint4& a, int b) { return _mm_srli_epi32(a, b); }
  245. ////////////////////////////////////////////////////////////////////////////////
  246. /// Assignment Operators
  247. ////////////////////////////////////////////////////////////////////////////////
  248. __forceinline vint4& operator +=(vint4& a, const vint4& b) { return a = a + b; }
  249. __forceinline vint4& operator +=(vint4& a, int b) { return a = a + b; }
  250. __forceinline vint4& operator -=(vint4& a, const vint4& b) { return a = a - b; }
  251. __forceinline vint4& operator -=(vint4& a, int b) { return a = a - b; }
  252. #if defined(__SSE4_1__)
  253. __forceinline vint4& operator *=(vint4& a, const vint4& b) { return a = a * b; }
  254. __forceinline vint4& operator *=(vint4& a, int b) { return a = a * b; }
  255. #endif
  256. __forceinline vint4& operator &=(vint4& a, const vint4& b) { return a = a & b; }
  257. __forceinline vint4& operator &=(vint4& a, int b) { return a = a & b; }
  258. __forceinline vint4& operator |=(vint4& a, const vint4& b) { return a = a | b; }
  259. __forceinline vint4& operator |=(vint4& a, int b) { return a = a | b; }
  260. __forceinline vint4& operator <<=(vint4& a, int b) { return a = a << b; }
  261. __forceinline vint4& operator >>=(vint4& a, int b) { return a = a >> b; }
  262. ////////////////////////////////////////////////////////////////////////////////
  263. /// Comparison Operators + Select
  264. ////////////////////////////////////////////////////////////////////////////////
  265. #if defined(__AVX512VL__)
  266. __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  267. __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  268. __forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  269. __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  270. __forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  271. __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
  272. #else
  273. __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }
  274. __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return !(a == b); }
  275. __forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmplt_epi32(a, b)); }
  276. __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return !(a < b); }
  277. __forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpgt_epi32(a, b)); }
  278. __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return !(a > b); }
  279. #endif
  280. __forceinline vboolf4 operator ==(const vint4& a, int b) { return a == vint4(b); }
  281. __forceinline vboolf4 operator ==(int a, const vint4& b) { return vint4(a) == b; }
  282. __forceinline vboolf4 operator !=(const vint4& a, int b) { return a != vint4(b); }
  283. __forceinline vboolf4 operator !=(int a, const vint4& b) { return vint4(a) != b; }
  284. __forceinline vboolf4 operator < (const vint4& a, int b) { return a < vint4(b); }
  285. __forceinline vboolf4 operator < (int a, const vint4& b) { return vint4(a) < b; }
  286. __forceinline vboolf4 operator >=(const vint4& a, int b) { return a >= vint4(b); }
  287. __forceinline vboolf4 operator >=(int a, const vint4& b) { return vint4(a) >= b; }
  288. __forceinline vboolf4 operator > (const vint4& a, int b) { return a > vint4(b); }
  289. __forceinline vboolf4 operator > (int a, const vint4& b) { return vint4(a) > b; }
  290. __forceinline vboolf4 operator <=(const vint4& a, int b) { return a <= vint4(b); }
  291. __forceinline vboolf4 operator <=(int a, const vint4& b) { return vint4(a) <= b; }
  292. __forceinline vboolf4 eq(const vint4& a, const vint4& b) { return a == b; }
  293. __forceinline vboolf4 ne(const vint4& a, const vint4& b) { return a != b; }
  294. __forceinline vboolf4 lt(const vint4& a, const vint4& b) { return a < b; }
  295. __forceinline vboolf4 ge(const vint4& a, const vint4& b) { return a >= b; }
  296. __forceinline vboolf4 gt(const vint4& a, const vint4& b) { return a > b; }
  297. __forceinline vboolf4 le(const vint4& a, const vint4& b) { return a <= b; }
  298. #if defined(__AVX512VL__)
  299. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  300. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  301. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  302. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  303. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  304. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
  305. #else
  306. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a == b); }
  307. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a != b); }
  308. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a < b); }
  309. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a >= b); }
  310. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a > b); }
  311. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a <= b); }
  312. #endif
  313. template<int mask>
  314. __forceinline vint4 select(const vint4& t, const vint4& f) {
  315. #if defined(__SSE4_1__)
  316. return _mm_castps_si128(_mm_blend_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), mask));
  317. #else
  318. return select(vboolf4(mask), t, f);
  319. #endif
  320. }
  321. #if defined(__SSE4_1__)
  322. __forceinline vint4 min(const vint4& a, const vint4& b) { return _mm_min_epi32(a, b); }
  323. __forceinline vint4 max(const vint4& a, const vint4& b) { return _mm_max_epi32(a, b); }
  324. __forceinline vint4 umin(const vint4& a, const vint4& b) { return _mm_min_epu32(a, b); }
  325. __forceinline vint4 umax(const vint4& a, const vint4& b) { return _mm_max_epu32(a, b); }
  326. #else
  327. __forceinline vint4 min(const vint4& a, const vint4& b) { return select(a < b,a,b); }
  328. __forceinline vint4 max(const vint4& a, const vint4& b) { return select(a < b,b,a); }
  329. #endif
  330. __forceinline vint4 min(const vint4& a, int b) { return min(a,vint4(b)); }
  331. __forceinline vint4 min(int a, const vint4& b) { return min(vint4(a),b); }
  332. __forceinline vint4 max(const vint4& a, int b) { return max(a,vint4(b)); }
  333. __forceinline vint4 max(int a, const vint4& b) { return max(vint4(a),b); }
  334. ////////////////////////////////////////////////////////////////////////////////
  335. // Movement/Shifting/Shuffling Functions
  336. ////////////////////////////////////////////////////////////////////////////////
  337. __forceinline vint4 unpacklo(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpacklo_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }
  338. __forceinline vint4 unpackhi(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpackhi_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }
  339. template<int i0, int i1, int i2, int i3>
  340. __forceinline vint4 shuffle(const vint4& v) {
  341. return _mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0));
  342. }
  343. template<int i0, int i1, int i2, int i3>
  344. __forceinline vint4 shuffle(const vint4& a, const vint4& b) {
  345. return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  346. }
  347. #if defined(__SSE3__)
  348. template<> __forceinline vint4 shuffle<0, 0, 2, 2>(const vint4& v) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(v))); }
  349. template<> __forceinline vint4 shuffle<1, 1, 3, 3>(const vint4& v) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(v))); }
  350. template<> __forceinline vint4 shuffle<0, 1, 0, 1>(const vint4& v) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(v))); }
  351. #endif
  352. template<int i>
  353. __forceinline vint4 shuffle(const vint4& v) {
  354. return shuffle<i,i,i,i>(v);
  355. }
  356. #if defined(__SSE4_1__)
  357. template<int src> __forceinline int extract(const vint4& b) { return _mm_extract_epi32(b, src); }
  358. template<int dst> __forceinline vint4 insert(const vint4& a, const int b) { return _mm_insert_epi32(a, b, dst); }
  359. #else
  360. template<int src> __forceinline int extract(const vint4& b) { return b[src&3]; }
  361. template<int dst> __forceinline vint4 insert(const vint4& a, int b) { vint4 c = a; c[dst&3] = b; return c; }
  362. #endif
  363. template<> __forceinline int extract<0>(const vint4& b) { return _mm_cvtsi128_si32(b); }
  364. __forceinline int toScalar(const vint4& v) { return _mm_cvtsi128_si32(v); }
  365. __forceinline size_t toSizeT(const vint4& v) {
  366. #if defined(__WIN32__) && !defined(__X86_64__) // win32 workaround
  367. return toScalar(v);
  368. #else
  369. return _mm_cvtsi128_si64(v);
  370. #endif
  371. }
  372. #if defined(__AVX512VL__)
  373. __forceinline vint4 permute(const vint4 &a, const vint4 &index) {
  374. return _mm_castps_si128(_mm_permutevar_ps(_mm_castsi128_ps(a),index));
  375. }
  376. template<int i>
  377. __forceinline vint4 align_shift_right(const vint4& a, const vint4& b) {
  378. return _mm_alignr_epi32(a, b, i);
  379. }
  380. #endif
  381. ////////////////////////////////////////////////////////////////////////////////
  382. /// Reductions
  383. ////////////////////////////////////////////////////////////////////////////////
  384. #if defined(__SSE4_1__)
  385. __forceinline 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); }
  386. __forceinline 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); }
  387. __forceinline vint4 vreduce_add(const vint4& v) { vint4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }
  388. __forceinline int reduce_min(const vint4& v) { return toScalar(vreduce_min(v)); }
  389. __forceinline int reduce_max(const vint4& v) { return toScalar(vreduce_max(v)); }
  390. __forceinline int reduce_add(const vint4& v) { return toScalar(vreduce_add(v)); }
  391. __forceinline size_t select_min(const vint4& v) { return bsf(movemask(v == vreduce_min(v))); }
  392. __forceinline size_t select_max(const vint4& v) { return bsf(movemask(v == vreduce_max(v))); }
  393. __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)))); }
  394. __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)))); }
  395. #else
  396. __forceinline int reduce_min(const vint4& v) { return min(v[0],v[1],v[2],v[3]); }
  397. __forceinline int reduce_max(const vint4& v) { return max(v[0],v[1],v[2],v[3]); }
  398. __forceinline int reduce_add(const vint4& v) { return v[0]+v[1]+v[2]+v[3]; }
  399. #endif
  400. ////////////////////////////////////////////////////////////////////////////////
  401. /// Sorting networks
  402. ////////////////////////////////////////////////////////////////////////////////
  403. #if defined(__SSE4_1__)
  404. __forceinline vint4 usort_ascending(const vint4& v)
  405. {
  406. const vint4 a0 = v;
  407. const vint4 b0 = shuffle<1,0,3,2>(a0);
  408. const vint4 c0 = umin(a0,b0);
  409. const vint4 d0 = umax(a0,b0);
  410. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  411. const vint4 b1 = shuffle<2,3,0,1>(a1);
  412. const vint4 c1 = umin(a1,b1);
  413. const vint4 d1 = umax(a1,b1);
  414. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  415. const vint4 b2 = shuffle<0,2,1,3>(a2);
  416. const vint4 c2 = umin(a2,b2);
  417. const vint4 d2 = umax(a2,b2);
  418. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  419. return a3;
  420. }
  421. __forceinline vint4 usort_descending(const vint4& v)
  422. {
  423. const vint4 a0 = v;
  424. const vint4 b0 = shuffle<1,0,3,2>(a0);
  425. const vint4 c0 = umax(a0,b0);
  426. const vint4 d0 = umin(a0,b0);
  427. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  428. const vint4 b1 = shuffle<2,3,0,1>(a1);
  429. const vint4 c1 = umax(a1,b1);
  430. const vint4 d1 = umin(a1,b1);
  431. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  432. const vint4 b2 = shuffle<0,2,1,3>(a2);
  433. const vint4 c2 = umax(a2,b2);
  434. const vint4 d2 = umin(a2,b2);
  435. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  436. return a3;
  437. }
  438. #else
  439. __forceinline vint4 usort_ascending(const vint4& v)
  440. {
  441. const vint4 a0 = v-vint4(0x80000000);
  442. const vint4 b0 = shuffle<1,0,3,2>(a0);
  443. const vint4 c0 = min(a0,b0);
  444. const vint4 d0 = max(a0,b0);
  445. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  446. const vint4 b1 = shuffle<2,3,0,1>(a1);
  447. const vint4 c1 = min(a1,b1);
  448. const vint4 d1 = max(a1,b1);
  449. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  450. const vint4 b2 = shuffle<0,2,1,3>(a2);
  451. const vint4 c2 = min(a2,b2);
  452. const vint4 d2 = max(a2,b2);
  453. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  454. return a3+vint4(0x80000000);
  455. }
  456. __forceinline vint4 usort_descending(const vint4& v)
  457. {
  458. const vint4 a0 = v-vint4(0x80000000);
  459. const vint4 b0 = shuffle<1,0,3,2>(a0);
  460. const vint4 c0 = max(a0,b0);
  461. const vint4 d0 = min(a0,b0);
  462. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  463. const vint4 b1 = shuffle<2,3,0,1>(a1);
  464. const vint4 c1 = max(a1,b1);
  465. const vint4 d1 = min(a1,b1);
  466. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  467. const vint4 b2 = shuffle<0,2,1,3>(a2);
  468. const vint4 c2 = max(a2,b2);
  469. const vint4 d2 = min(a2,b2);
  470. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  471. return a3+vint4(0x80000000);
  472. }
  473. #endif
  474. ////////////////////////////////////////////////////////////////////////////////
  475. /// Output Operators
  476. ////////////////////////////////////////////////////////////////////////////////
  477. __forceinline embree_ostream operator <<(embree_ostream cout, const vint4& a) {
  478. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  479. }
  480. }
  481. #undef vboolf
  482. #undef vboold
  483. #undef vint
  484. #undef vuint
  485. #undef vllong
  486. #undef vfloat
  487. #undef vdouble