Skip to content

Commit 48ec701

Browse files
committed
Replace generic macros with math equivalents
1 parent c414c2b commit 48ec701

21 files changed

+107
-109
lines changed

include/godot_cpp/core/math.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,31 @@ namespace godot {
5555
#undef MAX
5656
#undef CLAMP
5757

58-
// Generic ABS function, for math uses please use Math::abs.
58+
// DEPRECATED. Use `Math::abs` instead.
5959
template <typename T>
6060
constexpr T ABS(T m_v) {
6161
return m_v < 0 ? -m_v : m_v;
6262
}
6363

64+
// DEPRECATED. Use `Math::sign` instead.
6465
template <typename T>
6566
constexpr const T SIGN(const T m_v) {
6667
return m_v == 0 ? 0.0f : (m_v < 0 ? -1.0f : +1.0f);
6768
}
6869

70+
// DEPRECATED. Use `Math::min` instead.
6971
template <typename T, typename T2>
7072
constexpr auto MIN(const T m_a, const T2 m_b) {
7173
return m_a < m_b ? m_a : m_b;
7274
}
7375

76+
// DEPRECATED. Use `Math::max` instead.
7477
template <typename T, typename T2>
7578
constexpr auto MAX(const T m_a, const T2 m_b) {
7679
return m_a > m_b ? m_a : m_b;
7780
}
7881

82+
// DEPRECATED. Use `Math::clamp` instead.
7983
template <typename T, typename T2, typename T3>
8084
constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
8185
return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
@@ -539,34 +543,28 @@ inline float bezier_interpolate(float p_start, float p_control_1, float p_contro
539543
}
540544

541545
template <typename T>
542-
inline T clamp(T x, T minv, T maxv) {
543-
if (x < minv) {
544-
return minv;
545-
}
546-
if (x > maxv) {
547-
return maxv;
548-
}
549-
return x;
546+
constexpr T clamp(T x, T minv, T maxv) {
547+
return x < minv ? minv : (x > maxv ? maxv : x);
550548
}
551549

552550
template <typename T>
553-
inline T min(T a, T b) {
551+
constexpr T min(T a, T b) {
554552
return a < b ? a : b;
555553
}
556554

557555
template <typename T>
558-
inline T max(T a, T b) {
556+
constexpr T max(T a, T b) {
559557
return a > b ? a : b;
560558
}
561559

562560
template <typename T>
563-
inline T sign(T x) {
564-
return static_cast<T>(SIGN(x));
561+
constexpr T sign(T x) {
562+
return x > T(0) ? T(1) : (x < T(0) ? T(-1) : T(0));
565563
}
566564

567565
template <typename T>
568-
inline T abs(T x) {
569-
return std::abs(x);
566+
constexpr T abs(T x) {
567+
return x == T(0) ? T(0) : (x < T(0) ? -x : x);
570568
}
571569

572570
inline double deg_to_rad(double p_y) {

include/godot_cpp/templates/hash_map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class HashMap {
160160
uint32_t old_capacity = hash_table_size_primes[capacity_index];
161161

162162
// Capacity can't be 0.
163-
capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
163+
capacity_index = Math::max((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
164164

165165
uint32_t capacity = hash_table_size_primes[capacity_index];
166166

include/godot_cpp/templates/hash_set.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class HashSet {
141141

142142
void _resize_and_rehash(uint32_t p_new_capacity_index) {
143143
// Capacity can't be 0.
144-
capacity_index = MAX((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
144+
capacity_index = Math::max((uint32_t)MIN_CAPACITY_INDEX, p_new_capacity_index);
145145

146146
uint32_t capacity = hash_table_size_primes[capacity_index];
147147

include/godot_cpp/templates/vector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ class Vector {
165165

166166
const Size s = size();
167167

168-
Size begin = CLAMP(p_begin, -s, s);
168+
Size begin = Math::clamp(p_begin, -s, s);
169169
if (begin < 0) {
170170
begin += s;
171171
}
172-
Size end = CLAMP(p_end, -s, s);
172+
Size end = Math::clamp(p_end, -s, s);
173173
if (end < 0) {
174174
end += s;
175175
}

include/godot_cpp/variant/color.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ struct _NO_DISCARD_ Color {
135135

136136
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
137137

138-
float cRed = MAX(0.0f, MIN(sharedexp, r));
139-
float cGreen = MAX(0.0f, MIN(sharedexp, g));
140-
float cBlue = MAX(0.0f, MIN(sharedexp, b));
138+
float cRed = Math::max(0.0f, Math::min(sharedexp, r));
139+
float cGreen = Math::max(0.0f, Math::min(sharedexp, g));
140+
float cBlue = Math::max(0.0f, Math::min(sharedexp, b));
141141

142-
float cMax = MAX(cRed, MAX(cGreen, cBlue));
142+
float cMax = Math::max(cRed, Math::max(cGreen, cBlue));
143143

144-
float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
144+
float expp = Math::max<float>(-B - 1.0f, floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
145145

146146
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
147147

@@ -204,14 +204,14 @@ struct _NO_DISCARD_ Color {
204204
operator String() const;
205205

206206
// For the binder.
207-
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0f); }
208-
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
209-
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0f); }
210-
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
211-
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0f); }
212-
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
213-
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0f); }
214-
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }
207+
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (Math::clamp(r8, 0, 255) / 255.0f); }
208+
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(Math::clamp(Math::round(r * 255.0f), 0.0f, 255.0f)); }
209+
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (Math::clamp(g8, 0, 255) / 255.0f); }
210+
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(Math::clamp(Math::round(g * 255.0f), 0.0f, 255.0f)); }
211+
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (Math::clamp(b8, 0, 255) / 255.0f); }
212+
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(Math::clamp(Math::round(b * 255.0f), 0.0f, 255.0f)); }
213+
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (Math::clamp(a8, 0, 255) / 255.0f); }
214+
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(Math::clamp(Math::round(a * 255.0f), 0.0f, 255.0f)); }
215215

216216
_FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v(), a); }
217217
_FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v(), a); }

