math_funcs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*************************************************************************/
  2. /* math_funcs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef MATH_FUNCS_H
  31. #define MATH_FUNCS_H
  32. #include "math_defs.h"
  33. #include "typedefs.h"
  34. #include "thirdparty/misc/pcg.h"
  35. #include <float.h>
  36. #include <math.h>
  37. #define Math_PI 3.14159265358979323846
  38. #define Math_TAU 6.28318530717958647692
  39. #define Math_SQRT12 0.7071067811865475244008443621048490
  40. #define Math_LN2 0.693147180559945309417
  41. #define Math_INF INFINITY
  42. #define Math_NAN NAN
  43. class Math {
  44. static pcg32_random_t default_pcg;
  45. public:
  46. Math() {} // useless to instance
  47. static const uint64_t RANDOM_MAX = 4294967295;
  48. static _ALWAYS_INLINE_ double sin(double p_x) { return ::sin(p_x); }
  49. static _ALWAYS_INLINE_ float sin(float p_x) { return ::sinf(p_x); }
  50. static _ALWAYS_INLINE_ double cos(double p_x) { return ::cos(p_x); }
  51. static _ALWAYS_INLINE_ float cos(float p_x) { return ::cosf(p_x); }
  52. static _ALWAYS_INLINE_ double tan(double p_x) { return ::tan(p_x); }
  53. static _ALWAYS_INLINE_ float tan(float p_x) { return ::tanf(p_x); }
  54. static _ALWAYS_INLINE_ double sinh(double p_x) { return ::sinh(p_x); }
  55. static _ALWAYS_INLINE_ float sinh(float p_x) { return ::sinhf(p_x); }
  56. static _ALWAYS_INLINE_ double cosh(double p_x) { return ::cosh(p_x); }
  57. static _ALWAYS_INLINE_ float cosh(float p_x) { return ::coshf(p_x); }
  58. static _ALWAYS_INLINE_ double tanh(double p_x) { return ::tanh(p_x); }
  59. static _ALWAYS_INLINE_ float tanh(float p_x) { return ::tanhf(p_x); }
  60. static _ALWAYS_INLINE_ double asin(double p_x) { return ::asin(p_x); }
  61. static _ALWAYS_INLINE_ float asin(float p_x) { return ::asinf(p_x); }
  62. static _ALWAYS_INLINE_ double acos(double p_x) { return ::acos(p_x); }
  63. static _ALWAYS_INLINE_ float acos(float p_x) { return ::acosf(p_x); }
  64. static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); }
  65. static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); }
  66. static _ALWAYS_INLINE_ double atan2(double p_y, double p_x) { return ::atan2(p_y, p_x); }
  67. static _ALWAYS_INLINE_ float atan2(float p_y, float p_x) { return ::atan2f(p_y, p_x); }
  68. static _ALWAYS_INLINE_ double sqrt(double p_x) { return ::sqrt(p_x); }
  69. static _ALWAYS_INLINE_ float sqrt(float p_x) { return ::sqrtf(p_x); }
  70. static _ALWAYS_INLINE_ double fmod(double p_x, double p_y) { return ::fmod(p_x, p_y); }
  71. static _ALWAYS_INLINE_ float fmod(float p_x, float p_y) { return ::fmodf(p_x, p_y); }
  72. static _ALWAYS_INLINE_ double floor(double p_x) { return ::floor(p_x); }
  73. static _ALWAYS_INLINE_ float floor(float p_x) { return ::floorf(p_x); }
  74. static _ALWAYS_INLINE_ double ceil(double p_x) { return ::ceil(p_x); }
  75. static _ALWAYS_INLINE_ float ceil(float p_x) { return ::ceilf(p_x); }
  76. static _ALWAYS_INLINE_ double pow(double p_x, double p_y) { return ::pow(p_x, p_y); }
  77. static _ALWAYS_INLINE_ float pow(float p_x, float p_y) { return ::powf(p_x, p_y); }
  78. static _ALWAYS_INLINE_ double log(double p_x) { return ::log(p_x); }
  79. static _ALWAYS_INLINE_ float log(float p_x) { return ::logf(p_x); }
  80. static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); }
  81. static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); }
  82. static _ALWAYS_INLINE_ bool is_nan(double p_val) {
  83. #ifdef _MSC_VER
  84. return _isnan(p_val);
  85. #elif defined(__GNUC__) && __GNUC__ < 6
  86. union {
  87. uint64_t u;
  88. double f;
  89. } ieee754;
  90. ieee754.f = p_val;
  91. // (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000
  92. return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000);
  93. #else
  94. return isnan(p_val);
  95. #endif
  96. }
  97. static _ALWAYS_INLINE_ bool is_nan(float p_val) {
  98. #ifdef _MSC_VER
  99. return _isnan(p_val);
  100. #elif defined(__GNUC__) && __GNUC__ < 6
  101. union {
  102. uint32_t u;
  103. float f;
  104. } ieee754;
  105. ieee754.f = p_val;
  106. // -----------------------------------
  107. // (single-precision floating-point)
  108. // NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  109. // : (> 0x7f800000)
  110. // where,
  111. // s : sign
  112. // x : non-zero number
  113. // -----------------------------------
  114. return ((ieee754.u & 0x7fffffff) > 0x7f800000);
  115. #else
  116. return isnan(p_val);
  117. #endif
  118. }
  119. static _ALWAYS_INLINE_ bool is_inf(double p_val) {
  120. #ifdef _MSC_VER
  121. return !_finite(p_val);
  122. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  123. #elif defined(__GNUC__) && __GNUC__ < 6
  124. union {
  125. uint64_t u;
  126. double f;
  127. } ieee754;
  128. ieee754.f = p_val;
  129. return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
  130. ((unsigned)ieee754.u == 0);
  131. #else
  132. return isinf(p_val);
  133. #endif
  134. }
  135. static _ALWAYS_INLINE_ bool is_inf(float p_val) {
  136. #ifdef _MSC_VER
  137. return !_finite(p_val);
  138. // use an inline implementation of isinf as a workaround for problematic libstdc++ versions from gcc 5.x era
  139. #elif defined(__GNUC__) && __GNUC__ < 6
  140. union {
  141. uint32_t u;
  142. float f;
  143. } ieee754;
  144. ieee754.f = p_val;
  145. return (ieee754.u & 0x7fffffff) == 0x7f800000;
  146. #else
  147. return isinf(p_val);
  148. #endif
  149. }
  150. static _ALWAYS_INLINE_ double abs(double g) { return absd(g); }
  151. static _ALWAYS_INLINE_ float abs(float g) { return absf(g); }
  152. static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; }
  153. static _ALWAYS_INLINE_ double fposmod(double p_x, double p_y) { return (p_x >= 0) ? Math::fmod(p_x, p_y) : p_y - Math::fmod(-p_x, p_y); }
  154. static _ALWAYS_INLINE_ float fposmod(float p_x, float p_y) { return (p_x >= 0) ? Math::fmod(p_x, p_y) : p_y - Math::fmod(-p_x, p_y); }
  155. static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
  156. static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }
  157. static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * 180.0 / Math_PI; }
  158. static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * 180.0 / Math_PI; }
  159. static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
  160. static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
  161. static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { return (p_value - p_from) / (p_to - p_from); }
  162. static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { return (p_value - p_from) / (p_to - p_from); }
  163. static _ALWAYS_INLINE_ double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
  164. static _ALWAYS_INLINE_ float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); }
  165. static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
  166. static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; }
  167. static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
  168. static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); }
  169. static _ALWAYS_INLINE_ double round(double p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
  170. static _ALWAYS_INLINE_ float round(float p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); }
  171. static int wrapi(int value, int min, int max);
  172. static float wrapf(float value, float min, float max);
  173. // double only, as these functions are mainly used by the editor and not performance-critical,
  174. static double ease(double p_x, double p_c);
  175. static int step_decimals(double p_step);
  176. static double stepify(double p_value, double p_step);
  177. static double dectime(double p_value, double p_amount, double p_step);
  178. static uint32_t larger_prime(uint32_t p_val);
  179. static void seed(uint64_t x);
  180. static void randomize();
  181. static uint32_t rand_from_seed(uint64_t *seed);
  182. static uint32_t rand();
  183. static _ALWAYS_INLINE_ double randf() { return (double)rand() / (double)Math::RANDOM_MAX; }
  184. static _ALWAYS_INLINE_ float randd() { return (float)rand() / (float)Math::RANDOM_MAX; }
  185. static double random(double from, double to);
  186. static float random(float from, float to);
  187. static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
  188. static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) {
  189. // TODO: Comparing floats for approximate-equality is non-trivial.
  190. // Using epsilon should cover the typical cases in Godot (where a == b is used to compare two reals), such as matrix and vector comparison operators.
  191. // A proper implementation in terms of ULPs should eventually replace the contents of this function.
  192. // See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for details.
  193. return abs(a - b) < CMP_EPSILON;
  194. }
  195. static _ALWAYS_INLINE_ float absf(float g) {
  196. union {
  197. float f;
  198. uint32_t i;
  199. } u;
  200. u.f = g;
  201. u.i &= 2147483647u;
  202. return u.f;
  203. }
  204. static _ALWAYS_INLINE_ double absd(double g) {
  205. union {
  206. double d;
  207. uint64_t i;
  208. } u;
  209. u.d = g;
  210. u.i &= (uint64_t)9223372036854775807ll;
  211. return u.d;
  212. }
  213. //this function should be as fast as possible and rounding mode should not matter
  214. static _ALWAYS_INLINE_ int fast_ftoi(float a) {
  215. static int b;
  216. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  217. b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
  218. #elif defined(_MSC_VER) && _MSC_VER < 1800
  219. __asm fld a __asm fistp b
  220. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  221. // use AT&T inline assembly style, document that
  222. // we use memory as output (=m) and input (m)
  223. __asm__ __volatile__ (
  224. "flds %1 \n\t"
  225. "fistpl %0 \n\t"
  226. : "=m" (b)
  227. : "m" (a));*/
  228. #else
  229. b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
  230. #endif
  231. return b;
  232. }
  233. #if defined(__GNUC__)
  234. static _ALWAYS_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  235. static _ALWAYS_INLINE_ int64_t dtoll(float p_float) { return (int64_t)p_float; } ///@TODO OPTIMIZE and rename
  236. #else
  237. static _ALWAYS_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  238. static _ALWAYS_INLINE_ int64_t dtoll(float p_float) { return (int64_t)p_float; } ///@TODO OPTIMIZE and rename
  239. #endif
  240. static _ALWAYS_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h) {
  241. uint16_t h_exp, h_sig;
  242. uint32_t f_sgn, f_exp, f_sig;
  243. h_exp = (h & 0x7c00u);
  244. f_sgn = ((uint32_t)h & 0x8000u) << 16;
  245. switch (h_exp) {
  246. case 0x0000u: /* 0 or subnormal */
  247. h_sig = (h & 0x03ffu);
  248. /* Signed zero */
  249. if (h_sig == 0) {
  250. return f_sgn;
  251. }
  252. /* Subnormal */
  253. h_sig <<= 1;
  254. while ((h_sig & 0x0400u) == 0) {
  255. h_sig <<= 1;
  256. h_exp++;
  257. }
  258. f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
  259. f_sig = ((uint32_t)(h_sig & 0x03ffu)) << 13;
  260. return f_sgn + f_exp + f_sig;
  261. case 0x7c00u: /* inf or NaN */
  262. /* All-ones exponent and a copy of the significand */
  263. return f_sgn + 0x7f800000u + (((uint32_t)(h & 0x03ffu)) << 13);
  264. default: /* normalized */
  265. /* Just need to adjust the exponent and shift */
  266. return f_sgn + (((uint32_t)(h & 0x7fffu) + 0x1c000u) << 13);
  267. }
  268. }
  269. static _ALWAYS_INLINE_ float halfptr_to_float(const uint16_t *h) {
  270. union {
  271. uint32_t u32;
  272. float f32;
  273. } u;
  274. u.u32 = halfbits_to_floatbits(*h);
  275. return u.f32;
  276. }
  277. static _ALWAYS_INLINE_ float half_to_float(const uint16_t h) {
  278. return halfptr_to_float(&h);
  279. }
  280. static _ALWAYS_INLINE_ uint16_t make_half_float(float f) {
  281. union {
  282. float fv;
  283. uint32_t ui;
  284. } ci;
  285. ci.fv = f;
  286. uint32_t x = ci.ui;
  287. uint32_t sign = (unsigned short)(x >> 31);
  288. uint32_t mantissa;
  289. uint32_t exp;
  290. uint16_t hf;
  291. // get mantissa
  292. mantissa = x & ((1 << 23) - 1);
  293. // get exponent bits
  294. exp = x & (0xFF << 23);
  295. if (exp >= 0x47800000) {
  296. // check if the original single precision float number is a NaN
  297. if (mantissa && (exp == (0xFF << 23))) {
  298. // we have a single precision NaN
  299. mantissa = (1 << 23) - 1;
  300. } else {
  301. // 16-bit half-float representation stores number as Inf
  302. mantissa = 0;
  303. }
  304. hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
  305. (uint16_t)(mantissa >> 13);
  306. }
  307. // check if exponent is <= -15
  308. else if (exp <= 0x38000000) {
  309. /*// store a denorm half-float value or zero
  310. exp = (0x38000000 - exp) >> 23;
  311. mantissa >>= (14 + exp);
  312. hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
  313. */
  314. hf = 0; //denormals do not work for 3D, convert to zero
  315. } else {
  316. hf = (((uint16_t)sign) << 15) |
  317. (uint16_t)((exp - 0x38000000) >> 13) |
  318. (uint16_t)(mantissa >> 13);
  319. }
  320. return hf;
  321. }
  322. static _ALWAYS_INLINE_ float snap_scalar(float p_offset, float p_step, float p_target) {
  323. return p_step != 0 ? Math::stepify(p_target - p_offset, p_step) + p_offset : p_target;
  324. }
  325. static _ALWAYS_INLINE_ float snap_scalar_seperation(float p_offset, float p_step, float p_target, float p_separation) {
  326. if (p_step != 0) {
  327. float a = Math::stepify(p_target - p_offset, p_step + p_separation) + p_offset;
  328. float b = a;
  329. if (p_target >= 0)
  330. b -= p_separation;
  331. else
  332. b += p_step;
  333. return (Math::abs(p_target - a) < Math::abs(p_target - b)) ? a : b;
  334. }
  335. return p_target;
  336. }
  337. };
  338. #endif // MATH_FUNCS_H