vfloat8_avx.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. namespace embree
  18. {
  19. /* 8-wide AVX float type */
  20. template<>
  21. struct vfloat<8>
  22. {
  23. typedef vboolf8 Bool;
  24. typedef vint8 Int;
  25. typedef vfloat8 Float;
  26. enum { size = 8 }; // number of SIMD elements
  27. union { __m256 v; float f[8]; int i[8]; }; // data
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// Constructors, Assignment & Cast Operators
  30. ////////////////////////////////////////////////////////////////////////////////
  31. __forceinline vfloat ( ) {}
  32. __forceinline vfloat ( const vfloat8& other ) { v = other.v; }
  33. __forceinline vfloat8& operator=( const vfloat8& other ) { v = other.v; return *this; }
  34. __forceinline vfloat( const __m256 a ) : v(a) {}
  35. __forceinline operator const __m256&( void ) const { return v; }
  36. __forceinline operator __m256&( void ) { return v; }
  37. __forceinline explicit vfloat( const vfloat4& a ) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),a,1)) {}
  38. __forceinline vfloat( const vfloat4& a, const vfloat4& b ) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),b,1)) {}
  39. __forceinline explicit vfloat( const char* const a ) : v(_mm256_loadu_ps((const float*)a)) {}
  40. __forceinline vfloat( const float& a ) : v(_mm256_broadcast_ss(&a)) {}
  41. __forceinline vfloat( float a, float b) : v(_mm256_set_ps(b, a, b, a, b, a, b, a)) {}
  42. __forceinline vfloat( float a, float b, float c, float d ) : v(_mm256_set_ps(d, c, b, a, d, c, b, a)) {}
  43. __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)) {}
  44. __forceinline explicit vfloat( const __m256i a ) : v(_mm256_cvtepi32_ps(a)) {}
  45. ////////////////////////////////////////////////////////////////////////////////
  46. /// Constants
  47. ////////////////////////////////////////////////////////////////////////////////
  48. __forceinline vfloat( ZeroTy ) : v(_mm256_setzero_ps()) {}
  49. __forceinline vfloat( OneTy ) : v(_mm256_set1_ps(1.0f)) {}
  50. __forceinline vfloat( PosInfTy ) : v(_mm256_set1_ps(pos_inf)) {}
  51. __forceinline vfloat( NegInfTy ) : v(_mm256_set1_ps(neg_inf)) {}
  52. __forceinline vfloat( StepTy ) : v(_mm256_set_ps(7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f, 0.0f)) {}
  53. __forceinline vfloat( NaNTy ) : v(_mm256_set1_ps(nan)) {}
  54. ////////////////////////////////////////////////////////////////////////////////
  55. /// Loads and Stores
  56. ////////////////////////////////////////////////////////////////////////////////
  57. static __forceinline vfloat8 broadcast( const void* const a ) {
  58. return _mm256_broadcast_ss((float*)a);
  59. }
  60. static __forceinline vfloat8 broadcast2( const float* const a, const float* const b ) {
  61. #if defined(__INTEL_COMPILER)
  62. const vfloat8 v0 = _mm256_broadcast_ss(a);
  63. const vfloat8 v1 = _mm256_broadcast_ss(b);
  64. return _mm256_blend_ps(v1, v0, 0xf);
  65. #else
  66. return _mm256_set_ps(*b,*b,*b,*b,*a,*a,*a,*a);
  67. #endif
  68. }
  69. static __forceinline const vfloat8 broadcast4f(const vfloat4* ptr) {
  70. return _mm256_broadcast_ps((__m128*)ptr);
  71. }
  72. static __forceinline vfloat8 load( const unsigned char* const ptr ) {
  73. #if defined(__AVX2__)
  74. return _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  75. #else
  76. return vfloat8(vfloat4::load(ptr),vfloat4::load(ptr+4));
  77. #endif
  78. }
  79. static __forceinline vfloat8 load ( const void* const ptr) { return _mm256_load_ps((float*)ptr); }
  80. static __forceinline vfloat8 loadu( const void* const ptr) { return _mm256_loadu_ps((float*)ptr); }
  81. static __forceinline void store ( void* ptr, const vfloat8& v) { return _mm256_store_ps((float*)ptr,v); }
  82. static __forceinline void storeu( void* ptr, const vfloat8& v) { return _mm256_storeu_ps((float*)ptr,v); }
  83. #if defined(__AVX512VL__)
  84. static __forceinline vfloat8 load ( const vboolf8& mask, const void* const ptr) { return _mm256_mask_load_ps (_mm256_setzero_ps(),mask,(float*)ptr); }
  85. static __forceinline vfloat8 loadu( const vboolf8& mask, const void* const ptr) { return _mm256_mask_loadu_ps(_mm256_setzero_ps(),mask,(float*)ptr); }
  86. static __forceinline void store ( const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_mask_store_ps ((float*)ptr,mask,v); }
  87. static __forceinline void storeu( const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_mask_storeu_ps((float*)ptr,mask,v); }
  88. #else
  89. static __forceinline vfloat8 load ( const vboolf8& mask, const void* const ptr) { return _mm256_maskload_ps((float*)ptr,(__m256i)mask); }
  90. static __forceinline vfloat8 loadu( const vboolf8& mask, const void* const ptr) { return _mm256_maskload_ps((float*)ptr,(__m256i)mask); }
  91. static __forceinline void store ( const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_maskstore_ps((float*)ptr,(__m256i)mask,v); }
  92. static __forceinline void storeu( const vboolf8& mask, void* ptr, const vfloat8& v) { _mm256_maskstore_ps((float*)ptr,(__m256i)mask,v); }
  93. #endif
  94. #if defined(__AVX2__)
  95. static __forceinline vfloat8 load_nt(void* ptr) {
  96. return _mm256_castsi256_ps(_mm256_stream_load_si256((__m256i*)ptr));
  97. }
  98. #endif
  99. static __forceinline void store_nt(void* ptr, const vfloat8& v) {
  100. _mm256_stream_ps((float*)ptr,v);
  101. }
  102. template<int scale>
  103. static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vfloat8& v)
  104. {
  105. #if defined(__AVX512VL__)
  106. _mm256_mask_i32scatter_ps(ptr,mask,ofs,v,scale);
  107. #else
  108. if (likely(mask[0])) *(float*)(((char*)ptr)+scale*ofs[0]) = v[0];
  109. if (likely(mask[1])) *(float*)(((char*)ptr)+scale*ofs[1]) = v[1];
  110. if (likely(mask[2])) *(float*)(((char*)ptr)+scale*ofs[2]) = v[2];
  111. if (likely(mask[3])) *(float*)(((char*)ptr)+scale*ofs[3]) = v[3];
  112. if (likely(mask[4])) *(float*)(((char*)ptr)+scale*ofs[4]) = v[4];
  113. if (likely(mask[5])) *(float*)(((char*)ptr)+scale*ofs[5]) = v[5];
  114. if (likely(mask[6])) *(float*)(((char*)ptr)+scale*ofs[6]) = v[6];
  115. if (likely(mask[7])) *(float*)(((char*)ptr)+scale*ofs[7]) = v[7];
  116. #endif
  117. }
  118. static __forceinline void store(const vboolf8& mask, char* ptr, const vint8& ofs, const vfloat8& v) {
  119. scatter<1>(mask,ptr,ofs,v);
  120. }
  121. static __forceinline void store(const vboolf8& mask, float* ptr, const vint8& ofs, const vfloat8& v) {
  122. scatter<4>(mask,ptr,ofs,v);
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////
  125. /// Array Access
  126. ////////////////////////////////////////////////////////////////////////////////
  127. __forceinline const float& operator []( const size_t index ) const { assert(index < 8); return f[index]; }
  128. __forceinline float& operator []( const size_t index ) { assert(index < 8); return f[index]; }
  129. };
  130. ////////////////////////////////////////////////////////////////////////////////
  131. /// Unary Operators
  132. ////////////////////////////////////////////////////////////////////////////////
  133. __forceinline const vfloat8 asFloat ( const vint8& a ) { return _mm256_castsi256_ps(a); }
  134. __forceinline const vint8 asInt ( const vfloat8& a ) { return _mm256_castps_si256(a); }
  135. __forceinline const vfloat8 operator +( const vfloat8& a ) { return a; }
  136. __forceinline const vfloat8 operator -( const vfloat8& a ) {
  137. const __m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x80000000));
  138. return _mm256_xor_ps(a.v, mask);
  139. }
  140. __forceinline const vfloat8 abs ( const vfloat8& a ) {
  141. const __m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
  142. return _mm256_and_ps(a.v, mask);
  143. }
  144. __forceinline const vfloat8 sign ( const vfloat8& a ) { return _mm256_blendv_ps(vfloat8(one), -vfloat8(one), _mm256_cmp_ps(a, vfloat8(zero), _CMP_NGE_UQ )); }
  145. __forceinline const vfloat8 signmsk ( const vfloat8& a ) { return _mm256_and_ps(a.v,_mm256_castsi256_ps(_mm256_set1_epi32(0x80000000))); }
  146. __forceinline const vfloat8 rcp(const vfloat8& a)
  147. {
  148. #if defined(__AVX512VL__)
  149. const vfloat8 r = _mm256_rcp14_ps(a.v);
  150. #else
  151. const vfloat8 r = _mm256_rcp_ps(a.v);
  152. #endif
  153. #if defined(__AVX2__)
  154. return _mm256_mul_ps(r,_mm256_fnmadd_ps(r, a, vfloat8(2.0f)));
  155. #else
  156. return _mm256_mul_ps(r,_mm256_sub_ps(vfloat8(2.0f), _mm256_mul_ps(r, a)));
  157. #endif
  158. }
  159. __forceinline const vfloat8 sqr ( const vfloat8& a ) { return _mm256_mul_ps(a,a); }
  160. __forceinline const vfloat8 sqrt ( const vfloat8& a ) { return _mm256_sqrt_ps(a.v); }
  161. __forceinline const vfloat8 rsqrt( const vfloat8& a )
  162. {
  163. #if defined(__AVX512VL__)
  164. const vfloat8 r = _mm256_rsqrt14_ps(a.v);
  165. #else
  166. const vfloat8 r = _mm256_rsqrt_ps(a.v);
  167. #endif
  168. #if defined(__AVX2__)
  169. return _mm256_fmadd_ps(_mm256_set1_ps(1.5f), r,
  170. _mm256_mul_ps(_mm256_mul_ps(_mm256_mul_ps(a, _mm256_set1_ps(-0.5f)), r), _mm256_mul_ps(r, r)));
  171. #else
  172. return _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(1.5f), r),
  173. _mm256_mul_ps(_mm256_mul_ps(_mm256_mul_ps(a, _mm256_set1_ps(-0.5f)), r), _mm256_mul_ps(r, r)));
  174. #endif
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. /// Binary Operators
  178. ////////////////////////////////////////////////////////////////////////////////
  179. __forceinline const vfloat8 operator +( const vfloat8& a, const vfloat8& b ) { return _mm256_add_ps(a.v, b.v); }
  180. __forceinline const vfloat8 operator +( const vfloat8& a, const float b ) { return a + vfloat8(b); }
  181. __forceinline const vfloat8 operator +( const float a, const vfloat8& b ) { return vfloat8(a) + b; }
  182. __forceinline const vfloat8 operator -( const vfloat8& a, const vfloat8& b ) { return _mm256_sub_ps(a.v, b.v); }
  183. __forceinline const vfloat8 operator -( const vfloat8& a, const float b ) { return a - vfloat8(b); }
  184. __forceinline const vfloat8 operator -( const float a, const vfloat8& b ) { return vfloat8(a) - b; }
  185. __forceinline const vfloat8 operator *( const vfloat8& a, const vfloat8& b ) { return _mm256_mul_ps(a.v, b.v); }
  186. __forceinline const vfloat8 operator *( const vfloat8& a, const float b ) { return a * vfloat8(b); }
  187. __forceinline const vfloat8 operator *( const float a, const vfloat8& b ) { return vfloat8(a) * b; }
  188. __forceinline const vfloat8 operator /( const vfloat8& a, const vfloat8& b ) { return _mm256_div_ps(a.v, b.v); }
  189. __forceinline const vfloat8 operator /( const vfloat8& a, const float b ) { return a / vfloat8(b); }
  190. __forceinline const vfloat8 operator /( const float a, const vfloat8& b ) { return vfloat8(a) / b; }
  191. __forceinline const vfloat8 operator^( const vfloat8& a, const vfloat8& b ) { return _mm256_xor_ps(a.v,b.v); }
  192. __forceinline const vfloat8 operator^( const vfloat8& a, const vint8& b ) { return _mm256_xor_ps(a.v,_mm256_castsi256_ps(b.v)); }
  193. __forceinline const vfloat8 operator&( const vfloat8& a, const vfloat8& b ) { return _mm256_and_ps(a.v,b.v); }
  194. __forceinline const vfloat8 min( const vfloat8& a, const vfloat8& b ) { return _mm256_min_ps(a.v, b.v); }
  195. __forceinline const vfloat8 min( const vfloat8& a, const float b ) { return _mm256_min_ps(a.v, vfloat8(b)); }
  196. __forceinline const vfloat8 min( const float a, const vfloat8& b ) { return _mm256_min_ps(vfloat8(a), b.v); }
  197. __forceinline const vfloat8 max( const vfloat8& a, const vfloat8& b ) { return _mm256_max_ps(a.v, b.v); }
  198. __forceinline const vfloat8 max( const vfloat8& a, const float b ) { return _mm256_max_ps(a.v, vfloat8(b)); }
  199. __forceinline const vfloat8 max( const float a, const vfloat8& b ) { return _mm256_max_ps(vfloat8(a), b.v); }
  200. #if defined (__AVX2__)
  201. __forceinline vfloat8 mini(const vfloat8& a, const vfloat8& b) {
  202. const vint8 ai = _mm256_castps_si256(a);
  203. const vint8 bi = _mm256_castps_si256(b);
  204. const vint8 ci = _mm256_min_epi32(ai,bi);
  205. return _mm256_castsi256_ps(ci);
  206. }
  207. __forceinline vfloat8 maxi(const vfloat8& a, const vfloat8& b) {
  208. const vint8 ai = _mm256_castps_si256(a);
  209. const vint8 bi = _mm256_castps_si256(b);
  210. const vint8 ci = _mm256_max_epi32(ai,bi);
  211. return _mm256_castsi256_ps(ci);
  212. }
  213. __forceinline vfloat8 minui(const vfloat8& a, const vfloat8& b) {
  214. const vint8 ai = _mm256_castps_si256(a);
  215. const vint8 bi = _mm256_castps_si256(b);
  216. const vint8 ci = _mm256_min_epu32(ai,bi);
  217. return _mm256_castsi256_ps(ci);
  218. }
  219. __forceinline vfloat8 maxui(const vfloat8& a, const vfloat8& b) {
  220. const vint8 ai = _mm256_castps_si256(a);
  221. const vint8 bi = _mm256_castps_si256(b);
  222. const vint8 ci = _mm256_max_epu32(ai,bi);
  223. return _mm256_castsi256_ps(ci);
  224. }
  225. #else
  226. __forceinline vfloat8 mini(const vfloat8& a, const vfloat8& b) {
  227. return min(a,b);
  228. }
  229. __forceinline vfloat8 maxi(const vfloat8& a, const vfloat8& b) {
  230. return max(a,b);
  231. }
  232. #endif
  233. ////////////////////////////////////////////////////////////////////////////////
  234. /// Ternary Operators
  235. ////////////////////////////////////////////////////////////////////////////////
  236. #if defined(__AVX2__)
  237. __forceinline const vfloat8 madd ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fmadd_ps(a,b,c); }
  238. __forceinline const vfloat8 msub ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fmsub_ps(a,b,c); }
  239. __forceinline const vfloat8 nmadd ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fnmadd_ps(a,b,c); }
  240. __forceinline const vfloat8 nmsub ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return _mm256_fnmsub_ps(a,b,c); }
  241. #else
  242. __forceinline const vfloat8 madd ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return a*b+c; }
  243. __forceinline const vfloat8 msub ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return a*b-c; }
  244. __forceinline const vfloat8 nmadd ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return -a*b+c;}
  245. __forceinline const vfloat8 nmsub ( const vfloat8& a, const vfloat8& b, const vfloat8& c) { return -a*b-c; }
  246. #endif
  247. ////////////////////////////////////////////////////////////////////////////////
  248. /// Assignment Operators
  249. ////////////////////////////////////////////////////////////////////////////////
  250. __forceinline vfloat8& operator +=( vfloat8& a, const vfloat8& b ) { return a = a + b; }
  251. __forceinline vfloat8& operator +=( vfloat8& a, const float b ) { return a = a + b; }
  252. __forceinline vfloat8& operator -=( vfloat8& a, const vfloat8& b ) { return a = a - b; }
  253. __forceinline vfloat8& operator -=( vfloat8& a, const float b ) { return a = a - b; }
  254. __forceinline vfloat8& operator *=( vfloat8& a, const vfloat8& b ) { return a = a * b; }
  255. __forceinline vfloat8& operator *=( vfloat8& a, const float b ) { return a = a * b; }
  256. __forceinline vfloat8& operator /=( vfloat8& a, const vfloat8& b ) { return a = a / b; }
  257. __forceinline vfloat8& operator /=( vfloat8& a, const float b ) { return a = a / b; }
  258. ////////////////////////////////////////////////////////////////////////////////
  259. /// Comparison Operators + Select
  260. ////////////////////////////////////////////////////////////////////////////////
  261. #if defined(__AVX512VL__)
  262. __forceinline const vboolf8 operator ==( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_EQ); }
  263. __forceinline const vboolf8 operator !=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_NE); }
  264. __forceinline const vboolf8 operator < ( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_LT); }
  265. __forceinline const vboolf8 operator >=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_GE); }
  266. __forceinline const vboolf8 operator > ( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_GT); }
  267. __forceinline const vboolf8 operator <=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps_mask(a, b, _MM_CMPINT_LE); }
  268. __forceinline const vfloat8 select( const vboolf8& m, const vfloat8& t, const vfloat8& f ) {
  269. return _mm256_mask_blend_ps(m, f, t);
  270. }
  271. #else
  272. __forceinline const vboolf8 operator ==( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_EQ_OQ ); }
  273. __forceinline const vboolf8 operator !=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_NEQ_OQ); }
  274. __forceinline const vboolf8 operator < ( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_LT_OQ ); }
  275. __forceinline const vboolf8 operator >=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_GE_OQ ); }
  276. __forceinline const vboolf8 operator > ( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_GT_OQ ); }
  277. __forceinline const vboolf8 operator <=( const vfloat8& a, const vfloat8& b ) { return _mm256_cmp_ps(a, b, _CMP_LE_OQ ); }
  278. __forceinline const vfloat8 select( const vboolf8& m, const vfloat8& t, const vfloat8& f ) {
  279. return _mm256_blendv_ps(f, t, m);
  280. }
  281. #endif
  282. template<int mask>
  283. __forceinline const vfloat8 select( const vfloat8& t, const vfloat8& f ) {
  284. return _mm256_blend_ps(f, t, mask);
  285. }
  286. __forceinline const vboolf8 operator ==( const vfloat8& a, const float& b ) { return a == vfloat8(b); }
  287. __forceinline const vboolf8 operator ==( const float& a, const vfloat8& b ) { return vfloat8(a) == b; }
  288. __forceinline const vboolf8 operator !=( const vfloat8& a, const float& b ) { return a != vfloat8(b); }
  289. __forceinline const vboolf8 operator !=( const float& a, const vfloat8& b ) { return vfloat8(a) != b; }
  290. __forceinline const vboolf8 operator < ( const vfloat8& a, const float& b ) { return a < vfloat8(b); }
  291. __forceinline const vboolf8 operator < ( const float& a, const vfloat8& b ) { return vfloat8(a) < b; }
  292. __forceinline const vboolf8 operator >=( const vfloat8& a, const float& b ) { return a >= vfloat8(b); }
  293. __forceinline const vboolf8 operator >=( const float& a, const vfloat8& b ) { return vfloat8(a) >= b; }
  294. __forceinline const vboolf8 operator > ( const vfloat8& a, const float& b ) { return a > vfloat8(b); }
  295. __forceinline const vboolf8 operator > ( const float& a, const vfloat8& b ) { return vfloat8(a) > b; }
  296. __forceinline const vboolf8 operator <=( const vfloat8& a, const float& b ) { return a <= vfloat8(b); }
  297. __forceinline const vboolf8 operator <=( const float& a, const vfloat8& b ) { return vfloat8(a) <= b; }
  298. __forceinline vboolf8 eq(const vfloat8& a, const vfloat8& b) { return a == b; }
  299. __forceinline vboolf8 ne(const vfloat8& a, const vfloat8& b) { return a != b; }
  300. __forceinline vboolf8 lt(const vfloat8& a, const vfloat8& b) { return a < b; }
  301. __forceinline vboolf8 ge(const vfloat8& a, const vfloat8& b) { return a >= b; }
  302. __forceinline vboolf8 gt(const vfloat8& a, const vfloat8& b) { return a > b; }
  303. __forceinline vboolf8 le(const vfloat8& a, const vfloat8& b) { return a <= b; }
  304. #if defined(__AVX512VL__)
  305. __forceinline vboolf8 eq(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_EQ); }
  306. __forceinline vboolf8 ne(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_NE); }
  307. __forceinline vboolf8 lt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LT); }
  308. __forceinline vboolf8 ge(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GE); }
  309. __forceinline vboolf8 gt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GT); }
  310. __forceinline vboolf8 le(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return _mm256_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LE); }
  311. #else
  312. __forceinline vboolf8 eq(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a == b); }
  313. __forceinline vboolf8 ne(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a != b); }
  314. __forceinline vboolf8 lt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a < b); }
  315. __forceinline vboolf8 ge(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a >= b); }
  316. __forceinline vboolf8 gt(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a > b); }
  317. __forceinline vboolf8 le(const vboolf8& mask, const vfloat8& a, const vfloat8& b) { return mask & (a <= b); }
  318. #endif
  319. __forceinline vfloat8 lerp(const vfloat8& a, const vfloat8& b, const vfloat8& t) {
  320. return madd(t,b-a,a);
  321. }
  322. __forceinline bool isvalid ( const vfloat8& v ) {
  323. return all((v > vfloat8(-FLT_LARGE)) & (v < vfloat8(+FLT_LARGE)));
  324. }
  325. __forceinline bool is_finite ( const vfloat8& a ) {
  326. return all((a >= vfloat8(-FLT_MAX)) & (a <= vfloat8(+FLT_MAX)));
  327. }
  328. __forceinline bool is_finite ( const vboolf8& valid, const vfloat8& a ) {
  329. return all(valid, (a >= vfloat8(-FLT_MAX)) & (a <= vfloat8(+FLT_MAX)));
  330. }
  331. ////////////////////////////////////////////////////////////////////////////////
  332. /// Rounding Functions
  333. ////////////////////////////////////////////////////////////////////////////////
  334. __forceinline const vfloat8 floor( const vfloat8& a ) { return _mm256_round_ps(a, _MM_FROUND_TO_NEG_INF ); }
  335. __forceinline const vfloat8 ceil ( const vfloat8& a ) { return _mm256_round_ps(a, _MM_FROUND_TO_POS_INF ); }
  336. __forceinline const vfloat8 trunc( const vfloat8& a ) { return _mm256_round_ps(a, _MM_FROUND_TO_ZERO ); }
  337. __forceinline const vfloat8 frac ( const vfloat8& a ) { return a-floor(a); }
  338. ////////////////////////////////////////////////////////////////////////////////
  339. /// Movement/Shifting/Shuffling Functions
  340. ////////////////////////////////////////////////////////////////////////////////
  341. __forceinline vfloat8 unpacklo( const vfloat8& a, const vfloat8& b ) { return _mm256_unpacklo_ps(a.v, b.v); }
  342. __forceinline vfloat8 unpackhi( const vfloat8& a, const vfloat8& b ) { return _mm256_unpackhi_ps(a.v, b.v); }
  343. template<size_t i> __forceinline const vfloat8 shuffle( const vfloat8& a ) {
  344. return _mm256_permute_ps(a, _MM_SHUFFLE(i, i, i, i));
  345. }
  346. template<size_t i0, size_t i1> __forceinline const vfloat8 shuffle4( const vfloat8& a ) {
  347. return _mm256_permute2f128_ps(a, a, (i1 << 4) | (i0 << 0));
  348. }
  349. template<size_t i0, size_t i1> __forceinline const vfloat8 shuffle4( const vfloat8& a, const vfloat8& b) {
  350. return _mm256_permute2f128_ps(a, b, (i1 << 4) | (i0 << 0));
  351. }
  352. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vfloat8 shuffle( const vfloat8& a ) {
  353. return _mm256_permute_ps(a, _MM_SHUFFLE(i3, i2, i1, i0));
  354. }
  355. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vfloat8 shuffle( const vfloat8& a, const vfloat8& b ) {
  356. return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  357. }
  358. template<> __forceinline const vfloat8 shuffle<0, 0, 2, 2>( const vfloat8& b ) { return _mm256_moveldup_ps(b); }
  359. template<> __forceinline const vfloat8 shuffle<1, 1, 3, 3>( const vfloat8& b ) { return _mm256_movehdup_ps(b); }
  360. template<> __forceinline const vfloat8 shuffle<0, 1, 0, 1>( const vfloat8& b ) { return _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(b))); }
  361. __forceinline const vfloat8 broadcast(const float* ptr) { return _mm256_broadcast_ss(ptr); }
  362. template<size_t i> __forceinline const vfloat8 insert4(const vfloat8& a, const vfloat4& b) { return _mm256_insertf128_ps(a, b, i); }
  363. template<size_t i> __forceinline const vfloat4 extract4 (const vfloat8& a) { return _mm256_extractf128_ps(a, i); }
  364. template<> __forceinline const vfloat4 extract4<0>(const vfloat8& a) { return _mm256_castps256_ps128(a); }
  365. __forceinline float toScalar(const vfloat8& a) { return _mm_cvtss_f32(_mm256_castps256_ps128(a)); }
  366. __forceinline vfloat8 assign( const vfloat4& a ) { return _mm256_castps128_ps256(a); }
  367. #if defined (__AVX2__)
  368. __forceinline vfloat8 permute(const vfloat8 &a, const __m256i &index) {
  369. return _mm256_permutevar8x32_ps(a,index);
  370. }
  371. #endif
  372. #if defined(__AVX512VL__)
  373. template<int i>
  374. __forceinline vfloat8 align_shift_right(const vfloat8 &a, const vfloat8 &b) {
  375. return _mm256_castsi256_ps(_mm256_alignr_epi32(_mm256_castps_si256(a), _mm256_castps_si256(b), i));
  376. }
  377. #endif
  378. #if defined (__AVX_I__)
  379. template<const int mode>
  380. __forceinline vint4 convert_to_hf16(const vfloat8 &a) {
  381. return _mm256_cvtps_ph(a,mode);
  382. }
  383. __forceinline vfloat8 convert_from_hf16(const vint4 &a) {
  384. return _mm256_cvtph_ps(a);
  385. }
  386. #endif
  387. __forceinline vfloat4 broadcast4f( const vfloat8& a, const size_t k ) {
  388. return vfloat4::broadcast(&a[k]);
  389. }
  390. __forceinline vfloat8 broadcast8f( const vfloat8& a, const size_t k ) {
  391. return vfloat8::broadcast(&a[k]);
  392. }
  393. #if defined(__AVX512VL__)
  394. __forceinline vfloat8 shift_right_1( const vfloat8& x) {
  395. return align_shift_right<1>(zero,x);
  396. }
  397. #else
  398. __forceinline vfloat8 shift_right_1( const vfloat8& x) {
  399. const vfloat8 t0 = shuffle<1,2,3,0>(x);
  400. const vfloat8 t1 = shuffle4<1,0>(t0);
  401. return _mm256_blend_ps(t0,t1,0x88);
  402. }
  403. #endif
  404. __forceinline vint8 floori (const vfloat8& a) {
  405. return vint8(floor(a));
  406. }
  407. ////////////////////////////////////////////////////////////////////////////////
  408. /// Transpose
  409. ////////////////////////////////////////////////////////////////////////////////
  410. __forceinline void transpose(const vfloat8& r0, const vfloat8& r1, const vfloat8& r2, const vfloat8& r3, vfloat8& c0, vfloat8& c1, vfloat8& c2, vfloat8& c3)
  411. {
  412. vfloat8 l02 = unpacklo(r0,r2);
  413. vfloat8 h02 = unpackhi(r0,r2);
  414. vfloat8 l13 = unpacklo(r1,r3);
  415. vfloat8 h13 = unpackhi(r1,r3);
  416. c0 = unpacklo(l02,l13);
  417. c1 = unpackhi(l02,l13);
  418. c2 = unpacklo(h02,h13);
  419. c3 = unpackhi(h02,h13);
  420. }
  421. __forceinline void transpose(const vfloat8& r0, const vfloat8& r1, const vfloat8& r2, const vfloat8& r3, vfloat8& c0, vfloat8& c1, vfloat8& c2)
  422. {
  423. vfloat8 l02 = unpacklo(r0,r2);
  424. vfloat8 h02 = unpackhi(r0,r2);
  425. vfloat8 l13 = unpacklo(r1,r3);
  426. vfloat8 h13 = unpackhi(r1,r3);
  427. c0 = unpacklo(l02,l13);
  428. c1 = unpackhi(l02,l13);
  429. c2 = unpacklo(h02,h13);
  430. }
  431. __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,
  432. vfloat8& c0, vfloat8& c1, vfloat8& c2, vfloat8& c3, vfloat8& c4, vfloat8& c5, vfloat8& c6, vfloat8& c7)
  433. {
  434. vfloat8 h0,h1,h2,h3; transpose(r0,r1,r2,r3,h0,h1,h2,h3);
  435. vfloat8 h4,h5,h6,h7; transpose(r4,r5,r6,r7,h4,h5,h6,h7);
  436. c0 = shuffle4<0,2>(h0,h4);
  437. c1 = shuffle4<0,2>(h1,h5);
  438. c2 = shuffle4<0,2>(h2,h6);
  439. c3 = shuffle4<0,2>(h3,h7);
  440. c4 = shuffle4<1,3>(h0,h4);
  441. c5 = shuffle4<1,3>(h1,h5);
  442. c6 = shuffle4<1,3>(h2,h6);
  443. c7 = shuffle4<1,3>(h3,h7);
  444. }
  445. __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,
  446. vfloat8& c0, vfloat8& c1, vfloat8& c2)
  447. {
  448. transpose(vfloat8(r0,r4), vfloat8(r1,r5), vfloat8(r2,r6), vfloat8(r3,r7), c0, c1, c2);
  449. }
  450. ////////////////////////////////////////////////////////////////////////////////
  451. /// Reductions
  452. ////////////////////////////////////////////////////////////////////////////////
  453. __forceinline const vfloat8 vreduce_min2(const vfloat8& v) { return min(v,shuffle<1,0,3,2>(v)); }
  454. __forceinline const vfloat8 vreduce_min4(const vfloat8& v) { vfloat8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
  455. __forceinline const vfloat8 vreduce_min (const vfloat8& v) { vfloat8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
  456. __forceinline const vfloat8 vreduce_max2(const vfloat8& v) { return max(v,shuffle<1,0,3,2>(v)); }
  457. __forceinline const vfloat8 vreduce_max4(const vfloat8& v) { vfloat8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
  458. __forceinline const vfloat8 vreduce_max (const vfloat8& v) { vfloat8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
  459. __forceinline const vfloat8 vreduce_add2(const vfloat8& v) { return v + shuffle<1,0,3,2>(v); }
  460. __forceinline const vfloat8 vreduce_add4(const vfloat8& v) { vfloat8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
  461. __forceinline const vfloat8 vreduce_add (const vfloat8& v) { vfloat8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
  462. __forceinline float reduce_min(const vfloat8& v) { return toScalar(vreduce_min(v)); }
  463. __forceinline float reduce_max(const vfloat8& v) { return toScalar(vreduce_max(v)); }
  464. __forceinline float reduce_add(const vfloat8& v) { return toScalar(vreduce_add(v)); }
  465. __forceinline size_t select_min(const vboolf8& valid, const vfloat8& v)
  466. {
  467. const vfloat8 a = select(valid,v,vfloat8(pos_inf));
  468. const vbool8 valid_min = valid & (a == vreduce_min(a));
  469. return __bsf(movemask(any(valid_min) ? valid_min : valid));
  470. }
  471. __forceinline size_t select_max(const vboolf8& valid, const vfloat8& v)
  472. {
  473. const vfloat8 a = select(valid,v,vfloat8(neg_inf));
  474. const vbool8 valid_max = valid & (a == vreduce_max(a));
  475. return __bsf(movemask(any(valid_max) ? valid_max : valid));
  476. }
  477. ////////////////////////////////////////////////////////////////////////////////
  478. /// Euclidian Space Operators (pairs of Vec3fa's)
  479. ////////////////////////////////////////////////////////////////////////////////
  480. //__forceinline vfloat8 dot ( const vfloat8& a, const vfloat8& b ) {
  481. // return vreduce_add4(a*b);
  482. //}
  483. __forceinline vfloat8 dot ( const vfloat8& a, const vfloat8& b ) {
  484. return _mm256_dp_ps(a,b,0x7F);
  485. }
  486. __forceinline vfloat8 cross ( const vfloat8& a, const vfloat8& b )
  487. {
  488. const vfloat8 a0 = a;
  489. const vfloat8 b0 = shuffle<1,2,0,3>(b);
  490. const vfloat8 a1 = shuffle<1,2,0,3>(a);
  491. const vfloat8 b1 = b;
  492. return shuffle<1,2,0,3>(msub(a0,b0,a1*b1));
  493. }
  494. //__forceinline float sqr_length ( const vfloat<8>& a ) { return dot(a,a); }
  495. //__forceinline float rcp_length ( const vfloat<8>& a ) { return rsqrt(dot(a,a)); }
  496. //__forceinline float rcp_length2( const vfloat<8>& a ) { return rcp(dot(a,a)); }
  497. //__forceinline float length ( const vfloat<8>& a ) { return sqrt(dot(a,a)); }
  498. __forceinline vfloat<8> normalize( const vfloat<8>& a ) { return a*rsqrt(dot(a,a)); }
  499. //__forceinline float distance ( const vfloat<8>& a, const vfloat<8>& b ) { return length(a-b); }
  500. //__forceinline float halfArea ( const vfloat<8>& d ) { return madd(d.x,(d.y+d.z),d.y*d.z); }
  501. //__forceinline float area ( const vfloat<8>& d ) { return 2.0f*halfArea(d); }
  502. //__forceinline vfloat<8> reflect ( const vfloat<8>& V, const vfloat<8>& N ) { return 2.0f*dot(V,N)*N-V; }
  503. //__forceinline vfloat<8> normalize_safe( const vfloat<8>& a ) {
  504. // const float d = dot(a,a); if (unlikely(d == 0.0f)) return a; else return a*rsqrt(d);
  505. //}
  506. ////////////////////////////////////////////////////////////////////////////////
  507. /// In Register Sorting
  508. ////////////////////////////////////////////////////////////////////////////////
  509. __forceinline vfloat8 sortNetwork(const vfloat8& v)
  510. {
  511. const vfloat8 a0 = v;
  512. const vfloat8 b0 = shuffle<1,0,3,2>(a0);
  513. const vfloat8 c0 = min(a0,b0);
  514. const vfloat8 d0 = max(a0,b0);
  515. const vfloat8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
  516. const vfloat8 b1 = shuffle<2,3,0,1>(a1);
  517. const vfloat8 c1 = min(a1,b1);
  518. const vfloat8 d1 = max(a1,b1);
  519. const vfloat8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
  520. const vfloat8 b2 = shuffle<1,0,3,2>(a2);
  521. const vfloat8 c2 = min(a2,b2);
  522. const vfloat8 d2 = max(a2,b2);
  523. const vfloat8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
  524. const vfloat8 b3 = shuffle4<1,0>(a3);
  525. const vfloat8 c3 = min(a3,b3);
  526. const vfloat8 d3 = max(a3,b3);
  527. const vfloat8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
  528. const vfloat8 b4 = shuffle<2,3,0,1>(a4);
  529. const vfloat8 c4 = min(a4,b4);
  530. const vfloat8 d4 = max(a4,b4);
  531. const vfloat8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
  532. const vfloat8 b5 = shuffle<1,0,3,2>(a5);
  533. const vfloat8 c5 = min(a5,b5);
  534. const vfloat8 d5 = max(a5,b5);
  535. const vfloat8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
  536. return a6;
  537. }
  538. ////////////////////////////////////////////////////////////////////////////////
  539. /// Output Operators
  540. ////////////////////////////////////////////////////////////////////////////////
  541. inline std::ostream& operator<<(std::ostream& cout, const vfloat8& a) {
  542. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  543. }
  544. }