vfloat4_sse2.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. /* 4-wide SSE float type */
  14. template<>
  15. struct vfloat<4>
  16. {
  17. ALIGNED_STRUCT_(16);
  18. typedef vboolf4 Bool;
  19. typedef vint4 Int;
  20. typedef vfloat4 Float;
  21. enum { size = 4 }; // number of SIMD elements
  22. union { __m128 v; float f[4]; int i[4]; }; // data
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// Constructors, Assignment & Cast Operators
  25. ////////////////////////////////////////////////////////////////////////////////
  26. __forceinline vfloat() {}
  27. __forceinline vfloat(const vfloat4& other) { v = other.v; }
  28. //__forceinline vfloat(const vfloat4& other) = default;
  29. __forceinline vfloat4& operator =(const vfloat4& other) { v = other.v; return *this; }
  30. __forceinline vfloat(__m128 a) : v(a) {}
  31. __forceinline operator const __m128&() const { return v; }
  32. __forceinline operator __m128&() { return v; }
  33. __forceinline vfloat(float a) : v(_mm_set1_ps(a)) {}
  34. __forceinline vfloat(float a, float b, float c, float d) : v(_mm_set_ps(d, c, b, a)) {}
  35. __forceinline explicit vfloat(const vint4& a) : v(_mm_cvtepi32_ps(a)) {}
  36. #if defined(__aarch64__)
  37. __forceinline explicit vfloat(const vuint4& x) {
  38. v = vcvtq_f32_u32(vreinterpretq_u32_s32(x.v));
  39. }
  40. #else
  41. __forceinline explicit vfloat(const vuint4& x) {
  42. const __m128i a = _mm_and_si128(x,_mm_set1_epi32(0x7FFFFFFF));
  43. const __m128i b = _mm_and_si128(_mm_srai_epi32(x,31),_mm_set1_epi32(0x4F000000)); //0x4F000000 = 2^31
  44. const __m128 af = _mm_cvtepi32_ps(a);
  45. const __m128 bf = _mm_castsi128_ps(b);
  46. v = _mm_add_ps(af,bf);
  47. }
  48. #endif
  49. ////////////////////////////////////////////////////////////////////////////////
  50. /// Constants
  51. ////////////////////////////////////////////////////////////////////////////////
  52. __forceinline vfloat(ZeroTy) : v(_mm_setzero_ps()) {}
  53. __forceinline vfloat(OneTy) : v(_mm_set1_ps(1.0f)) {}
  54. __forceinline vfloat(PosInfTy) : v(_mm_set1_ps(pos_inf)) {}
  55. __forceinline vfloat(NegInfTy) : v(_mm_set1_ps(neg_inf)) {}
  56. __forceinline vfloat(StepTy) : v(_mm_set_ps(3.0f, 2.0f, 1.0f, 0.0f)) {}
  57. __forceinline vfloat(NaNTy) : v(_mm_set1_ps(nan)) {}
  58. __forceinline vfloat(UndefinedTy) : v(_mm_undefined_ps()) {}
  59. ////////////////////////////////////////////////////////////////////////////////
  60. /// Loads and Stores
  61. ////////////////////////////////////////////////////////////////////////////////
  62. static __forceinline vfloat4 load (const void* a) { return _mm_load_ps((float*)a); }
  63. static __forceinline vfloat4 loadu(const void* a) { return _mm_loadu_ps((float*)a); }
  64. static __forceinline void store (void* ptr, const vfloat4& v) { _mm_store_ps((float*)ptr,v); }
  65. static __forceinline void storeu(void* ptr, const vfloat4& v) { _mm_storeu_ps((float*)ptr,v); }
  66. #if defined(__AVX512VL__)
  67. static __forceinline vfloat4 load (const vboolf4& mask, const void* ptr) { return _mm_mask_load_ps (_mm_setzero_ps(),mask,(float*)ptr); }
  68. static __forceinline vfloat4 loadu(const vboolf4& mask, const void* ptr) { return _mm_mask_loadu_ps(_mm_setzero_ps(),mask,(float*)ptr); }
  69. static __forceinline void store (const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_mask_store_ps ((float*)ptr,mask,v); }
  70. static __forceinline void storeu(const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_mask_storeu_ps((float*)ptr,mask,v); }
  71. #elif defined(__AVX__)
  72. static __forceinline vfloat4 load (const vboolf4& mask, const void* ptr) { return _mm_maskload_ps((float*)ptr,mask); }
  73. static __forceinline vfloat4 loadu(const vboolf4& mask, const void* ptr) { return _mm_maskload_ps((float*)ptr,mask); }
  74. static __forceinline void store (const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,v); }
  75. static __forceinline void storeu(const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,v); }
  76. #else
  77. static __forceinline vfloat4 load (const vboolf4& mask, const void* ptr) { return _mm_and_ps(_mm_load_ps ((float*)ptr),mask); }
  78. static __forceinline vfloat4 loadu(const vboolf4& mask, const void* ptr) { return _mm_and_ps(_mm_loadu_ps((float*)ptr),mask); }
  79. static __forceinline void store (const vboolf4& mask, void* ptr, const vfloat4& v) { store (ptr,select(mask,v,load (ptr))); }
  80. static __forceinline void storeu(const vboolf4& mask, void* ptr, const vfloat4& v) { storeu(ptr,select(mask,v,loadu(ptr))); }
  81. #endif
  82. #if defined(__AVX__)
  83. static __forceinline vfloat4 broadcast(const void* a) { return _mm_broadcast_ss((float*)a); }
  84. #else
  85. static __forceinline vfloat4 broadcast(const void* a) { return _mm_set1_ps(*(float*)a); }
  86. #endif
  87. static __forceinline vfloat4 load_nt (const float* ptr) {
  88. #if defined (__SSE4_1__)
  89. return _mm_castsi128_ps(_mm_stream_load_si128((__m128i*)ptr));
  90. #else
  91. return _mm_load_ps(ptr);
  92. #endif
  93. }
  94. #if defined(__aarch64__)
  95. static __forceinline vfloat4 load(const char* ptr) {
  96. return __m128(_mm_load4epi8_f32(((__m128i*)ptr)));
  97. }
  98. #elif defined(__SSE4_1__)
  99. static __forceinline vfloat4 load(const char* ptr) {
  100. return _mm_cvtepi32_ps(_mm_cvtepi8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  101. }
  102. #else
  103. static __forceinline vfloat4 load(const char* ptr) {
  104. return vfloat4(ptr[0],ptr[1],ptr[2],ptr[3]);
  105. }
  106. #endif
  107. #if defined(__aarch64__)
  108. static __forceinline vfloat4 load(const unsigned char* ptr) {
  109. return __m128(_mm_load4epu8_f32(((__m128i*)ptr)));
  110. }
  111. #elif defined(__SSE4_1__)
  112. static __forceinline vfloat4 load(const unsigned char* ptr) {
  113. return _mm_cvtepi32_ps(_mm_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  114. }
  115. #else
  116. static __forceinline vfloat4 load(const unsigned char* ptr) {
  117. //return _mm_cvtpu8_ps(*(__m64*)ptr); // don't enable, will use MMX instructions
  118. return vfloat4(ptr[0],ptr[1],ptr[2],ptr[3]);
  119. }
  120. #endif
  121. #if defined(__aarch64__)
  122. static __forceinline vfloat4 load(const short* ptr) {
  123. return __m128(_mm_load4epi16_f32(((__m128i*)ptr)));
  124. }
  125. #elif defined(__SSE4_1__)
  126. static __forceinline vfloat4 load(const short* ptr) {
  127. return _mm_cvtepi32_ps(_mm_cvtepi16_epi32(_mm_loadu_si128((__m128i*)ptr)));
  128. }
  129. #else
  130. static __forceinline vfloat4 load(const short* ptr) {
  131. return vfloat4(ptr[0],ptr[1],ptr[2],ptr[3]);
  132. }
  133. #endif
  134. static __forceinline vfloat4 load(const unsigned short* ptr) {
  135. return _mm_mul_ps(vfloat4(vint4::load(ptr)),vfloat4(1.0f/65535.0f));
  136. }
  137. static __forceinline void store_nt(void* ptr, const vfloat4& v)
  138. {
  139. #if defined (__SSE4_1__)
  140. #if defined(__aarch64__)
  141. _mm_stream_ps((float*)ptr,v);
  142. #else
  143. _mm_stream_ps((float*)ptr,v);
  144. #endif
  145. #else
  146. _mm_store_ps((float*)ptr,v);
  147. #endif
  148. }
  149. template<int scale = 4>
  150. static __forceinline vfloat4 gather(const float* ptr, const vint4& index) {
  151. #if defined(__AVX2__) && !defined(__aarch64__)
  152. return _mm_i32gather_ps(ptr, index, scale);
  153. #else
  154. return vfloat4(
  155. *(float*)(((char*)ptr)+scale*index[0]),
  156. *(float*)(((char*)ptr)+scale*index[1]),
  157. *(float*)(((char*)ptr)+scale*index[2]),
  158. *(float*)(((char*)ptr)+scale*index[3]));
  159. #endif
  160. }
  161. template<int scale = 4>
  162. static __forceinline vfloat4 gather(const vboolf4& mask, const float* ptr, const vint4& index) {
  163. vfloat4 r = zero;
  164. #if defined(__AVX512VL__)
  165. return _mm_mmask_i32gather_ps(r, mask, index, ptr, scale);
  166. #elif defined(__AVX2__) && !defined(__aarch64__)
  167. return _mm_mask_i32gather_ps(r, ptr, index, mask, scale);
  168. #else
  169. if (likely(mask[0])) r[0] = *(float*)(((char*)ptr)+scale*index[0]);
  170. if (likely(mask[1])) r[1] = *(float*)(((char*)ptr)+scale*index[1]);
  171. if (likely(mask[2])) r[2] = *(float*)(((char*)ptr)+scale*index[2]);
  172. if (likely(mask[3])) r[3] = *(float*)(((char*)ptr)+scale*index[3]);
  173. return r;
  174. #endif
  175. }
  176. template<int scale = 4>
  177. static __forceinline void scatter(void* ptr, const vint4& index, const vfloat4& v)
  178. {
  179. #if defined(__AVX512VL__)
  180. _mm_i32scatter_ps((float*)ptr, index, v, scale);
  181. #else
  182. *(float*)(((char*)ptr)+scale*index[0]) = v[0];
  183. *(float*)(((char*)ptr)+scale*index[1]) = v[1];
  184. *(float*)(((char*)ptr)+scale*index[2]) = v[2];
  185. *(float*)(((char*)ptr)+scale*index[3]) = v[3];
  186. #endif
  187. }
  188. template<int scale = 4>
  189. static __forceinline void scatter(const vboolf4& mask, void* ptr, const vint4& index, const vfloat4& v)
  190. {
  191. #if defined(__AVX512VL__)
  192. _mm_mask_i32scatter_ps((float*)ptr ,mask, index, v, scale);
  193. #else
  194. if (likely(mask[0])) *(float*)(((char*)ptr)+scale*index[0]) = v[0];
  195. if (likely(mask[1])) *(float*)(((char*)ptr)+scale*index[1]) = v[1];
  196. if (likely(mask[2])) *(float*)(((char*)ptr)+scale*index[2]) = v[2];
  197. if (likely(mask[3])) *(float*)(((char*)ptr)+scale*index[3]) = v[3];
  198. #endif
  199. }
  200. static __forceinline void store(const vboolf4& mask, char* ptr, const vint4& ofs, const vfloat4& v) {
  201. scatter<1>(mask,ptr,ofs,v);
  202. }
  203. static __forceinline void store(const vboolf4& mask, float* ptr, const vint4& ofs, const vfloat4& v) {
  204. scatter<4>(mask,ptr,ofs,v);
  205. }
  206. ////////////////////////////////////////////////////////////////////////////////
  207. /// Array Access
  208. ////////////////////////////////////////////////////////////////////////////////
  209. __forceinline const float& operator [](size_t index) const { assert(index < 4); return f[index]; }
  210. __forceinline float& operator [](size_t index) { assert(index < 4); return f[index]; }
  211. friend __forceinline vfloat4 select(const vboolf4& m, const vfloat4& t, const vfloat4& f) {
  212. #if defined(__AVX512VL__)
  213. return _mm_mask_blend_ps(m, f, t);
  214. #elif defined(__SSE4_1__) || (defined(__aarch64__))
  215. return _mm_blendv_ps(f, t, m);
  216. #else
  217. return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
  218. #endif
  219. }
  220. };
  221. ////////////////////////////////////////////////////////////////////////////////
  222. /// Load/Store
  223. ////////////////////////////////////////////////////////////////////////////////
  224. template<> struct mem<vfloat4>
  225. {
  226. static __forceinline vfloat4 load (const vboolf4& mask, const void* ptr) { return vfloat4::load (mask,ptr); }
  227. static __forceinline vfloat4 loadu(const vboolf4& mask, const void* ptr) { return vfloat4::loadu(mask,ptr); }
  228. static __forceinline void store (const vboolf4& mask, void* ptr, const vfloat4& v) { vfloat4::store (mask,ptr,v); }
  229. static __forceinline void storeu(const vboolf4& mask, void* ptr, const vfloat4& v) { vfloat4::storeu(mask,ptr,v); }
  230. };
  231. ////////////////////////////////////////////////////////////////////////////////
  232. /// Unary Operators
  233. ////////////////////////////////////////////////////////////////////////////////
  234. __forceinline vfloat4 asFloat(const vint4& a) { return _mm_castsi128_ps(a); }
  235. __forceinline vint4 asInt (const vfloat4& a) { return _mm_castps_si128(a); }
  236. __forceinline vuint4 asUInt (const vfloat4& a) { return _mm_castps_si128(a); }
  237. __forceinline vint4 toInt (const vfloat4& a) { return vint4(a); }
  238. __forceinline vfloat4 toFloat(const vint4& a) { return vfloat4(a); }
  239. __forceinline vfloat4 operator +(const vfloat4& a) { return a; }
  240. #if defined(__aarch64__)
  241. __forceinline vfloat4 operator -(const vfloat4& a) {
  242. return vnegq_f32(a);
  243. }
  244. #else
  245. __forceinline vfloat4 operator -(const vfloat4& a) { return _mm_xor_ps(a, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))); }
  246. #endif
  247. #if defined(__aarch64__)
  248. __forceinline vfloat4 abs(const vfloat4& a) { return _mm_abs_ps(a); }
  249. #else
  250. __forceinline vfloat4 abs(const vfloat4& a) { return _mm_and_ps(a, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))); }
  251. #endif
  252. #if defined(__AVX512VL__)
  253. __forceinline vfloat4 sign(const vfloat4& a) { return _mm_mask_blend_ps(_mm_cmp_ps_mask(a, vfloat4(zero), _CMP_LT_OQ), vfloat4(one), -vfloat4(one)); }
  254. #else
  255. __forceinline vfloat4 sign(const vfloat4& a) { return blendv_ps(vfloat4(one), -vfloat4(one), _mm_cmplt_ps(a, vfloat4(zero))); }
  256. #endif
  257. __forceinline vfloat4 signmsk(const vfloat4& a) { return _mm_and_ps(a,_mm_castsi128_ps(_mm_set1_epi32(0x80000000))); }
  258. __forceinline vfloat4 rcp(const vfloat4& a)
  259. {
  260. #if defined(__aarch64__)
  261. return vfloat4(vdivq_f32(vdupq_n_f32(1.0f),a.v));
  262. #else
  263. #if defined(__AVX512VL__)
  264. const vfloat4 r = _mm_rcp14_ps(a);
  265. #else
  266. const vfloat4 r = _mm_rcp_ps(a);
  267. #endif
  268. #if defined(__AVX2__)
  269. return _mm_fmadd_ps(r, _mm_fnmadd_ps(a, r, vfloat4(1.0f)), r); // computes r + r * (1 - a * r)
  270. #else
  271. return _mm_add_ps(r,_mm_mul_ps(r, _mm_sub_ps(vfloat4(1.0f), _mm_mul_ps(a, r)))); // computes r + r * (1 - a * r)
  272. #endif
  273. #endif //defined(__aarch64__)
  274. }
  275. __forceinline vfloat4 sqr (const vfloat4& a) { return _mm_mul_ps(a,a); }
  276. __forceinline vfloat4 sqrt(const vfloat4& a) { return _mm_sqrt_ps(a); }
  277. __forceinline vfloat4 rsqrt(const vfloat4& a)
  278. {
  279. #if defined(__aarch64__)
  280. vfloat4 r = _mm_rsqrt_ps(a);
  281. r = vmulq_f32(r, vrsqrtsq_f32(vmulq_f32(a, r), r));
  282. r = vmulq_f32(r, vrsqrtsq_f32(vmulq_f32(a, r), r));
  283. r = vmulq_f32(r, vrsqrtsq_f32(vmulq_f32(a, r), r));
  284. return r;
  285. #else
  286. #if defined(__AVX512VL__)
  287. vfloat4 r = _mm_rsqrt14_ps(a);
  288. #else
  289. vfloat4 r = _mm_rsqrt_ps(a);
  290. #endif
  291. #if defined(__AVX2__)
  292. r = _mm_fmadd_ps(_mm_set1_ps(1.5f), r, _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
  293. #else
  294. r = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.5f), r), _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
  295. #endif
  296. #endif
  297. return r;
  298. }
  299. __forceinline vboolf4 isnan(const vfloat4& a) {
  300. const vfloat4 b = _mm_and_ps(a, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)));
  301. #if defined(__AVX512VL__)
  302. return _mm_cmp_epi32_mask(_mm_castps_si128(b), _mm_set1_epi32(0x7f800000), _MM_CMPINT_GT);
  303. #else
  304. return _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_castps_si128(b), _mm_set1_epi32(0x7f800000)));
  305. #endif
  306. }
  307. ////////////////////////////////////////////////////////////////////////////////
  308. /// Binary Operators
  309. ////////////////////////////////////////////////////////////////////////////////
  310. __forceinline vfloat4 operator +(const vfloat4& a, const vfloat4& b) { return _mm_add_ps(a, b); }
  311. __forceinline vfloat4 operator +(const vfloat4& a, float b) { return a + vfloat4(b); }
  312. __forceinline vfloat4 operator +(float a, const vfloat4& b) { return vfloat4(a) + b; }
  313. __forceinline vfloat4 operator -(const vfloat4& a, const vfloat4& b) { return _mm_sub_ps(a, b); }
  314. __forceinline vfloat4 operator -(const vfloat4& a, float b) { return a - vfloat4(b); }
  315. __forceinline vfloat4 operator -(float a, const vfloat4& b) { return vfloat4(a) - b; }
  316. __forceinline vfloat4 operator *(const vfloat4& a, const vfloat4& b) { return _mm_mul_ps(a, b); }
  317. __forceinline vfloat4 operator *(const vfloat4& a, float b) { return a * vfloat4(b); }
  318. __forceinline vfloat4 operator *(float a, const vfloat4& b) { return vfloat4(a) * b; }
  319. __forceinline vfloat4 operator /(const vfloat4& a, const vfloat4& b) { return _mm_div_ps(a,b); }
  320. __forceinline vfloat4 operator /(const vfloat4& a, float b) { return a/vfloat4(b); }
  321. __forceinline vfloat4 operator /(float a, const vfloat4& b) { return vfloat4(a)/b; }
  322. __forceinline vfloat4 operator &(const vfloat4& a, const vfloat4& b) { return _mm_and_ps(a,b); }
  323. __forceinline vfloat4 operator |(const vfloat4& a, const vfloat4& b) { return _mm_or_ps(a,b); }
  324. __forceinline vfloat4 operator ^(const vfloat4& a, const vfloat4& b) { return _mm_xor_ps(a,b); }
  325. __forceinline vfloat4 operator ^(const vfloat4& a, const vint4& b) { return _mm_xor_ps(a,_mm_castsi128_ps(b)); }
  326. __forceinline vfloat4 min(const vfloat4& a, const vfloat4& b) { return _mm_min_ps(a,b); }
  327. __forceinline vfloat4 min(const vfloat4& a, float b) { return _mm_min_ps(a,vfloat4(b)); }
  328. __forceinline vfloat4 min(float a, const vfloat4& b) { return _mm_min_ps(vfloat4(a),b); }
  329. __forceinline vfloat4 max(const vfloat4& a, const vfloat4& b) { return _mm_max_ps(a,b); }
  330. __forceinline vfloat4 max(const vfloat4& a, float b) { return _mm_max_ps(a,vfloat4(b)); }
  331. __forceinline vfloat4 max(float a, const vfloat4& b) { return _mm_max_ps(vfloat4(a),b); }
  332. #if defined(__SSE4_1__) || defined(__aarch64__)
  333. __forceinline vfloat4 mini(const vfloat4& a, const vfloat4& b) {
  334. const vint4 ai = _mm_castps_si128(a);
  335. const vint4 bi = _mm_castps_si128(b);
  336. const vint4 ci = _mm_min_epi32(ai,bi);
  337. return _mm_castsi128_ps(ci);
  338. }
  339. __forceinline vfloat4 maxi(const vfloat4& a, const vfloat4& b) {
  340. const vint4 ai = _mm_castps_si128(a);
  341. const vint4 bi = _mm_castps_si128(b);
  342. const vint4 ci = _mm_max_epi32(ai,bi);
  343. return _mm_castsi128_ps(ci);
  344. }
  345. __forceinline vfloat4 minui(const vfloat4& a, const vfloat4& b) {
  346. const vint4 ai = _mm_castps_si128(a);
  347. const vint4 bi = _mm_castps_si128(b);
  348. const vint4 ci = _mm_min_epu32(ai,bi);
  349. return _mm_castsi128_ps(ci);
  350. }
  351. __forceinline vfloat4 maxui(const vfloat4& a, const vfloat4& b) {
  352. const vint4 ai = _mm_castps_si128(a);
  353. const vint4 bi = _mm_castps_si128(b);
  354. const vint4 ci = _mm_max_epu32(ai,bi);
  355. return _mm_castsi128_ps(ci);
  356. }
  357. #else
  358. __forceinline vfloat4 mini(const vfloat4& a, const vfloat4& b) {
  359. return min(a,b);
  360. }
  361. __forceinline vfloat4 maxi(const vfloat4& a, const vfloat4& b) {
  362. return max(a,b);
  363. }
  364. #endif
  365. ////////////////////////////////////////////////////////////////////////////////
  366. /// Ternary Operators
  367. ////////////////////////////////////////////////////////////////////////////////
  368. #if defined(__AVX2__) || defined(__ARM_NEON)
  369. __forceinline vfloat4 madd (const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fmadd_ps(a,b,c); }
  370. __forceinline vfloat4 msub (const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fmsub_ps(a,b,c); }
  371. __forceinline vfloat4 nmadd(const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fnmadd_ps(a,b,c); }
  372. __forceinline vfloat4 nmsub(const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fnmsub_ps(a,b,c); }
  373. #else
  374. __forceinline vfloat4 madd (const vfloat4& a, const vfloat4& b, const vfloat4& c) { return a*b+c; }
  375. __forceinline vfloat4 nmadd(const vfloat4& a, const vfloat4& b, const vfloat4& c) { return -a*b+c;}
  376. __forceinline vfloat4 nmsub(const vfloat4& a, const vfloat4& b, const vfloat4& c) { return -a*b-c; }
  377. __forceinline vfloat4 msub (const vfloat4& a, const vfloat4& b, const vfloat4& c) { return a*b-c; }
  378. #endif
  379. ////////////////////////////////////////////////////////////////////////////////
  380. /// Assignment Operators
  381. ////////////////////////////////////////////////////////////////////////////////
  382. __forceinline vfloat4& operator +=(vfloat4& a, const vfloat4& b) { return a = a + b; }
  383. __forceinline vfloat4& operator +=(vfloat4& a, float b) { return a = a + b; }
  384. __forceinline vfloat4& operator -=(vfloat4& a, const vfloat4& b) { return a = a - b; }
  385. __forceinline vfloat4& operator -=(vfloat4& a, float b) { return a = a - b; }
  386. __forceinline vfloat4& operator *=(vfloat4& a, const vfloat4& b) { return a = a * b; }
  387. __forceinline vfloat4& operator *=(vfloat4& a, float b) { return a = a * b; }
  388. __forceinline vfloat4& operator /=(vfloat4& a, const vfloat4& b) { return a = a / b; }
  389. __forceinline vfloat4& operator /=(vfloat4& a, float b) { return a = a / b; }
  390. ////////////////////////////////////////////////////////////////////////////////
  391. /// Comparison Operators + Select
  392. ////////////////////////////////////////////////////////////////////////////////
  393. #if defined(__AVX512VL__)
  394. __forceinline vboolf4 operator ==(const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_EQ); }
  395. __forceinline vboolf4 operator !=(const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_NE); }
  396. __forceinline vboolf4 operator < (const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_LT); }
  397. __forceinline vboolf4 operator >=(const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_GE); }
  398. __forceinline vboolf4 operator > (const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_GT); }
  399. __forceinline vboolf4 operator <=(const vfloat4& a, const vfloat4& b) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_LE); }
  400. #else
  401. __forceinline vboolf4 operator ==(const vfloat4& a, const vfloat4& b) { return _mm_cmpeq_ps (a, b); }
  402. __forceinline vboolf4 operator !=(const vfloat4& a, const vfloat4& b) { return _mm_cmpneq_ps(a, b); }
  403. __forceinline vboolf4 operator < (const vfloat4& a, const vfloat4& b) { return _mm_cmplt_ps (a, b); }
  404. #if defined(__aarch64__)
  405. __forceinline vboolf4 operator >=(const vfloat4& a, const vfloat4& b) { return _mm_cmpge_ps (a, b); }
  406. __forceinline vboolf4 operator > (const vfloat4& a, const vfloat4& b) { return _mm_cmpgt_ps (a, b); }
  407. #else
  408. __forceinline vboolf4 operator >=(const vfloat4& a, const vfloat4& b) { return _mm_cmpnlt_ps(a, b); }
  409. __forceinline vboolf4 operator > (const vfloat4& a, const vfloat4& b) { return _mm_cmpnle_ps(a, b); }
  410. #endif
  411. __forceinline vboolf4 operator <=(const vfloat4& a, const vfloat4& b) { return _mm_cmple_ps (a, b); }
  412. #endif
  413. __forceinline vboolf4 operator ==(const vfloat4& a, float b) { return a == vfloat4(b); }
  414. __forceinline vboolf4 operator ==(float a, const vfloat4& b) { return vfloat4(a) == b; }
  415. __forceinline vboolf4 operator !=(const vfloat4& a, float b) { return a != vfloat4(b); }
  416. __forceinline vboolf4 operator !=(float a, const vfloat4& b) { return vfloat4(a) != b; }
  417. __forceinline vboolf4 operator < (const vfloat4& a, float b) { return a < vfloat4(b); }
  418. __forceinline vboolf4 operator < (float a, const vfloat4& b) { return vfloat4(a) < b; }
  419. __forceinline vboolf4 operator >=(const vfloat4& a, float b) { return a >= vfloat4(b); }
  420. __forceinline vboolf4 operator >=(float a, const vfloat4& b) { return vfloat4(a) >= b; }
  421. __forceinline vboolf4 operator > (const vfloat4& a, float b) { return a > vfloat4(b); }
  422. __forceinline vboolf4 operator > (float a, const vfloat4& b) { return vfloat4(a) > b; }
  423. __forceinline vboolf4 operator <=(const vfloat4& a, float b) { return a <= vfloat4(b); }
  424. __forceinline vboolf4 operator <=(float a, const vfloat4& b) { return vfloat4(a) <= b; }
  425. __forceinline vboolf4 eq(const vfloat4& a, const vfloat4& b) { return a == b; }
  426. __forceinline vboolf4 ne(const vfloat4& a, const vfloat4& b) { return a != b; }
  427. __forceinline vboolf4 lt(const vfloat4& a, const vfloat4& b) { return a < b; }
  428. __forceinline vboolf4 ge(const vfloat4& a, const vfloat4& b) { return a >= b; }
  429. __forceinline vboolf4 gt(const vfloat4& a, const vfloat4& b) { return a > b; }
  430. __forceinline vboolf4 le(const vfloat4& a, const vfloat4& b) { return a <= b; }
  431. #if defined(__AVX512VL__)
  432. __forceinline vboolf4 eq(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_EQ); }
  433. __forceinline vboolf4 ne(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_NE); }
  434. __forceinline vboolf4 lt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LT); }
  435. __forceinline vboolf4 ge(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GE); }
  436. __forceinline vboolf4 gt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GT); }
  437. __forceinline vboolf4 le(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LE); }
  438. #else
  439. __forceinline vboolf4 eq(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a == b); }
  440. __forceinline vboolf4 ne(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a != b); }
  441. __forceinline vboolf4 lt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a < b); }
  442. __forceinline vboolf4 ge(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a >= b); }
  443. __forceinline vboolf4 gt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a > b); }
  444. __forceinline vboolf4 le(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a <= b); }
  445. #endif
  446. template<int mask>
  447. __forceinline vfloat4 select(const vfloat4& t, const vfloat4& f)
  448. {
  449. #if defined(__SSE4_1__)
  450. return _mm_blend_ps(f, t, mask);
  451. #else
  452. return select(vboolf4(mask), t, f);
  453. #endif
  454. }
  455. __forceinline vfloat4 lerp(const vfloat4& a, const vfloat4& b, const vfloat4& t) {
  456. return madd(t,b-a,a);
  457. }
  458. __forceinline bool isvalid(const vfloat4& v) {
  459. return all((v > vfloat4(-FLT_LARGE)) & (v < vfloat4(+FLT_LARGE)));
  460. }
  461. __forceinline bool is_finite(const vfloat4& a) {
  462. return all((a >= vfloat4(-FLT_MAX)) & (a <= vfloat4(+FLT_MAX)));
  463. }
  464. __forceinline bool is_finite(const vboolf4& valid, const vfloat4& a) {
  465. return all(valid, (a >= vfloat4(-FLT_MAX)) & (a <= vfloat4(+FLT_MAX)));
  466. }
  467. ////////////////////////////////////////////////////////////////////////////////
  468. /// Rounding Functions
  469. ////////////////////////////////////////////////////////////////////////////////
  470. #if defined(__aarch64__)
  471. __forceinline vfloat4 floor(const vfloat4& a) { return vrndmq_f32(a.v); } // towards -inf
  472. __forceinline vfloat4 ceil (const vfloat4& a) { return vrndpq_f32(a.v); } // toward +inf
  473. __forceinline vfloat4 trunc(const vfloat4& a) { return vrndq_f32(a.v); } // towards 0
  474. __forceinline vfloat4 round(const vfloat4& a) { return vrndnq_f32(a.v); } // to nearest, ties to even. NOTE(LTE): arm clang uses vrndnq, old gcc uses vrndqn?
  475. #elif defined (__SSE4_1__)
  476. __forceinline vfloat4 floor(const vfloat4& a) { return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF ); }
  477. __forceinline vfloat4 ceil (const vfloat4& a) { return _mm_round_ps(a, _MM_FROUND_TO_POS_INF ); }
  478. __forceinline vfloat4 trunc(const vfloat4& a) { return _mm_round_ps(a, _MM_FROUND_TO_ZERO ); }
  479. __forceinline vfloat4 round(const vfloat4& a) { return _mm_round_ps(a, _MM_FROUND_TO_NEAREST_INT); }
  480. #else
  481. __forceinline vfloat4 floor(const vfloat4& a) { return vfloat4(floorf(a[0]),floorf(a[1]),floorf(a[2]),floorf(a[3])); }
  482. __forceinline vfloat4 ceil (const vfloat4& a) { return vfloat4(ceilf (a[0]),ceilf (a[1]),ceilf (a[2]),ceilf (a[3])); }
  483. __forceinline vfloat4 trunc(const vfloat4& a) { return vfloat4(truncf(a[0]),truncf(a[1]),truncf(a[2]),truncf(a[3])); }
  484. __forceinline vfloat4 round(const vfloat4& a) { return vfloat4(roundf(a[0]),roundf(a[1]),roundf(a[2]),roundf(a[3])); }
  485. #endif
  486. __forceinline vfloat4 frac(const vfloat4& a) { return a-floor(a); }
  487. __forceinline vint4 floori(const vfloat4& a) {
  488. #if defined(__aarch64__)
  489. return vcvtq_s32_f32(floor(a));
  490. #elif defined(__SSE4_1__)
  491. return vint4(floor(a));
  492. #else
  493. return vint4(a-vfloat4(0.5f));
  494. #endif
  495. }
  496. ////////////////////////////////////////////////////////////////////////////////
  497. /// Movement/Shifting/Shuffling Functions
  498. ////////////////////////////////////////////////////////////////////////////////
  499. __forceinline vfloat4 unpacklo(const vfloat4& a, const vfloat4& b) { return _mm_unpacklo_ps(a, b); }
  500. __forceinline vfloat4 unpackhi(const vfloat4& a, const vfloat4& b) { return _mm_unpackhi_ps(a, b); }
  501. #if defined(__aarch64__)
  502. template<int i0, int i1, int i2, int i3>
  503. __forceinline vfloat4 shuffle(const vfloat4& v) {
  504. return vreinterpretq_f32_u8(vqtbl1q_u8( (uint8x16_t)v.v, _MN_SHUFFLE(i0, i1, i2, i3)));
  505. }
  506. template<int i0, int i1, int i2, int i3>
  507. __forceinline vfloat4 shuffle(const vfloat4& a, const vfloat4& b) {
  508. return vreinterpretq_f32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));
  509. }
  510. #else
  511. template<int i0, int i1, int i2, int i3>
  512. __forceinline vfloat4 shuffle(const vfloat4& v) {
  513. return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v), _MM_SHUFFLE(i3, i2, i1, i0)));
  514. }
  515. template<int i0, int i1, int i2, int i3>
  516. __forceinline vfloat4 shuffle(const vfloat4& a, const vfloat4& b) {
  517. return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  518. }
  519. #endif
  520. #if defined(__SSE3__) && !defined(__aarch64__)
  521. template<> __forceinline vfloat4 shuffle<0, 0, 2, 2>(const vfloat4& v) { return _mm_moveldup_ps(v); }
  522. template<> __forceinline vfloat4 shuffle<1, 1, 3, 3>(const vfloat4& v) { return _mm_movehdup_ps(v); }
  523. template<> __forceinline vfloat4 shuffle<0, 1, 0, 1>(const vfloat4& v) { return _mm_castpd_ps(_mm_movedup_pd(_mm_castps_pd(v))); }
  524. #endif
  525. template<int i>
  526. __forceinline vfloat4 shuffle(const vfloat4& v) {
  527. return shuffle<i,i,i,i>(v);
  528. }
  529. #if defined(__aarch64__)
  530. template<int i> __forceinline float extract(const vfloat4& a) { return a[i]; }
  531. #else
  532. template<int i> __forceinline float extract (const vfloat4& a) { return _mm_cvtss_f32(shuffle<i>(a)); }
  533. template<> __forceinline float extract<0>(const vfloat4& a) { return _mm_cvtss_f32(a); }
  534. #endif
  535. #if defined (__SSE4_1__) && !defined(__aarch64__)
  536. template<int dst, int src, int clr> __forceinline vfloat4 insert(const vfloat4& a, const vfloat4& b) { return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr); }
  537. template<int dst, int src> __forceinline vfloat4 insert(const vfloat4& a, const vfloat4& b) { return insert<dst, src, 0>(a, b); }
  538. template<int dst> __forceinline vfloat4 insert(const vfloat4& a, const float b) { return insert<dst, 0>(a, _mm_set_ss(b)); }
  539. #else
  540. template<int dst, int src> __forceinline vfloat4 insert(const vfloat4& a, const vfloat4& b) { vfloat4 c = a; c[dst&3] = b[src&3]; return c; }
  541. template<int dst> __forceinline vfloat4 insert(const vfloat4& a, float b) { vfloat4 c = a; c[dst&3] = b; return c; }
  542. #endif
  543. __forceinline float toScalar(const vfloat4& v) { return _mm_cvtss_f32(v); }
  544. __forceinline vfloat4 shift_right_1(const vfloat4& x) {
  545. return _mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(x), 4));
  546. }
  547. #if defined (__AVX2__)
  548. __forceinline vfloat4 permute(const vfloat4 &a, const __m128i &index) {
  549. return _mm_permutevar_ps(a,index);
  550. }
  551. __forceinline vfloat4 broadcast1f(const void* a) { return _mm_broadcast_ss((float*)a); }
  552. #endif
  553. #if defined(__AVX512VL__)
  554. template<int i>
  555. __forceinline vfloat4 align_shift_right(const vfloat4& a, const vfloat4& b) {
  556. return _mm_castsi128_ps(_mm_alignr_epi32(_mm_castps_si128(a), _mm_castps_si128(b), i));
  557. }
  558. #endif
  559. ////////////////////////////////////////////////////////////////////////////////
  560. /// Sorting Network
  561. ////////////////////////////////////////////////////////////////////////////////
  562. __forceinline vfloat4 sort_ascending(const vfloat4& v)
  563. {
  564. const vfloat4 a0 = v;
  565. const vfloat4 b0 = shuffle<1,0,3,2>(a0);
  566. const vfloat4 c0 = min(a0,b0);
  567. const vfloat4 d0 = max(a0,b0);
  568. const vfloat4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  569. const vfloat4 b1 = shuffle<2,3,0,1>(a1);
  570. const vfloat4 c1 = min(a1,b1);
  571. const vfloat4 d1 = max(a1,b1);
  572. const vfloat4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  573. const vfloat4 b2 = shuffle<0,2,1,3>(a2);
  574. const vfloat4 c2 = min(a2,b2);
  575. const vfloat4 d2 = max(a2,b2);
  576. const vfloat4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  577. return a3;
  578. }
  579. __forceinline vfloat4 sort_descending(const vfloat4& v)
  580. {
  581. const vfloat4 a0 = v;
  582. const vfloat4 b0 = shuffle<1,0,3,2>(a0);
  583. const vfloat4 c0 = max(a0,b0);
  584. const vfloat4 d0 = min(a0,b0);
  585. const vfloat4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  586. const vfloat4 b1 = shuffle<2,3,0,1>(a1);
  587. const vfloat4 c1 = max(a1,b1);
  588. const vfloat4 d1 = min(a1,b1);
  589. const vfloat4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  590. const vfloat4 b2 = shuffle<0,2,1,3>(a2);
  591. const vfloat4 c2 = max(a2,b2);
  592. const vfloat4 d2 = min(a2,b2);
  593. const vfloat4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  594. return a3;
  595. }
  596. ////////////////////////////////////////////////////////////////////////////////
  597. /// Transpose
  598. ////////////////////////////////////////////////////////////////////////////////
  599. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, vfloat4& c0, vfloat4& c1, vfloat4& c2, vfloat4& c3)
  600. {
  601. vfloat4 l02 = unpacklo(r0,r2);
  602. vfloat4 h02 = unpackhi(r0,r2);
  603. vfloat4 l13 = unpacklo(r1,r3);
  604. vfloat4 h13 = unpackhi(r1,r3);
  605. c0 = unpacklo(l02,l13);
  606. c1 = unpackhi(l02,l13);
  607. c2 = unpacklo(h02,h13);
  608. c3 = unpackhi(h02,h13);
  609. }
  610. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, vfloat4& c0, vfloat4& c1, vfloat4& c2)
  611. {
  612. vfloat4 l02 = unpacklo(r0,r2);
  613. vfloat4 h02 = unpackhi(r0,r2);
  614. vfloat4 l13 = unpacklo(r1,r3);
  615. vfloat4 h13 = unpackhi(r1,r3);
  616. c0 = unpacklo(l02,l13);
  617. c1 = unpackhi(l02,l13);
  618. c2 = unpacklo(h02,h13);
  619. }
  620. ////////////////////////////////////////////////////////////////////////////////
  621. /// Reductions
  622. ////////////////////////////////////////////////////////////////////////////////
  623. #if defined(__aarch64__)
  624. __forceinline vfloat4 vreduce_min(const vfloat4& v) { float h = vminvq_f32(v); return vdupq_n_f32(h); }
  625. __forceinline vfloat4 vreduce_max(const vfloat4& v) { float h = vmaxvq_f32(v); return vdupq_n_f32(h); }
  626. __forceinline vfloat4 vreduce_add(const vfloat4& v) { float h = vaddvq_f32(v); return vdupq_n_f32(h); }
  627. #else
  628. __forceinline vfloat4 vreduce_min(const vfloat4& v) { vfloat4 h = min(shuffle<1,0,3,2>(v),v); return min(shuffle<2,3,0,1>(h),h); }
  629. __forceinline vfloat4 vreduce_max(const vfloat4& v) { vfloat4 h = max(shuffle<1,0,3,2>(v),v); return max(shuffle<2,3,0,1>(h),h); }
  630. __forceinline vfloat4 vreduce_add(const vfloat4& v) { vfloat4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }
  631. #endif
  632. #if defined(__aarch64__)
  633. __forceinline float reduce_min(const vfloat4& v) { return vminvq_f32(v); }
  634. __forceinline float reduce_max(const vfloat4& v) { return vmaxvq_f32(v); }
  635. __forceinline float reduce_add(const vfloat4& v) { return vaddvq_f32(v); }
  636. #else
  637. __forceinline float reduce_min(const vfloat4& v) { return _mm_cvtss_f32(vreduce_min(v)); }
  638. __forceinline float reduce_max(const vfloat4& v) { return _mm_cvtss_f32(vreduce_max(v)); }
  639. __forceinline float reduce_add(const vfloat4& v) { return _mm_cvtss_f32(vreduce_add(v)); }
  640. #endif
  641. __forceinline size_t select_min(const vboolf4& valid, const vfloat4& v)
  642. {
  643. const vfloat4 a = select(valid,v,vfloat4(pos_inf));
  644. const vbool4 valid_min = valid & (a == vreduce_min(a));
  645. return bsf(movemask(any(valid_min) ? valid_min : valid));
  646. }
  647. __forceinline size_t select_max(const vboolf4& valid, const vfloat4& v)
  648. {
  649. const vfloat4 a = select(valid,v,vfloat4(neg_inf));
  650. const vbool4 valid_max = valid & (a == vreduce_max(a));
  651. return bsf(movemask(any(valid_max) ? valid_max : valid));
  652. }
  653. ////////////////////////////////////////////////////////////////////////////////
  654. /// Euclidean Space Operators
  655. ////////////////////////////////////////////////////////////////////////////////
  656. __forceinline float dot(const vfloat4& a, const vfloat4& b) {
  657. return reduce_add(a*b);
  658. }
  659. __forceinline vfloat4 cross(const vfloat4& a, const vfloat4& b)
  660. {
  661. const vfloat4 a0 = a;
  662. const vfloat4 b0 = shuffle<1,2,0,3>(b);
  663. const vfloat4 a1 = shuffle<1,2,0,3>(a);
  664. const vfloat4 b1 = b;
  665. return shuffle<1,2,0,3>(msub(a0,b0,a1*b1));
  666. }
  667. ////////////////////////////////////////////////////////////////////////////////
  668. /// Output Operators
  669. ////////////////////////////////////////////////////////////////////////////////
  670. __forceinline embree_ostream operator <<(embree_ostream cout, const vfloat4& a) {
  671. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  672. }
  673. }
  674. #undef vboolf
  675. #undef vboold
  676. #undef vint
  677. #undef vuint
  678. #undef vllong
  679. #undef vfloat
  680. #undef vdouble