Blender  V3.3
BLI_math_base.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
4 #pragma once
5 
10 #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
11 # define _USE_MATH_DEFINES
12 #endif
13 
14 #include "BLI_assert.h"
15 #include "BLI_math_inline.h"
16 #include "BLI_sys_types.h"
17 #include <math.h>
18 
19 #ifndef M_PI
20 # define M_PI 3.14159265358979323846 /* pi */
21 #endif
22 #ifndef M_PI_2
23 # define M_PI_2 1.57079632679489661923 /* pi/2 */
24 #endif
25 #ifndef M_PI_4
26 # define M_PI_4 0.78539816339744830962 /* pi/4 */
27 #endif
28 #ifndef M_SQRT2
29 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
30 #endif
31 #ifndef M_SQRT1_2
32 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
33 #endif
34 #ifndef M_SQRT3
35 # define M_SQRT3 1.73205080756887729352 /* sqrt(3) */
36 #endif
37 #ifndef M_SQRT1_3
38 # define M_SQRT1_3 0.57735026918962576450 /* 1/sqrt(3) */
39 #endif
40 #ifndef M_1_PI
41 # define M_1_PI 0.318309886183790671538 /* 1/pi */
42 #endif
43 #ifndef M_E
44 # define M_E 2.7182818284590452354 /* e */
45 #endif
46 #ifndef M_LOG2E
47 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
48 #endif
49 #ifndef M_LOG10E
50 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
51 #endif
52 #ifndef M_LN2
53 # define M_LN2 0.69314718055994530942 /* log_e 2 */
54 #endif
55 #ifndef M_LN10
56 # define M_LN10 2.30258509299404568402 /* log_e 10 */
57 #endif
58 
59 #if defined(__GNUC__)
60 # define NAN_FLT __builtin_nanf("")
61 #else /* evil quiet NaN definition */
62 static const int NAN_INT = 0x7FC00000;
63 # define NAN_FLT (*((float *)(&NAN_INT)))
64 #endif
65 
66 #if BLI_MATH_DO_INLINE
67 # include "intern/math_base_inline.c"
68 #endif
69 
70 #ifdef BLI_MATH_GCC_WARN_PRAGMA
71 # pragma GCC diagnostic push
72 # pragma GCC diagnostic ignored "-Wredundant-decls"
73 #endif
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 /******************************* Float ******************************/
80 
81 /* `powf` is really slow for raising to integer powers. */
82 
83 MINLINE float pow2f(float x);
84 MINLINE float pow3f(float x);
85 MINLINE float pow4f(float x);
86 MINLINE float pow7f(float x);
87 
88 MINLINE float sqrt3f(float f);
89 MINLINE double sqrt3d(double d);
90 
91 MINLINE float sqrtf_signed(float f);
92 
93 MINLINE float saacosf(float f);
94 MINLINE float saasinf(float f);
95 MINLINE float sasqrtf(float f);
96 MINLINE float saacos(float fac);
97 MINLINE float saasin(float fac);
98 MINLINE float sasqrt(float fac);
99 
100 MINLINE float interpf(float a, float b, float t);
101 MINLINE double interpd(double a, double b, double t);
102 
103 MINLINE float ratiof(float min, float max, float pos);
104 MINLINE double ratiod(double min, double max, double pos);
105 
109 MINLINE float scalenorm(float a, float b, float x);
113 MINLINE double scalenormd(double a, double b, double x);
114 
115 /* NOTE: Compilers will upcast all types smaller than int to int when performing arithmetic
116  * operation. */
117 
118 MINLINE int square_s(short a);
119 MINLINE int square_uchar(unsigned char a);
120 MINLINE int cube_s(short a);
121 MINLINE int cube_uchar(unsigned char a);
122 
123 MINLINE int square_i(int a);
124 MINLINE unsigned int square_uint(unsigned int a);
125 MINLINE float square_f(float a);
126 MINLINE double square_d(double a);
127 
128 MINLINE int cube_i(int a);
129 MINLINE unsigned int cube_uint(unsigned int a);
130 MINLINE float cube_f(float a);
131 MINLINE double cube_d(double a);
132 
133 MINLINE float min_ff(float a, float b);
134 MINLINE float max_ff(float a, float b);
135 MINLINE float min_fff(float a, float b, float c);
136 MINLINE float max_fff(float a, float b, float c);
137 MINLINE float min_ffff(float a, float b, float c, float d);
138 MINLINE float max_ffff(float a, float b, float c, float d);
139 
140 MINLINE double min_dd(double a, double b);
141 MINLINE double max_dd(double a, double b);
142 
143 MINLINE int min_ii(int a, int b);
144 MINLINE int max_ii(int a, int b);
145 MINLINE int min_iii(int a, int b, int c);
146 MINLINE int max_iii(int a, int b, int c);
147 MINLINE int min_iiii(int a, int b, int c, int d);
148 MINLINE int max_iiii(int a, int b, int c, int d);
149 
152 
153 MINLINE size_t min_zz(size_t a, size_t b);
154 MINLINE size_t max_zz(size_t a, size_t b);
155 
156 MINLINE char min_cc(char a, char b);
157 MINLINE char max_cc(char a, char b);
158 
159 MINLINE int clamp_i(int value, int min, int max);
160 MINLINE float clamp_f(float value, float min, float max);
161 MINLINE size_t clamp_z(size_t value, size_t min, size_t max);
162 
168 MINLINE int compare_ff(float a, float b, float max_diff);
180 MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps);
181 MINLINE bool compare_threshold_relative(float value1, float value2, float thresh);
182 
183 MINLINE float signf(float f);
184 MINLINE int signum_i_ex(float a, float eps);
185 MINLINE int signum_i(float a);
186 
190 MINLINE float power_of_2(float f);
191 
196 MINLINE int integer_digits_f(float f);
201 MINLINE int integer_digits_d(double d);
202 MINLINE int integer_digits_i(int i);
203 
204 /* These don't really fit anywhere but were being copied about a lot. */
205 
206 MINLINE int is_power_of_2_i(int n);
207 
208 MINLINE unsigned int log2_floor_u(unsigned int x);
209 MINLINE unsigned int log2_ceil_u(unsigned int x);
210 
214 MINLINE int power_of_2_max_i(int n);
215 MINLINE int power_of_2_min_i(int n);
216 MINLINE unsigned int power_of_2_max_u(unsigned int x);
217 MINLINE unsigned int power_of_2_min_u(unsigned int x);
218 
223 MINLINE int divide_round_i(int a, int b);
224 
230 
236 
240 MINLINE int mod_i(int i, int n);
241 
245 MINLINE float round_to_even(float f);
246 
247 MINLINE signed char round_fl_to_char(float a);
248 MINLINE unsigned char round_fl_to_uchar(float a);
249 MINLINE short round_fl_to_short(float a);
250 MINLINE unsigned short round_fl_to_ushort(float a);
251 MINLINE int round_fl_to_int(float a);
252 MINLINE unsigned int round_fl_to_uint(float a);
253 
254 MINLINE signed char round_db_to_char(double a);
255 MINLINE unsigned char round_db_to_uchar(double a);
256 MINLINE short round_db_to_short(double a);
257 MINLINE unsigned short round_db_to_ushort(double a);
258 MINLINE int round_db_to_int(double a);
259 MINLINE unsigned int round_db_to_uint(double a);
260 
261 MINLINE signed char round_fl_to_char_clamp(float a);
262 MINLINE unsigned char round_fl_to_uchar_clamp(float a);
263 MINLINE short round_fl_to_short_clamp(float a);
264 MINLINE unsigned short round_fl_to_ushort_clamp(float a);
265 MINLINE int round_fl_to_int_clamp(float a);
266 MINLINE unsigned int round_fl_to_uint_clamp(float a);
267 
268 MINLINE signed char round_db_to_char_clamp(double a);
269 MINLINE unsigned char round_db_to_uchar_clamp(double a);
270 MINLINE short round_db_to_short_clamp(double a);
271 MINLINE unsigned short round_db_to_ushort_clamp(double a);
272 MINLINE int round_db_to_int_clamp(double a);
273 MINLINE unsigned int round_db_to_uint_clamp(double a);
274 
275 int pow_i(int base, int exp);
276 
280 double double_round(double x, int ndigits);
281 
291 float floor_power_of_10(float f);
301 float ceil_power_of_10(float f);
302 
303 #ifdef BLI_MATH_GCC_WARN_PRAGMA
304 # pragma GCC diagnostic pop
305 #endif
306 
307 /* Asserts, some math functions expect normalized inputs
308  * check the vector is unit length, or zero length (which can't be helped in some cases). */
309 
310 #ifndef NDEBUG
312 # define BLI_ASSERT_UNIT_EPSILON 0.0002f
313 # define BLI_ASSERT_UNIT_EPSILON_DB 0.0002
319 # define BLI_ASSERT_UNIT_V3(v) \
320  { \
321  const float _test_unit = len_squared_v3(v); \
322  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
323  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
324  } \
325  (void)0
326 
327 # define BLI_ASSERT_UNIT_V3_DB(v) \
328  { \
329  const double _test_unit = len_squared_v3_db(v); \
330  BLI_assert(!(fabs(_test_unit - 1.0) >= BLI_ASSERT_UNIT_EPSILON_DB) || \
331  !(fabs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON_DB)); \
332  } \
333  (void)0
334 
335 # define BLI_ASSERT_UNIT_V2(v) \
336  { \
337  const float _test_unit = len_squared_v2(v); \
338  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
339  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
340  } \
341  (void)0
342 
343 # define BLI_ASSERT_UNIT_QUAT(q) \
344  { \
345  const float _test_unit = dot_qtqt(q, q); \
346  BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON * 10) || \
347  !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON * 10)); \
348  } \
349  (void)0
350 
351 # define BLI_ASSERT_ZERO_M3(m) \
352  { \
353  BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 9) != 0.0); \
354  } \
355  (void)0
356 
357 # define BLI_ASSERT_ZERO_M4(m) \
358  { \
359  BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 16) != 0.0); \
360  } \
361  (void)0
362 # define BLI_ASSERT_UNIT_M3(m) \
363  { \
364  BLI_ASSERT_UNIT_V3((m)[0]); \
365  BLI_ASSERT_UNIT_V3((m)[1]); \
366  BLI_ASSERT_UNIT_V3((m)[2]); \
367  } \
368  (void)0
369 #else
370 # define BLI_ASSERT_UNIT_V2(v) (void)(v)
371 # define BLI_ASSERT_UNIT_V3(v) (void)(v)
372 # define BLI_ASSERT_UNIT_V3_DB(v) (void)(v)
373 # define BLI_ASSERT_UNIT_QUAT(v) (void)(v)
374 # define BLI_ASSERT_ZERO_M3(m) (void)(m)
375 # define BLI_ASSERT_ZERO_M4(m) (void)(m)
376 # define BLI_ASSERT_UNIT_M3(m) (void)(m)
377 #endif
378 
379 #ifdef __cplusplus
380 }
381 #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 int integer_digits_f(float f)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
MINLINE float saacos(float fac)
MINLINE unsigned short round_fl_to_ushort_clamp(float a)
MINLINE uint min_uu(uint a, uint b)
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:12
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 uint divide_ceil_u(uint a, uint b)
MINLINE short round_db_to_short(double a)
MINLINE int power_of_2_max_i(int n)
MINLINE int integer_digits_d(double d)
MINLINE float min_ffff(float a, float b, float c, float d)
MINLINE unsigned int power_of_2_max_u(unsigned int x)
MINLINE uint max_uu(uint a, uint b)
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 double scalenormd(double a, double b, double x)
MINLINE size_t max_zz(size_t a, size_t b)
MINLINE int cube_s(short a)
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 uint64_t divide_ceil_ul(uint64_t a, uint64_t b)
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:64
MINLINE float saacosf(float f)
MINLINE signed char round_fl_to_char_clamp(float a)
MINLINE float cube_f(float a)
MINLINE int integer_digits_i(int i)
MINLINE unsigned short round_db_to_ushort_clamp(double a)
MINLINE float scalenorm(float a, float b, float x)
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:62
MINLINE int min_iii(int a, int b, int c)
MINLINE int divide_round_i(int a, int b)
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 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:73
MINLINE bool compare_threshold_relative(float value1, float value2, float thresh)
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)
MINLINE float round_to_even(float f)
double double_round(double x, int ndigits)
Definition: math_base.c:27
MINLINE int cube_uchar(unsigned char a)
MINLINE unsigned int square_uint(unsigned int a)
MINLINE int square_s(short a)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
MINLINE float sasqrt(float fac)
MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps)
MINLINE unsigned char round_db_to_uchar_clamp(double a)
MINLINE int square_uchar(unsigned char a)
MINLINE int compare_ff(float a, float b, float max_diff)
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
unsigned int uint
Definition: BLI_sys_types.h:67
_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
ccl_device_inline float3 exp(float3 v)
Definition: math_float3.h:392
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
const btScalar eps
Definition: poly34.cpp:11
#define min(a, b)
Definition: sort.c:35
unsigned __int64 uint64_t
Definition: stdint.h:90
float max