Blender V5.3
gpu_shader_math_safe.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
10
11/* -------------------------------------------------------------------- */
14
19float safe_mod(float a, float b)
20{
21 return (b != 0.0f) ? mod(a, b) : 0.0f;
22}
23
28float safe_divide(float a, float b)
29{
30 return (b != 0.0f) ? (a / b) : 0.0f;
31}
32
37float safe_rcp(float a)
38{
39 return (a != 0.0f) ? (1.0f / a) : 0.0f;
40}
41
46float safe_sqrt(float a)
47{
48 return sqrt(max(0.0f, a));
49}
50
56float safe_acos(float a)
57{
58 if (a <= -1.0f) {
59 return M_PI;
60 }
61 else if (a >= 1.0f) {
62 return 0.0f;
63 }
64 return acos(a);
65}
66
71float fallback_pow(float x, float y, float fallback)
72{
73 if (x < 0.0f || (x == 0.0f && y <= 0.0f)) {
74 return fallback;
75 }
76
77 return pow(x, y);
78}
79
83float compatible_pow(float x, float y)
84{
85 if (y == 0.0f) { /* x^0 -> 1, including 0^0 */
86 return 1.0f;
87 }
88
89 /* GLSL pow doesn't accept negative x. */
90 if (x < 0.0f) {
91 if (mod(-y, 2.0f) == 0.0f) {
92 return pow(-x, y);
93 }
94 else {
95 return -pow(-x, y);
96 }
97 }
98 else if (x == 0.0f) {
99 return 0.0f;
100 }
101
102 return pow(x, y);
103}
104
109float compatible_mod(float a, float b)
110{
111 if (b != 0.0f) {
112 int N = int(a / b);
113 return a - N * b;
114 }
115 return 0.0f;
116}
117
121float wrap(float a, float b, float c)
122{
123 float range = b - c;
124 /* Avoid discrepancy on some hardware due to floating point accuracy and fast math. */
125 float s = (a != b) ? floor((a - c) / range) : 1.0f;
126 return (range != 0.0f) ? a - range * s : c;
127}
128
#define M_PI
btScalar max
#define pow
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
#define floor
#define sqrt
#define acos
float safe_rcp(float a)
float wrap(float a, float b, float c)
float fallback_pow(float x, float y, float fallback)
float safe_divide(float a, float b)
float compatible_pow(float x, float y)
float safe_sqrt(float a)
float safe_acos(float a)
float safe_mod(float a, float b)
float compatible_mod(float a, float b)
#define N