Blender V5.3
BLI_math_base_c.hh
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
47
48#include "BLI_assert.hh"
49#include "BLI_math_constants.hh" // IWYU pragma: export
50#include "BLI_math_inline.hh" // IWYU pragma: export
51#include "BLI_sys_types.hh"
52
53#if defined(__GNUC__)
54# define NAN_FLT __builtin_nanf("")
55#else /* evil quiet NaN definition */
56static const int NAN_INT = 0x7FC00000;
57# define NAN_FLT (*((float *)(&NAN_INT)))
58#endif
59
60#if BLI_MATH_DO_INLINE
61# include "intern/math_base_inline.cc" // IWYU pragma: export
62#endif
63
64#ifdef BLI_MATH_GCC_WARN_PRAGMA
65# pragma GCC diagnostic push
66# pragma GCC diagnostic ignored "-Wredundant-decls"
67#endif
68
69namespace blender {
70
71/******************************* Float ******************************/
72
73/* `powf` is really slow for raising to integer powers. */
74
75MINLINE float pow2f(float x);
76MINLINE float pow3f(float x);
77MINLINE float pow4f(float x);
78MINLINE float pow7f(float x);
79
80MINLINE float sqrt3f(float f);
81MINLINE double sqrt3d(double d);
82
83MINLINE float sqrtf_signed(float f);
84
85/* Compute linear interpolation (lerp) between origin and target. */
86MINLINE float interpf(float target, float origin, float t);
87MINLINE double interpd(double target, double origin, double t);
88
89MINLINE float ratiof(float min, float max, float pos);
90MINLINE double ratiod(double min, double max, double pos);
91
92/* NOTE: Compilers will up-cast all types smaller than int to int when performing arithmetic
93 * operation. */
94
95MINLINE int square_s(short a);
96
97MINLINE int square_i(int a);
98MINLINE unsigned int square_uint(unsigned int a);
99MINLINE float square_f(float a);
100
101MINLINE int cube_i(int a);
102MINLINE float cube_f(float a);
103
104MINLINE float min_ff(float a, float b);
105MINLINE float max_ff(float a, float b);
106
107MINLINE double min_dd(double a, double b);
108MINLINE double max_dd(double a, double b);
109
110MINLINE int min_ii(int a, int b);
111MINLINE int max_ii(int a, int b);
112
115
116MINLINE int clamp_i(int value, int min, int max);
117MINLINE float clamp_f(float value, float min, float max);
118
124MINLINE int compare_ff(float a, float b, float max_diff);
144MINLINE uint ulp_diff_ff(float a, float b);
156MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps);
157MINLINE bool compare_threshold_relative(float value1, float value2, float thresh);
158
167MINLINE float increment_ulp(float value);
168
177MINLINE float decrement_ulp(float value);
178
179MINLINE float signf(float f);
180MINLINE int signum_i_ex(float a, float eps);
181MINLINE int signum_i(float a);
182
186MINLINE float power_of_2(float f);
187
192MINLINE int integer_digits_f(float f);
197MINLINE int integer_digits_d(double d);
198MINLINE int integer_digits_i(int i);
199
200/* These don't really fit anywhere but were being copied about a lot. */
201
202MINLINE int is_power_of_2_i(int n);
203
204MINLINE unsigned int log2_floor_u(unsigned int x);
205MINLINE unsigned int log2_ceil_u(unsigned int x);
206
210MINLINE int power_of_2_max_i(int n);
211MINLINE int power_of_2_min_i(int n);
212MINLINE unsigned int power_of_2_max_u(unsigned int x);
213
218MINLINE int divide_round_i(int a, int b);
219
225
231
240MINLINE int mod_i(int i, int n);
241
250MINLINE float floored_fmod(float f, float n);
251
255MINLINE float round_to_even(float f);
256
257MINLINE unsigned char round_fl_to_uchar(float a);
258MINLINE short round_fl_to_short(float a);
259MINLINE int round_fl_to_int(float a);
260MINLINE unsigned int round_fl_to_uint(float a);
261
262MINLINE int round_db_to_int(double a);
263
264MINLINE unsigned char round_fl_to_uchar_clamp(float a);
265MINLINE int round_fl_to_int_clamp(float a);
266
267MINLINE unsigned char round_db_to_uchar_clamp(double a);
268MINLINE short round_db_to_short_clamp(double a);
269MINLINE int round_db_to_int_clamp(double a);
270
271int pow_i(int base, int exp);
272
276double double_round(double x, int ndigits);
277
287float floor_power_of_10(float f);
297float ceil_power_of_10(float f);
298
299#ifdef BLI_MATH_GCC_WARN_PRAGMA
300# pragma GCC diagnostic pop
301#endif
302
303/* Asserts, some math functions expect normalized inputs
304 * check the vector is unit length, or zero length (which can't be helped in some cases). */
305
306#ifndef NDEBUG
308# define BLI_ASSERT_UNIT_EPSILON 0.0002f
309# define BLI_ASSERT_UNIT_EPSILON_DB 0.0002
315# define BLI_ASSERT_UNIT_V3(v) \
316 { \
317 const float _test_unit = len_squared_v3(v); \
318 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
319 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
320 } \
321 (void)0
322
323# define BLI_ASSERT_UNIT_V2(v) \
324 { \
325 const float _test_unit = len_squared_v2(v); \
326 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON) || \
327 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON)); \
328 } \
329 (void)0
330
331# define BLI_ASSERT_UNIT_QUAT(q) \
332 { \
333 const float _test_unit = dot_qtqt(q, q); \
334 BLI_assert(!(fabsf(_test_unit - 1.0f) >= BLI_ASSERT_UNIT_EPSILON * 10) || \
335 !(fabsf(_test_unit) >= BLI_ASSERT_UNIT_EPSILON * 10)); \
336 } \
337 (void)0
338
339# define BLI_ASSERT_ZERO_M3(m) \
340 { \
341 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 9) != 0.0); \
342 } \
343 (void)0
344
345# define BLI_ASSERT_ZERO_M4(m) \
346 { \
347 BLI_assert(dot_vn_vn((const float *)m, (const float *)m, 16) != 0.0); \
348 } \
349 (void)0
350# define BLI_ASSERT_UNIT_M3(m) \
351 { \
352 BLI_ASSERT_UNIT_V3((m)[0]); \
353 BLI_ASSERT_UNIT_V3((m)[1]); \
354 BLI_ASSERT_UNIT_V3((m)[2]); \
355 } \
356 (void)0
357#else
358# define BLI_ASSERT_UNIT_V2(v) (void)(v)
359# define BLI_ASSERT_UNIT_V3(v) (void)(v)
360# define BLI_ASSERT_UNIT_QUAT(v) (void)(v)
361# define BLI_ASSERT_ZERO_M3(m) (void)(m)
362# define BLI_ASSERT_ZERO_M4(m) (void)(m)
363# define BLI_ASSERT_UNIT_M3(m) (void)(m)
364#endif
365
366} // namespace blender
static const int NAN_INT
#define MINLINE
unsigned long long int uint64_t
btScalar max
#define exp
float pow4f(float x)
float pow2f(float x)
float pow7f(float x)
float pow3f(float x)
ccl_device_inline float signf(const float f)
Definition math_base.h:502
ccl_device_inline int power_of_2(const int a)
Definition math_base.h:679
BMesh Iterators.
MINLINE float min_ff(float a, float b)
MINLINE unsigned char round_db_to_uchar_clamp(double a)
MINLINE unsigned int round_fl_to_uint(float a)
MINLINE int round_fl_to_int_clamp(float a)
MINLINE double min_dd(double a, double b)
MINLINE short round_fl_to_short(float a)
MINLINE int mod_i(int i, int n)
MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps)
unsigned int uint
Definition BKE_text.h:55
MINLINE unsigned char round_fl_to_uchar(float a)
MINLINE int round_fl_to_int(float a)
MINLINE int square_i(int a)
MINLINE int power_of_2_min_i(int n)
MINLINE int round_db_to_int_clamp(double a)
MINLINE double interpd(double target, double origin, double t)
MINLINE int integer_digits_i(int i)
MINLINE int power_of_2_max_i(int n)
double double_round(double x, int ndigits)
Definition math_base.cc:30
MINLINE uint max_uu(uint a, uint b)
MINLINE int signum_i_ex(float a, float eps)
MINLINE int max_ii(int a, int b)
float ceil_power_of_10(float f)
Definition math_base.cc:76
MINLINE bool compare_threshold_relative(float value1, float value2, float thresh)
MINLINE float cube_f(float a)
MINLINE uint ulp_diff_ff(float a, float b)
MINLINE unsigned int power_of_2_max_u(unsigned int x)
MINLINE unsigned int log2_ceil_u(unsigned int x)
MINLINE float sqrtf_signed(float f)
MINLINE double ratiod(double min, double max, double pos)
MINLINE float increment_ulp(float value)
MINLINE int integer_digits_f(float f)
MINLINE int cube_i(int a)
MINLINE int divide_round_i(int a, int b)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
MINLINE uint64_t divide_ceil_ul(uint64_t a, uint64_t b)
MINLINE int signum_i(float a)
MINLINE double max_dd(double a, double b)
MINLINE float sqrt3f(float f)
MINLINE float decrement_ulp(float value)
MINLINE double sqrt3d(double d)
int pow_i(int base, int exp)
Definition math_base.cc:15
MINLINE unsigned int log2_floor_u(unsigned int x)
MINLINE float ratiof(float min, float max, float pos)
MINLINE float max_ff(float a, float b)
float floor_power_of_10(float f)
Definition math_base.cc:67
MINLINE uint divide_ceil_u(uint a, uint b)
MINLINE float square_f(float a)
MINLINE unsigned char round_fl_to_uchar_clamp(float a)
MINLINE float interpf(float target, float origin, float t)
MINLINE float round_to_even(float f)
MINLINE int square_s(short a)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
MINLINE short round_db_to_short_clamp(double a)
MINLINE int integer_digits_d(double d)
MINLINE int is_power_of_2_i(int n)
MINLINE int min_ii(int a, int b)
MINLINE int clamp_i(int value, int min, int max)
MINLINE unsigned int square_uint(unsigned int a)
MINLINE int compare_ff(float a, float b, float max_diff)
MINLINE int round_db_to_int(double a)
MINLINE float floored_fmod(float f, float n)
MINLINE float clamp_f(float value, float min, float max)
MINLINE uint min_uu(uint a, uint b)
const btScalar eps
Definition poly34.cpp:11
#define min(a, b)
Definition sort.cc:38