vfloat4_sse2.h 36 KB

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