include/godot_cpp/variant/vector2.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@ struct _NO_DISCARD_ Vector2 {
8888
Vector2 limit_length(const real_t p_len = 1.0) const;
8989

9090
Vector2 min(const Vector2 &p_vector2) const {
91-
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
91+
return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y));
9292
}
9393

9494
Vector2 minf(real_t p_scalar) const {
95-
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
95+
return Vector2(Math::min(x, p_scalar), Math::min(y, p_scalar));
9696
}
9797

9898
Vector2 max(const Vector2 &p_vector2) const {
99-
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
99+
return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y));
100100
}
101101

102102
Vector2 maxf(real_t p_scalar) const {
103-
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
103+
return Vector2(Math::max(x, p_scalar), Math::max(y, p_scalar));
104104
}
105105

106106
real_t distance_to(const Vector2 &p_vector2) const;

include/godot_cpp/variant/vector2i.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ struct _NO_DISCARD_ Vector2i {
8080
}
8181

8282
Vector2i min(const Vector2i &p_vector2i) const {
83-
return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
83+
return Vector2i(Math::min(x, p_vector2i.x), Math::min(y, p_vector2i.y));
8484
}
8585

8686
Vector2i mini(int32_t p_scalar) const {
87-
return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
87+
return Vector2i(Math::min(x, p_scalar), Math::min(y, p_scalar));
8888
}
8989

9090
Vector2i max(const Vector2i &p_vector2i) const {
91-
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
91+
return Vector2i(Math::max(x, p_vector2i.x), Math::max(y, p_vector2i.y));
9292
}
9393

9494
Vector2i maxi(int32_t p_scalar) const {
95-
return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
95+
return Vector2i(Math::max(x, p_scalar), Math::max(y, p_scalar));
9696
}
9797

