Blender  V2.93
BLI_math_base.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  *
19  * The Original Code is: some of this file.
20  */
21 
22 #pragma once
23 
28 #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
29 # define _USE_MATH_DEFINES
30 #endif
31 
32 #include "BLI_assert.h"
33 #include "BLI_math_inline.h"
34 #include "BLI_sys_types.h"
35 #include <math.h>
36 
37 #ifndef M_PI
38 # define M_PI 3.14159265358979323846 /* pi */
39 #endif
40 #ifndef M_PI_2
41 # define M_PI_2 1.57079632679489661923 /* pi/2 */
42 #endif
43 #ifndef M_PI_4
44 # define M_PI_4 0.78539816339744830962 /* pi/4 */
45 #endif
46 #ifndef M_SQRT2
47 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
48 #endif
49 #ifndef M_SQRT1_2
50 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
51 #endif
52 #ifndef M_SQRT3
53 # define M_SQRT3 1.73205080756887729352 /* sqrt(3) */
54 #endif
55 #ifndef M_SQRT1_3
56 # define M_SQRT1_3 0.57735026918962576450 /* 1/sqrt(3) */
57 #endif
58 #ifndef M_1_PI
59 # define M_1_PI 0.318309886183790671538 /* 1/pi */
60 #endif
61 #ifndef M_E
62 # define M_E 2.7182818284590452354 /* e */
63 #endif
64 #ifndef M_LOG2E
65 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
66 #endif
67 #ifndef M_LOG10E
68 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
69 #endif
70 #ifndef M_LN2
71 # define M_LN2 0.69314718055994530942 /* log_e 2 */
72 #endif
73 #ifndef M_LN10
74 # define M_LN10 2.30258509299404568402 /* log_e 10 */
75 #endif
76 
77 #if defined(__GNUC__)
78 # define NAN_FLT __builtin_nanf("")
79 #else
80 /* evil quiet NaN definition */
81 static const int NAN_INT = 0x7FC00000;
82 # define NAN_FLT (*((float *)(&NAN_INT)))
83 #endif
84 
85 #if BLI_MATH_DO_INLINE
86 # include "intern/math_base_inline.c"
87 #endif
88 
89 #ifdef BLI_MATH_GCC_WARN_PRAGMA
90 # pragma GCC diagnostic push
91 # pragma GCC diagnostic ignored "-Wredundant-decls"
92 #endif
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 /******************************* Float ******************************/
99 
100 MINLINE float pow2f(float x);
101 MINLINE float pow3f(float x);
102 MINLINE float pow4f(float x);
103 MINLINE float pow7f(float x);
104 
105 MINLINE float sqrt3f(float f);
106 MINLINE double sqrt3d(double d);
107 
108 MINLINE float sqrtf_signed(float f);
109 
110 MINLINE float saacosf(float f);
111 MINLINE float saasinf(float f);
112 MINLINE float sasqrtf(float f);
113 MINLINE float saacos(float fac);
114 MINLINE float saasin(float fac);
115 MINLINE float sasqrt(float fac);
116 
117 MINLINE float interpf(float a, float b, float t);
118 MINLINE double interpd(double a, double b, double t);
119 
120 MINLINE float ratiof(float min, float max, float pos);
121 MINLINE double ratiod(double min, double max, double pos);
122 
123 /* NOTE: Compilers will upcast all types smaller than int to int when performing arithmetic
124  * operation. */
125 MINLINE int square_s(short a);
126 MINLINE int square_uchar(unsigned char a);
127 MINLINE int cube_s(short a);
128 MINLINE int cube_uchar(unsigned char a);
129 
130 MINLINE int square_i(int a);
131 MINLINE unsigned int square_uint(unsigned int a);
132 MINLINE float square_f(float a);
133 MINLINE double square_d(double a);
134 
135 MINLINE int cube_i(int a);
136 MINLINE unsigned int cube_uint(unsigned int a);
137 MINLINE float cube_f(float a);
138 MINLINE double cube_d(double a);
139 
140 MINLINE float min_ff(float a, float b);
141 MINLINE float max_ff(float a, float b);
142 MINLINE float min_fff(float a, float b, float c);
143 MINLINE float max_fff(float a, float b, float c);
144 MINLINE float min_ffff(float a, float b, float c, float d);
145 MINLINE float max_ffff(float a, float b, float c, float d);
146 
147 MINLINE double min_dd(double a, double b);
148 MINLINE double max_dd(double a, double b);
149 
150 MINLINE int min_ii(int a, int b);
151 MINLINE int max_ii(int a, int b);
152 MINLINE int min_iii(int a, int b, int c);
153 MINLINE int max_iii(int a, int b, int c);
154 MINLINE int min_iiii(int a, int b, int c, int d);
155 MINLINE int max_iiii(int a, int b, int c, int d);
156 
157 MINLINE size_t min_zz(size_t a, size_t b);
158 MINLINE size_t max_zz(size_t a, size_t b);
159 
160 MINLINE char min_cc(char a, char b);
161 MINLINE char max_cc(char a, char b);
162 
163 MINLINE int clamp_i(int value, int min, int max);
164 MINLINE float clamp_f(float value, float min, float max);
165 MINLINE size_t clamp_z(size_t value, size_t min, size_t max);
166 
167 MINLINE int compare_ff(float a, float b, const float max_diff);
168 MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps);
169 
170 MINLINE float signf(float f);
171 MINLINE int signum_i_ex(float a, float eps);
172 MINLINE int signum_i(float a);
173 
174 MINLINE float power_of_2(float f);
175 
176 MINLINE int integer_digits_f(const float f);
177 MINLINE int integer_digits_d(const double d);
178 MINLINE int integer_digits_i(const int i);
179 
180 /* these don't really fit anywhere but were being copied about a lot */
181 MINLINE int is_power_of_2_i(int n);
182 MINLINE int power_of_2_max_i(int n);
183 MINLINE int power_of_2_min_i(int n);
184 
185 MINLINE unsigned int power_of_2_max_u(unsigned int x);
186 MINLINE unsigned int power_of_2_min_u(unsigned int x);
187 MINLINE unsigned int log2_floor_u(unsigned int x);
188 MINLINE unsigned int log2_ceil_u(unsigned int x);
189 
190 MINLINE int divide_round_i(int a, int b);
191 MINLINE int mod_i(int i, int n);
192 
193 MINLINE signed char round_fl_to_char(float a);
194 MINLINE unsigned char round_fl_to_uchar(float a);
195 MINLINE short round_fl_to_short(float a);
196 MINLINE unsigned short round_fl_to_ushort(float a);
197 MINLINE int round_fl_to_int(float a);
198 MINLINE unsigned int round_fl_to_uint(float a);
199 
200 MINLINE signed char round_db_to_char(double a);
201 MINLINE unsigned char round_db_to_uchar(double a);
202 MINLINE short round_db_to_short(double a);
203 MINLINE unsigned short round_db_to_ushort(double a);
204 MINLINE int round_db_to_int(double a);
205 MINLINE unsigned int round_db_to_uint(double a);
206 
207 MINLINE signed char round_fl_to_char_clamp(float a);
208 MINLINE unsigned char round_fl_to_uchar_clamp(float a);
209 MINLINE short round_fl_to_short_clamp(float a);
210 MINLINE unsigned short round_fl_to_ushort_clamp(float a);
211 MINLINE int round_fl_to_int_clamp(float a);
212 MINLINE unsigned int round_fl_to_uint_clamp(float a);
213 
214 MINLINE signed char round_db_to_char_clamp(double a);
215 MINLINE unsigned char round_db_to_uchar_clamp(double a);
216 MINLINE short round_db_to_short_clamp(double a);
217 MINLINE unsigned short round_db_to_ushort_clamp(double a);
218 MINLINE int round_db_to_int_clamp(double a);
219 MINLINE unsigned int round_db_to_uint_clamp(double a);
220 
221 int pow_i(int base, int exp);
222 double double_round(double x, int ndigits);
223 
224 float floor_power_of_10(float f);
225 float ceil_power_of_10(float f);
226 
227 #ifdef BLI_MATH_GCC_WARN_PRAGMA
228 # pragma GCC diagnostic pop
229 #endif
230 
231 /* asserts, some math functions expect normalized inputs
232  * check the vector is unit length, or zero length (which can't be helped in some cases).
233  */
234 #ifndef NDEBUG
236 # define BLI_ASSERT_UNIT_EPSILON 0.0002f
242 # define BLI_ASSERT_UNIT_V3(v) \
243  { \
244  const float _test_unit = len_squared_v3(v); \
245  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
246  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
247  } \
248  (void)0
249 
250 # define BLI_ASSERT_UNIT_V3_DB(v) \
251  { \
252  const double _test_unit = len_squared_v3_db(v); \
253  BLI_assert(!(fabs(_test_unit - 1.0) >= BLI_ASSERT_UNIT_EPSILON) || \
254  !(fabs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
255  } \
256  (void)0
257 
258 # define BLI_ASSERT_UNIT_V2(v) \
259  { \
260  const float _test_unit = len_squared_v2(v); \
261  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
262  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
263  } \
264  (void)0
265 
266 # define BLI_ASSERT_UNIT_QUAT(q) \
267  { \
268  const float _test_unit = dot_qtqt(q, q); \
269  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON * 10) || \
270  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON * 10)); \
271  } \
272  (void)0
273 
274 # define BLI_ASSERT_ZERO_M3(m) \
275  { \
276  BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 9) != 0.0); \
277  } \
278  (void)0
279 
280 # define BLI_ASSERT_ZERO_M4(m) \
281  { \
282  BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 16) != 0.0); \
283  } \
284  (void)0
285 # define BLI_ASSERT_UNIT_M3(m) \
286  { \
287  BLI_ASSERT_UNIT_V3((m)[0]); \
288  BLI_ASSERT_UNIT_V3((m)[1]); \
289  BLI_ASSERT_UNIT_V3((m)[2]); \
290  } \
291  (void)0
292 #else
293 # define BLI_ASSERT_UNIT_V2(v) (void)(v)
294 # define BLI_ASSERT_UNIT_V3(v) (void)(v)
295 # define BLI_ASSERT_UNIT_QUAT(v) (void)(v)
296 # define BLI_ASSERT_ZERO_M3(m) (void)(m)
297 # define BLI_ASSERT_ZERO_M4(m) (void)(m)
298 # define BLI_ASSERT_UNIT_M3(m) (void)(m)
299 #endif
300 
301 #ifdef __cplusplus
302 }
303 #endif
MINLINE int round_fl_to_int_clamp(float a)
MINLINE unsigned char round_fl_to_uchar(float a)
MINLINE float max_fff(float a, float b, float c)
MINLINE float saacos(float fac)
MINLINE unsigned short round_fl_to_ushort_clamp(float a)
MINLINE float max_ffff(float a, float b, float c, float d)
MINLINE unsigned int round_db_to_uint_clamp(double a)
MINLINE unsigned int log2_ceil_u(unsigned int x)
MINLINE signed char round_fl_to_char(float a)
MINLINE int power_of_2_min_i(int n)
MINLINE float sasqrtf(float f)
MINLINE int round_fl_to_int(float a)
MINLINE short round_db_to_short_clamp(double a)
MINLINE signed char round_db_to_char_clamp(double a)
MINLINE float max_ff(float a, float b)
int pow_i(int base, int exp)
Definition: math_base.c:30
MINLINE unsigned int cube_uint(unsigned int a)
MINLINE size_t min_zz(size_t a, size_t b)
MINLINE int min_ii(int a, int b)
MINLINE short round_db_to_short(double a)
MINLINE int power_of_2_max_i(int n)
MINLINE int compare_ff(float a, float b, const float max_diff)
MINLINE float min_ffff(float a, float b, float c, float d)
MINLINE unsigned int power_of_2_max_u(unsigned int x)
MINLINE int cube_i(int a)
MINLINE unsigned short round_fl_to_ushort(float a)
MINLINE float pow2f(float x)
MINLINE double interpd(double a, double b, double t)
MINLINE unsigned int round_fl_to_uint_clamp(float a)
MINLINE double square_d(double a)
MINLINE double ratiod(double min, double max, double pos)
MINLINE float saasinf(float f)
MINLINE float clamp_f(float value, float min, float max)
MINLINE float min_ff(float a, float b)
MINLINE size_t max_zz(size_t a, size_t b)
MINLINE int cube_s(short a)
MINLINE int integer_digits_d(const double d)
MINLINE int square_i(int a)
MINLINE short round_fl_to_short_clamp(float a)
MINLINE int max_ii(int a, int b)
MINLINE short round_fl_to_short(float a)
MINLINE unsigned int round_db_to_uint(double a)
MINLINE unsigned int power_of_2_min_u(unsigned int x)
MINLINE double min_dd(double a, double b)
float floor_power_of_10(float f)
Definition: math_base.c:91
MINLINE float saacosf(float f)
MINLINE signed char round_fl_to_char_clamp(float a)
MINLINE float cube_f(float a)
MINLINE unsigned short round_db_to_ushort_clamp(double a)
MINLINE unsigned char round_fl_to_uchar_clamp(float a)
MINLINE double cube_d(double a)
static const int NAN_INT
Definition: BLI_math_base.h:81
MINLINE int min_iii(int a, int b, int c)
MINLINE int divide_round_i(int a, int b)
MINLINE int integer_digits_f(const float f)
MINLINE int integer_digits_i(const int i)
MINLINE int mod_i(int i, int n)
MINLINE float power_of_2(float f)
MINLINE float square_f(float a)
MINLINE unsigned int round_fl_to_uint(float a)
MINLINE float sqrtf_signed(float f)
MINLINE double max_dd(double a, double b)
MINLINE float interpf(float a, float b, float t)
MINLINE int round_db_to_int_clamp(double a)
MINLINE char min_cc(char a, char b)
MINLINE signed char round_db_to_char(double a)
MINLINE int is_power_of_2_i(int n)
MINLINE float pow3f(float x)
MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps)
MINLINE double sqrt3d(double d)
MINLINE int round_db_to_int(double a)
MINLINE int max_iiii(int a, int b, int c, int d)
MINLINE float min_fff(float a, float b, float c)
MINLINE int signum_i_ex(float a, float eps)
MINLINE int min_iiii(int a, int b, int c, int d)
MINLINE float saasin(float fac)
MINLINE unsigned int log2_floor_u(unsigned int x)
MINLINE float signf(float f)
MINLINE int max_iii(int a, int b, int c)
float ceil_power_of_10(float f)
Definition: math_base.c:109
MINLINE size_t clamp_z(size_t value, size_t min, size_t max)
MINLINE unsigned short round_db_to_ushort(double a)
MINLINE int clamp_i(int value, int min, int max)
MINLINE int signum_i(float a)
MINLINE float ratiof(float min, float max, float pos)
double double_round(double x, int ndigits)
Definition: math_base.c:47
MINLINE int cube_uchar(unsigned char a)
MINLINE unsigned int square_uint(unsigned int a)
MINLINE int square_s(short a)
MINLINE float sasqrt(float fac)
MINLINE unsigned char round_db_to_uchar_clamp(double a)
MINLINE int square_uchar(unsigned char a)
MINLINE float pow4f(float x)
MINLINE float sqrt3f(float f)
MINLINE char max_cc(char a, char b)
MINLINE float pow7f(float x)
MINLINE unsigned char round_db_to_uchar(double a)
#define MINLINE
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
uint pos
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:295
const btScalar eps
Definition: poly34.cpp:11
#define min(a, b)
Definition: sort.c:51
float max