math_funcs.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************/
  2. /* math_funcs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef NO_MATH_H
  35. #include "math.h"
  36. #endif
  37. class Math {
  38. static uint32_t default_seed;
  39. public:
  40. Math(){}; // useless to instance
  41. enum {
  42. RANDOM_MAX = 2147483647L
  43. };
  44. static double sin(double p_x);
  45. static double cos(double p_x);
  46. static double tan(double p_x);
  47. static double sinh(double p_x);
  48. static double cosh(double p_x);
  49. static double tanh(double p_x);
  50. static double asin(double p_x);
  51. static double acos(double p_x);
  52. static double atan(double p_x);
  53. static double atan2(double p_y, double p_x);
  54. static double deg2rad(double p_y);
  55. static double rad2deg(double p_y);
  56. static double sqrt(double p_x);
  57. static double fmod(double p_x, double p_y);
  58. static double fposmod(double p_x, double p_y);
  59. static uint32_t rand_from_seed(uint32_t *seed);
  60. static double floor(double p_x);
  61. static double ceil(double p_x);
  62. static double ease(double p_x, double p_c);
  63. static int step_decimals(double p_step);
  64. static double stepify(double p_value, double p_step);
  65. static void seed(uint32_t x = 0);
  66. static void randomize();
  67. static uint32_t larger_prime(uint32_t p_val);
  68. static double dectime(double p_value, double p_amount, double p_step);
  69. static inline double linear2db(double p_linear) {
  70. return Math::log(p_linear) * 8.6858896380650365530225783783321;
  71. }
  72. static inline double db2linear(double p_db) {
  73. return Math::exp(p_db * 0.11512925464970228420089957273422);
  74. }
  75. static bool is_nan(double p_val);
  76. static bool is_inf(double p_val);
  77. static uint32_t rand();
  78. static double randf();
  79. static double round(double p_val);
  80. static double random(double from, double to);
  81. static _FORCE_INLINE_ real_t abs(real_t g) {
  82. #ifdef REAL_T_IS_DOUBLE
  83. return absd(g);
  84. #else
  85. return absf(g);
  86. #endif
  87. }
  88. static _FORCE_INLINE_ float absf(float g) {
  89. union {
  90. float f;
  91. uint32_t i;
  92. } u;
  93. u.f = g;
  94. u.i &= 2147483647u;
  95. return u.f;
  96. }
  97. static _FORCE_INLINE_ double absd(double g) {
  98. union {
  99. double d;
  100. uint64_t i;
  101. } u;
  102. u.d = g;
  103. u.i &= (uint64_t)9223372036854775807ll;
  104. return u.d;
  105. }
  106. //this function should be as fast as possible and rounding mode should not matter
  107. static _FORCE_INLINE_ int fast_ftoi(float a) {
  108. static int b;
  109. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  110. b = (int)((a > 0.0f) ? (a + 0.5f) : (a - 0.5f));
  111. #elif defined(_MSC_VER) && _MSC_VER < 1800
  112. __asm fld a __asm fistp b
  113. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  114. // use AT&T inline assembly style, document that
  115. // we use memory as output (=m) and input (m)
  116. __asm__ __volatile__ (
  117. "flds %1 \n\t"
  118. "fistpl %0 \n\t"
  119. : "=m" (b)
  120. : "m" (a));*/
  121. #else
  122. b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
  123. #endif
  124. return b;
  125. }
  126. #if defined(__GNUC__)
  127. static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  128. #else
  129. static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  130. #endif
  131. static _FORCE_INLINE_ float lerp(float a, float b, float c) {
  132. return a + (b - a) * c;
  133. }
  134. static double pow(double x, double y);
  135. static double log(double x);
  136. static double exp(double x);
  137. };
  138. #define Math_PI 3.14159265358979323846
  139. #define Math_SQRT12 0.7071067811865475244008443621048490
  140. #endif // MATH_FUNCS_H