math_funcs.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*************************************************************************/
  2. /* math_funcs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef MATH_FUNCS_H
  30. #define MATH_FUNCS_H
  31. #include "typedefs.h"
  32. #include "math_defs.h"
  33. #ifndef NO_MATH_H
  34. #include "math.h"
  35. #endif
  36. class Math {
  37. static uint32_t default_seed;
  38. public:
  39. Math() {}; // useless to instance
  40. enum {
  41. RANDOM_MAX=2147483647L
  42. };
  43. static double sin(double p_x);
  44. static double cos(double p_x);
  45. static double tan(double p_x);
  46. static double sinh(double p_x);
  47. static double cosh(double p_x);
  48. static double tanh(double p_x);
  49. static double asin(double p_x);
  50. static double acos(double p_x);
  51. static double atan(double p_x);
  52. static double atan2(double p_y, double p_x);
  53. static double deg2rad(double p_y);
  54. static double rad2deg(double p_y);
  55. static double sqrt(double p_x);
  56. static double fmod(double p_x,double p_y);
  57. static double fposmod(double p_x,double p_y);
  58. static uint32_t rand_from_seed(uint32_t *seed);
  59. static double floor(double p_x);
  60. static double ceil(double p_x);
  61. static double ease(double p_x, double p_c);
  62. static int step_decimals(double p_step);
  63. static double stepify(double p_value,double p_step);
  64. static void seed(uint32_t x=0);
  65. static void randomize();
  66. static uint32_t larger_prime(uint32_t p_val);
  67. static double dectime(double p_value,double p_amount, double p_step);
  68. static inline double linear2db(double p_linear) {
  69. return Math::log( p_linear ) * 8.6858896380650365530225783783321;
  70. }
  71. static inline double db2linear(double p_db) {
  72. return Math::exp( p_db * 0.11512925464970228420089957273422 );
  73. }
  74. static bool is_nan(double p_val);
  75. static bool is_inf(double p_val);
  76. static uint32_t rand();
  77. static double randf();
  78. static double round(double p_val);
  79. static double random(double from, double to);
  80. static _FORCE_INLINE_ bool isequal_approx(real_t a, real_t b) {
  81. // TODO: Comparing floats for approximate-equality is non-trivial.
  82. // 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.
  83. // A proper implementation in terms of ULPs should eventually replace the contents of this function.
  84. // See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for details.
  85. return abs(a-b) < CMP_EPSILON;
  86. }
  87. static _FORCE_INLINE_ real_t abs(real_t g) {
  88. #ifdef REAL_T_IS_DOUBLE
  89. return absd(g);
  90. #else
  91. return absf(g);
  92. #endif
  93. }
  94. static _FORCE_INLINE_ float absf(float g) {
  95. union {
  96. float f;
  97. uint32_t i;
  98. } u;
  99. u.f=g;
  100. u.i&=2147483647u;
  101. return u.f;
  102. }
  103. static _FORCE_INLINE_ double absd(double g) {
  104. union {
  105. double d;
  106. uint64_t i;
  107. } u;
  108. u.d=g;
  109. u.i&=(uint64_t)9223372036854775807ll;
  110. return u.d;
  111. }
  112. //this function should be as fast as possible and rounding mode should not matter
  113. static _FORCE_INLINE_ int fast_ftoi(float a) {
  114. static int b;
  115. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  116. b = (int)((a>0.0f) ? (a + 0.5f):(a -0.5f));
  117. #elif defined(_MSC_VER) && _MSC_VER < 1800
  118. __asm fld a
  119. __asm fistp b
  120. /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
  121. // use AT&T inline assembly style, document that
  122. // we use memory as output (=m) and input (m)
  123. __asm__ __volatile__ (
  124. "flds %1 \n\t"
  125. "fistpl %0 \n\t"
  126. : "=m" (b)
  127. : "m" (a));*/
  128. #else
  129. b=lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
  130. #endif
  131. return b;
  132. }
  133. #if defined(__GNUC__)
  134. static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  135. #else
  136. static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; } ///@TODO OPTIMIZE
  137. #endif
  138. static _FORCE_INLINE_ float lerp(float a, float b, float c) {
  139. return a+(b-a)*c;
  140. }
  141. static double pow(double x, double y);
  142. static double log(double x);
  143. static double exp(double x);
  144. static _FORCE_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h)
  145. {
  146. uint16_t h_exp, h_sig;
  147. uint32_t f_sgn, f_exp, f_sig;
  148. h_exp = (h&0x7c00u);
  149. f_sgn = ((uint32_t)h&0x8000u) << 16;
  150. switch (h_exp) {
  151. case 0x0000u: /* 0 or subnormal */
  152. h_sig = (h&0x03ffu);
  153. /* Signed zero */
  154. if (h_sig == 0) {
  155. return f_sgn;
  156. }
  157. /* Subnormal */
  158. h_sig <<= 1;
  159. while ((h_sig&0x0400u) == 0) {
  160. h_sig <<= 1;
  161. h_exp++;
  162. }
  163. f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
  164. f_sig = ((uint32_t)(h_sig&0x03ffu)) << 13;
  165. return f_sgn + f_exp + f_sig;
  166. case 0x7c00u: /* inf or NaN */
  167. /* All-ones exponent and a copy of the significand */
  168. return f_sgn + 0x7f800000u + (((uint32_t)(h&0x03ffu)) << 13);
  169. default: /* normalized */
  170. /* Just need to adjust the exponent and shift */
  171. return f_sgn + (((uint32_t)(h&0x7fffu) + 0x1c000u) << 13);
  172. }
  173. }
  174. static _FORCE_INLINE_ float halfptr_to_float(const uint16_t *h) {
  175. union {
  176. uint32_t u32;
  177. float f32;
  178. } u;
  179. u.u32=halfbits_to_floatbits(*h);
  180. return u.f32;
  181. }
  182. static _FORCE_INLINE_ uint16_t make_half_float(float f) {
  183. union {
  184. float fv;
  185. uint32_t ui;
  186. } ci;
  187. ci.fv=f;
  188. uint32_t x = ci.ui;
  189. uint32_t sign = (unsigned short)(x >> 31);
  190. uint32_t mantissa;
  191. uint32_t exp;
  192. uint16_t hf;
  193. // get mantissa
  194. mantissa = x & ((1 << 23) - 1);
  195. // get exponent bits
  196. exp = x & (0xFF << 23);
  197. if (exp >= 0x47800000)
  198. {
  199. // check if the original single precision float number is a NaN
  200. if (mantissa && (exp == (0xFF << 23)))
  201. {
  202. // we have a single precision NaN
  203. mantissa = (1 << 23) - 1;
  204. }
  205. else
  206. {
  207. // 16-bit half-float representation stores number as Inf
  208. mantissa = 0;
  209. }
  210. hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
  211. (uint16_t)(mantissa >> 13);
  212. }
  213. // check if exponent is <= -15
  214. else if (exp <= 0x38000000)
  215. {
  216. /*// store a denorm half-float value or zero
  217. exp = (0x38000000 - exp) >> 23;
  218. mantissa >>= (14 + exp);
  219. hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
  220. */
  221. hf=0; //denormals do not work for 3D, convert to zero
  222. }
  223. else
  224. {
  225. hf = (((uint16_t)sign) << 15) |
  226. (uint16_t)((exp - 0x38000000) >> 13) |
  227. (uint16_t)(mantissa >> 13);
  228. }
  229. return hf;
  230. }
  231. };
  232. #define Math_PI 3.14159265358979323846
  233. #define Math_SQRT12 0.7071067811865475244008443621048490
  234. #endif // MATH_FUNCS_H