math_funcs.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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-2016 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_ real_t abs(real_t g) {
  81. #ifdef REAL_T_IS_DOUBLE
  82. return absd(g);
  83. #else
  84. return absf(g);
  85. #endif
  86. }
  87. static _FORCE_INLINE_ float absf(float g) {
  88. union {
  89. float f;
  90. uint32_t i;
  91. } u;
  92. u.f=g;
  93. u.i&=2147483647u;
  94. return u.f;
  95. }
  96. static _FORCE_INLINE_ double absd(double g) {
  97. union {
  98. double d;
  99. uint64_t i;
  100. } u;
  101. u.d=g;
  102. u.i&=(uint64_t)9223372036854775807ll;
  103. return u.d;
  104. }
  105. //this function should be as fast as possible and rounding mode should not matter
  106. static _FORCE_INLINE_ int fast_ftoi(float a) {
  107. static int b;
  108. #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
  109. b = (int)((a>0.0f) ? (a + 0.5f):(a -0.5f));
  110. #elif defined(_MSC_VER) && _MSC_VER < 1800
  111. __asm fld a
  112. __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. static _FORCE_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h)
  138. {
  139. uint16_t h_exp, h_sig;
  140. uint32_t f_sgn, f_exp, f_sig;
  141. h_exp = (h&0x7c00u);
  142. f_sgn = ((uint32_t)h&0x8000u) << 16;
  143. switch (h_exp) {
  144. case 0x0000u: /* 0 or subnormal */
  145. h_sig = (h&0x03ffu);
  146. /* Signed zero */
  147. if (h_sig == 0) {
  148. return f_sgn;
  149. }
  150. /* Subnormal */
  151. h_sig <<= 1;
  152. while ((h_sig&0x0400u) == 0) {
  153. h_sig <<= 1;
  154. h_exp++;
  155. }
  156. f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
  157. f_sig = ((uint32_t)(h_sig&0x03ffu)) << 13;
  158. return f_sgn + f_exp + f_sig;
  159. case 0x7c00u: /* inf or NaN */
  160. /* All-ones exponent and a copy of the significand */
  161. return f_sgn + 0x7f800000u + (((uint32_t)(h&0x03ffu)) << 13);
  162. default: /* normalized */
  163. /* Just need to adjust the exponent and shift */
  164. return f_sgn + (((uint32_t)(h&0x7fffu) + 0x1c000u) << 13);
  165. }
  166. }
  167. static _FORCE_INLINE_ float halfptr_to_float(const uint16_t *h) {
  168. union {
  169. uint32_t u32;
  170. float f32;
  171. } u;
  172. u.u32=halfbits_to_floatbits(*h);
  173. return u.f32;
  174. }
  175. static _FORCE_INLINE_ uint16_t make_half_float(float f) {
  176. union {
  177. float fv;
  178. uint32_t ui;
  179. } ci;
  180. ci.fv=f;
  181. uint32_t x = ci.ui;
  182. uint32_t sign = (unsigned short)(x >> 31);
  183. uint32_t mantissa;
  184. uint32_t exp;
  185. uint16_t hf;
  186. // get mantissa
  187. mantissa = x & ((1 << 23) - 1);
  188. // get exponent bits
  189. exp = x & (0xFF << 23);
  190. if (exp >= 0x47800000)
  191. {
  192. // check if the original single precision float number is a NaN
  193. if (mantissa && (exp == (0xFF << 23)))
  194. {
  195. // we have a single precision NaN
  196. mantissa = (1 << 23) - 1;
  197. }
  198. else
  199. {
  200. // 16-bit half-float representation stores number as Inf
  201. mantissa = 0;
  202. }
  203. hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
  204. (uint16_t)(mantissa >> 13);
  205. }
  206. // check if exponent is <= -15
  207. else if (exp <= 0x38000000)
  208. {
  209. /*// store a denorm half-float value or zero
  210. exp = (0x38000000 - exp) >> 23;
  211. mantissa >>= (14 + exp);
  212. hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
  213. */
  214. hf=0; //denormals do not work for 3D, convert to zero
  215. }
  216. else
  217. {
  218. hf = (((uint16_t)sign) << 15) |
  219. (uint16_t)((exp - 0x38000000) >> 13) |
  220. (uint16_t)(mantissa >> 13);
  221. }
  222. return hf;
  223. }
  224. };
  225. #define Math_PI 3.14159265358979323846
  226. #define Math_SQRT12 0.7071067811865475244008443621048490
  227. #endif // MATH_FUNCS_H