vfloat8_avx.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #define vboolf vboolf_impl
  5. #define vboold vboold_impl
  6. #define vint vint_impl
  7. #define vuint vuint_impl
  8. #define vllong vllong_impl
  9. #define vfloat vfloat_impl
  10. #define vdouble vdouble_impl
  11. namespace embree
  12. {
  13. /* 8-wide AVX float type */
  14. template<>
  15. struct vfloat<8>
  16. {
  17. ALIGNED_STRUCT_(32);
  18. typedef vboolf8 Bool;
  19. typedef vint8 Int;
  20. typedef vfloat8 Float;
  21. enum { size = 8 }; // number of SIMD elements
  22. union { __m256 v; float f[8]; int i[8]; }; // data
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// Constructors, Assignment & Cast Operators
  25. ////////////////////////////////////////////////////////////////////////////////
  26. __forceinline vfloat() {}
  27. __forceinline vfloat(const vfloat8& other) { v = other.v; }
  28. __forceinline vfloat8& operator =(const vfloat8& other) { v = other.v; return *this; }
  29. __forceinline vfloat(__m256 a) : v(a) {}
  30. __forceinline operator const __m256&() const { return v; }
  31. __forceinline operator __m256&() { return v; }
  32. __forceinline explicit vfloat(const vfloat4& a) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),a,1)) {}
  33. __forceinline vfloat(const vfloat4& a, const vfloat4& b) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),b,1)) {}
  34. __forceinline explicit vfloat(const char* a) : v(_mm256_loadu_ps((const float*)a)) {}
  35. __forceinline vfloat(float a) : v(_mm256_set1_ps(a)) {}
  36. __forceinline vfloat(float a, float b) : v(_mm256_set_ps(b, a, b, a, b, a, b, a)) {}
  37. __forceinline vfloat(float a, float b, float c, float d) : v(_mm256_set_ps(d, c, b, a, d, c, b, a)) {}
  38. __forceinline vfloat(float a, float b, float c, float d, float e, float f, float g, float h) : v(_mm256_set_ps(h, g, f, e, d, c, b, a)) {}
  39. __forceinline explicit vfloat(__m256i a) : v(_mm256_cvtepi32_ps(a)) {}
  40. ////////////////////////////////////////////////////////////////////////////////
  41. /// Constants
  42. ////////////////////////////////////////////////////////////////////////////////
  43. __forceinline vfloat(ZeroTy) : v(_mm256_setzero_ps()) {}
  44. __forceinline vfloat(OneTy) : v(_mm256_set1_ps(1.0f)) {}
  45. __forceinline vfloat(PosInfTy) : v(_mm256_set1_ps(pos_inf)) {}
  46. __forceinline vfloat(NegInfTy) : v(_mm256_set1_ps(neg_inf)) {}
  47. __forceinline vfloat(StepTy) : v(_mm256_set_ps(7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f, 0.0f)) {}
  48. __forceinline vfloat(NaNTy) : v(_mm256_set1_ps(nan)) {}
  49. __forceinline vfloat(UndefinedTy) : v(_mm256_undefined_ps()) {}
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// Loads and Stores
  52. ////////////////////////////////////////////////////////////////////////////////
  53. static __forceinline vfloat8 broadcast(const void* a) {
  54. return _mm256_broadcast_ss((float*)a);
  55. }
  56. static __forceinline vfloat8 load(const char* ptr) {
  57. #if defined(__AVX2__)
  58. return _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  59. #else
  60. return vfloat8(vfloat4::load(ptr),vfloat4::load(ptr+4));
  61. #endif
  62. }
  63. static __forceinline vfloat8 load(const unsigned char* ptr) {
  64. #if defined(__AVX2__)
  65. return _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  66. #else
  67. return vfloat8(vfloat4::load(ptr),vfloat4::load(ptr+4));
  68. #endif
  69. }
  70. static __forceinline vfloat8 load(const short* ptr) {
  71. #if defined(__AVX2__)
  72. return _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)ptr)));
  73. #else
  74. return vfloat8(vfloat4::load(ptr),vfloat4::load(ptr+4));
  75. #endif
  76. }
  77. static __forceinline vfloat8 load (const void* ptr) { return _mm256_load_ps((float*)ptr); }
  78. static __forceinline vfloat8 loadu(const void* ptr) { return _mm256_loadu_ps((float*)ptr); }
  79. static __forceinline void store (void* ptr, const vfloat8& v) { return _mm256_store_ps((float*)ptr,v); }
  80. static __forceinline void storeu(void* ptr, const vfloat8& v) { return _mm256_storeu_ps((float*)ptr,v); }
  81. #if defined(__AVX512VL__)
  82. static __forceinline vfloat8 load (const vboolf8& mask, const void* ptr) { return _mm256_mask_load_ps (_mm256_setzero_ps(),mask,(float*)ptr); }
  83. static __forceinline vfloat8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_mask_loadu_ps(_mm256_setzero_ps(),mask,(float*)ptr); }
  84. static __forceinline void store (const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_mask_store_ps ((float*)ptr,mask,v); }
  85. static __forceinline void storeu(const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_mask_storeu_ps((float*)ptr,mask,v); }
  86. #else
  87. static __forceinline vfloat8 load (const vboolf8& mask, const void* ptr) { return _mm256_maskload_ps((float*)ptr,_mm256_castps_si256(mask.v)); }
  88. static __forceinline vfloat8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_maskload_ps((float*)ptr,_mm256_castps_si256(mask.v)); }
  89. static __forceinline void store (const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),v); }
  90. static __forceinline void storeu(const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),v); }
  91. #endif
  92. #if defined(__AVX2__)
  93. static __forceinline vfloat8 load_nt(void* ptr) {
  94. return _mm256_castsi256_ps(_mm256_stream_load_si256((__m256i*)ptr));
  95. }
  96. #endif
  97. static __forceinline void store_nt(void* ptr, const vfloat8& v) {
  98. _mm256_stream_ps((float*)ptr,v);
  99. }
  100. template<int scale = 4>
  101. static __forceinline vfloat8 gather(const float* ptr, const vint8& index) {
  102. #if defined(__AVX2__) && !defined(__aarch64__)
  103. return _mm256_i32gather_ps(ptr, index ,scale);
  104. #else
  105. return vfloat8(
  106. *(float*)(((char*)ptr)+scale*index[0]),
  107. *(float*)(((char*)ptr)+scale*index[1]),
  108. *(float*)(((char*)ptr)+scale*index[2]),
  109. *(float*)(((char*)ptr)+scale*index[3]),
  110. *(float*)(((char*)ptr)+scale*index[4]),
  111. *(float*)(((char*)ptr)+scale*index[5]),
  112. *(float*)(((char*)ptr)+scale*index[6]),
  113. *(float*)(((char*)ptr)+scale*index[7]));
  114. #endif
  115. }
  116. template<int scale = 4>
  117. static __forceinline vfloat8 gather(const vboolf8& mask, const float* ptr, const vint8& index) {
  118. vfloat8 r = zero;
  119. #if defined(__AVX512VL__)
  120. return _mm256_mmask_i32gather_ps(r, mask, index, ptr, scale);
  121. #elif defined(__AVX2__) && !defined(__aarch64__)
  122. return _mm256_mask_i32gather_ps(r, ptr, index, mask, scale);
  123. #else
  124. if (likely(mask[0])) r[0] = *(float*)(((char*)ptr)+scale*index[0]);
  125. if (likely(mask[1])) r[1] = *(float*)(((char*)ptr)+scale*index[1]);
  126. if (likely(mask[2])) r[2] = *(float*)(((char*)ptr)+scale*index[2]);
  127. if (likely(mask[3])) r[3] = *(float*)(((char*)ptr)+scale*index[3]);
  128. if (likely(mask[4])) r[4] = *(float*)(((char*)ptr)+scale*index[4]);
  129. if (likely(mask[5])) r[5] = *(float*)(((char*)ptr)+scale*index[5]);
  130. if (likely(mask[6])) r[6] = *(float*)(((char*)ptr)+scale*index[6]);
  131. if (likely(mask[7])) r[7] = *(float*)(((char*)ptr)+scale*index[7]);
  132. return r;
  133. #endif
  134. }
  135. template<int scale = 4>
  136. static __forceinline void scatter(void* ptr, const vint8& ofs, const vfloat8& v)
  137. {
  138. #if defined(__AVX512VL__)
  139. _mm256_i32scatter_ps((float*)ptr, ofs, v, scale);
  140. #else
  141. *(float*)(((char*)ptr)+scale*ofs[0]) = v[0];
  142. *(float*)(((char*)ptr)+scale*ofs[1]) = v[1];
  143. *(float*)(((char*)ptr)+scale*ofs[2]) = v[2];
  144. *(float*)(((char*)ptr)+scale*ofs[3]) = v[3];
  145. *(float*)(((char*)ptr)+scale*ofs[4]) = v[4];
  146. *(float*)(((char*)ptr)+scale*ofs[5]) = v[5];
  147. *(float*)(((char*)ptr)+scale*ofs[6]) = v[6];
  148. *(float*)(((char*)ptr)+scale*ofs[7]) = v[7];
  149. #endif
  150. }
  151. template<int scale = 4>
  152. static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vfloat8& v)
  153. {
  154. #if defined(__AVX512VL__)
  155. _mm256_mask_i32scatter_ps((float*)ptr, mask, ofs, v, scale);
  156. #else
  157. if (likely(mask[0])) *(float*)(((char*)ptr)+scale*ofs[0]) = v[0];
  158. if (likely(mask[1])) *(float*)(((char*)ptr)+scale*ofs[1]) = v[1];
  159. if (likely(mask[2])) *(float*)(((char*)ptr)+scale*ofs[2]) = v[2];
  160. if (likely(mask[3])) *(float*)(((char*)ptr)+scale*ofs[3]) = v[3];
  161. if (likely(mask[4])) *(float*)(((char*)ptr)+scale*ofs[4]) = v[4];
  162. if (likely(mask[5])) *(float*)(((char*)ptr)+scale*ofs[5]) = v[5];
  163. if (likely(mask[6])) *(float*)(((char*)ptr)+scale*ofs[6]) = v[6];
  164. if (likely(mask[7])) *(float*)(((char*)ptr)+scale*ofs[7]) = v[7];
  165. #endif
  166. }
  167. ////////////////////////////////////////////////////////////////////////////////
  168. /// Array Access
  169. ////////////////////////////////////////////////////////////////////////////////
  170. __forceinline const float& operator [](size_t index) const { assert(index < 8); return f[index]; }
  171. __forceinline float& operator [](size_t index) { assert(index < 8); return f[index]; }
  172. };
  173. ////////////////////////////////////////////////////////////////////////////////
  174. /// Unary Operators
  175. ////////////////////////////////////////////////////////////////////////////////
  176. __forceinline vfloat8 asFloat(const vint8& a) { return _mm256_castsi256_ps(a); }
  177. __forceinline vint8 asInt (const vfloat8& a) { return _mm256_castps_si256(a); }
  178. __forceinline vint8 toInt (const vfloat8& a) { return vint8(a); }
  179. __forceinline vfloat8 toFloat(const vint8& a) { return vfloat8(a); }
  180. __forceinline vfloat8 operator +(const vfloat8& a) { return a; }
  181. #if !defined(__aarch64__)
  182. __forceinline vfloat8 operator -(const vfloat8& a) {
  183. const __m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x80000000));
  184. return _mm256_xor_ps(a, mask);
  185. }
  186. #else
  187. __forceinline vfloat8 operator -(const vfloat8& a) {
  188. __m256 res;
  189. res.lo = vnegq_f32(a.v.lo);
  190. res.hi = vnegq_f32(a.v.hi);
  191. return res;
  192. }
  193. #endif
  194. #if !defined(__aarch64__)
  195. __forceinline vfloat8 abs(const vfloat8& a) {
  196. const __m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
  197. return _mm256_and_ps(a, mask);
  198. }
  199. #else
  200. __forceinline vfloat8 abs(const vfloat8& a) {
  201. __m256 res;
  202. res.lo = vabsq_f32(a.v.lo);
  203. res.hi = vabsq_f32(a.v.hi);
  204. return res;
  205. }
  206. #endif
  207. #if !defined(__aarch64__)
  208. __forceinline vfloat8 sign (const vfloat8& a) { return _mm256_blendv_ps(vfloat8(one), -vfloat8(one), _mm256_cmp_ps(a, vfloat8(zero), _CMP_NGE_UQ)); }
  209. #else
  210. __forceinline vfloat8 sign (const vfloat8& a) { return _mm256_blendv_ps(vfloat8(one), -vfloat8(one), _mm256_cmplt_ps(a, vfloat8(zero))); }
  211. #endif
  212. __forceinline vfloat8 signmsk(const vfloat8& a) { return _mm256_and_ps(a,_mm256_castsi256_ps(_mm256_set1_epi32(0x80000000))); }
  213. static __forceinline vfloat8 rcp(const vfloat8& a)
  214. {
  215. #if defined(__aarch64__)
  216. vfloat8 ret;
  217. const float32x4_t one = vdupq_n_f32(1.0f);
  218. ret.v.lo = vdivq_f32(one, a.v.lo);
  219. ret.v.hi = vdivq_f32(one, a.v.hi);
  220. return ret;
  221. #endif
  222. #if defined(__AVX512VL__)
  223. const vfloat8 r = _mm256_rcp14_ps(a);
  224. #else
  225. const vfloat8 r = _mm256_rcp_ps(a);
  226. #endif
  227. #if defined(__AVX2__)
  228. // First, compute 1 - a * r (which will be very close to 0)
  229. const vfloat8 h_n = _mm256_fnmadd_ps(a, r, vfloat8(1.0f));
  230. // Then compute r + r * h_n
  231. return _mm256_fmadd_ps(r, h_n, r);
  232. #else
  233. return _mm256_add_ps(r,_mm256_mul_ps(r, _mm256_sub_ps(vfloat8(1.0f), _mm256_mul_ps(a, r)))); // computes r + r * (1 - a * r)
  234. #endif
  235. }
  236. __forceinline vfloat8 sqr (const vfloat8& a) { return _mm256_mul_ps(a,a); }
  237. __forceinline vfloat8 sqrt(const vfloat8& a) { return _mm256_sqrt_ps(a); }
  238. static __forceinline vfloat8 rsqrt(const vfloat8& a)
  239. {
  240. #if defined(__AVX512VL__)
  241. const vfloat8 r = _mm256_rsqrt14_ps(a);
  242. #else
  243. const vfloat8 r = _mm256_rsqrt_ps(a);
  244. #endif
  245. #if defined(__AVX2__)
  246. return _mm256_fmadd_ps(_mm256_set1_ps(1.5f), r,
  247. _mm256_mul_ps(_mm256_mul_ps(_mm256_mul_ps(a, _mm256_set1_ps(-0.5f)), r), _mm256_mul_ps(r, r)));
  248. #else
  249. return _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(1.5f), r),
  250. _mm256_mul_ps(_mm256_mul_ps(_mm256_mul_ps(a, _mm256_set1_ps(-0.5f)), r), _mm256_mul_ps(r, r)));
  251. #endif
  252. }
  253. ////////////////////////////////////////////////////////////////////////////////
  254. /// Binary Operators
  255. ////////////////////////////////////////////////////////////////////////////////
  256. __forceinline vfloat8 operator +(const vfloat8& a, const vfloat8& b) { return _mm256_add_ps(a, b); }
  257. __forceinline vfloat8 operator +(const vfloat8& a, float b) { return a + vfloat8(b); }
  258. __forceinline vfloat8 operator +(float a, const vfloat8& b) { return vfloat8(a) + b; }
  259. __forceinline vfloat8 operator -(const vfloat8& a, const vfloat8& b) { return _mm256_sub_ps(a, b); }
  260. __forceinline vfloat8 operator -(const vfloat8& a, float b) { return a - vfloat8(b); }
  261. __forceinline vfloat8 operator -(float a, const vfloat8& b) { return vfloat8(a) - b; }
  262. __forceinline vfloat8 operator *(const vfloat8& a, const vfloat8& b) { return _mm256_mul_ps(a, b); }
  263. __forceinline vfloat8 operator *(const vfloat8& a, float b) { return a * vfloat8(b); }
  264. __forceinline vfloat8 operator *(float a, const vfloat8& b) { return vfloat8(a) * b; }
  265. __forceinline vfloat8 operator /(const vfloat8& a, const vfloat8& b) { return _mm256_div_ps(a, b); }
  266. __forceinline vfloat8 operator /(const vfloat8& a, float b) { return a / vfloat8(b); }
  267. __forceinline vfloat8 operator /(float a, const vfloat8& b) { return vfloat8(a) / b; }
  268. __forceinline vfloat8 operator &(const vfloat8& a, const vfloat8& b) { return _mm256_and_ps(a,b); }
  269. __forceinline vfloat8 operator |(const vfloat8& a, const vfloat8& b) { return _mm256_or_ps(a,b); }
  270. __forceinline vfloat8 operator ^(const vfloat8& a, const vfloat8& b) { return _mm256_xor_ps(a,b); }
  271. __forceinline vfloat8 operator ^(const vfloat8& a, const vint8& b) { return _mm256_xor_ps(a,_mm256_castsi256_ps(b)); }
  272. __forceinline vfloat8 min(const vfloat8& a, const vfloat8& b) { return _mm256_min_ps(a, b); }
  273. __forceinline vfloat8 min(const vfloat8& a, float b) { return _mm256_min_ps(a, vfloat8(b)); }
  274. __forceinline vfloat8 min(float a, const vfloat8& b) { return _mm256_min_ps(vfloat8(a), b); }
  275. __forceinline vfloat8 max(const vfloat8& a, const vfloat8& b) { return _mm256_max_ps(a, b); }
  276. __forceinline vfloat8 max(const vfloat8& a, float b) { return _mm256_max_ps(a, vfloat8(b)); }
  277. __forceinline vfloat8 max(float a, const vfloat8& b) { return _mm256_max_ps(vfloat8(a), b); }
  278. /* need "static __forceinline for MSVC, otherwise we'll link the wrong version in debug mode */
  279. #if defined(__AVX2__)
  280. static __forceinline vfloat8 mini(const vfloat8& a, const vfloat8& b) {
  281. const vint8 ai = _mm256_castps_si256(a);
  282. const vint8 bi = _mm256_castps_si256(b);
  283. const vint8 ci = _mm256_min_epi32(ai,bi);
  284. return _mm256_castsi256_ps(ci);
  285. }
  286. static __forceinline vfloat8 maxi(const vfloat8& a, const vfloat8& b) {
  287. const vint8 ai = _mm256_castps_si256(a);
  288. const vint8 bi = _mm256_castps_si256(b);
  289. const vint8 ci = _mm256_max_epi32(ai,bi);
  290. return _mm256_castsi256_ps(ci);
  291. }
  292. static __forceinline vfloat8 minui(const vfloat8& a, const vfloat8& b) {
  293. const vint8 ai = _mm256_castps_si256(a);
  294. const vint8 bi = _mm256_castps_si256(b);
  295. const vint8 ci = _mm256_min_epu32(ai,bi);
  296. return _mm256_castsi256_ps(ci);
  297. }
  298. static __forceinline vfloat8 maxui(const vfloat8& a, const vfloat8& b) {
  299. const vint8 ai = _mm256_castps_si256(a);
  300. const vint8 bi = _mm256_castps_si256(b);
  301. const vint8 ci = _mm256_max_epu32(ai,bi);
  302. return _mm256_castsi256_ps(ci);
  303. }
  304. #else
  305. static __forceinline vfloat8 mini(const vfloat8& a, const vfloat8& b) {
  306. return asFloat(min(asInt(a),asInt(b)));
  307. }
  308. static __forceinline vfloat8 maxi(const vfloat8& a, const vfloat8& b) {
  309. return asFloat(max(asInt(a),asInt(b)));
  310. }
  311. #endif
  312. ////////////////////////////////////////////////////////////////////////////////
  313. /// Ternary Operators
  314. ////////////////////////////////////////////////////////////////////////////////
  315. #if defined(__AVX2__)
  316. static __forceinline vfloat8 madd (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fmadd_ps(a,b,c); }
  317. static __forceinline vfloat8 msub (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fmsub_ps(a,b,c); }
  318. static __forceinline vfloat8 nmadd (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fnmadd_ps(a,b,c); }
  319. static __forceinline vfloat8 nmsub (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fnmsub_ps(a,b,c); }
  320. #else
  321. static __forceinline vfloat8 madd (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return a*b+c; }
  322. static __forceinline vfloat8 msub (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return a*b-c; }
  323. static __forceinline vfloat8 nmadd (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return -a*b+c;}
  324. static __forceinline vfloat8 nmsub (const vfloat8& a, const vfloat8& b, const vfloat8& c) { return -a*b-c; }
  325. #endif
  326. ////////////////////////////////////////////////////////////////////////////////
  327. /// Assignment Operators
  328. ////////////////////////////////////////////////////////////////////////////////
  329. __forceinline vfloat8& operator +=(vfloat8& a, const vfloat8& b) { return a = a + b; }
  330. __forceinline vfloat8& operator +=(vfloat8& a, float b) { return a = a + b; }
  331. __forceinline vfloat8& operator -=(vfloat8& a, const vfloat8& b) { return a = a - b; }
  332. __forceinline vfloat8& operator -=(vfloat8& a, float b) { return a = a - b; }
  333. __forceinline vfloat8& operator *=(vfloat8& a, const vfloat8& b) { return a = a * b; }
  334. __forceinline vfloat8& operator *=(vfloat8& a, float b) { return a = a * b; }
  335. __forceinline vfloat8& operator /=(vfloat8& a, const vfloat8& b) { return a = a / b; }
  336. __forceinline vfloat8& operator /=(vfloat8& a, float b) { return a = a / b; }
  337. ////////////////////////////////////////////////////////////////////////////////
  338. /// Comparison Operators + Select
  339. ////////////////////////////////////////////////////////////////////////////////
  340. #if defined(__AVX512VL__)
  341. static __forceinline vboolf8 operator ==(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_EQ); }
  342. static __forceinline vboolf8 operator !=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_NE); }
  343. static __forceinline vboolf8 operator < (const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_LT); }
  344. static __forceinline vboolf8 operator >=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_GE); }
  345. static __forceinline vboolf8 operator > (const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_GT); }
  346. static __forceinline vboolf8 operator <=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_LE); }
  347. static __forceinline vfloat8 select(const vboolf8& m, const vfloat8& t, const vfloat8& f) {
  348. return _mm256_mask_blend_ps(m, f, t);
  349. }
  350. #elif !defined(__aarch64__)
  351. static __forceinline vboolf8 operator ==(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_EQ_OQ); }
  352. static __forceinline vboolf8 operator !=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_NEQ_UQ); }
  353. static __forceinline vboolf8 operator < (const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_LT_OS); }
  354. static __forceinline vboolf8 operator >=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_NLT_US); }
  355. static __forceinline vboolf8 operator > (const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_NLE_US); }
  356. static __forceinline vboolf8 operator <=(const vfloat8& a, const vfloat8& b) { return _mm256_cmp_ps(a, b, _CMP_LE_OS); }
  357. static __forceinline vfloat8 select(const vboolf8& m, const vfloat8& t, const vfloat8& f) {
  358. return _mm256_blendv_ps(f, t, m);
  359. }
  360. #else
  361. static __forceinline vboolf8 operator ==(const vfloat8& a, const vfloat8& b) { return _mm256_cmpeq_ps(a, b); }
  362. static __forceinline vboolf8 operator !=(const vfloat8& a, const vfloat8& b) { return _mm256_cmpneq_ps(a, b); }
  363. static __forceinline vboolf8 operator < (const vfloat8& a, const vfloat8& b) { return _mm256_cmplt_ps(a, b); }
  364. static __forceinline vboolf8 operator >=(const vfloat8& a, const vfloat8& b) { return _mm256_cmpge_ps(a, b); }
  365. static __forceinline vboolf8 operator > (const vfloat8& a, const vfloat8& b) { return _mm256_cmpgt_ps(a, b); }
  366. static __forceinline vboolf8 operator <=(const vfloat8& a, const vfloat8& b) { return _mm256_cmple_ps(a, b); }
  367. static __forceinline vfloat8 select(const vboolf8& m, const vfloat8& t, const vfloat8& f) {
  368. return _mm256_blendv_ps(f, t, m);
  369. }
  370. #endif
  371. template<int mask>
  372. __forceinline vfloat8 select(const vfloat8& t, const vfloat8& f) {
  373. return _mm256_blend_ps(f, t, mask);
  374. }
  375. __forceinline vboolf8 operator ==(const vfloat8& a, const float& b) { return a == vfloat8(b); }
  376. __forceinline vboolf8 operator ==(const float& a, const vfloat8& b) { return vfloat8(a) == b; }
  377. __forceinline vboolf8 operator !=(const vfloat8& a, const float& b) { return a != vfloat8(b); }
  378. __forceinline vboolf8 operator !=(const float& a, const vfloat8& b) { return vfloat8(a) != b; }
  379. __forceinline vboolf8 operator < (const vfloat8& a, const float& b) { return a < vfloat8(b); }
  380. __forceinline vboolf8 operator < (const float& a, const vfloat8& b) { return vfloat8(a) < b; }
  381. __forceinline vboolf8 operator >=(const vfloat8& a, const float& b) { return a >= vfloat8(b); }
  382. __forceinline vboolf8 operator >=(const float& a, const vfloat8& b) { return vfloat8(a) >= b; }
  383. __forceinline vboolf8 operator > (const vfloat8& a, const float& b) { return a > vfloat8(b); }
  384. __forceinline vboolf8 operator > (const float& a, const vfloat8& b) { return vfloat8(a) > b; }
  385. __forceinline vboolf8 operator <=(const vfloat8& a, const float& b) { return a <= vfloat8(b); }
  386. __forceinline vboolf8 operator <=(const float& a, const vfloat8& b) { return vfloat8(a) <= b; }
  387. __forceinline vboolf8 eq(const vfloat8& a, const vfloat8& b) { return a == b; }
  388. __forceinline vboolf8 ne(const vfloat8& a, const vfloat8& b) { return a != b; }
  389. __forceinline vboolf8 lt(const vfloat8& a, const vfloat8& b) { return a < b; }
  390. __forceinline vboolf8 ge(const vfloat8& a, const vfloat8& b) { return a >= b; }
  391. __forceinline vboolf8 gt(const vfloat8& a, const vfloat8& b) { return a > b; }
  392. __forceinline vboolf8 le(const vfloat8& a, const vfloat8& b) { return a <= b; }
  393. #if defined(__AVX512VL__)
  394. static __forceinline vboolf8 eq(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_EQ); }
  395. static __forceinline vboolf8 ne(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_NE); }
  396. static __forceinline vboolf8 lt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LT); }
  397. static __forceinline vboolf8 ge(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GE); }
  398. static __forceinline vboolf8 gt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GT); }
  399. static __forceinline vboolf8 le(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LE); }
  400. #else
  401. static __forceinline vboolf8 eq(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a == b); }
  402. static __forceinline vboolf8 ne(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a != b); }
  403. static __forceinline vboolf8 lt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a < b); }
  404. static __forceinline vboolf8 ge(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a >= b); }
  405. static __forceinline vboolf8 gt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a > b); }
  406. static __forceinline vboolf8 le(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a <= b); }
  407. #endif
  408. __forceinline vfloat8 lerp(const vfloat8& a, const vfloat8& b, const vfloat8& t) {
  409. return madd(t,b-a,a);
  410. }
  411. __forceinline bool isvalid (const vfloat8& v) {
  412. return all((v > vfloat8(-FLT_LARGE)) & (v < vfloat8(+FLT_LARGE)));
  413. }
  414. __forceinline bool is_finite (const vfloat8& a) {
  415. return all((a >= vfloat8(-FLT_MAX)) & (a <= vfloat8(+FLT_MAX)));
  416. }
  417. __forceinline bool is_finite (const vboolf8& valid, const vfloat8& a) {
  418. return all(valid, (a >= vfloat8(-FLT_MAX)) & (a <= vfloat8(+FLT_MAX)));
  419. }
  420. ////////////////////////////////////////////////////////////////////////////////
  421. /// Rounding Functions
  422. ////////////////////////////////////////////////////////////////////////////////
  423. #if !defined(__aarch64__)
  424. __forceinline vfloat8 floor(const vfloat8& a) { return _mm256_round_ps(a, _MM_FROUND_TO_NEG_INF ); }
  425. __forceinline vfloat8 ceil (const vfloat8& a) { return _mm256_round_ps(a, _MM_FROUND_TO_POS_INF ); }
  426. __forceinline vfloat8 trunc(const vfloat8& a) { return _mm256_round_ps(a, _MM_FROUND_TO_ZERO ); }
  427. __forceinline vfloat8 round(const vfloat8& a) { return _mm256_round_ps(a, _MM_FROUND_TO_NEAREST_INT); }
  428. #else
  429. __forceinline vfloat8 floor(const vfloat8& a) { return _mm256_floor_ps(a); }
  430. __forceinline vfloat8 ceil (const vfloat8& a) { return _mm256_ceil_ps(a); }
  431. #endif
  432. __forceinline vfloat8 frac (const vfloat8& a) { return a-floor(a); }
  433. ////////////////////////////////////////////////////////////////////////////////
  434. /// Movement/Shifting/Shuffling Functions
  435. ////////////////////////////////////////////////////////////////////////////////
  436. __forceinline vfloat8 unpacklo(const vfloat8& a, const vfloat8& b) { return _mm256_unpacklo_ps(a, b); }
  437. __forceinline vfloat8 unpackhi(const vfloat8& a, const vfloat8& b) { return _mm256_unpackhi_ps(a, b); }
  438. template<int i>
  439. __forceinline vfloat8 shuffle(const vfloat8& v) {
  440. return _mm256_permute_ps(v, _MM_SHUFFLE(i, i, i, i));
  441. }
  442. template<int i0, int i1>
  443. __forceinline vfloat8 shuffle4(const vfloat8& v) {
  444. return _mm256_permute2f128_ps(v, v, (i1 << 4) | (i0 << 0));
  445. }
  446. template<int i0, int i1>
  447. __forceinline vfloat8 shuffle4(const vfloat8& a, const vfloat8& b) {
  448. return _mm256_permute2f128_ps(a, b, (i1 << 4) | (i0 << 0));
  449. }
  450. template<int i0, int i1, int i2, int i3>
  451. __forceinline vfloat8 shuffle(const vfloat8& v) {
  452. return _mm256_permute_ps(v, _MM_SHUFFLE(i3, i2, i1, i0));
  453. }
  454. template<int i0, int i1, int i2, int i3>
  455. __forceinline vfloat8 shuffle(const vfloat8& a, const vfloat8& b) {
  456. return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  457. }
  458. #if !defined(__aarch64__)
  459. template<> __forceinline vfloat8 shuffle<0, 0, 2, 2>(const vfloat8& v) { return _mm256_moveldup_ps(v); }
  460. template<> __forceinline vfloat8 shuffle<1, 1, 3, 3>(const vfloat8& v) { return _mm256_movehdup_ps(v); }
  461. template<> __forceinline vfloat8 shuffle<0, 1, 0, 1>(const vfloat8& v) { return _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(v))); }
  462. #endif
  463. __forceinline vfloat8 broadcast(const float* ptr) { return _mm256_broadcast_ss(ptr); }
  464. template<size_t i> __forceinline vfloat8 insert4(const vfloat8& a, const vfloat4& b) { return _mm256_insertf128_ps(a, b, i); }
  465. template<size_t i> __forceinline vfloat4 extract4 (const vfloat8& a) { return _mm256_extractf128_ps(a, i); }
  466. template<> __forceinline vfloat4 extract4<0>(const vfloat8& a) { return _mm256_castps256_ps128(a); }
  467. __forceinline float toScalar(const vfloat8& v) { return _mm_cvtss_f32(_mm256_castps256_ps128(v)); }
  468. #if defined (__AVX2__) && !defined(__aarch64__)
  469. static __forceinline vfloat8 permute(const vfloat8& a, const __m256i& index) {
  470. return _mm256_permutevar8x32_ps(a, index);
  471. }
  472. #endif
  473. #if defined(__AVX512VL__)
  474. template<int i>
  475. static __forceinline vfloat8 align_shift_right(const vfloat8& a, const vfloat8& b) {
  476. return _mm256_castsi256_ps(_mm256_alignr_epi32(_mm256_castps_si256(a), _mm256_castps_si256(b), i));
  477. }
  478. #endif
  479. #if defined (__AVX_I__)
  480. template<const int mode>
  481. static __forceinline vint4 convert_to_hf16(const vfloat8& a) {
  482. return _mm256_cvtps_ph(a, mode);
  483. }
  484. static __forceinline vfloat8 convert_from_hf16(const vint4& a) {
  485. return _mm256_cvtph_ps(a);
  486. }
  487. #endif
  488. #if defined(__AVX512VL__)
  489. static __forceinline vfloat8 shift_right_1(const vfloat8& x) {
  490. return align_shift_right<1>(zero,x);
  491. }
  492. #else
  493. static __forceinline vfloat8 shift_right_1(const vfloat8& x) {
  494. const vfloat8 t0 = shuffle<1,2,3,0>(x);
  495. const vfloat8 t1 = shuffle4<1,0>(t0);
  496. return _mm256_blend_ps(t0,t1,0x88);
  497. }
  498. #endif
  499. __forceinline vint8 floori(const vfloat8& a) {
  500. return vint8(floor(a));
  501. }
  502. ////////////////////////////////////////////////////////////////////////////////
  503. /// Transpose
  504. ////////////////////////////////////////////////////////////////////////////////
  505. __forceinline void transpose(const vfloat8& r0, const vfloat8& r1, const vfloat8& r2, const vfloat8& r3, vfloat8& c0, vfloat8& c1, vfloat8& c2, vfloat8& c3)
  506. {
  507. vfloat8 l02 = unpacklo(r0,r2);
  508. vfloat8 h02 = unpackhi(r0,r2);
  509. vfloat8 l13 = unpacklo(r1,r3);
  510. vfloat8 h13 = unpackhi(r1,r3);
  511. c0 = unpacklo(l02,l13);
  512. c1 = unpackhi(l02,l13);
  513. c2 = unpacklo(h02,h13);
  514. c3 = unpackhi(h02,h13);
  515. }
  516. __forceinline void transpose(const vfloat8& r0, const vfloat8& r1, const vfloat8& r2, const vfloat8& r3, vfloat8& c0, vfloat8& c1, vfloat8& c2)
  517. {
  518. vfloat8 l02 = unpacklo(r0,r2);
  519. vfloat8 h02 = unpackhi(r0,r2);
  520. vfloat8 l13 = unpacklo(r1,r3);
  521. vfloat8 h13 = unpackhi(r1,r3);
  522. c0 = unpacklo(l02,l13);
  523. c1 = unpackhi(l02,l13);
  524. c2 = unpacklo(h02,h13);
  525. }
  526. __forceinline void transpose(const vfloat8& r0, const vfloat8& r1, const vfloat8& r2, const vfloat8& r3, const vfloat8& r4, const vfloat8& r5, const vfloat8& r6, const vfloat8& r7,
  527. vfloat8& c0, vfloat8& c1, vfloat8& c2, vfloat8& c3, vfloat8& c4, vfloat8& c5, vfloat8& c6, vfloat8& c7)
  528. {
  529. vfloat8 h0,h1,h2,h3; transpose(r0,r1,r2,r3,h0,h1,h2,h3);
  530. vfloat8 h4,h5,h6,h7; transpose(r4,r5,r6,r7,h4,h5,h6,h7);
  531. c0 = shuffle4<0,2>(h0,h4);
  532. c1 = shuffle4<0,2>(h1,h5);
  533. c2 = shuffle4<0,2>(h2,h6);
  534. c3 = shuffle4<0,2>(h3,h7);
  535. c4 = shuffle4<1,3>(h0,h4);
  536. c5 = shuffle4<1,3>(h1,h5);
  537. c6 = shuffle4<1,3>(h2,h6);
  538. c7 = shuffle4<1,3>(h3,h7);
  539. }
  540. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, const vfloat4& r4, const vfloat4& r5, const vfloat4& r6, const vfloat4& r7,
  541. vfloat8& c0, vfloat8& c1, vfloat8& c2, vfloat8& c3)
  542. {
  543. transpose(vfloat8(r0,r4), vfloat8(r1,r5), vfloat8(r2,r6), vfloat8(r3,r7), c0, c1, c2, c3);
  544. }
  545. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, const vfloat4& r4, const vfloat4& r5, const vfloat4& r6, const vfloat4& r7,
  546. vfloat8& c0, vfloat8& c1, vfloat8& c2)
  547. {
  548. transpose(vfloat8(r0,r4), vfloat8(r1,r5), vfloat8(r2,r6), vfloat8(r3,r7), c0, c1, c2);
  549. }
  550. ////////////////////////////////////////////////////////////////////////////////
  551. /// Reductions
  552. ////////////////////////////////////////////////////////////////////////////////
  553. #if !defined(__aarch64__)
  554. __forceinline vfloat8 vreduce_min2(const vfloat8& v) { return min(v,shuffle<1,0,3,2>(v)); }
  555. __forceinline vfloat8 vreduce_min4(const vfloat8& v) { vfloat8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
  556. __forceinline vfloat8 vreduce_min (const vfloat8& v) { vfloat8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
  557. __forceinline vfloat8 vreduce_max2(const vfloat8& v) { return max(v,shuffle<1,0,3,2>(v)); }
  558. __forceinline vfloat8 vreduce_max4(const vfloat8& v) { vfloat8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
  559. __forceinline vfloat8 vreduce_max (const vfloat8& v) { vfloat8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
  560. __forceinline vfloat8 vreduce_add2(const vfloat8& v) { return v + shuffle<1,0,3,2>(v); }
  561. __forceinline vfloat8 vreduce_add4(const vfloat8& v) { vfloat8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
  562. __forceinline vfloat8 vreduce_add (const vfloat8& v) { vfloat8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
  563. __forceinline float reduce_min(const vfloat8& v) { return toScalar(vreduce_min(v)); }
  564. __forceinline float reduce_max(const vfloat8& v) { return toScalar(vreduce_max(v)); }
  565. __forceinline float reduce_add(const vfloat8& v) { return toScalar(vreduce_add(v)); }
  566. #else
  567. __forceinline float reduce_min(const vfloat8& v) { return vminvq_f32(_mm_min_ps(v.v.lo,v.v.hi)); }
  568. __forceinline float reduce_max(const vfloat8& v) { return vmaxvq_f32(_mm_max_ps(v.v.lo,v.v.hi)); }
  569. __forceinline vfloat8 vreduce_min(const vfloat8& v) { return vfloat8(reduce_min(v)); }
  570. __forceinline vfloat8 vreduce_max(const vfloat8& v) { return vfloat8(reduce_max(v)); }
  571. __forceinline float reduce_add(const vfloat8& v) { return vaddvq_f32(_mm_add_ps(v.v.lo,v.v.hi)); }
  572. #endif
  573. __forceinline size_t select_min(const vboolf8& valid, const vfloat8& v)
  574. {
  575. const vfloat8 a = select(valid,v,vfloat8(pos_inf));
  576. const vbool8 valid_min = valid & (a == vreduce_min(a));
  577. return bsf(movemask(any(valid_min) ? valid_min : valid));
  578. }
  579. __forceinline size_t select_max(const vboolf8& valid, const vfloat8& v)
  580. {
  581. const vfloat8 a = select(valid,v,vfloat8(neg_inf));
  582. const vbool8 valid_max = valid & (a == vreduce_max(a));
  583. return bsf(movemask(any(valid_max) ? valid_max : valid));
  584. }
  585. ////////////////////////////////////////////////////////////////////////////////
  586. /// Euclidean Space Operators (pairs of Vec3fa's)
  587. ////////////////////////////////////////////////////////////////////////////////
  588. //__forceinline vfloat8 dot(const vfloat8& a, const vfloat8& b) {
  589. // return vreduce_add4(a*b);
  590. //}
  591. __forceinline vfloat8 dot(const vfloat8& a, const vfloat8& b) {
  592. return _mm256_dp_ps(a,b,0x7F);
  593. }
  594. __forceinline vfloat8 cross(const vfloat8& a, const vfloat8& b)
  595. {
  596. const vfloat8 a0 = a;
  597. const vfloat8 b0 = shuffle<1,2,0,3>(b);
  598. const vfloat8 a1 = shuffle<1,2,0,3>(a);
  599. const vfloat8 b1 = b;
  600. return shuffle<1,2,0,3>(msub(a0,b0,a1*b1));
  601. }
  602. //__forceinline float sqr_length (const vfloat<8>& a) { return dot(a,a); }
  603. //__forceinline float rcp_length (const vfloat<8>& a) { return rsqrt(dot(a,a)); }
  604. //__forceinline float rcp_length2(const vfloat<8>& a) { return rcp(dot(a,a)); }
  605. //__forceinline float length (const vfloat<8>& a) { return sqrt(dot(a,a)); }
  606. __forceinline vfloat<8> normalize(const vfloat<8>& a) { return a*rsqrt(dot(a,a)); }
  607. //__forceinline float distance(const vfloat<8>& a, const vfloat<8>& b) { return length(a-b); }
  608. //__forceinline float halfArea(const vfloat<8>& d) { return madd(d.x,(d.y+d.z),d.y*d.z); }
  609. //__forceinline float area (const vfloat<8>& d) { return 2.0f*halfArea(d); }
  610. //__forceinline vfloat<8> reflect(const vfloat<8>& V, const vfloat<8>& N) { return 2.0f*dot(V,N)*N-V; }
  611. //__forceinline vfloat<8> normalize_safe(const vfloat<8>& a) {
  612. // const float d = dot(a,a); if (unlikely(d == 0.0f)) return a; else return a*rsqrt(d);
  613. //}
  614. ////////////////////////////////////////////////////////////////////////////////
  615. /// In Register Sorting
  616. ////////////////////////////////////////////////////////////////////////////////
  617. __forceinline vfloat8 sort_ascending(const vfloat8& v)
  618. {
  619. const vfloat8 a0 = v;
  620. const vfloat8 b0 = shuffle<1,0,3,2>(a0);
  621. const vfloat8 c0 = min(a0,b0);
  622. const vfloat8 d0 = max(a0,b0);
  623. const vfloat8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  624. const vfloat8 b1 = shuffle<2,3,0,1>(a1);
  625. const vfloat8 c1 = min(a1,b1);
  626. const vfloat8 d1 = max(a1,b1);
  627. const vfloat8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  628. const vfloat8 b2 = shuffle<1,0,3,2>(a2);
  629. const vfloat8 c2 = min(a2,b2);
  630. const vfloat8 d2 = max(a2,b2);
  631. const vfloat8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  632. const vfloat8 b3 = shuffle4<1,0>(a3);
  633. const vfloat8 c3 = min(a3,b3);
  634. const vfloat8 d3 = max(a3,b3);
  635. const vfloat8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  636. const vfloat8 b4 = shuffle<2,3,0,1>(a4);
  637. const vfloat8 c4 = min(a4,b4);
  638. const vfloat8 d4 = max(a4,b4);
  639. const vfloat8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  640. const vfloat8 b5 = shuffle<1,0,3,2>(a5);
  641. const vfloat8 c5 = min(a5,b5);
  642. const vfloat8 d5 = max(a5,b5);
  643. const vfloat8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  644. return a6;
  645. }
  646. __forceinline vfloat8 sort_descending(const vfloat8& v)
  647. {
  648. const vfloat8 a0 = v;
  649. const vfloat8 b0 = shuffle<1,0,3,2>(a0);
  650. const vfloat8 c0 = max(a0,b0);
  651. const vfloat8 d0 = min(a0,b0);
  652. const vfloat8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  653. const vfloat8 b1 = shuffle<2,3,0,1>(a1);
  654. const vfloat8 c1 = max(a1,b1);
  655. const vfloat8 d1 = min(a1,b1);
  656. const vfloat8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  657. const vfloat8 b2 = shuffle<1,0,3,2>(a2);
  658. const vfloat8 c2 = max(a2,b2);
  659. const vfloat8 d2 = min(a2,b2);
  660. const vfloat8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  661. const vfloat8 b3 = shuffle4<1,0>(a3);
  662. const vfloat8 c3 = max(a3,b3);
  663. const vfloat8 d3 = min(a3,b3);
  664. const vfloat8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  665. const vfloat8 b4 = shuffle<2,3,0,1>(a4);
  666. const vfloat8 c4 = max(a4,b4);
  667. const vfloat8 d4 = min(a4,b4);
  668. const vfloat8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  669. const vfloat8 b5 = shuffle<1,0,3,2>(a5);
  670. const vfloat8 c5 = max(a5,b5);
  671. const vfloat8 d5 = min(a5,b5);
  672. const vfloat8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  673. return a6;
  674. }
  675. ////////////////////////////////////////////////////////////////////////////////
  676. /// Output Operators
  677. ////////////////////////////////////////////////////////////////////////////////
  678. __forceinline embree_ostream operator <<(embree_ostream cout, const vfloat8& a) {
  679. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  680. }
  681. }
  682. #undef vboolf
  683. #undef vboold
  684. #undef vint
  685. #undef vuint
  686. #undef vllong
  687. #undef vfloat
  688. #undef vdouble