Blender V5.3
math_base.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7/* Math
8 *
9 * Basic math functions on scalar and vector types. This header is used by
10 * both the kernel code when compiled as C++, and other C++ non-kernel code. */
11
12#include "util/defines.h"
13#include "util/types_base.h"
14
15#ifdef __HIP__
16# include <hip/hip_vector_types.h>
17#endif
18
19#if !defined(__KERNEL_METAL__)
20# include <cfloat> // IWYU pragma: export
21# include <cmath> // IWYU pragma: export
22#endif
23
24#if !defined(__KERNEL_GPU__)
25# include <bit>
26#endif
27
29
30/* Float Pi variations */
31
32/* Division */
33#ifndef M_PI_F
34# define M_PI_F (3.1415926535897932f) /* `pi` */
35#endif
36#ifndef M_PI_2_F
37# define M_PI_2_F (1.5707963267948966f) /* `pi/2` */
38#endif
39#ifndef M_PI_4_F
40# define M_PI_4_F (0.7853981633974830f) /* `pi/4` */
41#endif
42#ifndef M_1_PI_F
43# define M_1_PI_F (0.3183098861837067f) /* `1/pi` */
44#endif
45#ifndef M_2_PI_F
46# define M_2_PI_F (0.6366197723675813f) /* `2/pi` */
47#endif
48#ifndef M_1_2PI_F
49# define M_1_2PI_F (0.1591549430918953f) /* `1/(2*pi)` */
50#endif
51#ifndef M_1_4PI_F
52# define M_1_4PI_F (0.0795774715459476f) /* `1/(4*pi)` */
53#endif
54#ifndef M_SQRT_PI_8_F
55# define M_SQRT_PI_8_F (0.6266570686577501f) /* `sqrt(pi/8)` */
56#endif
57#ifndef M_LN_2PI_F
58# define M_LN_2PI_F (1.8378770664093454f) /* `ln(2*pi)` */
59#endif
60
61/* Multiplication */
62#ifndef M_2PI_F
63# define M_2PI_F (6.2831853071795864f) /* `2*pi` */
64#endif
65#ifndef M_4PI_F
66# define M_4PI_F (12.566370614359172f) /* `4*pi` */
67#endif
68#ifndef M_PI_4F
69# define M_PI_4F 0.78539816339744830962f /* `pi/4` */
70#endif
71
72/* Float sqrt variations */
73#ifndef M_SQRT2_F
74# define M_SQRT2_F (1.4142135623730950f) /* `sqrt(2)` */
75#endif
76#ifndef M_CBRT2_F
77# define M_CBRT2_F 1.2599210498948732f /* `cbrt(2)` */
78#endif
79#ifndef M_SQRT1_2F
80# define M_SQRT1_2F 0.70710678118654752440f /* `sqrt(1/2)` */
81#endif
82#ifndef M_SQRT3_F
83# define M_SQRT3_F (1.7320508075688772f) /* `sqrt(3)` */
84#endif
85#ifndef M_LN2_F
86# define M_LN2_F (0.6931471805599453f) /* `ln(2)` */
87#endif
88#ifndef M_LN10_F
89# define M_LN10_F (2.3025850929940457f) /* `ln(10)` */
90#endif
91
92/* Scalar */
93
94#if !defined(__HIP__) && !defined(__KERNEL_ONEAPI__)
95# ifdef _WIN32
96ccl_device_inline float fmaxf(const float a, const float b)
97{
98 return (a > b) ? a : b;
99}
100
101ccl_device_inline float fminf(const float a, const float b)
102{
103 return (a < b) ? a : b;
104}
105
106# endif /* _WIN32 */
107#endif /* __HIP__, __KERNEL_ONEAPI__ */
108
109#if defined(__KERNEL_METAL__)
110using metal::isfinite;
111using metal::isnan;
112using metal::sqrt;
113
114using metal::abs;
115using metal::max;
116using metal::min;
117#endif /* defined(__KERNEL_METAL__) */
118
119#if !defined(__KERNEL_GPU__) || defined(__KERNEL_ONEAPI__)
120# ifndef __KERNEL_ONEAPI__
121using std::isfinite;
122using std::isnan;
123using std::sqrt;
124# else
125# define isfinite(x) sycl::isfinite((x))
126# define isnan(x) sycl::isnan((x))
127# endif
128
129ccl_device_inline int abs(const int x)
130{
131 return (x > 0) ? x : -x;
132}
133
134ccl_device_inline int max(const int a, const int b)
135{
136 return (a > b) ? a : b;
137}
138
139ccl_device_inline int min(const int a, const int b)
140{
141 return (a < b) ? a : b;
142}
143
144ccl_device_inline uint32_t max(const uint32_t a, const uint32_t b)
145{
146 return (a > b) ? a : b;
147}
148
149ccl_device_inline uint32_t min(const uint32_t a, const uint32_t b)
150{
151 return (a < b) ? a : b;
152}
153
155{
156 return (a > b) ? a : b;
157}
158
160{
161 return (a < b) ? a : b;
162}
163
164/* NOTE: On 64bit Darwin the `size_t` is defined as `unsigned long int` and `uint64_t` is defined
165 * as `unsigned long long`. Both of the definitions are 64 bit unsigned integer, but the automatic
166 * substitution does not allow to automatically pick function defined for `uint64_t` as it is not
167 * exactly the same type definition.
168 * Work this around by adding a templated function enabled for `size_t` type which will be used
169 * when there is no explicit specialization of `min()`/`max()` above. */
170
171template<class T>
172ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> max(T a, T b)
173{
174 return (a > b) ? a : b;
175}
176
177template<class T>
178ccl_device_inline typename std::enable_if_t<std::is_same_v<T, size_t>, T> min(T a, T b)
179{
180 return (a < b) ? a : b;
181}
182
183ccl_device_inline float max(const float a, const float b)
184{
185 return (a > b) ? a : b;
186}
187
188ccl_device_inline float min(const float a, const float b)
189{
190 return (a < b) ? a : b;
191}
192
193ccl_device_inline double max(const double a, const double b)
194{
195 return (a > b) ? a : b;
196}
197
198ccl_device_inline double min(const double a, const double b)
199{
200 return (a < b) ? a : b;
201}
202
203/* These 2 guys are templated for usage with registers data.
204 *
205 * NOTE: Since this is CPU-only functions it is ok to use references here.
206 * But for other devices we'll need to be careful about this.
207 */
208
209template<typename T> ccl_device_inline T min4(const T &a, const T &b, const T &c, const T &d)
210{
211 return min(min(a, b), min(c, d));
212}
213
214template<typename T> ccl_device_inline T max4(const T &a, const T &b, const T &c, const T &d)
215{
216 return max(max(a, b), max(c, d));
217}
218#endif /* __KERNEL_GPU__ */
219
220ccl_device_inline float min4(const float a, const float b, float c, const float d)
221{
222 return min(min(a, b), min(c, d));
223}
224
225ccl_device_inline float max4(const float a, const float b, float c, const float d)
226{
227 return max(max(a, b), max(c, d));
228}
229
230template<typename T> ccl_device_inline T make_zero();
231
233{
234 return 0.0f;
235}
236
237#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
238/* Int/Float conversion */
239
240ccl_device_inline int as_int(const uint i)
241{
242 union {
243 uint ui;
244 int i;
245 } u;
246 u.ui = i;
247 return u.i;
248}
249
250ccl_device_inline uint as_uint(const int i)
251{
252 union {
253 uint ui;
254 int i;
255 } u;
256 u.i = i;
257 return u.ui;
258}
259
260ccl_device_inline uint as_uint(const float f)
261{
262 union {
263 uint i;
264 float f;
265 } u;
266 u.f = f;
267 return u.i;
268}
269
270# ifndef __HIP__
272{
273 union {
274 int i;
275 float f;
276 } u;
277 u.f = f;
278 return u.i;
279}
280
282{
283 union {
284 int i;
285 float f;
286 } u;
287 u.i = i;
288 return u.f;
289}
290
292{
293 union {
294 uint i;
295 float f;
296 } u;
297 u.f = f;
298 return u.i;
299}
300
302{
303 union {
304 uint i;
305 float f;
306 } u;
307 u.i = i;
308 return u.f;
309}
310# endif
311
312#endif /* !defined(__KERNEL_METAL__) */
313
314#if defined(__KERNEL_METAL__)
315ccl_device_forceinline bool isnan_safe(const float f)
316{
317 return isnan(f);
318}
319
320ccl_device_forceinline bool isfinite_safe(const float f)
321{
322 return isfinite(f);
323}
324#else
325template<typename T> ccl_device_inline uint pointer_pack_to_uint_0(T *ptr)
326{
327 return ((uint64_t)ptr) & 0xFFFFFFFF;
328}
329
330template<typename T> ccl_device_inline uint pointer_pack_to_uint_1(T *ptr)
331{
332 return (((uint64_t)ptr) >> 32) & 0xFFFFFFFF;
333}
334
335template<typename T> ccl_device_inline T *pointer_unpack_from_uint(const uint a, const uint b)
336{
337 return (T *)(((uint64_t)b << 32) | a);
338}
339
340ccl_device_inline uint uint16_pack_to_uint(const uint a, const uint b)
341{
342 return (a << 16) | b;
343}
344
346{
347 return i >> 16;
348}
349
351{
352 return i & 0xFFFF;
353}
354
355/* Versions of functions which are safe for fast math. */
356ccl_device_inline bool isnan_safe(const float f)
357{
358 const unsigned int x = __float_as_uint(f);
359 return (x << 1) > 0xff000000u;
360}
361
363{
364 /* By IEEE 754 rule, 2*Inf equals Inf */
365 const unsigned int x = __float_as_uint(f);
366 return (f == f) && (x == 0 || x == (1u << 31) || (f != 2.0f * f)) && !((x << 1) > 0xff000000u);
367}
368#endif
369
371{
372 return isfinite_safe(v) ? v : 0.0f;
373}
374
375#if defined(__KERNEL_METAL__)
376using metal::clamp;
377using metal::mix;
378using metal::smoothstep;
379#else
380ccl_device_inline int clamp(const int a, const int mn, const int mx)
381{
382 return min(max(a, mn), mx);
383}
384
385ccl_device_inline float clamp(const float a, const float mn, const float mx)
386{
387 return min(max(a, mn), mx);
388}
389
390ccl_device_inline float smoothstep(const float edge0, const float edge1, const float x)
391{
392 float result;
393 if (x < edge0) {
394 result = 0.0f;
395 }
396 else if (x >= edge1) {
397 result = 1.0f;
398 }
399 else {
400 const float t = (x - edge0) / (edge1 - edge0);
401 result = (3.0f - 2.0f * t) * (t * t);
402 }
403 return result;
404}
405
406/* There are two common ways of implementing a linear interpolation: result = a + t * (b - a) and
407 * result = (1 - t) * a + t * b. The former variant is called "mix" in our code and it ensures that
408 * result always changes monotonically when t increases monotonically. This comes at the cost of
409 * the fact that generally result != b when t == 1, which becomes particularly noticeable when the
410 * magnitudes of a and b are vastly different. The latter variant is called
411 * "endvalue_preserving_mix" in our code ensures that result == b when t == 1. This comes at the
412 * cost of an additional multiplication step compared to the former version and the fact that
413 * result may not change monotonically when a and b have different signs and t increases
414 * monotonically, which however isn't noticeable in most cases as long as monotony isn't explicitly
415 * required. In general, "endvalue_preserving_mix" should be preferred over "mix" when it is
416 * important that result == b when t == 1 or when a and b may have vastly different magnitudes.*/
417template<typename T1, typename T2> ccl_device_inline T1 mix(const T1 a, const T1 b, const T2 t)
418{
419 return a + t * (b - a);
420}
421
422#endif /* !defined(__KERNEL_METAL__) */
423
424/* Same as the "mix" function but with different numerical behavior. See comment above the "mix"
425 * function for more information. */
426template<typename T1, typename T2>
428{
429 return (1.0f - t) * a + t * b;
430}
431
432#if !defined(__KERNEL_METAL__)
433ccl_device_inline float saturatef(const float a)
434{
435# ifdef __KERNEL_OPTIX__
436 /* Workaround OptiX driver bug which somehow rounds constant values to
437 * integers when using __saturatef. This particular logic works around the
438 * problem, just using clamp gets optimized back to saturate. See #159954. */
439 if (!(a >= 0.0f)) {
440 return 0.0f;
441 }
442 if (!(a <= 1.0f)) {
443 return 1.0f;
444 }
445 return a;
446# elif defined(__KERNEL_CUDA__)
447 return __saturatef(a);
448# else
449 return clamp(a, 0.0f, 1.0f);
450# endif
451}
452#endif /* __KERNEL_CUDA__ */
453
455{
456 return (int)f;
457}
458
460{
461 return float_to_int(floorf(f));
462}
463
464ccl_device_inline float floorfrac(const float x, ccl_private int *i)
465{
466 const float f = floorf(x);
467 *i = float_to_int(f);
468 return x - f;
469}
470
472{
473 return float_to_int(ceilf(f));
474}
475
476ccl_device_inline float fractf(const float x)
477{
478 return x - floorf(x);
479}
480
481/* Adapted from `godot-engine` math_funcs.h. */
482ccl_device_inline float wrapf(const float value, const float max, const float min)
483{
484 const float range = max - min;
485 return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min;
486}
487
488ccl_device_inline float pingpongf(const float a, const float b)
489{
490 return (b != 0.0f) ? fabsf(fractf((a - b) / (b * 2.0f)) * b * 2.0f - b) : 0.0f;
491}
492
493ccl_device_inline float smoothminf(const float a, const float b, float k)
494{
495 if (k != 0.0f) {
496 const float h = fmaxf(k - fabsf(a - b), 0.0f) / k;
497 return fminf(a, b) - h * h * h * k * (1.0f / 6.0f);
498 }
499 return fminf(a, b);
500}
501
502ccl_device_inline float signf(const float f)
503{
504 return (f < 0.0f) ? -1.0f : 1.0f;
505}
506
507ccl_device_inline float nonzerof(const float f, const float eps)
508{
509 if (fabsf(f) < eps) {
510 return signf(f) * eps;
511 }
512 return f;
513}
514
515/* The behavior of `atan2(0, 0)` is undefined on many platforms, to ensure consistent behavior, we
516 * return 0 in this case. See !126951.
517 * Computes the angle between the positive x axis and the vector pointing from origin to (x, y). */
518ccl_device_inline float compatible_atan2(const float y, const float x)
519{
520 return (x == 0.0f && y == 0.0f) ? 0.0f : atan2f(y, x);
521}
522
523/* `signum` function testing for zero. Matches GLSL and OSL functions. */
525{
526 if (f == 0.0f) {
527 return 0.0f;
528 }
529 return signf(f);
530}
531
532ccl_device_inline float smoothstepf(const float f)
533{
534 if (f <= 0.0f) {
535 return 0.0f;
536 }
537 if (f >= 1.0f) {
538 return 1.0f;
539 }
540 const float ff = f * f;
541 return (3.0f * ff - 2.0f * ff * f);
542}
543
544ccl_device_inline int mod(const int x, const int m)
545{
546 return (x % m + m) % m;
547}
548
549ccl_device_inline float interp(const float a, const float b, const float t)
550{
551 return a + t * (b - a);
552}
553
554ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
555{
556 return (x - a) / (b - a);
557}
558
559/* Cubic interpolation between b and c, a and d are the previous and next point. */
560ccl_device_inline float cubic_interp(const float a, const float b, float c, const float d, float x)
561{
562 return 0.5f *
563 (((d + 3.0f * (b - c) - a) * x + (2.0f * a - 5.0f * b + 4.0f * c - d)) * x +
564 (c - a)) *
565 x +
566 b;
567}
568
569/* NaN-safe math ops */
570
571ccl_device_inline float safe_sqrtf(const float f)
572{
573 return sqrtf(max(f, 0.0f));
574}
575
576ccl_device_inline float inversesqrtf(const float f)
577{
578#if defined(__KERNEL_METAL__)
579 return (f > 0.0f) ? metal::rsqrt(f) : 0.0f;
580#else
581 return (f > 0.0f) ? 1.0f / sqrtf(f) : 0.0f;
582#endif
583}
584
585ccl_device float safe_asinf(const float a)
586{
587 return asinf(clamp(a, -1.0f, 1.0f));
588}
589
590ccl_device float safe_acosf(const float a)
591{
592 return acosf(clamp(a, -1.0f, 1.0f));
593}
594
595ccl_device float compatible_powf(const float x, const float y)
596{
597 if (y == 0.0f) {
598 /* x^0 -> 1, including 0^0. */
599 return 1.0f;
600 }
601 if (x == 0.0f) {
602 return 0.0f;
603 }
604#ifdef __KERNEL_GPU__
605 /* GPU pow doesn't accept negative x, do manual checks here */
606 if (x < 0.0f) {
607 if (fmodf(-y, 2.0f) == 0.0f) {
608 return powf(-x, y);
609 }
610 else {
611 return -powf(-x, y);
612 }
613 }
614#endif
615 return powf(x, y);
616}
617
618ccl_device float safe_powf(const float a, const float b)
619{
620 if (UNLIKELY(a < 0.0f && b != float_to_int(b))) {
621 return 0.0f;
622 }
623
624 return compatible_powf(a, b);
625}
626
627ccl_device float safe_divide(const float a, const float b)
628{
629 return (b != 0.0f) ? a / b : 0.0f;
630}
631
632ccl_device float safe_logf(const float a, const float b)
633{
634 if (UNLIKELY(a <= 0.0f || b <= 0.0f)) {
635 return 0.0f;
636 }
637
638 return safe_divide(logf(a), logf(b));
639}
640
641ccl_device float safe_modulo(const float a, const float b)
642{
643 return (b != 0.0f) ? fmodf(a, b) : 0.0f;
644}
645
646ccl_device float safe_floored_modulo(const float a, const float b)
647{
648 return (b != 0.0f) ? a - floorf(a / b) * b : 0.0f;
649}
650
651ccl_device_inline float sqr(const float a)
652{
653 return a * a;
654}
655
656ccl_device_inline float sin_from_cos(const float c)
657{
658 return safe_sqrtf(1.0f - sqr(c));
659}
660
661ccl_device_inline float cos_from_sin(const float s)
662{
663 return safe_sqrtf(1.0f - sqr(s));
664}
665
667{
668 /* Using second-order Taylor expansion at small angles for better accuracy. */
669 return s_sq > 0.0004f ? 1.0f - safe_sqrtf(1.0f - s_sq) : 0.5f * s_sq;
670}
671
673{
674 /* Using second-order Taylor expansion at small angles for better accuracy. */
675 return angle > 0.02f ? 1.0f - cosf(angle) : 0.5f * sqr(angle);
676}
677
678/* 2^a. */
680{
681 return 1 << a;
682}
683
684ccl_device_inline float pow20(const float a)
685{
686 return sqr(sqr(sqr(sqr(a)) * a));
687}
688
689ccl_device_inline float pow22(const float a)
690{
691 return sqr(a * sqr(sqr(sqr(a)) * a));
692}
693
694#ifdef __KERNEL_METAL__
695ccl_device_inline float lgammaf(const float x)
696{
697 /* Nemes, Gergő (2010), "New asymptotic expansion for the Gamma function", Archiv der Mathematik
698 */
699 const float _1_180 = 1.0f / 180.0f;
700 const float log2pi = 1.83787706641f;
701 const float logx = metal::log(x);
702 return (log2pi - logx +
703 x * (logx * 2.0f + metal::log(x * sinhf(1.0f / x) + (_1_180 / powf(x, 6.0f))) - 2.0f)) *
704 0.5f;
705}
706#endif
707
708ccl_device_inline float beta(const float x, const float y)
709{
710 return expf(lgammaf(x) + lgammaf(y) - lgammaf(x + y));
711}
712
713ccl_device_inline float xor_mask(const float x, const uint y)
714{
716}
717
718ccl_device_inline float or_mask(const float x, const uint y)
719{
721}
722
723ccl_device float bits_to_01(const uint bits)
724{
725 return bits * (1.0f / (float)0xFFFFFFFF);
726}
727
728ccl_device_inline bool is_zero(const float a)
729{
730 return a == 0.0f;
731}
732
733#if !defined(__KERNEL_GPU__)
735{
736 return std::popcount(x);
737}
739{
740 return std::popcount(x);
741}
742#elif defined(__KERNEL_ONEAPI__)
743# define popcount(x) sycl::popcount(x)
744#elif defined(__KERNEL_HIP__)
745/* Use popcll to support 64-bit wave for pre-RDNA AMD GPUs */
746# define popcount(x) __popcll(x)
747#elif defined(__KERNEL_METAL__)
748using metal::popcount;
749#else
750# define popcount(x) __popc(x)
751#endif
752
754{
755#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
756 return __clz(x);
757#elif defined(__KERNEL_METAL__)
758 return metal::clz(x);
759#elif defined(__KERNEL_ONEAPI__)
760 return sycl::clz(x);
761#else
762 assert(x != 0);
763# ifdef _MSC_VER
764 unsigned long leading_zero = 0;
765 _BitScanReverse(&leading_zero, x);
766 return (31 - leading_zero);
767# else
768 return __builtin_clz(x);
769# endif
770#endif
771}
772
774{
775#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
776 return (__ffs(x) - 1);
777#elif defined(__KERNEL_METAL__)
778 return metal::ctz(x);
779#elif defined(__KERNEL_ONEAPI__)
780 return sycl::ctz(x);
781#else
782 assert(x != 0);
783# ifdef _MSC_VER
784 unsigned long ctz = 0;
785 _BitScanForward(&ctz, x);
786 return ctz;
787# else
788 return __builtin_ctz(x);
789# endif
790#endif
791}
792
794{
795#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
796 return __ffs(x);
797#elif defined(__KERNEL_METAL__)
798 return (x != 0) ? metal::ctz(x) + 1 : 0;
799#else
800# ifdef _MSC_VER
801 return (x != 0) ? (32 - count_leading_zeros(x & (~x + 1))) : 0;
802# else
803 return __builtin_ffs(x);
804# endif
805#endif
806}
807
808/* Compares two floats.
809 * Returns true if their absolute difference is smaller than abs_diff (for numbers near zero)
810 * or their relative difference is less than ulp_diff ULPs.
811 * Based on
812 * https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
813 */
814
816 const float b,
817 float abs_diff,
818 const int ulp_diff)
819{
820 if (fabsf(a - b) < abs_diff) {
821 return true;
822 }
823
824 if ((a < 0.0f) != (b < 0.0f)) {
825 return false;
826 }
827
828 return (abs(__float_as_int(a) - __float_as_int(b)) < ulp_diff);
829}
830
831/* Return value which is greater than the given one and is a power of two. */
833{
834 return x == 0 ? 1 : 1 << (32 - count_leading_zeros(x));
835}
836
837/* Return value which is lower than the given one and is a power of two. */
839{
840 return x < 2 ? x : 1 << (31 - count_leading_zeros(x - 1));
841}
842
843#ifndef __has_builtin
844# define __has_builtin(v) 0
845#endif
846
847/* Reverses the bits of a 32 bit integer. */
849{
850 /* Use a native instruction if it exists. */
851#if defined(__KERNEL_CUDA__)
852 return __brev(x);
853#elif defined(__KERNEL_METAL__)
854 return metal::reverse_bits(x);
855#elif defined(__aarch64__) || (defined(_M_ARM64) && !defined(_MSC_VER))
856 /* Assume the rbit is always available on 64bit ARM architecture. */
857 __asm__("rbit %w0, %w1" : "=r"(x) : "r"(x));
858 return x;
859#elif defined(__arm__) && ((__ARM_ARCH > 7) || __ARM_ARCH == 6 && __ARM_ARCH_ISA_THUMB >= 2)
860 /* This ARM instruction is available in ARMv6T2 and above.
861 * This 32-bit Thumb instruction is available in ARMv6T2 and above. */
862 __asm__("rbit %0, %1" : "=r"(x) : "r"(x));
863 return x;
864#elif __has_builtin(__builtin_bitreverse32)
865 return __builtin_bitreverse32(x);
866#else
867 /* Flip pairwise. */
868 x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
869 /* Flip pairs. */
870 x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
871 /* Flip nibbles. */
872 x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
873 /* Flip bytes. CPUs have an instruction for that, pretty fast one. */
874# ifdef _MSC_VER
875 return _byteswap_ulong(x);
876# elif defined(__INTEL_COMPILER)
877 return (uint32_t)_bswap((int)x);
878# else
879 /* Assuming gcc or clang. */
880 return __builtin_bswap32(x);
881# endif
882#endif
883}
884
885/* Solve quadratic equation a*x^2 + b*x + c = 0, adapted from Mitsuba 3
886 * The solution is ordered so that x1 <= x2.
887 * Returns true if at least one solution is found. */
889 const float a, const float b, const float c, ccl_private float &x1, ccl_private float &x2)
890{
891 /* If the equation is linear, the solution is -c/b, but b has to be non-zero. */
892 const bool valid_linear = (a == 0.0f) && (b != 0.0f);
893 x1 = x2 = -c / b;
894
895 const float discriminant = sqr(b) - 4.0f * a * c;
896 /* Allow slightly negative discriminant in case of numerical precision issues. */
897 const bool valid_quadratic = (a != 0.0f) && (discriminant > -1e-5f);
898
899 if (valid_quadratic) {
900 /* Numerically stable version of (-b ± sqrt(discriminant)) / (2 * a), avoiding catastrophic
901 * cancellation when `b` is very close to `sqrt(discriminant)`, by finding the solution of
902 * greater magnitude which does not suffer from loss of precision, then using the identity
903 * x1 * x2 = c / a. */
904 const float temp = -0.5f * (b + copysignf(safe_sqrtf(discriminant), b));
905 const float r1 = temp / a;
906 const float r2 = c / temp;
907
908 x1 = fminf(r1, r2);
909 x2 = fmaxf(r1, r2);
910 }
911
912 return (valid_linear || valid_quadratic);
913}
914
915/* Defines a closed interval [min, max]. */
916template<typename T> struct Interval {
919
921 {
922 /* NaN-safe comparison. */
923 return !(min < max);
924 }
925
927 {
928 return value >= min && value <= max;
929 }
930
932 {
933 return max - min;
934 }
935};
936
937template<typename T1, typename T2>
939{
940 interval.min /= f;
941 interval.max /= f;
942 return interval;
943}
944
945/* Computes the intersection of two intervals. */
946template<typename T>
948 const ccl_private Interval<T> &second)
949{
950 return {max(first.min, second.min), min(first.max, second.max)};
951}
952
953/* Defines the minimal and maximal values of a quantity. */
954template<typename T> struct Extrema {
957 Extrema() = default;
958 ccl_device_inline_method Extrema(T value) : min(value), max(value) {}
959 ccl_device_inline_method Extrema(T min_, T max_) : min(min_), max(max_) {}
960
962 {
963 return max - min;
964 }
965};
966
967template<typename T> ccl_device_inline Extrema<T> operator*(const Extrema<T> a, const T b)
968{
969 return {a.min * b, a.max * b};
970}
971
972template<typename T>
974 const ccl_private Extrema<T> &b)
975{
976 return {a.min + b.min, a.max + b.max};
977}
978
979template<typename T>
984
985/* Returns the extrema of both extrema. */
986template<typename T>
988 const ccl_private Extrema<T> &b)
989{
990 return {min(a.min, b.min), max(a.max, b.max)};
991}
992
993template<typename T>
995{
996 return {min(a.min, v), max(a.max, v)};
997}
998
1000{
1001 /* Use rounding semantic to reduce the average roundtrip error of
1002 * byte_to_float(float_to_byte(x)). */
1003 return ((val <= 0.0f) ?
1004 0 :
1005 ((val > (1.0f - 0.5f / 255.0f)) ? 255 : (uchar)((255.0f * val) + 0.5f))); // NOLINT
1006}
1007
1009{
1010 return val * (1.0f / 255.0f);
1011}
1012
#define UNLIKELY(x)
unsigned long long int uint64_t
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define ccl_device_forceinline
#define ccl_private
#define ccl_device_inline
#define ccl_device_template_spec
#define ccl_device_inline_method
#define logf(x)
#define expf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
#define saturatef(x)
#define asinf(x)
#define fmodf(x, y)
#define acosf(x)
#define copysignf(x, y)
#define __int_as_float(x)
#define sinhf(x)
#define __float_as_int(x)
#define __float_as_uint(x)
#define __uint_as_float(x)
#define lgammaf(x)
#define assert(assertion)
#define isnan
T smoothstep(T, T, T) RET
#define abs
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
uint as_uint(string_t)
unsigned int uchar
float endvalue_preserving_mix(float a, float b, float t)
float cos_from_sin(float s)
float sin_from_cos(float c)
bool is_zero(float3x3 a)
float safe_divide(float a, float b)
__forceinline LightTreeMeasure operator+(const LightTreeMeasure &a, const LightTreeMeasure &b)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
Definition math_base.h:464
ccl_device_inline bool isnan_safe(const float f)
Definition math_base.h:356
ccl_device_inline float smoothstepf(const float f)
Definition math_base.h:532
ccl_device_inline float byte_to_float(const uchar val)
Definition math_base.h:1008
ccl_device_inline Interval< T1 > operator/=(ccl_private Interval< T1 > &interval, const T2 f)
Definition math_base.h:938
ccl_device_inline uint prev_power_of_two(const uint x)
Definition math_base.h:838
ccl_device_inline int ceil_to_int(const float f)
Definition math_base.h:471
ccl_device float safe_asinf(const float a)
Definition math_base.h:585
ccl_device_inline uint uint16_unpack_from_uint_1(const uint i)
Definition math_base.h:350
ccl_device_inline float or_mask(const float x, const uint y)
Definition math_base.h:718
ccl_device_inline float signf(const float f)
Definition math_base.h:502
ccl_device float safe_powf(const float a, const float b)
Definition math_base.h:618
ccl_device_inline T min4(const T &a, const T &b, const T &c, const T &d)
Definition math_base.h:209
ccl_device_inline float pow22(const float a)
Definition math_base.h:689
ccl_device_inline uint count_leading_zeros(const uint x)
Definition math_base.h:753
ccl_device float bits_to_01(const uint bits)
Definition math_base.h:723
ccl_device_inline float inversesqrtf(const float f)
Definition math_base.h:576
ccl_device_inline float compatible_atan2(const float y, const float x)
Definition math_base.h:518
ccl_device float compatible_powf(const float x, const float y)
Definition math_base.h:595
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
Definition math_base.h:967
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline uint pointer_pack_to_uint_1(T *ptr)
Definition math_base.h:330
ccl_device_inline float cubic_interp(const float a, const float b, float c, const float d, float x)
Definition math_base.h:560
ccl_device_inline int floor_to_int(const float f)
Definition math_base.h:459
ccl_device_inline uint32_t reverse_integer_bits(uint32_t x)
Definition math_base.h:848
ccl_device_inline int power_of_2(const int a)
Definition math_base.h:679
ccl_device_inline Extrema< T > operator+=(ccl_private Extrema< T > &a, const ccl_private Extrema< T > &b)
Definition math_base.h:980
ccl_device_inline float sin_sqr_to_one_minus_cos(const float s_sq)
Definition math_base.h:666
ccl_device float safe_acosf(const float a)
Definition math_base.h:590
ccl_device_inline float compatible_signf(const float f)
Definition math_base.h:524
ccl_device_inline float xor_mask(const float x, const uint y)
Definition math_base.h:713
ccl_device_inline uint uint16_pack_to_uint(const uint a, const uint b)
Definition math_base.h:340
ccl_device_inline int as_int(const uint i)
Definition math_base.h:240
ccl_device_inline float nonzerof(const float f, const float eps)
Definition math_base.h:507
ccl_device_inline bool solve_quadratic(const float a, const float b, const float c, ccl_private float &x1, ccl_private float &x2)
Definition math_base.h:888
ccl_device_inline float smoothminf(const float a, const float b, float k)
Definition math_base.h:493
ccl_device_inline uint pointer_pack_to_uint_0(T *ptr)
Definition math_base.h:325
ccl_device_inline T * pointer_unpack_from_uint(const uint a, const uint b)
Definition math_base.h:335
ccl_device_inline uint next_power_of_two(const uint x)
Definition math_base.h:832
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
Definition math_base.h:554
ccl_device_inline T max4(const T &a, const T &b, const T &c, const T &d)
Definition math_base.h:214
ccl_device_inline float pow20(const float a)
Definition math_base.h:684
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:454
ccl_device_inline uint find_first_set(const uint x)
Definition math_base.h:793
ccl_device_inline Interval< T > intervals_intersection(const ccl_private Interval< T > &first, const ccl_private Interval< T > &second)
Definition math_base.h:947
ccl_device_inline float ensure_finite(const float v)
Definition math_base.h:370
ccl_device_inline uchar float_to_byte(const float val)
Definition math_base.h:999
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
ccl_device_inline uint uint16_unpack_from_uint_0(const uint i)
Definition math_base.h:345
ccl_device_inline uint popcount(const uint x)
Definition math_base.h:734
ccl_device_inline float wrapf(const float value, const float max, const float min)
Definition math_base.h:482
ccl_device_inline bool compare_floats(const float a, const float b, float abs_diff, const int ulp_diff)
Definition math_base.h:815
ccl_device float safe_logf(const float a, const float b)
Definition math_base.h:632
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline float one_minus_cos(const float angle)
Definition math_base.h:672
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
ccl_device_inline float pingpongf(const float a, const float b)
Definition math_base.h:488
ccl_device_inline uint count_trailing_zeros(const uint x)
Definition math_base.h:773
ccl_device_inline T make_zero()
Definition math_dual.h:20
#define T
#define T2
Definition md5.cpp:21
#define T1
Definition md5.cpp:20
float safe_floored_modulo(float a, float b)
Definition node_math.h:27
float safe_modulo(float a, float b)
Definition node_math.h:22
const btScalar eps
Definition poly34.cpp:11
#define mix
#define sqr
#define floorf
#define fabsf
#define sqrtf
#define ccl_device
#define fmaxf
#define fractf
#define fminf
#define cosf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
ccl_device_inline_method Extrema(T value)
Definition math_base.h:958
ccl_device_inline_method Extrema(T min_, T max_)
Definition math_base.h:959
ccl_device_inline_method T range() const
Definition math_base.h:961
Extrema()=default
ccl_device_inline_method T length() const
Definition math_base.h:931
ccl_device_inline_method bool contains(T value) const
Definition math_base.h:926
ccl_device_inline_method bool is_empty() const
Definition math_base.h:920