vfloat4_sse2.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /* 4-wide SSE float type */
  20. template<>
  21. struct vfloat<4>
  22. {
  23. typedef vboolf4 Bool;
  24. typedef vint4 Int;
  25. typedef vfloat4 Float;
  26. enum { size = 4 }; // number of SIMD elements
  27. union { __m128 v; float f[4]; int i[4]; }; // data
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// Constructors, Assignment & Cast Operators
  30. ////////////////////////////////////////////////////////////////////////////////
  31. __forceinline vfloat ( ) {}
  32. __forceinline vfloat ( const vfloat4& other ) { v = other.v; }
  33. __forceinline vfloat4& operator=( const vfloat4& other ) { v = other.v; return *this; }
  34. __forceinline vfloat( const __m128 a ) : v(a) {}
  35. __forceinline operator const __m128&( void ) const { return v; }
  36. __forceinline operator __m128&( void ) { return v; }
  37. __forceinline vfloat( float a ) : v(_mm_set1_ps(a)) {}
  38. __forceinline vfloat( float a, float b, float c, float d) : v(_mm_set_ps(d, c, b, a)) {}
  39. __forceinline explicit vfloat( const __m128i a ) : v(_mm_cvtepi32_ps(a)) {}
  40. ////////////////////////////////////////////////////////////////////////////////
  41. /// Constants
  42. ////////////////////////////////////////////////////////////////////////////////
  43. __forceinline vfloat( ZeroTy ) : v(_mm_setzero_ps()) {}
  44. __forceinline vfloat( OneTy ) : v(_mm_set1_ps(1.0f)) {}
  45. __forceinline vfloat( PosInfTy ) : v(_mm_set1_ps(pos_inf)) {}
  46. __forceinline vfloat( NegInfTy ) : v(_mm_set1_ps(neg_inf)) {}
  47. __forceinline vfloat( StepTy ) : v(_mm_set_ps(3.0f, 2.0f, 1.0f, 0.0f)) {}
  48. __forceinline vfloat( NaNTy ) : v(_mm_set1_ps(nan)) {}
  49. ////////////////////////////////////////////////////////////////////////////////
  50. /// Loads and Stores
  51. ////////////////////////////////////////////////////////////////////////////////
  52. static __forceinline vfloat4 load ( const void* const a ) { return _mm_load_ps((float*)a); }
  53. static __forceinline vfloat4 loadu( const void* const a ) { return _mm_loadu_ps((float*)a); }
  54. static __forceinline void store ( void* ptr, const vfloat4& v ) { _mm_store_ps((float*)ptr,v); }
  55. static __forceinline void storeu( void* ptr, const vfloat4& v ) { _mm_storeu_ps((float*)ptr,v); }
  56. #if defined(__AVX512VL__)
  57. static __forceinline vfloat4 load ( const vboolf4& mask, const void* const ptr) { return _mm_mask_load_ps (_mm_setzero_ps(),mask,(float*)ptr); }
  58. static __forceinline vfloat4 loadu( const vboolf4& mask, const void* const ptr) { return _mm_mask_loadu_ps(_mm_setzero_ps(),mask,(float*)ptr); }
  59. static __forceinline void store ( const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_mask_store_ps ((float*)ptr,mask,v); }
  60. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vfloat4& v) { _mm_mask_storeu_ps((float*)ptr,mask,v); }
  61. #elif defined(__AVX__)
  62. static __forceinline vfloat4 load ( const vboolf4& mask, const void* const ptr ) { return _mm_maskload_ps((float*)ptr,mask); }
  63. static __forceinline vfloat4 loadu( const vboolf4& mask, const void* const ptr ) { return _mm_maskload_ps((float*)ptr,mask); }
  64. static __forceinline void store ( const vboolf4& mask, void* ptr, const vfloat4& v ) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,v); }
  65. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vfloat4& v ) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,v); }
  66. #else
  67. static __forceinline vfloat4 load ( const vboolf4& mask, const void* const ptr ) { return _mm_and_ps(_mm_load_ps ((float*)ptr),mask); }
  68. static __forceinline vfloat4 loadu( const vboolf4& mask, const void* const ptr ) { return _mm_and_ps(_mm_loadu_ps((float*)ptr),mask); }
  69. static __forceinline void store ( const vboolf4& mask, void* ptr, const vfloat4& v ) { store (ptr,select(mask,v,load (ptr))); }
  70. static __forceinline void storeu( const vboolf4& mask, void* ptr, const vfloat4& v ) { storeu(ptr,select(mask,v,loadu(ptr))); }
  71. #endif
  72. #if defined(__AVX__)
  73. static __forceinline vfloat4 broadcast( const void* const a ) { return _mm_broadcast_ss((float*)a); }
  74. #else
  75. static __forceinline vfloat4 broadcast( const void* const a ) { return _mm_set1_ps(*(float*)a); }
  76. #endif
  77. static __forceinline vfloat4 load_nt ( const float* ptr ) {
  78. #if defined (__SSE4_1__)
  79. return _mm_castsi128_ps(_mm_stream_load_si128((__m128i*)ptr));
  80. #else
  81. return _mm_load_ps(ptr);
  82. #endif
  83. }
  84. #if defined(__SSE4_1__)
  85. static __forceinline vfloat4 load( const unsigned char* const ptr ) {
  86. return _mm_cvtepi32_ps(_mm_cvtepu8_epi32(_mm_loadu_si128((__m128i*)ptr)));
  87. }
  88. #else
  89. static __forceinline vfloat4 load( const unsigned char* const ptr ) {
  90. //return _mm_cvtpu8_ps(*(__m64*)ptr); // don't enable, will use MMX instructions
  91. return vfloat4(ptr[0],ptr[1],ptr[2],ptr[3]);
  92. }
  93. #endif
  94. static __forceinline vfloat4 load(const unsigned short* const ptr) {
  95. return _mm_mul_ps(vfloat4(vint4::load(ptr)),vfloat4(1.0f/65535.0f));
  96. }
  97. static __forceinline void store_nt ( void* ptr, const vfloat4& v)
  98. {
  99. #if defined (__SSE4_1__)
  100. _mm_stream_ps((float*)ptr,v);
  101. #else
  102. _mm_store_ps((float*)ptr,v);
  103. #endif
  104. }
  105. template<int scale>
  106. static __forceinline void scatter(const vboolf4& mask, void* ptr, const vint4& ofs, const vfloat4& v)
  107. {
  108. #if defined(__AVX512VL__)
  109. _mm_mask_i32scatter_ps(ptr,mask,ofs,v,scale);
  110. #else
  111. if (likely(mask[0])) *(float*)(((char*)ptr)+scale*ofs[0]) = v[0];
  112. if (likely(mask[1])) *(float*)(((char*)ptr)+scale*ofs[1]) = v[1];
  113. if (likely(mask[2])) *(float*)(((char*)ptr)+scale*ofs[2]) = v[2];
  114. if (likely(mask[3])) *(float*)(((char*)ptr)+scale*ofs[3]) = v[3];
  115. #endif
  116. }
  117. static __forceinline void store(const vboolf4& mask, char* ptr, const vint4& ofs, const vfloat4& v) {
  118. scatter<1>(mask,ptr,ofs,v);
  119. }
  120. static __forceinline void store(const vboolf4& mask, float* ptr, const vint4& ofs, const vfloat4& v) {
  121. scatter<4>(mask,ptr,ofs,v);
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////
  124. /// Array Access
  125. ////////////////////////////////////////////////////////////////////////////////
  126. __forceinline const float& operator []( const size_t index ) const { assert(index < 4); return f[index]; }
  127. __forceinline float& operator []( const size_t index ) { assert(index < 4); return f[index]; }
  128. friend __forceinline const vfloat4 select( const vboolf4& m, const vfloat4& t, const vfloat4& f ) {
  129. #if defined(__AVX512VL__)
  130. return _mm_mask_blend_ps(m, f, t);
  131. #elif defined(__SSE4_1__)
  132. return _mm_blendv_ps(f, t, m);
  133. #else
  134. return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
  135. #endif
  136. }
  137. };
  138. ////////////////////////////////////////////////////////////////////////////////
  139. /// Unary Operators
  140. ////////////////////////////////////////////////////////////////////////////////
  141. __forceinline const vfloat4 asFloat ( const __m128i& a ) { return _mm_castsi128_ps(a); }
  142. __forceinline const vfloat4 operator +( const vfloat4& a ) { return a; }
  143. __forceinline const vfloat4 operator -( const vfloat4& a ) { return _mm_xor_ps(a.v, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))); }
  144. __forceinline const vfloat4 abs ( const vfloat4& a ) { return _mm_and_ps(a.v, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))); }
  145. #if defined(__AVX512VL__)
  146. __forceinline const vfloat4 sign ( const vfloat4& a ) { return _mm_mask_blend_ps(_mm_cmp_ps_mask(a, vfloat4(zero), _CMP_LT_OQ), vfloat4(one), -vfloat4(one)); }
  147. #else
  148. __forceinline const vfloat4 sign ( const vfloat4& a ) { return blendv_ps(vfloat4(one), -vfloat4(one), _mm_cmplt_ps(a, vfloat4(zero))); }
  149. #endif
  150. __forceinline const vfloat4 signmsk ( const vfloat4& a ) { return _mm_and_ps(a.v,_mm_castsi128_ps(_mm_set1_epi32(0x80000000))); }
  151. __forceinline const vfloat4 rcp ( const vfloat4& a )
  152. {
  153. #if defined(__AVX512VL__)
  154. const vfloat4 r = _mm_rcp14_ps(a.v);
  155. #else
  156. const vfloat4 r = _mm_rcp_ps(a.v);
  157. #endif
  158. #if defined(__AVX2__)
  159. return _mm_mul_ps(r,_mm_fnmadd_ps(r, a, vfloat4(2.0f)));
  160. #else
  161. return _mm_mul_ps(r,_mm_sub_ps(vfloat4(2.0f), _mm_mul_ps(r, a)));
  162. #endif
  163. }
  164. __forceinline const vfloat4 sqr ( const vfloat4& a ) { return _mm_mul_ps(a,a); }
  165. __forceinline const vfloat4 sqrt ( const vfloat4& a ) { return _mm_sqrt_ps(a.v); }
  166. __forceinline const vfloat4 rsqrt( const vfloat4& a )
  167. {
  168. #if defined(__AVX512VL__)
  169. const vfloat4 r = _mm_rsqrt14_ps(a.v);
  170. #else
  171. const vfloat4 r = _mm_rsqrt_ps(a.v);
  172. #endif
  173. #if defined(__AVX2__)
  174. return _mm_fmadd_ps(_mm_set1_ps(1.5f), r,
  175. _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
  176. #else
  177. return _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.5f), r),
  178. _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
  179. #endif
  180. }
  181. __forceinline const vboolf4 isnan( const vfloat4& a ) {
  182. const vfloat4 b = _mm_and_ps(a.v, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)));
  183. #if defined(__AVX512VL__)
  184. return _mm_cmp_epi32_mask(_mm_castps_si128(b), _mm_set1_epi32(0x7f800000), _MM_CMPINT_GT);
  185. #else
  186. return _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_castps_si128(b), _mm_set1_epi32(0x7f800000)));
  187. #endif
  188. }
  189. ////////////////////////////////////////////////////////////////////////////////
  190. /// Binary Operators
  191. ////////////////////////////////////////////////////////////////////////////////
  192. __forceinline const vfloat4 operator +( const vfloat4& a, const vfloat4& b ) { return _mm_add_ps(a.v, b.v); }
  193. __forceinline const vfloat4 operator +( const vfloat4& a, const float& b ) { return a + vfloat4(b); }
  194. __forceinline const vfloat4 operator +( const float& a, const vfloat4& b ) { return vfloat4(a) + b; }
  195. __forceinline const vfloat4 operator -( const vfloat4& a, const vfloat4& b ) { return _mm_sub_ps(a.v, b.v); }
  196. __forceinline const vfloat4 operator -( const vfloat4& a, const float& b ) { return a - vfloat4(b); }
  197. __forceinline const vfloat4 operator -( const float& a, const vfloat4& b ) { return vfloat4(a) - b; }
  198. __forceinline const vfloat4 operator *( const vfloat4& a, const vfloat4& b ) { return _mm_mul_ps(a.v, b.v); }
  199. __forceinline const vfloat4 operator *( const vfloat4& a, const float& b ) { return a * vfloat4(b); }
  200. __forceinline const vfloat4 operator *( const float& a, const vfloat4& b ) { return vfloat4(a) * b; }
  201. __forceinline const vfloat4 operator /( const vfloat4& a, const vfloat4& b ) { return _mm_div_ps(a.v,b.v); }
  202. __forceinline const vfloat4 operator /( const vfloat4& a, const float& b ) { return a/vfloat4(b); }
  203. __forceinline const vfloat4 operator /( const float& a, const vfloat4& b ) { return vfloat4(a)/b; }
  204. __forceinline const vfloat4 operator^( const vfloat4& a, const vfloat4& b ) { return _mm_xor_ps(a.v,b.v); }
  205. __forceinline const vfloat4 operator^( const vfloat4& a, const vint4& b ) { return _mm_xor_ps(a.v,_mm_castsi128_ps(b.v)); }
  206. __forceinline const vfloat4 min( const vfloat4& a, const vfloat4& b ) { return _mm_min_ps(a.v,b.v); }
  207. __forceinline const vfloat4 min( const vfloat4& a, const float& b ) { return _mm_min_ps(a.v,vfloat4(b)); }
  208. __forceinline const vfloat4 min( const float& a, const vfloat4& b ) { return _mm_min_ps(vfloat4(a),b.v); }
  209. __forceinline const vfloat4 max( const vfloat4& a, const vfloat4& b ) { return _mm_max_ps(a.v,b.v); }
  210. __forceinline const vfloat4 max( const vfloat4& a, const float& b ) { return _mm_max_ps(a.v,vfloat4(b)); }
  211. __forceinline const vfloat4 max( const float& a, const vfloat4& b ) { return _mm_max_ps(vfloat4(a),b.v); }
  212. #if defined(__SSE4_1__)
  213. __forceinline vfloat4 mini(const vfloat4& a, const vfloat4& b) {
  214. const vint4 ai = _mm_castps_si128(a);
  215. const vint4 bi = _mm_castps_si128(b);
  216. const vint4 ci = _mm_min_epi32(ai,bi);
  217. return _mm_castsi128_ps(ci);
  218. }
  219. __forceinline vfloat4 maxi(const vfloat4& a, const vfloat4& b) {
  220. const vint4 ai = _mm_castps_si128(a);
  221. const vint4 bi = _mm_castps_si128(b);
  222. const vint4 ci = _mm_max_epi32(ai,bi);
  223. return _mm_castsi128_ps(ci);
  224. }
  225. __forceinline vfloat4 minui(const vfloat4& a, const vfloat4& b) {
  226. const vint4 ai = _mm_castps_si128(a);
  227. const vint4 bi = _mm_castps_si128(b);
  228. const vint4 ci = _mm_min_epu32(ai,bi);
  229. return _mm_castsi128_ps(ci);
  230. }
  231. __forceinline vfloat4 maxui(const vfloat4& a, const vfloat4& b) {
  232. const vint4 ai = _mm_castps_si128(a);
  233. const vint4 bi = _mm_castps_si128(b);
  234. const vint4 ci = _mm_max_epu32(ai,bi);
  235. return _mm_castsi128_ps(ci);
  236. }
  237. #else
  238. __forceinline vfloat4 mini(const vfloat4& a, const vfloat4& b) {
  239. return min(a,b);
  240. }
  241. __forceinline vfloat4 maxi(const vfloat4& a, const vfloat4& b) {
  242. return max(a,b);
  243. }
  244. #endif
  245. ////////////////////////////////////////////////////////////////////////////////
  246. /// Ternary Operators
  247. ////////////////////////////////////////////////////////////////////////////////
  248. #if defined(__AVX2__)
  249. __forceinline const vfloat4 madd ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fmadd_ps(a,b,c); }
  250. __forceinline const vfloat4 msub ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fmsub_ps(a,b,c); }
  251. __forceinline const vfloat4 nmadd ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fnmadd_ps(a,b,c); }
  252. __forceinline const vfloat4 nmsub ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return _mm_fnmsub_ps(a,b,c); }
  253. #else
  254. __forceinline const vfloat4 madd ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return a*b+c; }
  255. __forceinline const vfloat4 msub ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return a*b-c; }
  256. __forceinline const vfloat4 nmadd ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return -a*b+c;}
  257. __forceinline const vfloat4 nmsub ( const vfloat4& a, const vfloat4& b, const vfloat4& c) { return -a*b-c; }
  258. #endif
  259. ////////////////////////////////////////////////////////////////////////////////
  260. /// Assignment Operators
  261. ////////////////////////////////////////////////////////////////////////////////
  262. __forceinline vfloat4& operator +=( vfloat4& a, const vfloat4& b ) { return a = a + b; }
  263. __forceinline vfloat4& operator +=( vfloat4& a, const float& b ) { return a = a + b; }
  264. __forceinline vfloat4& operator -=( vfloat4& a, const vfloat4& b ) { return a = a - b; }
  265. __forceinline vfloat4& operator -=( vfloat4& a, const float& b ) { return a = a - b; }
  266. __forceinline vfloat4& operator *=( vfloat4& a, const vfloat4& b ) { return a = a * b; }
  267. __forceinline vfloat4& operator *=( vfloat4& a, const float& b ) { return a = a * b; }
  268. __forceinline vfloat4& operator /=( vfloat4& a, const vfloat4& b ) { return a = a / b; }
  269. __forceinline vfloat4& operator /=( vfloat4& a, const float& b ) { return a = a / b; }
  270. ////////////////////////////////////////////////////////////////////////////////
  271. /// Comparison Operators + Select
  272. ////////////////////////////////////////////////////////////////////////////////
  273. #if defined(__AVX512VL__)
  274. __forceinline const vboolf4 operator ==( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_EQ); }
  275. __forceinline const vboolf4 operator !=( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_NE); }
  276. __forceinline const vboolf4 operator < ( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_LT); }
  277. __forceinline const vboolf4 operator >=( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_GE); }
  278. __forceinline const vboolf4 operator > ( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_GT); }
  279. __forceinline const vboolf4 operator <=( const vfloat4& a, const vfloat4& b ) { return _mm_cmp_ps_mask(a, b, _MM_CMPINT_LE); }
  280. #else
  281. __forceinline const vboolf4 operator ==( const vfloat4& a, const vfloat4& b ) { return _mm_cmpeq_ps (a, b); }
  282. __forceinline const vboolf4 operator !=( const vfloat4& a, const vfloat4& b ) { return _mm_cmpneq_ps(a, b); }
  283. __forceinline const vboolf4 operator < ( const vfloat4& a, const vfloat4& b ) { return _mm_cmplt_ps (a, b); }
  284. __forceinline const vboolf4 operator >=( const vfloat4& a, const vfloat4& b ) { return _mm_cmpnlt_ps(a, b); }
  285. __forceinline const vboolf4 operator > ( const vfloat4& a, const vfloat4& b ) { return _mm_cmpnle_ps(a, b); }
  286. __forceinline const vboolf4 operator <=( const vfloat4& a, const vfloat4& b ) { return _mm_cmple_ps (a, b); }
  287. #endif
  288. __forceinline const vboolf4 operator ==( const vfloat4& a, const float& b ) { return a == vfloat4(b); }
  289. __forceinline const vboolf4 operator ==( const float& a, const vfloat4& b ) { return vfloat4(a) == b; }
  290. __forceinline const vboolf4 operator !=( const vfloat4& a, const float& b ) { return a != vfloat4(b); }
  291. __forceinline const vboolf4 operator !=( const float& a, const vfloat4& b ) { return vfloat4(a) != b; }
  292. __forceinline const vboolf4 operator < ( const vfloat4& a, const float& b ) { return a < vfloat4(b); }
  293. __forceinline const vboolf4 operator < ( const float& a, const vfloat4& b ) { return vfloat4(a) < b; }
  294. __forceinline const vboolf4 operator >=( const vfloat4& a, const float& b ) { return a >= vfloat4(b); }
  295. __forceinline const vboolf4 operator >=( const float& a, const vfloat4& b ) { return vfloat4(a) >= b; }
  296. __forceinline const vboolf4 operator > ( const vfloat4& a, const float& b ) { return a > vfloat4(b); }
  297. __forceinline const vboolf4 operator > ( const float& a, const vfloat4& b ) { return vfloat4(a) > b; }
  298. __forceinline const vboolf4 operator <=( const vfloat4& a, const float& b ) { return a <= vfloat4(b); }
  299. __forceinline const vboolf4 operator <=( const float& a, const vfloat4& b ) { return vfloat4(a) <= b; }
  300. __forceinline vboolf4 eq(const vfloat4& a, const vfloat4& b) { return a == b; }
  301. __forceinline vboolf4 ne(const vfloat4& a, const vfloat4& b) { return a != b; }
  302. __forceinline vboolf4 lt(const vfloat4& a, const vfloat4& b) { return a < b; }
  303. __forceinline vboolf4 ge(const vfloat4& a, const vfloat4& b) { return a >= b; }
  304. __forceinline vboolf4 gt(const vfloat4& a, const vfloat4& b) { return a > b; }
  305. __forceinline vboolf4 le(const vfloat4& a, const vfloat4& b) { return a <= b; }
  306. #if defined(__AVX512VL__)
  307. __forceinline vboolf4 eq(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_EQ); }
  308. __forceinline vboolf4 ne(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_NE); }
  309. __forceinline vboolf4 lt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LT); }
  310. __forceinline vboolf4 ge(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GE); }
  311. __forceinline vboolf4 gt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_GT); }
  312. __forceinline vboolf4 le(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return _mm_mask_cmp_ps_mask(mask, a, b, _MM_CMPINT_LE); }
  313. #else
  314. __forceinline vboolf4 eq(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a == b); }
  315. __forceinline vboolf4 ne(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a != b); }
  316. __forceinline vboolf4 lt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a < b); }
  317. __forceinline vboolf4 ge(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a >= b); }
  318. __forceinline vboolf4 gt(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a > b); }
  319. __forceinline vboolf4 le(const vboolf4& mask, const vfloat4& a, const vfloat4& b) { return mask & (a <= b); }
  320. #endif
  321. template<int mask>
  322. __forceinline const vfloat4 select(const vfloat4& t, const vfloat4& f)
  323. {
  324. #if defined(__SSE4_1__)
  325. return _mm_blend_ps(f, t, mask);
  326. #else
  327. return select(vboolf4(mask), t, f);
  328. #endif
  329. }
  330. __forceinline vfloat4 lerp(const vfloat4& a, const vfloat4& b, const vfloat4& t) {
  331. return madd(t,b-a,a);
  332. }
  333. __forceinline bool isvalid ( const vfloat4& v ) {
  334. return all((v > vfloat4(-FLT_LARGE)) & (v < vfloat4(+FLT_LARGE)));
  335. }
  336. __forceinline bool is_finite ( const vfloat4& a ) {
  337. return all((a >= vfloat4(-FLT_MAX)) & (a <= vfloat4(+FLT_MAX)));
  338. }
  339. __forceinline bool is_finite ( const vboolf4& valid, const vfloat4& a ) {
  340. return all(valid, (a >= vfloat4(-FLT_MAX)) & (a <= vfloat4(+FLT_MAX)));
  341. }
  342. ////////////////////////////////////////////////////////////////////////////////
  343. /// Rounding Functions
  344. ////////////////////////////////////////////////////////////////////////////////
  345. #if defined (__SSE4_1__)
  346. __forceinline const vfloat4 floor ( const vfloat4& a ) { return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF ); }
  347. __forceinline const vfloat4 ceil ( const vfloat4& a ) { return _mm_round_ps(a, _MM_FROUND_TO_POS_INF ); }
  348. __forceinline const vfloat4 trunc ( const vfloat4& a ) { return _mm_round_ps(a, _MM_FROUND_TO_ZERO ); }
  349. #else
  350. __forceinline const vfloat4 floor ( const vfloat4& a ) { return vfloat4(floorf(a[0]),floorf(a[1]),floorf(a[2]),floorf(a[3])); }
  351. __forceinline const vfloat4 ceil ( const vfloat4& a ) { return vfloat4(ceilf (a[0]),ceilf (a[1]),ceilf (a[2]),ceilf (a[3])); }
  352. //__forceinline const vfloat4 trunc ( const vfloat4& a ) { return vfloat4(truncf(a[0]),truncf(a[1]),truncf(a[2]),truncf(a[3])); }
  353. #endif
  354. __forceinline const vfloat4 frac ( const vfloat4& a ) { return a-floor(a); }
  355. __forceinline vint4 floori (const vfloat4& a) {
  356. #if defined(__SSE4_1__)
  357. return vint4(floor(a));
  358. #else
  359. return vint4(a-vfloat4(0.5f));
  360. #endif
  361. }
  362. ////////////////////////////////////////////////////////////////////////////////
  363. /// Movement/Shifting/Shuffling Functions
  364. ////////////////////////////////////////////////////////////////////////////////
  365. __forceinline vfloat4 unpacklo( const vfloat4& a, const vfloat4& b ) { return _mm_unpacklo_ps(a.v, b.v); }
  366. __forceinline vfloat4 unpackhi( const vfloat4& a, const vfloat4& b ) { return _mm_unpackhi_ps(a.v, b.v); }
  367. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vfloat4 shuffle( const vfloat4& b ) {
  368. return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  369. }
  370. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const vfloat4 shuffle( const vfloat4& a, const vfloat4& b ) {
  371. return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  372. }
  373. #if defined (__SSSE3__)
  374. __forceinline const vfloat4 shuffle8(const vfloat4& a, const vint4& shuf) {
  375. return _mm_castsi128_ps(_mm_shuffle_epi8(_mm_castps_si128(a), shuf));
  376. }
  377. #endif
  378. #if defined(__SSE3__)
  379. template<> __forceinline const vfloat4 shuffle<0, 0, 2, 2>( const vfloat4& b ) { return _mm_moveldup_ps(b); }
  380. template<> __forceinline const vfloat4 shuffle<1, 1, 3, 3>( const vfloat4& b ) { return _mm_movehdup_ps(b); }
  381. template<> __forceinline const vfloat4 shuffle<0, 1, 0, 1>( const vfloat4& b ) { return _mm_castpd_ps(_mm_movedup_pd(_mm_castps_pd(b))); }
  382. #endif
  383. template<size_t i0> __forceinline const vfloat4 shuffle( const vfloat4& b ) {
  384. return shuffle<i0,i0,i0,i0>(b);
  385. }
  386. #if defined (__SSE4_1__) && !defined(__GNUC__)
  387. template<size_t i> __forceinline float extract ( const vfloat4& a ) { return _mm_cvtss_f32(_mm_extract_ps(a,i)); }
  388. #else
  389. template<size_t i> __forceinline float extract ( const vfloat4& a ) { return _mm_cvtss_f32(shuffle<i,i,i,i>(a)); }
  390. #endif
  391. template<> __forceinline float extract<0>( const vfloat4& a ) { return _mm_cvtss_f32(a); }
  392. #if defined (__SSE4_1__)
  393. template<size_t dst, size_t src, size_t clr> __forceinline const vfloat4 insert( const vfloat4& a, const vfloat4& b ) { return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr); }
  394. template<size_t dst, size_t src> __forceinline const vfloat4 insert( const vfloat4& a, const vfloat4& b ) { return insert<dst, src, 0>(a, b); }
  395. template<size_t dst> __forceinline const vfloat4 insert( const vfloat4& a, const float b ) { return insert<dst, 0>(a, _mm_set_ss(b)); }
  396. #else
  397. template<size_t dst, size_t src> __forceinline const vfloat4 insert( const vfloat4& a, const vfloat4& b ) { vfloat4 c = a; c[dst] = b[src]; return c; }
  398. template<size_t dst> __forceinline const vfloat4 insert( const vfloat4& a, const float b ) { vfloat4 c = a; c[dst] = b; return c; }
  399. #endif
  400. __forceinline float toScalar(const vfloat4& a) { return _mm_cvtss_f32(a); }
  401. __forceinline vfloat4 broadcast4f( const vfloat4& a, const size_t k ) {
  402. return vfloat4::broadcast(&a[k]);
  403. }
  404. __forceinline vfloat4 shift_right_1( const vfloat4& x) {
  405. return _mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(x), 4));
  406. }
  407. #if defined (__AVX2__)
  408. __forceinline vfloat4 permute(const vfloat4 &a, const __m128i &index) {
  409. return _mm_permutevar_ps(a,index);
  410. }
  411. __forceinline vfloat4 broadcast1f( const void* const a ) { return _mm_broadcast_ss((float*)a); }
  412. #endif
  413. ////////////////////////////////////////////////////////////////////////////////
  414. /// Sorting Network
  415. ////////////////////////////////////////////////////////////////////////////////
  416. __forceinline vfloat4 sortNetwork(const vfloat4& v)
  417. {
  418. const vfloat4 a0 = v;
  419. const vfloat4 b0 = shuffle<1,0,3,2>(a0);
  420. const vfloat4 c0 = min(a0,b0);
  421. const vfloat4 d0 = max(a0,b0);
  422. const vfloat4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
  423. const vfloat4 b1 = shuffle<2,3,0,1>(a1);
  424. const vfloat4 c1 = min(a1,b1);
  425. const vfloat4 d1 = max(a1,b1);
  426. const vfloat4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
  427. const vfloat4 b2 = shuffle<0,2,1,3>(a2);
  428. const vfloat4 c2 = min(a2,b2);
  429. const vfloat4 d2 = max(a2,b2);
  430. const vfloat4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
  431. return a3;
  432. }
  433. ////////////////////////////////////////////////////////////////////////////////
  434. /// Transpose
  435. ////////////////////////////////////////////////////////////////////////////////
  436. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, vfloat4& c0, vfloat4& c1, vfloat4& c2, vfloat4& c3)
  437. {
  438. vfloat4 l02 = unpacklo(r0,r2);
  439. vfloat4 h02 = unpackhi(r0,r2);
  440. vfloat4 l13 = unpacklo(r1,r3);
  441. vfloat4 h13 = unpackhi(r1,r3);
  442. c0 = unpacklo(l02,l13);
  443. c1 = unpackhi(l02,l13);
  444. c2 = unpacklo(h02,h13);
  445. c3 = unpackhi(h02,h13);
  446. }
  447. __forceinline void transpose(const vfloat4& r0, const vfloat4& r1, const vfloat4& r2, const vfloat4& r3, vfloat4& c0, vfloat4& c1, vfloat4& c2)
  448. {
  449. vfloat4 l02 = unpacklo(r0,r2);
  450. vfloat4 h02 = unpackhi(r0,r2);
  451. vfloat4 l13 = unpacklo(r1,r3);
  452. vfloat4 h13 = unpackhi(r1,r3);
  453. c0 = unpacklo(l02,l13);
  454. c1 = unpackhi(l02,l13);
  455. c2 = unpacklo(h02,h13);
  456. }
  457. ////////////////////////////////////////////////////////////////////////////////
  458. /// Reductions
  459. ////////////////////////////////////////////////////////////////////////////////
  460. __forceinline const 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); }
  461. __forceinline const 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); }
  462. __forceinline const vfloat4 vreduce_add(const vfloat4& v) { vfloat4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }
  463. __forceinline float reduce_min(const vfloat4& v) { return _mm_cvtss_f32(vreduce_min(v)); }
  464. __forceinline float reduce_max(const vfloat4& v) { return _mm_cvtss_f32(vreduce_max(v)); }
  465. __forceinline float reduce_add(const vfloat4& v) { return _mm_cvtss_f32(vreduce_add(v)); }
  466. __forceinline size_t select_min(const vboolf4& valid, const vfloat4& v)
  467. {
  468. const vfloat4 a = select(valid,v,vfloat4(pos_inf));
  469. const vbool4 valid_min = valid & (a == vreduce_min(a));
  470. return __bsf(movemask(any(valid_min) ? valid_min : valid));
  471. }
  472. __forceinline size_t select_max(const vboolf4& valid, const vfloat4& v)
  473. {
  474. const vfloat4 a = select(valid,v,vfloat4(neg_inf));
  475. const vbool4 valid_max = valid & (a == vreduce_max(a));
  476. return __bsf(movemask(any(valid_max) ? valid_max : valid));
  477. }
  478. ////////////////////////////////////////////////////////////////////////////////
  479. /// Euclidian Space Operators
  480. ////////////////////////////////////////////////////////////////////////////////
  481. __forceinline float dot ( const vfloat4& a, const vfloat4& b ) {
  482. return reduce_add(a*b);
  483. }
  484. __forceinline vfloat4 cross ( const vfloat4& a, const vfloat4& b )
  485. {
  486. const vfloat4 a0 = a;
  487. const vfloat4 b0 = shuffle<1,2,0,3>(b);
  488. const vfloat4 a1 = shuffle<1,2,0,3>(a);
  489. const vfloat4 b1 = b;
  490. return shuffle<1,2,0,3>(msub(a0,b0,a1*b1));
  491. }
  492. ////////////////////////////////////////////////////////////////////////////////
  493. /// Output Operators
  494. ////////////////////////////////////////////////////////////////////////////////
  495. inline std::ostream& operator<<(std::ostream& cout, const vfloat4& a) {
  496. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  497. }
  498. }