9898
Vector2i operator+(const Vector2i &p_v) const;
@@ -129,7 +129,7 @@ struct _NO_DISCARD_ Vector2i {
129129
double distance_to(const Vector2i &p_to) const;
130130

131131
real_t aspect() const { return width / (real_t)height; }
132-
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
132+
Vector2i sign() const { return Vector2i(Math::sign(x), Math::sign(y)); }
133133
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
134134
Vector2i snapped(const Vector2i &p_step) const;
135135
Vector2i snappedi(int32_t p_step) const;

include/godot_cpp/variant/vector3.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ struct _NO_DISCARD_ Vector3 {
7979
}
8080

8181
Vector3 min(const Vector3 &p_vector3) const {
82-
return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z));
82+
return Vector3(Math::min(x, p_vector3.x), Math::min(y, p_vector3.y), Math::min(z, p_vector3.z));
8383
}
8484

8585
Vector3 minf(real_t p_scalar) const {
86-
return Vector3(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
86+
return Vector3(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
8787
}
8888

8989
Vector3 max(const Vector3 &p_vector3) const {
90-
return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
90+
return Vector3(Math::max(x, p_vector3.x), Math::max(y, p_vector3.y), Math::max(z, p_vector3.z));
9191
}
9292

9393
Vector3 maxf(real_t p_scalar) const {
94-
return Vector3(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
94+
return Vector3(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
9595
}
9696

9797
_FORCE_INLINE_ real_t length() const;
@@ -213,7 +213,7 @@ Vector3 Vector3::abs() const {
213213
}
214214

215215
Vector3 Vector3::sign() const {
216-
return Vector3(SIGN(x), SIGN(y), SIGN(z));
216+
return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
217217
}
218218

219219
Vector3 Vector3::floor() const {

include/godot_cpp/variant/vector3i.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ struct _NO_DISCARD_ Vector3i {
7272
Vector3i::Axis max_axis_index() const;
7373

7474
Vector3i min(const Vector3i &p_vector3i) const {
75-
return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z));
75+
return Vector3i(Math::min(x, p_vector3i.x), Math::min(y, p_vector3i.y), Math::min(z, p_vector3i.z));
7676
}
7777

7878
Vector3i mini(int32_t p_scalar) const {
79-
return Vector3i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
79+
return Vector3i(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar));
8080
}
8181

8282
Vector3i max(const Vector3i &p_vector3i) const {
83-
return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
83+
return Vector3i(Math::max(x, p_vector3i.x), Math::max(y, p_vector3i.y), Math::max(z, p_vector3i.z));
8484
}
8585

8686
Vector3i maxi(int32_t p_scalar) const {
87-
return Vector3i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
87+
return Vector3i(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar));
8888
}
8989

9090
_FORCE_INLINE_ int64_t length_squared() const;
@@ -163,7 +163,7 @@ Vector3i Vector3i::abs() const {
163163
}
164164

165165
Vector3i Vector3i::sign() const {
166-
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
166+
return Vector3i(Math::sign(x), Math::sign(y), Math::sign(z));
167167
}
168168

169169
/* Operators */

include/godot_cpp/variant/vector4.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ struct _NO_DISCARD_ Vector4 {
7171
Vector4::Axis max_axis_index() const;
7272

7373
Vector4 min(const Vector4 &p_vector4) const {
74-
return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
74+
return Vector4(Math::min(x, p_vector4.x), Math::min(y, p_vector4.y), Math::min(z, p_vector4.z), Math::min(w, p_vector4.w));
7575
}
7676

7777
Vector4 minf(real_t p_scalar) const {
78-
return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
78+
return Vector4(Math::min(x, p_scalar), Math::min(y, p_scalar), Math::min(z, p_scalar), Math::min(w, p_scalar));
7979
}
8080

8181
Vector4 max(const Vector4 &p_vector4) const {
82-
return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
82+
return Vector4(Math::max(x, p_vector4.x), Math::max(y, p_vector4.y), Math::max(z, p_vector4.z), Math::max(w, p_vector4.w));
8383
}
8484

8585
Vector4 maxf(real_t p_scalar) const {
86-
return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
86+
return Vector4(Math::max(x, p_scalar), Math::max(y, p_scalar), Math::max(z, p_scalar), Math::max(w, p_scalar));
8787
}
8888

8989
_FORCE_INLINE_ real_t length_squared() const;

0 commit comments

Comments
 (0)