Blender V4.3
BLI_math_base.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
48#if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
49# define _USE_MATH_DEFINES
50#endif
51
52#include "BLI_assert.h"
53#include "BLI_math_inline.h"
54#include "BLI_sys_types.h"
55#include <math.h>
56
57#ifndef M_PI
58# define M_PI 3.14159265358979323846 /* pi */
59#endif
60#ifndef M_PI_2
61# define M_PI_2 1.57079632679489661923 /* pi/2 */
62#endif
63#ifndef M_PI_4
64# define M_PI_4 0.78539816339744830962 /* pi/4 */
65#endif
66#ifndef M_SQRT2
67# define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
68#endif
69#ifndef M_SQRT1_2
70# define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
71#endif
72#ifndef M_SQRT3
73# define M_SQRT3 1.73205080756887729352 /* sqrt(3) */
74#endif
75#ifndef M_SQRT1_3
76# define M_SQRT1_3 0.57735026918962576450 /* 1/sqrt(3) */
77#endif
78#ifndef M_1_PI
79# define M_1_PI 0.318309886183790671538 /* 1/pi */
80#endif
81#ifndef M_E
82# define M_E 2.7182818284590452354 /* e */
83#endif
84#ifndef M_LOG2E
85# define M_LOG2E 1.4426950408889634074 /* log_2 e */
86#endif
87#ifndef M_LOG10E
88# define M_LOG10E 0.43429448190325182765 /* log_10 e */
89#endif
90#ifndef M_LN2
91# define M_LN2 0.69314718055994530942 /* log_e 2 */
92#endif
93#ifndef M_LN10
94# define M_LN10 2.30258509299404568402 /* log_e 10 */
95#endif
96
97#if defined(__GNUC__)
98# define NAN_FLT __builtin_nanf("")
99#else /* evil quiet NaN definition */
100static const int NAN_INT = 0x7FC00000;
101# define NAN_FLT (*((float *)(&NAN_INT)))
102#endif
103
104#if BLI_MATH_DO_INLINE
106#endif
107
108#ifdef BLI_MATH_GCC_WARN_PRAGMA
109# pragma GCC diagnostic push
110# pragma GCC diagnostic ignored "-Wredundant-decls"
111#endif
112
113#ifdef __cplusplus
114extern "C" {
115#endif
116
117/******************************* Float ******************************/
118
119/* `powf` is really slow for raising to integer powers. */
120
121MINLINE float pow2f(float x);
122MINLINE float pow3f(float x);
123MINLINE float pow4f(float x);
124MINLINE float pow7f(float x);
125
126MINLINE float sqrt3f(float f);
127MINLINE double sqrt3d(double d);
128
129MINLINE float sqrtf_signed(float f);
130
131/* Compute linear interpolation (lerp) between origin and target. */
132MINLINE float interpf(float target, float origin, float t);
133MINLINE double interpd(double target, double origin, double t);
134
135MINLINE float ratiof(float min, float max, float pos);
136MINLINE double ratiod(double min, double max, double pos);
137
141MINLINE float scalenorm(float a, float b, float x);
145MINLINE double scalenormd(double a, double b, double x);
146
147/* NOTE: Compilers will up-cast all types smaller than int to int when performing arithmetic
148 * operation. */
149
150MINLINE int square_s(short a);
151MINLINE int square_uchar(unsigned char a);
152MINLINE int cube_s(short a);
153MINLINE int cube_uchar(unsigned char a);
154
155MINLINE int square_i(int a);
156MINLINE unsigned int square_uint(unsigned int a);
157MINLINE float square_f(float a);
158MINLINE double square_d(double a);
159
160MINLINE int cube_i(int a);
161MINLINE unsigned int cube_uint(unsigned int a);
162MINLINE float cube_f(float a);
163MINLINE double cube_d(double a);
164
165MINLINE float min_ff(float a, float b);
166MINLINE float max_ff(float a, float b);
167MINLINE float min_fff(float a, float b, float c);
168MINLINE float max_fff(float a, float b, float c);
169MINLINE float min_ffff(float a, float b, float c, float d);
170MINLINE float max_ffff(float a, float b, float c, float d);
171
172MINLINE double min_dd(double a, double b);
173MINLINE double max_dd(double a, double b);
174MINLINE double min_ddd(double a, double b, double c);
175MINLINE double max_ddd(double a, double b, double c);
176
177MINLINE int min_ii(int a, int b);
178MINLINE int max_ii(int a, int b);
179MINLINE int min_iii(int a, int b, int c);
180MINLINE int max_iii(int a, int b, int c);
181MINLINE int min_iiii(int a, int b, int c, int d);
182MINLINE int max_iiii(int a, int b, int c, int d);
183
186
187MINLINE size_t min_zz(size_t a, size_t b);
188MINLINE size_t max_zz(size_t a, size_t b);
189
190MINLINE char min_cc(char a, char b);
191MINLINE char max_cc(char a, char b);
192
193MINLINE int clamp_i(int value, int min, int max);
194MINLINE float clamp_f(float value, float min, float max);
195MINLINE size_t clamp_z(size_t value, size_t min, size_t max);
196
202MINLINE int compare_ff(float a, float b, float max_diff);
222MINLINE uint ulp_diff_ff(float a, float b);
234MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps);
235MINLINE bool compare_threshold_relative(float value1, float value2, float thresh);
236
237MINLINE float signf(float f);
238MINLINE int signum_i_ex(float a, float eps);
239MINLINE int signum_i(float a);
240
244MINLINE float power_of_2(float f);
245
250MINLINE int integer_digits_f(float f);
255MINLINE int integer_digits_d(double d);
256MINLINE int integer_digits_i(int i);
257
258/* These don't really fit anywhere but were being copied about a lot. */
259
260MINLINE int is_power_of_2_i(int n);
261
262MINLINE unsigned int log2_floor_u(unsigned int x);
263MINLINE unsigned int log2_ceil_u(unsigned int x);
264
268MINLINE int power_of_2_max_i(int n);
269MINLINE int power_of_2_min_i(int n);
270MINLINE unsigned int power_of_2_max_u(unsigned int x);
271MINLINE unsigned int power_of_2_min_u(unsigned int x);
272
277MINLINE int divide_round_i(int a, int b);
278
284
290
299MINLINE int mod_i(int i, int n);
300
309MINLINE float floored_fmod(float f, float n);
310
314MINLINE float round_to_even(float f);
315
316MINLINE signed char round_fl_to_char(float a);
317MINLINE unsigned char round_fl_to_uchar(float a);
318MINLINE short round_fl_to_short(float a);
319MINLINE unsigned short round_fl_to_ushort(float a);
320MINLINE int round_fl_to_int(float a);
321MINLINE unsigned int round_fl_to_uint(float a);
322
323MINLINE signed char round_db_to_char(double a);
324MINLINE unsigned char round_db_to_uchar(double a);
325MINLINE short round_db_to_short(double a);
326MINLINE unsigned short round_db_to_ushort(double a);
327MINLINE int round_db_to_int(double a);
328MINLINE unsigned int round_db_to_uint(double a);
329
330MINLINE signed char round_fl_to_char_clamp(float a);
331MINLINE unsigned char round_fl_to_uchar_clamp(float a);
332MINLINE short round_fl_to_short_clamp(float a);
333MINLINE unsigned short round_fl_to_ushort_clamp(float a);
334MINLINE int round_fl_to_int_clamp(float a);
335MINLINE unsigned int round_fl_to_uint_clamp(float a);
336
337MINLINE signed char round_db_to_char_clamp(double a);
338MINLINE unsigned char round_db_to_uchar_clamp(double a);
339MINLINE short round_db_to_short_clamp(double a);
340MINLINE unsigned short round_db_to_ushort_clamp(double a);
341MINLINE int round_db_to_int_clamp(double a);
342MINLINE unsigned int round_db_to_uint_clamp(double a);
343
344int pow_i(int base, int exp);
345
349double double_round(double x, int ndigits);
350
360float floor_power_of_10(float f);
370float ceil_power_of_10(float f);
371
372#ifdef BLI_MATH_GCC_WARN_PRAGMA
373# pragma GCC diagnostic pop
374#endif
375
376/* Asserts, some math functions expect normalized inputs
377 * check the vector is unit length, or zero length (which can't be helped in some cases). */
378
379#ifndef NDEBUG
381# define BLI_ASSERT_UNIT_EPSILON 0.0002f
382# define BLI_ASSERT_UNIT_EPSILON_DB 0.0002
388# define BLI_ASSERT_UNIT_V3(v) \
389 { \
390 const float _test_unit = len_squared_v3(v); \
391 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
392 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
393 } \
394 (void)0
395
396# define BLI_ASSERT_UNIT_V3_DB(v) \
397 { \
398 const double _test_unit = len_squared_v3_db(v); \
399 BLI_assert(!(fabs(_test_unit - 1.0) >= BLI_ASSERT_UNIT_EPSILON_DB) || \
400 !(fabs(_test_unit) >= BLI_ASSERT_UNIT_EPSILON_DB)); \
401 } \
402 (void)0
403
404# define BLI_ASSERT_UNIT_V2(v) \
405 { \
406 const float _test_unit = len_squared_v2(v); \
407 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
408 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
409 } \
410 (void)0
411
412# define BLI_ASSERT_UNIT_QUAT(q) \
413 { \
414 const float _test_unit = dot_qtqt(q, q); \
415 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON * 10) || \
416 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON * 10)); \
417 } \
418 (void)0
419
420# define BLI_ASSERT_ZERO_M3(m) \
421 { \
422 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 9) != 0.0); \
423 } \
424 (void)0
425
426# define BLI_ASSERT_ZERO_M4(m) \
427 { \
428 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 16) != 0.0); \
429 } \
430 (void)0
431# define BLI_ASSERT_UNIT_M3(m) \
432 { \
433 BLI_ASSERT_UNIT_V3((m)[0]); \
434 BLI_ASSERT_UNIT_V3((m)[1]); \
435 BLI_ASSERT_UNIT_V3((m)[2]); \
436 } \
437 (void)0
438#else
439# define BLI_ASSERT_UNIT_V2(v) (void)(v)
440# define BLI_ASSERT_UNIT_V3(v) (void)(v)
441# define BLI_ASSERT_UNIT_V3_DB(v) (void)(v)
442# define BLI_ASSERT_UNIT_QUAT(v) (void)(v)
443# define BLI_ASSERT_ZERO_M3(m) (void)(m)
444# define BLI_ASSERT_ZERO_M4(m) (void)(m)
445# define BLI_ASSERT_UNIT_M3(m) (void)(m)
446#endif
447
448#ifdef __cplusplus
449}
450#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 double max_ddd(double a, double b, double c)
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 double min_ddd(double a, double b, double c)
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:13
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 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 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:65
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
MINLINE int min_iii(int a, int b, int c)
MINLINE float floored_fmod(float f, float n)
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 uint ulp_diff_ff(float a, float b)
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 float interpf(float target, float origin, float t)
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 unsigned int log2_floor_u(unsigned int x)
MINLINE double interpd(double target, double origin, double t)
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:74
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:28
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 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
local_group_size(16, 16) .push_constant(Type b
ccl_device_inline float3 exp(float3 v)
const btScalar eps
Definition poly34.cpp:11
#define min(a, b)
Definition sort.c:32
unsigned __int64 uint64_t
Definition stdint.h:90