math.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #include "../sys/platform.h"
  18. #include "constants.h"
  19. #include <cmath>
  20. #include <emmintrin.h>
  21. #include <xmmintrin.h>
  22. #include <immintrin.h>
  23. #if defined(__WIN32__)
  24. #if (__MSV_VER <= 1700)
  25. namespace std
  26. {
  27. __forceinline bool isinf ( const float x ) { return _finite(x) == 0; }
  28. __forceinline bool isnan ( const float x ) { return _isnan(x) != 0; }
  29. __forceinline bool isfinite (const float x) { return _finite(x) != 0; }
  30. }
  31. #endif
  32. #endif
  33. namespace embree
  34. {
  35. __forceinline bool isvalid ( const float& v ) {
  36. return (v > -FLT_LARGE) & (v < +FLT_LARGE);
  37. }
  38. __forceinline int cast_f2i(float f) {
  39. union { float f; int i; } v; v.f = f; return v.i;
  40. }
  41. __forceinline float cast_i2f(int i) {
  42. union { float f; int i; } v; v.i = i; return v.f;
  43. }
  44. #if defined(__WIN32__)
  45. __forceinline bool finite ( const float x ) { return _finite(x) != 0; }
  46. #endif
  47. __forceinline float sign ( const float x ) { return x<0?-1.0f:1.0f; }
  48. __forceinline float sqr ( const float x ) { return x*x; }
  49. __forceinline float rcp ( const float x )
  50. {
  51. const __m128 a = _mm_set_ss(x);
  52. #if defined(__AVX512VL__)
  53. const __m128 r = _mm_rcp14_ps(a);
  54. #else
  55. const __m128 r = _mm_rcp_ps(a);
  56. #endif
  57. #if defined(__AVX2__)
  58. return _mm_cvtss_f32(_mm_mul_ps(r,_mm_fnmadd_ps(r, a, _mm_set_ss(2.0f))));
  59. #else
  60. return _mm_cvtss_f32(_mm_mul_ps(r,_mm_sub_ps(_mm_set_ss(2.0f), _mm_mul_ps(r, a))));
  61. #endif
  62. }
  63. __forceinline float signmsk ( const float x ) {
  64. return _mm_cvtss_f32(_mm_and_ps(_mm_set_ss(x),_mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
  65. }
  66. __forceinline float xorf( const float x, const float y ) {
  67. return _mm_cvtss_f32(_mm_xor_ps(_mm_set_ss(x),_mm_set_ss(y)));
  68. }
  69. __forceinline float andf( const float x, const unsigned y ) {
  70. return _mm_cvtss_f32(_mm_and_ps(_mm_set_ss(x),_mm_castsi128_ps(_mm_set1_epi32(y))));
  71. }
  72. __forceinline float rsqrt( const float x )
  73. {
  74. const __m128 a = _mm_set_ss(x);
  75. #if defined(__AVX512VL__)
  76. const __m128 r = _mm_rsqrt14_ps(a);
  77. #else
  78. const __m128 r = _mm_rsqrt_ps(a);
  79. #endif
  80. const __m128 c = _mm_add_ps(_mm_mul_ps(_mm_set_ps(1.5f, 1.5f, 1.5f, 1.5f), r),
  81. _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set_ps(-0.5f, -0.5f, -0.5f, -0.5f)), r), _mm_mul_ps(r, r)));
  82. return _mm_cvtss_f32(c);
  83. }
  84. #if defined(__WIN32__) && (__MSC_VER <= 1700)
  85. __forceinline float nextafter(float x, float y) { if ((x<y) == (x>0)) return x*(1.1f+float(ulp)); else return x*(0.9f-float(ulp)); }
  86. __forceinline double nextafter(double x, double y) { return _nextafter(x, y); }
  87. #else
  88. __forceinline float nextafter(float x, float y) { return ::nextafterf(x, y); }
  89. __forceinline double nextafter(double x, double y) { return ::nextafter(x, y); }
  90. #endif
  91. __forceinline float abs ( const float x ) { return ::fabsf(x); }
  92. __forceinline float acos ( const float x ) { return ::acosf (x); }
  93. __forceinline float asin ( const float x ) { return ::asinf (x); }
  94. __forceinline float atan ( const float x ) { return ::atanf (x); }
  95. __forceinline float atan2( const float y, const float x ) { return ::atan2f(y, x); }
  96. __forceinline float cos ( const float x ) { return ::cosf (x); }
  97. __forceinline float cosh ( const float x ) { return ::coshf (x); }
  98. __forceinline float exp ( const float x ) { return ::expf (x); }
  99. __forceinline float fmod ( const float x, const float y ) { return ::fmodf (x, y); }
  100. __forceinline float log ( const float x ) { return ::logf (x); }
  101. __forceinline float log10( const float x ) { return ::log10f(x); }
  102. __forceinline float pow ( const float x, const float y ) { return ::powf (x, y); }
  103. __forceinline float sin ( const float x ) { return ::sinf (x); }
  104. __forceinline float sinh ( const float x ) { return ::sinhf (x); }
  105. __forceinline float sqrt ( const float x ) { return ::sqrtf (x); }
  106. __forceinline float tan ( const float x ) { return ::tanf (x); }
  107. __forceinline float tanh ( const float x ) { return ::tanhf (x); }
  108. __forceinline float floor( const float x ) { return ::floorf (x); }
  109. __forceinline float ceil ( const float x ) { return ::ceilf (x); }
  110. __forceinline float frac ( const float x ) { return x-floor(x); }
  111. __forceinline double abs ( const double x ) { return ::fabs(x); }
  112. __forceinline double sign ( const double x ) { return x<0?-1.0:1.0; }
  113. __forceinline double acos ( const double x ) { return ::acos (x); }
  114. __forceinline double asin ( const double x ) { return ::asin (x); }
  115. __forceinline double atan ( const double x ) { return ::atan (x); }
  116. __forceinline double atan2( const double y, const double x ) { return ::atan2(y, x); }
  117. __forceinline double cos ( const double x ) { return ::cos (x); }
  118. __forceinline double cosh ( const double x ) { return ::cosh (x); }
  119. __forceinline double exp ( const double x ) { return ::exp (x); }
  120. __forceinline double fmod ( const double x, const double y ) { return ::fmod (x, y); }
  121. __forceinline double log ( const double x ) { return ::log (x); }
  122. __forceinline double log10( const double x ) { return ::log10(x); }
  123. __forceinline double pow ( const double x, const double y ) { return ::pow (x, y); }
  124. __forceinline double rcp ( const double x ) { return 1.0/x; }
  125. __forceinline double rsqrt( const double x ) { return 1.0/::sqrt(x); }
  126. __forceinline double sin ( const double x ) { return ::sin (x); }
  127. __forceinline double sinh ( const double x ) { return ::sinh (x); }
  128. __forceinline double sqr ( const double x ) { return x*x; }
  129. __forceinline double sqrt ( const double x ) { return ::sqrt (x); }
  130. __forceinline double tan ( const double x ) { return ::tan (x); }
  131. __forceinline double tanh ( const double x ) { return ::tanh (x); }
  132. __forceinline double floor( const double x ) { return ::floor (x); }
  133. __forceinline double ceil ( const double x ) { return ::ceil (x); }
  134. #if defined(__SSE4_1__)
  135. __forceinline float mini(float a, float b) {
  136. const __m128i ai = _mm_castps_si128(_mm_set_ss(a));
  137. const __m128i bi = _mm_castps_si128(_mm_set_ss(b));
  138. const __m128i ci = _mm_min_epi32(ai,bi);
  139. return _mm_cvtss_f32(_mm_castsi128_ps(ci));
  140. }
  141. #endif
  142. #if defined(__SSE4_1__)
  143. __forceinline float maxi(float a, float b) {
  144. const __m128i ai = _mm_castps_si128(_mm_set_ss(a));
  145. const __m128i bi = _mm_castps_si128(_mm_set_ss(b));
  146. const __m128i ci = _mm_max_epi32(ai,bi);
  147. return _mm_cvtss_f32(_mm_castsi128_ps(ci));
  148. }
  149. #endif
  150. template<typename T>
  151. __forceinline T twice(const T& a) { return a+a; }
  152. __forceinline int min(int a, int b) { return a<b ? a:b; }
  153. __forceinline unsigned min(unsigned a, unsigned b) { return a<b ? a:b; }
  154. __forceinline int64_t min(int64_t a, int64_t b) { return a<b ? a:b; }
  155. __forceinline float min(float a, float b) { return a<b ? a:b; }
  156. __forceinline double min(double a, double b) { return a<b ? a:b; }
  157. #if defined(__X86_64__)
  158. __forceinline size_t min(size_t a, size_t b) { return a<b ? a:b; }
  159. #endif
  160. template<typename T> __forceinline T min(const T& a, const T& b, const T& c) { return min(min(a,b),c); }
  161. template<typename T> __forceinline T min(const T& a, const T& b, const T& c, const T& d) { return min(min(a,b),min(c,d)); }
  162. template<typename T> __forceinline T min(const T& a, const T& b, const T& c, const T& d, const T& e) { return min(min(min(a,b),min(c,d)),e); }
  163. template<typename T> __forceinline T mini(const T& a, const T& b, const T& c) { return mini(mini(a,b),c); }
  164. template<typename T> __forceinline T mini(const T& a, const T& b, const T& c, const T& d) { return mini(mini(a,b),mini(c,d)); }
  165. template<typename T> __forceinline T mini(const T& a, const T& b, const T& c, const T& d, const T& e) { return mini(mini(mini(a,b),mini(c,d)),e); }
  166. __forceinline int max(int a, int b) { return a<b ? b:a; }
  167. __forceinline unsigned max(unsigned a, unsigned b) { return a<b ? b:a; }
  168. __forceinline int64_t max(int64_t a, int64_t b) { return a<b ? b:a; }
  169. __forceinline float max(float a, float b) { return a<b ? b:a; }
  170. __forceinline double max(double a, double b) { return a<b ? b:a; }
  171. #if defined(__X86_64__)
  172. __forceinline size_t max(size_t a, size_t b) { return a<b ? b:a; }
  173. #endif
  174. template<typename T> __forceinline T max(const T& a, const T& b, const T& c) { return max(max(a,b),c); }
  175. template<typename T> __forceinline T max(const T& a, const T& b, const T& c, const T& d) { return max(max(a,b),max(c,d)); }
  176. template<typename T> __forceinline T max(const T& a, const T& b, const T& c, const T& d, const T& e) { return max(max(max(a,b),max(c,d)),e); }
  177. template<typename T> __forceinline T maxi(const T& a, const T& b, const T& c) { return maxi(maxi(a,b),c); }
  178. template<typename T> __forceinline T maxi(const T& a, const T& b, const T& c, const T& d) { return maxi(maxi(a,b),maxi(c,d)); }
  179. template<typename T> __forceinline T maxi(const T& a, const T& b, const T& c, const T& d, const T& e) { return maxi(maxi(maxi(a,b),maxi(c,d)),e); }
  180. #if defined(__MACOSX__)
  181. __forceinline ssize_t min(ssize_t a, ssize_t b) { return a<b ? a:b; }
  182. __forceinline ssize_t max(ssize_t a, ssize_t b) { return a<b ? b:a; }
  183. #endif
  184. #if defined(__MACOSX__) && !defined(__INTEL_COMPILER)
  185. __forceinline void sincosf(float x, float *sin, float *cos) {
  186. __sincosf(x,sin,cos);
  187. }
  188. #endif
  189. #if defined(__WIN32__) || defined(__FreeBSD__)
  190. __forceinline void sincosf(float x, float *s, float *c) {
  191. *s = sinf(x); *c = cosf(x);
  192. }
  193. #endif
  194. template<typename T> __forceinline T clamp(const T& x, const T& lower = T(zero), const T& upper = T(one)) { return max(min(x,upper),lower); }
  195. template<typename T> __forceinline T clampz(const T& x, const T& upper) { return max(T(zero), min(x,upper)); }
  196. template<typename T> __forceinline T deg2rad ( const T& x ) { return x * T(1.74532925199432957692e-2f); }
  197. template<typename T> __forceinline T rad2deg ( const T& x ) { return x * T(5.72957795130823208768e1f); }
  198. template<typename T> __forceinline T sin2cos ( const T& x ) { return sqrt(max(T(zero),T(one)-x*x)); }
  199. template<typename T> __forceinline T cos2sin ( const T& x ) { return sin2cos(x); }
  200. #if defined(__AVX2__)
  201. __forceinline float madd ( const float a, const float b, const float c) { return _mm_cvtss_f32(_mm_fmadd_ss(_mm_set_ss(a),_mm_set_ss(b),_mm_set_ss(c))); }
  202. __forceinline float msub ( const float a, const float b, const float c) { return _mm_cvtss_f32(_mm_fmsub_ss(_mm_set_ss(a),_mm_set_ss(b),_mm_set_ss(c))); }
  203. __forceinline float nmadd ( const float a, const float b, const float c) { return _mm_cvtss_f32(_mm_fnmadd_ss(_mm_set_ss(a),_mm_set_ss(b),_mm_set_ss(c))); }
  204. __forceinline float nmsub ( const float a, const float b, const float c) { return _mm_cvtss_f32(_mm_fnmsub_ss(_mm_set_ss(a),_mm_set_ss(b),_mm_set_ss(c))); }
  205. #else
  206. __forceinline float madd ( const float a, const float b, const float c) { return a*b+c; }
  207. __forceinline float msub ( const float a, const float b, const float c) { return a*b-c; }
  208. __forceinline float nmadd ( const float a, const float b, const float c) { return -a*b+c;}
  209. __forceinline float nmsub ( const float a, const float b, const float c) { return -a*b-c; }
  210. #endif
  211. /*! random functions */
  212. template<typename T> T random() { return T(0); }
  213. #if defined(_WIN32)
  214. template<> __forceinline int random() { return int(rand()) ^ (int(rand()) << 8) ^ (int(rand()) << 16); }
  215. template<> __forceinline uint32_t random() { return uint32_t(rand()) ^ (uint32_t(rand()) << 8) ^ (uint32_t(rand()) << 16); }
  216. #else
  217. template<> __forceinline int random() { return int(rand()); }
  218. template<> __forceinline uint32_t random() { return uint32_t(rand()) ^ (uint32_t(rand()) << 16); }
  219. #endif
  220. template<> __forceinline float random() { return rand()/float(RAND_MAX); }
  221. template<> __forceinline double random() { return rand()/double(RAND_MAX); }
  222. #if _WIN32
  223. __forceinline double drand48() {
  224. return double(rand())/double(RAND_MAX);
  225. }
  226. __forceinline void srand48(long seed) {
  227. return srand(seed);
  228. }
  229. #endif
  230. /*! selects */
  231. __forceinline bool select(bool s, bool t , bool f) { return s ? t : f; }
  232. __forceinline int select(bool s, int t, int f) { return s ? t : f; }
  233. __forceinline float select(bool s, float t, float f) { return s ? t : f; }
  234. template<typename V>
  235. __forceinline V lerp(const V& v0, const V& v1, const float t) {
  236. return madd(V(1.0f-t),v0,t*v1);
  237. }
  238. template<typename T>
  239. __forceinline T lerp2(const float x0, const float x1, const float x2, const float x3, const T& u, const T& v) {
  240. return madd((1.0f-u),madd((1.0f-v),T(x0),v*T(x2)),u*madd((1.0f-v),T(x1),v*T(x3)));
  241. }
  242. /*! exchange */
  243. template<typename T> __forceinline void xchg ( T& a, T& b ) { const T tmp = a; a = b; b = tmp; }
  244. /*! bit reverse operation */
  245. template<class T>
  246. __forceinline T bitReverse(const T& vin)
  247. {
  248. T v = vin;
  249. v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
  250. v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
  251. v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
  252. v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
  253. v = ( v >> 16 ) | ( v << 16);
  254. return v;
  255. }
  256. /*! bit interleave operation */
  257. template<class T>
  258. __forceinline T bitInterleave(const T& xin, const T& yin, const T& zin)
  259. {
  260. T x = xin, y = yin, z = zin;
  261. x = (x | (x << 16)) & 0x030000FF;
  262. x = (x | (x << 8)) & 0x0300F00F;
  263. x = (x | (x << 4)) & 0x030C30C3;
  264. x = (x | (x << 2)) & 0x09249249;
  265. y = (y | (y << 16)) & 0x030000FF;
  266. y = (y | (y << 8)) & 0x0300F00F;
  267. y = (y | (y << 4)) & 0x030C30C3;
  268. y = (y | (y << 2)) & 0x09249249;
  269. z = (z | (z << 16)) & 0x030000FF;
  270. z = (z | (z << 8)) & 0x0300F00F;
  271. z = (z | (z << 4)) & 0x030C30C3;
  272. z = (z | (z << 2)) & 0x09249249;
  273. return x | (y << 1) | (z << 2);
  274. }
  275. #if defined(__AVX2__)
  276. template<>
  277. __forceinline unsigned int bitInterleave(const unsigned int &xi, const unsigned int& yi, const unsigned int& zi)
  278. {
  279. const unsigned int xx = pdep(xi,0x49249249 /* 0b01001001001001001001001001001001 */ );
  280. const unsigned int yy = pdep(yi,0x92492492 /* 0b10010010010010010010010010010010 */);
  281. const unsigned int zz = pdep(zi,0x24924924 /* 0b00100100100100100100100100100100 */);
  282. return xx | yy | zz;
  283. }
  284. #endif
  285. /*! bit interleave operation for 64bit data types*/
  286. template<class T>
  287. __forceinline T bitInterleave64(const T& xin, const T& yin, const T& zin){
  288. T x = xin & 0x1fffff;
  289. T y = yin & 0x1fffff;
  290. T z = zin & 0x1fffff;
  291. x = (x | x << 32) & 0x1f00000000ffff;
  292. x = (x | x << 16) & 0x1f0000ff0000ff;
  293. x = (x | x << 8) & 0x100f00f00f00f00f;
  294. x = (x | x << 4) & 0x10c30c30c30c30c3;
  295. x = (x | x << 2) & 0x1249249249249249;
  296. y = (y | y << 32) & 0x1f00000000ffff;
  297. y = (y | y << 16) & 0x1f0000ff0000ff;
  298. y = (y | y << 8) & 0x100f00f00f00f00f;
  299. y = (y | y << 4) & 0x10c30c30c30c30c3;
  300. y = (y | y << 2) & 0x1249249249249249;
  301. z = (z | z << 32) & 0x1f00000000ffff;
  302. z = (z | z << 16) & 0x1f0000ff0000ff;
  303. z = (z | z << 8) & 0x100f00f00f00f00f;
  304. z = (z | z << 4) & 0x10c30c30c30c30c3;
  305. z = (z | z << 2) & 0x1249249249249249;
  306. return x | (y << 1) | (z << 2);
  307. }
  308. }