Blender V5.3
math_base_inline.cc
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
8
9#pragma once
10
11#include <float.h>
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16
17#include "BLI_assert.hh"
18#include "BLI_math_inline.hh"
19#include "BLI_sys_types.hh"
20
21namespace blender {
22
23/* copied from BLI_utildefines.hh */
24#ifdef __GNUC__
25# define UNLIKELY(x) __builtin_expect(!!(x), 0)
26#else
27# define UNLIKELY(x) (x)
28#endif
29
30MINLINE float pow2f(float x)
31{
32 return x * x;
33}
34MINLINE float pow3f(float x)
35{
36 return pow2f(x) * x;
37}
38MINLINE float pow4f(float x)
39{
40 return pow2f(pow2f(x));
41}
42MINLINE float pow5f(float x)
43{
44 return pow4f(x) * x;
45}
46MINLINE float pow7f(float x)
47{
48 return pow2f(pow3f(x)) * x;
49}
50
51MINLINE float sqrt3f(float f)
52{
53 if (f == 0.0f) [[unlikely]] {
54 return 0.0f;
55 }
56 if (f < 0.0f) [[unlikely]] {
57 return -float(exp(log(-f) / 3.0));
58 }
59 return float(exp(log(f) / 3.0));
60}
61
62MINLINE double sqrt3d(double d)
63{
64 if (d == 0.0) [[unlikely]] {
65 return 0.0;
66 }
67 if (d < 0.0) [[unlikely]] {
68 return -exp(log(-d) / 3.0);
69 }
70 return exp(log(d) / 3.0);
71}
72
73MINLINE float sqrtf_signed(float f)
74{
75 return (f >= 0.0f) ? sqrtf(f) : -sqrtf(-f);
76}
77
78MINLINE float interpf(float target, float origin, float fac)
79{
80 return (fac * target) + (1.0f - fac) * origin;
81}
82
83MINLINE double interpd(double target, double origin, double fac)
84{
85 return (fac * target) + (1.0f - fac) * origin;
86}
87
88MINLINE float ratiof(float min, float max, float pos)
89{
90 float range = max - min;
91 return range == 0 ? 0 : ((pos - min) / range);
92}
93
94MINLINE double ratiod(double min, double max, double pos)
95{
96 double range = max - min;
97 return range == 0 ? 0 : ((pos - min) / range);
98}
99
100MINLINE float power_of_2(float val)
101{
102 return float(pow(2.0, ceil(log(double(val)) / M_LN2)));
103}
104
106{
107 return (n & (n - 1)) == 0;
108}
109
111{
112 if (is_power_of_2_i(n)) {
113 return n;
114 }
115
116 do {
117 n = n & (n - 1);
118 } while (!is_power_of_2_i(n));
119
120 return n * 2;
121}
122
124{
125 while (!is_power_of_2_i(n)) {
126 n = n & (n - 1);
127 }
128
129 return n;
130}
131
132MINLINE unsigned int power_of_2_max_u(unsigned int x)
133{
134 x -= 1;
135 x |= (x >> 1);
136 x |= (x >> 2);
137 x |= (x >> 4);
138 x |= (x >> 8);
139 x |= (x >> 16);
140 return x + 1;
141}
142
143MINLINE unsigned int log2_floor_u(unsigned int x)
144{
145 return x <= 1 ? 0 : 1 + log2_floor_u(x >> 1);
146}
147
148MINLINE unsigned int log2_ceil_u(unsigned int x)
149{
150 if (is_power_of_2_i(int(x))) {
151 return log2_floor_u(x);
152 }
153 return log2_floor_u(x) + 1;
154}
155
156/* rounding and clamping */
157
158#define _round_clamp_fl_impl(arg, ty, min, max) \
159 { \
160 float r = floorf(arg + 0.5f); \
161 if (r <= float(min)) [[unlikely]] { \
162 return (ty)min; \
163 } \
164 if (r >= float(max)) [[unlikely]] { \
165 return (ty)max; \
166 } \
167 return (ty)r; \
168 }
169
170#define _round_clamp_db_impl(arg, ty, min, max) \
171 { \
172 double r = floor(arg + 0.5); \
173 if (r <= double(min)) [[unlikely]] { \
174 return (ty)min; \
175 } \
176 if (r >= double(max)) [[unlikely]] { \
177 return (ty)max; \
178 } \
179 return (ty)r; \
180 }
181
182#define _round_fl_impl(arg, ty) \
183 { \
184 return (ty)floorf(arg + 0.5f); \
185 }
186#define _round_db_impl(arg, ty) \
187 { \
188 return (ty)floor(arg + 0.5); \
189 }
190
191MINLINE unsigned char round_fl_to_uchar(float a){_round_fl_impl(a, unsigned char)} MINLINE
192 short round_fl_to_short(float a){_round_fl_impl(a, short)} MINLINE
194 unsigned int round_fl_to_uint(float a){_round_fl_impl(a, unsigned int)}
195
197
198#undef _round_fl_impl
199#undef _round_db_impl
200
201MINLINE unsigned char round_fl_to_uchar_clamp(float a){
202 _round_clamp_fl_impl(a, unsigned char, 0, UCHAR_MAX)} MINLINE
203 int round_fl_to_int_clamp(float a){_round_clamp_fl_impl(a, int, INT_MIN, INT_MAX)}
204
205MINLINE unsigned char round_db_to_uchar_clamp(double a){
206 _round_clamp_db_impl(a, unsigned char, 0, UCHAR_MAX)} MINLINE
207 short round_db_to_short_clamp(double a){
208 _round_clamp_db_impl(a, short, SHRT_MIN, SHRT_MAX)} MINLINE
209 int round_db_to_int_clamp(double a){_round_clamp_db_impl(a, int, INT_MIN, INT_MAX)}
210
211#undef _round_clamp_fl_impl
212#undef _round_clamp_db_impl
213
214MINLINE float round_to_even(float f)
215{
216 return roundf(f * 0.5f) * 2.0f;
217}
218
219MINLINE int divide_round_i(int a, int b)
220{
221 return (2 * a + b) / (2 * b);
222}
223
228MINLINE int divide_floor_i(int a, int b)
229{
230 int d = a / b;
231 int r = a % b; /* Optimizes into a single division. */
232 return r ? d - ((a < 0) ^ (b < 0)) : d;
233}
234
236{
237 return (a + b - 1) / b;
238}
239
241{
242 return (a + b - 1) / b;
243}
244
246{
247 return divide_ceil_u(a, b) * b;
248}
249
254
255MINLINE int mod_i(int i, int n)
256{
257 return (i % n + n) % n;
258}
259
260MINLINE float floored_fmod(const float f, const float n)
261{
262 return f - n * floorf(f / n);
263}
264
265MINLINE float fractf(float a)
266{
267 return a - floorf(a);
268}
269
270/* Adapted from `godot-engine` math_funcs.h. */
271MINLINE float wrapf(float value, float max, float min)
272{
273 float range = max - min;
274 return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min;
275}
276
277MINLINE float pingpongf(float value, float scale)
278{
279 if (scale == 0.0f) {
280 return 0.0f;
281 }
282 return fabsf(fractf((value - scale) / (scale * 2.0f)) * scale * 2.0f - scale);
283}
284
285/* Square. */
286
287MINLINE int square_s(short a)
288{
289 return a * a;
290}
291
293{
294 return a * a;
295}
296
297MINLINE unsigned int square_uint(unsigned int a)
298{
299 return a * a;
300}
301
302MINLINE float square_f(float a)
303{
304 return a * a;
305}
306
307/* Cube. */
308
309MINLINE int cube_i(int a)
310{
311 return a * a * a;
312}
313
314MINLINE float cube_f(float a)
315{
316 return a * a * a;
317}
318
319/* Min/max */
320
321MINLINE float min_ff(float a, float b)
322{
323 return (a < b) ? a : b;
324}
325MINLINE float max_ff(float a, float b)
326{
327 return (a > b) ? a : b;
328}
329/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
330MINLINE float smoothminf(float a, float b, float c)
331{
332 if (c != 0.0f) {
333 float h = max_ff(c - fabsf(a - b), 0.0f) / c;
334 return min_ff(a, b) - h * h * h * c * (1.0f / 6.0f);
335 }
336 return min_ff(a, b);
337}
338
339MINLINE float smoothstep(float edge0, float edge1, float x)
340{
341 float result;
342 if (x < edge0) {
343 result = 0.0f;
344 }
345 else if (x >= edge1) {
346 result = 1.0f;
347 }
348 else {
349 float t = (x - edge0) / (edge1 - edge0);
350 result = (3.0f - 2.0f * t) * (t * t);
351 }
352 return result;
353}
354
355MINLINE double min_dd(double a, double b)
356{
357 return (a < b) ? a : b;
358}
359MINLINE double max_dd(double a, double b)
360{
361 return (a > b) ? a : b;
362}
363
364MINLINE int min_ii(int a, int b)
365{
366 return (a < b) ? a : b;
367}
368MINLINE int max_ii(int a, int b)
369{
370 return (b < a) ? a : b;
371}
372
374{
375 return (a < b) ? a : b;
376}
378{
379 return (b < a) ? a : b;
380}
381
382MINLINE unsigned long long min_ulul(unsigned long long a, unsigned long long b)
383{
384 return (a < b) ? a : b;
385}
386MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
387{
388 return (b < a) ? a : b;
389}
390
391MINLINE size_t min_zz(size_t a, size_t b)
392{
393 return (a < b) ? a : b;
394}
395MINLINE size_t max_zz(size_t a, size_t b)
396{
397 return (b < a) ? a : b;
398}
399
400MINLINE int clamp_i(int value, int min, int max)
401{
402 return min_ii(max_ii(value, min), max);
403}
404
405MINLINE float clamp_f(float value, float min, float max)
406{
407 if (value > max) {
408 return max;
409 }
410 if (value < min) {
411 return min;
412 }
413 return value;
414}
415
416MINLINE int compare_ff(float a, float b, const float max_diff)
417{
418 return fabsf(a - b) <= max_diff;
419}
420
421MINLINE uint ulp_diff_ff(float a, float b)
422{
423 BLI_assert(sizeof(float) == sizeof(uint));
424
425 const uint sign_bit = 0x80000000;
426 const uint infinity = 0x7f800000;
427
428 union {
429 float f;
430 uint i;
431 } ua, ub;
432 ua.f = a;
433 ub.f = b;
434
435 const uint a_sign = ua.i & sign_bit;
436 const uint b_sign = ub.i & sign_bit;
437 const uint a_abs = ua.i & ~sign_bit;
438 const uint b_abs = ub.i & ~sign_bit;
439
440 if (a_abs > infinity || b_abs > infinity) {
441 /* NaNs always return maximum ulps apart. */
442 return 0xffffffff;
443 }
444 if (a_sign == b_sign) {
445 const uint min_abs = a_abs < b_abs ? a_abs : b_abs;
446 const uint max_abs = a_abs > b_abs ? a_abs : b_abs;
447 return max_abs - min_abs;
448 }
449 return a_abs + b_abs;
450}
451
452MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps)
453{
454 BLI_assert(max_ulps >= 0 && max_ulps < (1 << 22));
455
456 if (fabsf(a - b) <= max_diff) {
457 return 1;
458 }
459
460 return (ulp_diff_ff(a, b) <= uint(max_ulps)) ? 1 : 0;
461}
462
463MINLINE bool compare_threshold_relative(const float value1, const float value2, const float thresh)
464{
465 const float abs_diff = fabsf(value1 - value2);
466 /* Avoid letting the threshold get too small just because the values happen to be close to zero.
467 */
468 if (fabsf(value2) < 1) {
469 return abs_diff > thresh;
470 }
471 /* Using relative threshold in general. */
472 return abs_diff > thresh * fabsf(value2);
473}
474
475MINLINE float increment_ulp(const float value)
476{
477 if (!isfinite(value)) {
478 return value;
479 }
480
481 union {
482 float f;
483 uint i;
484 } v;
485 v.f = value;
486
487 if (v.f > 0.0f) {
488 v.i += 1;
489 }
490 else if (v.f < -0.0f) {
491 v.i -= 1;
492 }
493 else {
494 v.i = 0x00000001;
495 }
496
497 return v.f;
498}
499
500MINLINE float decrement_ulp(const float value)
501{
502 if (!isfinite(value)) {
503 return value;
504 }
505
506 union {
507 float f;
508 uint i;
509 } v;
510 v.f = value;
511
512 if (v.f > 0.0f) {
513 v.i -= 1;
514 }
515 else if (v.f < -0.0f) {
516 v.i += 1;
517 }
518 else {
519 v.i = 0x80000001;
520 }
521
522 return v.f;
523}
524
525MINLINE float signf(float f)
526{
527 return (f < 0.0f) ? -1.0f : 1.0f;
528}
529
531{
532 if (f > 0.0f) {
533 return 1.0f;
534 }
535 if (f < 0.0f) {
536 return -1.0f;
537 }
538 return 0.0f;
539}
540
541MINLINE int signum_i_ex(float a, float eps)
542{
543 if (a > eps) {
544 return 1;
545 }
546 if (a < -eps) {
547 return -1;
548 }
549 return 0;
550}
551
552MINLINE int signum_i(float a)
553{
554 if (a > 0.0f) {
555 return 1;
556 }
557 if (a < 0.0f) {
558 return -1;
559 }
560 return 0;
561}
562
563MINLINE int integer_digits_f(const float f)
564{
565 return (f == 0.0f) ? 0 : int(floor(log10(fabs(f)))) + 1;
566}
567
568MINLINE int integer_digits_d(const double d)
569{
570 return (d == 0.0) ? 0 : int(floor(log10(fabs(d)))) + 1;
571}
572
573MINLINE int integer_digits_i(const int i)
574{
575 return int(log10(double(i))) + 1;
576}
577
578/* Low level conversion functions */
579MINLINE unsigned char unit_float_to_uchar_clamp(float val)
580{
581 return static_cast<unsigned char>(
582 ((val <= 0.0f) ? 0 : ((val > (1.0f - 0.5f / 255.0f)) ? 255 : ((255.0f * val) + 0.5f))));
583}
584
585MINLINE unsigned short unit_float_to_ushort_clamp(float val)
586{
587 return static_cast<unsigned short>((val >= 1.0f - 0.5f / 65535) ? 65535 :
588 (val <= 0.0f) ? 0 :
589 (val * 65535.0f + 0.5f));
590}
591
592MINLINE unsigned char unit_ushort_to_uchar(unsigned short val)
593{
594 return static_cast<unsigned char>(((val) >= 65535 - 128) ? 255 : ((val) + 128) >> 8);
595}
596
597#define unit_float_to_uchar_clamp_v3(v1, v2) \
598 { \
599 (v1)[0] = unit_float_to_uchar_clamp((v2[0])); \
600 (v1)[1] = unit_float_to_uchar_clamp((v2[1])); \
601 (v1)[2] = unit_float_to_uchar_clamp((v2[2])); \
602 } \
603 ((void)0)
604#define unit_float_to_uchar_clamp_v4(v1, v2) \
605 { \
606 (v1)[0] = unit_float_to_uchar_clamp((v2[0])); \
607 (v1)[1] = unit_float_to_uchar_clamp((v2[1])); \
608 (v1)[2] = unit_float_to_uchar_clamp((v2[2])); \
609 (v1)[3] = unit_float_to_uchar_clamp((v2[3])); \
610 } \
611 ((void)0)
612
613} // namespace blender
#define M_LN2
#define MINLINE
btScalar max
#define roundf(x)
#define SHRT_MAX
#define INT_MAX
#define log
#define pow
#define exp
#define floor
#define ceil
float3x3 scale(float3x3 mat, float2 scale)
#define _round_clamp_fl_impl(arg, ty, min, max)
#define _round_fl_impl(arg, ty)
#define _round_db_impl(arg, ty)
#define _round_clamp_db_impl(arg, ty, min, max)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
MINLINE double min_dd(double a, double b)
MINLINE unsigned long long min_ulul(unsigned long long a, unsigned long long b)
unsigned __int64 uint64_t
Definition BKE_text.h:91
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)
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
MINLINE float wrapf(float value, float max, float min)
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)
MINLINE float fractf(float a)
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)
ATTR_WARN_UNUSED_RESULT const BMVert * v
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 float pow3f(float x)
MINLINE unsigned int log2_ceil_u(unsigned int x)
MINLINE float sqrtf_signed(float f)
MINLINE float pow4f(float x)
MINLINE double ratiod(double min, double max, double pos)
MINLINE float pow2f(float x)
MINLINE float increment_ulp(float value)
MINLINE int integer_digits_f(float f)
MINLINE size_t max_zz(size_t a, size_t b)
MINLINE int cube_i(int a)
MINLINE float pow5f(float x)
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 float pingpongf(float value, float scale)
MINLINE float signf(float f)
MINLINE size_t min_zz(size_t a, size_t b)
MINLINE float power_of_2(float f)
MINLINE double max_dd(double a, double b)
MINLINE float sqrt3f(float f)
MINLINE float decrement_ulp(float value)
MINLINE float pow7f(float x)
MINLINE float smoothstep(float edge0, float edge1, float x)
MINLINE float compatible_signf(float f)
MINLINE double sqrt3d(double d)
MINLINE float smoothminf(float a, float b, float c)
MINLINE unsigned char unit_ushort_to_uchar(unsigned short val)
MINLINE unsigned int log2_floor_u(unsigned int x)
MINLINE unsigned short unit_float_to_ushort_clamp(float val)
MINLINE float ratiof(float min, float max, float pos)
MINLINE float max_ff(float a, float b)
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 int divide_floor_i(int a, int b)
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 floorf
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38