math_funcs.h
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 
32 #include "typedefs.h"
33 #include "math_defs.h"
34 
35 #ifndef NO_MATH_H
36 #include "math.h"
37 #endif
38 
39 class Math {
40 
41 
42  static uint32_t default_seed;
43 public:
44  Math() {}; // useless to instance
45 
46  enum {
47  RANDOM_MAX=2147483647L
48  };
49 
50  static double sin(double p_x);
51  static double cos(double p_x);
52  static double tan(double p_x);
53  static double sinh(double p_x);
54  static double cosh(double p_x);
55  static double tanh(double p_x);
56  static double asin(double p_x);
57  static double acos(double p_x);
58  static double atan(double p_x);
59  static double atan2(double p_y, double p_x);
60  static double deg2rad(double p_y);
61  static double rad2deg(double p_y);
62  static double sqrt(double p_x);
63  static double fmod(double p_x,double p_y);
64  static double fposmod(double p_x,double p_y);
65  static uint32_t rand_from_seed(uint32_t *seed);
66  static double floor(double p_x);
67  static double ceil(double p_x);
68  static double ease(double p_x, double p_c);
69  static int decimals(double p_step);
70  static double stepify(double p_value,double p_step);
71  static void seed(uint32_t x=0);
72  static void randomize();
73  static uint32_t larger_prime(uint32_t p_val);
74  static double dectime(double p_value,double p_amount, double p_step);
75 
76 
77  static inline double linear2db(double p_linear) {
78 
79  return Math::log( p_linear ) * 8.6858896380650365530225783783321;
80  }
81 
82  static inline double db2linear(double p_db) {
83 
84  return Math::exp( p_db * 0.11512925464970228420089957273422 );
85  }
86 
87  static bool is_nan(double p_val);
88  static bool is_inf(double p_val);
89 
90 
91 
92  static uint32_t rand();
93  static double randf();
94 
95  static double round(double p_val);
96 
97  static double random(double from, double to);
98 
99 
100  static _FORCE_INLINE_ real_t abs(real_t g) {
101 
102 #ifdef REAL_T_IS_DOUBLE
103 
104  return absd(g);
105 #else
106 
107  return absf(g);
108 #endif
109  }
110 
111  static _FORCE_INLINE_ float absf(float g) {
112 
113  union {
114  float f;
115  uint32_t i;
116  } u;
117 
118  u.f=g;
119  u.i&=2147483647u;
120  return u.f;
121  }
122 
123  static _FORCE_INLINE_ double absd(double g) {
124 
125  union {
126  double d;
127  uint64_t i;
128  } u;
129  u.d=g;
130  u.i&=(uint64_t)9223372036854775807ll;
131  return u.d;
132  }
133 
134  //this function should be as fast as possible and rounding mode should not matter
135  static _FORCE_INLINE_ int fast_ftoi(float a) {
136 
137  static int b;
138 
139 #if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP // windows 8 phone?
140  b = (int)((a>0.0f) ? (a + 0.5f):(a -0.5f));
141 
142 #elif defined(_MSC_VER) && _MSC_VER < 1800
143  __asm fld a
144  __asm fistp b
145 /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
146  // use AT&T inline assembly style, document that
147  // we use memory as output (=m) and input (m)
148  __asm__ __volatile__ (
149  "flds %1 \n\t"
150  "fistpl %0 \n\t"
151  : "=m" (b)
152  : "m" (a));*/
153 
154 #else
155  b=lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
156 #endif
157  return b;
158  }
159 
160 
161 #if defined(__GNUC__)
162 
163  static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; }
164 #else
165 
166  static _FORCE_INLINE_ int64_t dtoll(double p_double) { return (int64_t)p_double; }
167 #endif
168 
169  static _FORCE_INLINE_ float lerp(float a, float b, float c) {
170 
171  return a+(b-a)*c;
172  }
173 
174  static double pow(double x, double y);
175  static double log(double x);
176  static double exp(double x);
177 
178 };
179 
180 
181 #define Math_PI 3.14159265358979323846
182 #define Math_SQRT12 0.7071067811865475244008443621048490
183 
184 #endif // MATH_FUNCS_H
static _FORCE_INLINE_ float lerp(float a, float b, float c)
OPTIMIZE
Definition: math_funcs.h:169
Definition: math_funcs.h:39