vint4_sse2.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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(__aarch64__)
  83. static __forceinline vint4 load(const unsigned char* ptr) {
  84. return _mm_load4epu8_epi32(((__m128i*)ptr));
  85. }
  86. static __forceinline vint4 loadu(const unsigned char* ptr) {
  87. return _mm_load4epu8_epi32(((__m128i*)ptr));
  88. }
  89. #elif defined(__SSE4_1__)
  90. static __forceinline vint4 load(const unsigned char* ptr) {
  91. return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));
  92. }
  93. static __forceinline vint4 loadu(const unsigned char* ptr) {
  94. return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));
  95. }
  96. #else
  97. static __forceinline vint4 load(const unsigned char* ptr) {
  98. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  99. }
  100. static __forceinline vint4 loadu(const unsigned char* ptr) {
  101. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  102. }
  103. #endif
  104. static __forceinline vint4 load(const unsigned short* ptr) {
  105. #if defined(__aarch64__)
  106. return __m128i(vmovl_u16(vld1_u16(ptr)));
  107. #elif defined (__SSE4_1__)
  108. return _mm_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr));
  109. #else
  110. return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);
  111. #endif
  112. }
  113. static __forceinline void store(unsigned char* ptr, const vint4& v) {
  114. #if defined(__aarch64__)
  115. int32x4_t x = v;
  116. uint16x4_t y = vqmovn_u32(uint32x4_t(x));
  117. uint8x8_t z = vqmovn_u16(vcombine_u16(y, y));
  118. vst1_lane_u32((uint32_t *)ptr,uint32x2_t(z), 0);
  119. #elif defined(__SSE4_1__)
  120. __m128i x = v;
  121. x = _mm_packus_epi32(x, x);
  122. x = _mm_packus_epi16(x, x);
  123. *(int*)ptr = _mm_cvtsi128_si32(x);
  124. #else
  125. for (size_t i=0;i<4;i++)
  126. ptr[i] = (unsigned char)v[i];
  127. #endif
  128. }
  129. static __forceinline void store(unsigned short* ptr, const vint4& v) {
  130. #if defined(__aarch64__)
  131. uint32x4_t x = uint32x4_t(v.v);
  132. uint16x4_t y = vqmovn_u32(x);
  133. vst1_u16(ptr, y);
  134. #else
  135. for (size_t i=0;i<4;i++)
  136. ptr[i] = (unsigned short)v[i];
  137. #endif
  138. }
  139. static __forceinline vint4 load_nt(void* ptr) {
  140. #if defined(__aarch64__) || defined(__SSE4_1__)
  141. return _mm_stream_load_si128((__m128i*)ptr);
  142. #else
  143. return _mm_load_si128((__m128i*)ptr);
  144. #endif
  145. }
  146. static __forceinline void store_nt(void* ptr, const vint4& v) {
  147. #if !defined(__aarch64__) && defined(__SSE4_1__)
  148. _mm_stream_ps((float*)ptr, _mm_castsi128_ps(v));
  149. #else
  150. _mm_store_si128((__m128i*)ptr,v);
  151. #endif
  152. }
  153. template<int scale = 4>
  154. static __forceinline vint4 gather(const int* ptr, const vint4& index) {
  155. #if defined(__AVX2__) && !defined(__aarch64__)
  156. return _mm_i32gather_epi32(ptr, index, scale);
  157. #else
  158. return vint4(
  159. *(int*)(((char*)ptr)+scale*index[0]),
  160. *(int*)(((char*)ptr)+scale*index[1]),
  161. *(int*)(((char*)ptr)+scale*index[2]),
  162. *(int*)(((char*)ptr)+scale*index[3]));
  163. #endif
  164. }
  165. template<int scale = 4>
  166. static __forceinline vint4 gather(const vboolf4& mask, const int* ptr, const vint4& index) {
  167. vint4 r = zero;
  168. #if defined(__AVX512VL__)
  169. return _mm_mmask_i32gather_epi32(r, mask, index, ptr, scale);
  170. #elif defined(__AVX2__) && !defined(__aarch64__)
  171. return _mm_mask_i32gather_epi32(r, ptr, index, mask, scale);
  172. #else
  173. if (likely(mask[0])) r[0] = *(int*)(((char*)ptr)+scale*index[0]);
  174. if (likely(mask[1])) r[1] = *(int*)(((char*)ptr)+scale*index[1]);
  175. if (likely(mask[2])) r[2] = *(int*)(((char*)ptr)+scale*index[2]);
  176. if (likely(mask[3])) r[3] = *(int*)(((char*)ptr)+scale*index[3]);
  177. return r;
  178. #endif
  179. }
  180. template<int scale = 4>
  181. static __forceinline void scatter(void* ptr, const vint4& index, const vint4& v)
  182. {
  183. #if defined(__AVX512VL__)
  184. _mm_i32scatter_epi32((int*)ptr, index, v, scale);
  185. #else
  186. *(int*)(((char*)ptr)+scale*index[0]) = v[0];
  187. *(int*)(((char*)ptr)+scale*index[1]) = v[1];
  188. *(int*)(((char*)ptr)+scale*index[2]) = v[2];
  189. *(int*)(((char*)ptr)+scale*index[3]) = v[3];
  190. #endif
  191. }
  192. template<int scale = 4>
  193. static __forceinline void scatter(const vboolf4& mask, void* ptr, const vint4& index, const vint4& v)
  194. {
  195. #if defined(__AVX512VL__)
  196. _mm_mask_i32scatter_epi32((int*)ptr, mask, index, v, scale);
  197. #else
  198. if (likely(mask[0])) *(int*)(((char*)ptr)+scale*index[0]) = v[0];
  199. if (likely(mask[1])) *(int*)(((char*)ptr)+scale*index[1]) = v[1];
  200. if (likely(mask[2])) *(int*)(((char*)ptr)+scale*index[2]) = v[2];
  201. if (likely(mask[3])) *(int*)(((char*)ptr)+scale*index[3]) = v[3];
  202. #endif
  203. }
  204. #if defined(__x86_64__) || defined(__aarch64__)
  205. static __forceinline vint4 broadcast64(long long a) { return _mm_set1_epi64x(a); }
  206. #endif
  207. ////////////////////////////////////////////////////////////////////////////////
  208. /// Array Access
  209. ////////////////////////////////////////////////////////////////////////////////
  210. __forceinline const int& operator [](size_t index) const { assert(index < 4); return i[index]; }
  211. __forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }
  212. friend __forceinline vint4 select(const vboolf4& m, const vint4& t, const vint4& f) {
  213. #if defined(__AVX512VL__)
  214. return _mm_mask_blend_epi32(m, (__m128i)f, (__m128i)t);
  215. #elif defined(__aarch64__)
  216. return _mm_castps_si128(_mm_blendv_ps((__m128)f.v,(__m128) t.v, (__m128)m.v));
  217. #elif defined(__SSE4_1__)
  218. return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m));
  219. #else
  220. return _mm_or_si128(_mm_and_si128(m, t), _mm_andnot_si128(m, f));
  221. #endif
  222. }
  223. };
  224. ////////////////////////////////////////////////////////////////////////////////
  225. /// Unary Operators
  226. ////////////////////////////////////////////////////////////////////////////////
  227. #if defined(__AVX512VL__)
  228. __forceinline vboolf4 asBool(const vint4& a) { return _mm_movepi32_mask(a); }
  229. #else
  230. __forceinline vboolf4 asBool(const vint4& a) { return _mm_castsi128_ps(a); }
  231. #endif
  232. __forceinline vint4 operator +(const vint4& a) { return a; }
  233. __forceinline vint4 operator -(const vint4& a) { return _mm_sub_epi32(_mm_setzero_si128(), a); }
  234. #if defined(__aarch64__)
  235. __forceinline vint4 abs(const vint4& a) { return vabsq_s32(a.v); }
  236. #elif defined(__SSSE3__)
  237. __forceinline vint4 abs(const vint4& a) { return _mm_abs_epi32(a); }
  238. #endif
  239. ////////////////////////////////////////////////////////////////////////////////
  240. /// Binary Operators
  241. ////////////////////////////////////////////////////////////////////////////////
  242. __forceinline vint4 operator +(const vint4& a, const vint4& b) { return _mm_add_epi32(a, b); }
  243. __forceinline vint4 operator +(const vint4& a, int b) { return a + vint4(b); }
  244. __forceinline vint4 operator +(int a, const vint4& b) { return vint4(a) + b; }
  245. __forceinline vint4 operator -(const vint4& a, const vint4& b) { return _mm_sub_epi32(a, b); }
  246. __forceinline vint4 operator -(const vint4& a, int b) { return a - vint4(b); }
  247. __forceinline vint4 operator -(int a, const vint4& b) { return vint4(a) - b; }
  248. #if (defined(__aarch64__)) || defined(__SSE4_1__)
  249. __forceinline vint4 operator *(const vint4& a, const vint4& b) { return _mm_mullo_epi32(a, b); }
  250. #else
  251. __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]); }
  252. #endif
  253. __forceinline vint4 operator *(const vint4& a, int b) { return a * vint4(b); }
  254. __forceinline vint4 operator *(int a, const vint4& b) { return vint4(a) * b; }
  255. __forceinline vint4 operator &(const vint4& a, const vint4& b) { return _mm_and_si128(a, b); }
  256. __forceinline vint4 operator &(const vint4& a, int b) { return a & vint4(b); }
  257. __forceinline vint4 operator &(int a, const vint4& b) { return vint4(a) & b; }
  258. __forceinline vint4 operator |(const vint4& a, const vint4& b) { return _mm_or_si128(a, b); }
  259. __forceinline vint4 operator |(const vint4& a, int b) { return a | vint4(b); }
  260. __forceinline vint4 operator |(int a, const vint4& b) { return vint4(a) | b; }
  261. __forceinline vint4 operator ^(const vint4& a, const vint4& b) { return _mm_xor_si128(a, b); }
  262. __forceinline vint4 operator ^(const vint4& a, int b) { return a ^ vint4(b); }
  263. __forceinline vint4 operator ^(int a, const vint4& b) { return vint4(a) ^ b; }
  264. __forceinline vint4 operator <<(const vint4& a, const int n) { return _mm_slli_epi32(a, n); }
  265. __forceinline vint4 operator >>(const vint4& a, const int n) { return _mm_srai_epi32(a, n); }
  266. __forceinline vint4 sll (const vint4& a, int b) { return _mm_slli_epi32(a, b); }
  267. __forceinline vint4 sra (const vint4& a, int b) { return _mm_srai_epi32(a, b); }
  268. __forceinline vint4 srl (const vint4& a, int b) { return _mm_srli_epi32(a, b); }
  269. ////////////////////////////////////////////////////////////////////////////////
  270. /// Assignment Operators
  271. ////////////////////////////////////////////////////////////////////////////////
  272. __forceinline vint4& operator +=(vint4& a, const vint4& b) { return a = a + b; }
  273. __forceinline vint4& operator +=(vint4& a, int b) { return a = a + b; }
  274. __forceinline vint4& operator -=(vint4& a, const vint4& b) { return a = a - b; }
  275. __forceinline vint4& operator -=(vint4& a, int b) { return a = a - b; }
  276. #if (defined(__aarch64__)) || defined(__SSE4_1__)
  277. __forceinline vint4& operator *=(vint4& a, const vint4& b) { return a = a * b; }
  278. __forceinline vint4& operator *=(vint4& a, int b) { return a = a * b; }
  279. #endif
  280. __forceinline vint4& operator &=(vint4& a, const vint4& b) { return a = a & b; }
  281. __forceinline vint4& operator &=(vint4& a, int b) { return a = a & b; }
  282. __forceinline vint4& operator |=(vint4& a, const vint4& b) { return a = a | b; }
  283. __forceinline vint4& operator |=(vint4& a, int b) { return a = a | b; }
  284. __forceinline vint4& operator <<=(vint4& a, int b) { return a = a << b; }
  285. __forceinline vint4& operator >>=(vint4& a, int b) { return a = a >> b; }
  286. ////////////////////////////////////////////////////////////////////////////////
  287. /// Comparison Operators + Select
  288. ////////////////////////////////////////////////////////////////////////////////
  289. #if defined(__AVX512VL__)
  290. __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  291. __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  292. __forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  293. __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  294. __forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  295. __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
  296. #else
  297. __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }
  298. __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return !(a == b); }
  299. __forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmplt_epi32(a, b)); }
  300. __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return !(a < b); }
  301. __forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpgt_epi32(a, b)); }
  302. __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return !(a > b); }
  303. #endif
  304. __forceinline vboolf4 operator ==(const vint4& a, int b) { return a == vint4(b); }
  305. __forceinline vboolf4 operator ==(int a, const vint4& b) { return vint4(a) == b; }
  306. __forceinline vboolf4 operator !=(const vint4& a, int b) { return a != vint4(b); }
  307. __forceinline vboolf4 operator !=(int a, const vint4& b) { return vint4(a) != b; }
  308. __forceinline vboolf4 operator < (const vint4& a, int b) { return a < vint4(b); }
  309. __forceinline vboolf4 operator < (int a, const vint4& b) { return vint4(a) < b; }
  310. __forceinline vboolf4 operator >=(const vint4& a, int b) { return a >= vint4(b); }
  311. __forceinline vboolf4 operator >=(int a, const vint4& b) { return vint4(a) >= b; }
  312. __forceinline vboolf4 operator > (const vint4& a, int b) { return a > vint4(b); }
  313. __forceinline vboolf4 operator > (int a, const vint4& b) { return vint4(a) > b; }
  314. __forceinline vboolf4 operator <=(const vint4& a, int b) { return a <= vint4(b); }
  315. __forceinline vboolf4 operator <=(int a, const vint4& b) { return vint4(a) <= b; }
  316. __forceinline vboolf4 eq(const vint4& a, const vint4& b) { return a == b; }
  317. __forceinline vboolf4 ne(const vint4& a, const vint4& b) { return a != b; }
  318. __forceinline vboolf4 lt(const vint4& a, const vint4& b) { return a < b; }
  319. __forceinline vboolf4 ge(const vint4& a, const vint4& b) { return a >= b; }
  320. __forceinline vboolf4 gt(const vint4& a, const vint4& b) { return a > b; }
  321. __forceinline vboolf4 le(const vint4& a, const vint4& b) { return a <= b; }
  322. #if defined(__AVX512VL__)
  323. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  324. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  325. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  326. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  327. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  328. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
  329. #else
  330. __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a == b); }
  331. __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a != b); }
  332. __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a < b); }
  333. __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a >= b); }
  334. __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a > b); }
  335. __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a <= b); }
  336. #endif
  337. template<int mask>
  338. __forceinline vint4 select(const vint4& t, const vint4& f) {
  339. #if defined(__SSE4_1__)
  340. return _mm_castps_si128(_mm_blend_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), mask));
  341. #else
  342. return select(vboolf4(mask), t, f);
  343. #endif
  344. }
  345. #if defined(__aarch64__) || defined(__SSE4_1__)
  346. __forceinline vint4 min(const vint4& a, const vint4& b) { return _mm_min_epi32(a, b); }
  347. __forceinline vint4 max(const vint4& a, const vint4& b) { return _mm_max_epi32(a, b); }
  348. __forceinline vint4 umin(const vint4& a, const vint4& b) { return _mm_min_epu32(a, b); }
  349. __forceinline vint4 umax(const vint4& a, const vint4& b) { return _mm_max_epu32(a, b); }
  350. #else
  351. __forceinline vint4 min(const vint4& a, const vint4& b) { return select(a < b,a,b); }
  352. __forceinline vint4 max(const vint4& a, const vint4& b) { return select(a < b,b,a); }
  353. #endif
  354. __forceinline vint4 min(const vint4& a, int b) { return min(a,vint4(b)); }
  355. __forceinline vint4 min(int a, const vint4& b) { return min(vint4(a),b); }
  356. __forceinline vint4 max(const vint4& a, int b) { return max(a,vint4(b)); }
  357. __forceinline vint4 max(int a, const vint4& b) { return max(vint4(a),b); }
  358. ////////////////////////////////////////////////////////////////////////////////
  359. // Movement/Shifting/Shuffling Functions
  360. ////////////////////////////////////////////////////////////////////////////////
  361. __forceinline vint4 unpacklo(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpacklo_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }
  362. __forceinline vint4 unpackhi(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpackhi_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }
  363. #if defined(__aarch64__)
  364. template<int i0, int i1, int i2, int i3>
  365. __forceinline vint4 shuffle(const vint4& v) {
  366. return vreinterpretq_s32_u8(vqtbl1q_u8( (uint8x16_t)v.v, _MN_SHUFFLE(i0, i1, i2, i3)));
  367. }
  368. template<int i0, int i1, int i2, int i3>
  369. __forceinline vint4 shuffle(const vint4& a, const vint4& b) {
  370. return vreinterpretq_s32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));
  371. }
  372. #else
  373. template<int i0, int i1, int i2, int i3>
  374. __forceinline vint4 shuffle(const vint4& v) {
  375. return _mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0));
  376. }
  377. template<int i0, int i1, int i2, int i3>
  378. __forceinline vint4 shuffle(const vint4& a, const vint4& b) {
  379. return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  380. }
  381. #endif
  382. #if defined(__SSE3__)
  383. template<> __forceinline vint4 shuffle<0, 0, 2, 2>(const vint4& v) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(v))); }
  384. template<> __forceinline vint4 shuffle<1, 1, 3, 3>(const vint4& v) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(v))); }
  385. template<> __forceinline vint4 shuffle<0, 1, 0, 1>(const vint4& v) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(v))); }
  386. #endif
  387. template<int i>
  388. __forceinline vint4 shuffle(const vint4& v) {
  389. return shuffle<i,i,i,i>(v);
  390. }
  391. #if defined(__SSE4_1__) && !defined(__aarch64__)
  392. template<int src> __forceinline int extract(const vint4& b) { return _mm_extract_epi32(b, src); }
  393. template<int dst> __forceinline vint4 insert(const vint4& a, const int b) { return _mm_insert_epi32(a, b, dst); }
  394. #else
  395. template<int src> __forceinline int extract(const vint4& b) { return b[src&3]; }
  396. template<int dst> __forceinline vint4 insert(const vint4& a, int b) { vint4 c = a; c[dst&3] = b; return c; }
  397. #endif
  398. template<> __forceinline int extract<0>(const vint4& b) { return _mm_cvtsi128_si32(b); }
  399. __forceinline int toScalar(const vint4& v) { return _mm_cvtsi128_si32(v); }
  400. #if defined(__aarch64__)
  401. __forceinline size_t toSizeT(const vint4& v) {
  402. uint64x2_t x = uint64x2_t(v.v);
  403. return x[0];
  404. }
  405. #else
  406. __forceinline size_t toSizeT(const vint4& v) {
  407. #if defined(__WIN32__) && !defined(__X86_64__) // win32 workaround
  408. return toScalar(v);
  409. #elif defined(__ARM_NEON)
  410. // FIXME(LTE): Do we need a swap(i.e. use lane 1)?
  411. return vgetq_lane_u64(*(reinterpret_cast<const uint64x2_t *>(&v)), 0);
  412. #else
  413. return _mm_cvtsi128_si64(v);
  414. #endif
  415. }
  416. #endif
  417. #if defined(__AVX512VL__)
  418. __forceinline vint4 permute(const vint4 &a, const vint4 &index) {
  419. return _mm_castps_si128(_mm_permutevar_ps(_mm_castsi128_ps(a),index));
  420. }
  421. template<int i>
  422. __forceinline vint4 align_shift_right(const vint4& a, const vint4& b) {
  423. return _mm_alignr_epi32(a, b, i);
  424. }
  425. #endif
  426. ////////////////////////////////////////////////////////////////////////////////
  427. /// Reductions
  428. ////////////////////////////////////////////////////////////////////////////////
  429. #if defined(__aarch64__) || defined(__SSE4_1__)
  430. #if defined(__aarch64__)
  431. __forceinline vint4 vreduce_min(const vint4& v) { int h = vminvq_s32(v); return vdupq_n_s32(h); }
  432. __forceinline vint4 vreduce_max(const vint4& v) { int h = vmaxvq_s32(v); return vdupq_n_s32(h); }
  433. __forceinline vint4 vreduce_add(const vint4& v) { int h = vaddvq_s32(v); return vdupq_n_s32(h); }
  434. __forceinline int reduce_min(const vint4& v) { return vminvq_s32(v); }
  435. __forceinline int reduce_max(const vint4& v) { return vmaxvq_s32(v); }
  436. __forceinline int reduce_add(const vint4& v) { return vaddvq_s32(v); }
  437. #else
  438. __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); }
  439. __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); }
  440. __forceinline vint4 vreduce_add(const vint4& v) { vint4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }
  441. __forceinline int reduce_min(const vint4& v) { return toScalar(vreduce_min(v)); }
  442. __forceinline int reduce_max(const vint4& v) { return toScalar(vreduce_max(v)); }
  443. __forceinline int reduce_add(const vint4& v) { return toScalar(vreduce_add(v)); }
  444. #endif
  445. __forceinline size_t select_min(const vint4& v) { return bsf(movemask(v == vreduce_min(v))); }
  446. __forceinline size_t select_max(const vint4& v) { return bsf(movemask(v == vreduce_max(v))); }
  447. __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)))); }
  448. __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)))); }
  449. #else
  450. __forceinline int reduce_min(const vint4& v) { return min(v[0],v[1],v[2],v[3]); }
  451. __forceinline int reduce_max(const vint4& v) { return max(v[0],v[1],v[2],v[3]); }
  452. __forceinline int reduce_add(const vint4& v) { return v[0]+v[1]+v[2]+v[3]; }
  453. #endif
  454. ////////////////////////////////////////////////////////////////////////////////
  455. /// Sorting networks
  456. ////////////////////////////////////////////////////////////////////////////////
  457. #if (defined(__aarch64__)) || defined(__SSE4_1__)
  458. __forceinline vint4 usort_ascending(const vint4& v)
  459. {
  460. const vint4 a0 = v;
  461. const vint4 b0 = shuffle<1,0,3,2>(a0);
  462. const vint4 c0 = umin(a0,b0);
  463. const vint4 d0 = umax(a0,b0);
  464. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  465. const vint4 b1 = shuffle<2,3,0,1>(a1);
  466. const vint4 c1 = umin(a1,b1);
  467. const vint4 d1 = umax(a1,b1);
  468. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  469. const vint4 b2 = shuffle<0,2,1,3>(a2);
  470. const vint4 c2 = umin(a2,b2);
  471. const vint4 d2 = umax(a2,b2);
  472. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  473. return a3;
  474. }
  475. __forceinline vint4 usort_descending(const vint4& v)
  476. {
  477. const vint4 a0 = v;
  478. const vint4 b0 = shuffle<1,0,3,2>(a0);
  479. const vint4 c0 = umax(a0,b0);
  480. const vint4 d0 = umin(a0,b0);
  481. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  482. const vint4 b1 = shuffle<2,3,0,1>(a1);
  483. const vint4 c1 = umax(a1,b1);
  484. const vint4 d1 = umin(a1,b1);
  485. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  486. const vint4 b2 = shuffle<0,2,1,3>(a2);
  487. const vint4 c2 = umax(a2,b2);
  488. const vint4 d2 = umin(a2,b2);
  489. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  490. return a3;
  491. }
  492. #else
  493. __forceinline vint4 usort_ascending(const vint4& v)
  494. {
  495. const vint4 a0 = v-vint4(0x80000000);
  496. const vint4 b0 = shuffle<1,0,3,2>(a0);
  497. const vint4 c0 = min(a0,b0);
  498. const vint4 d0 = max(a0,b0);
  499. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  500. const vint4 b1 = shuffle<2,3,0,1>(a1);
  501. const vint4 c1 = min(a1,b1);
  502. const vint4 d1 = max(a1,b1);
  503. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  504. const vint4 b2 = shuffle<0,2,1,3>(a2);
  505. const vint4 c2 = min(a2,b2);
  506. const vint4 d2 = max(a2,b2);
  507. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  508. return a3+vint4(0x80000000);
  509. }
  510. __forceinline vint4 usort_descending(const vint4& v)
  511. {
  512. const vint4 a0 = v-vint4(0x80000000);
  513. const vint4 b0 = shuffle<1,0,3,2>(a0);
  514. const vint4 c0 = max(a0,b0);
  515. const vint4 d0 = min(a0,b0);
  516. const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  517. const vint4 b1 = shuffle<2,3,0,1>(a1);
  518. const vint4 c1 = max(a1,b1);
  519. const vint4 d1 = min(a1,b1);
  520. const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  521. const vint4 b2 = shuffle<0,2,1,3>(a2);
  522. const vint4 c2 = max(a2,b2);
  523. const vint4 d2 = min(a2,b2);
  524. const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  525. return a3+vint4(0x80000000);
  526. }
  527. #endif
  528. ////////////////////////////////////////////////////////////////////////////////
  529. /// Output Operators
  530. ////////////////////////////////////////////////////////////////////////////////
  531. __forceinline embree_ostream operator <<(embree_ostream cout, const vint4& a) {
  532. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  533. }
  534. }
  535. #undef vboolf
  536. #undef vboold
  537. #undef vint
  538. #undef vuint
  539. #undef vllong
  540. #undef vfloat
  541. #undef vdouble