Skip to content

Commit

Permalink
Fix cases where floats are promoted to doubles and then converted bac…
Browse files Browse the repository at this point in the history
…k to floats. (-Wdouble-promotion)
  • Loading branch information
BenLubar committed Jan 31, 2025
1 parent a8476db commit 2271e34
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 66 deletions.
17 changes: 9 additions & 8 deletions include/godot_cpp/core/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ inline float sinh(float p_x) {
}

inline float sinc(float p_x) {
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
return p_x == 0 ? 1 : ::sinf(p_x) / p_x;
}
inline double sinc(double p_x) {
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
}

inline float sincn(float p_x) {
return (float)sinc(Math_PI * p_x);
return sinc((float)Math_PI * p_x);
}
inline double sincn(double p_x) {
return sinc(Math_PI * p_x);
Expand Down Expand Up @@ -653,8 +653,8 @@ inline bool is_equal_approx(double a, double b) {
return true;
}
// Then check for approximate equality.
double tolerance = CMP_EPSILON * abs(a);
if (tolerance < CMP_EPSILON) {
double tolerance = (double)CMP_EPSILON * abs(a);
if (tolerance < (double)CMP_EPSILON) {
tolerance = CMP_EPSILON;
}
return abs(a - b) < tolerance;
Expand All @@ -670,7 +670,7 @@ inline bool is_equal_approx(double a, double b, double tolerance) {
}

inline bool is_zero_approx(double s) {
return abs(s) < CMP_EPSILON;
return abs(s) < (double)CMP_EPSILON;
}

inline float absf(float g) {
Expand Down Expand Up @@ -769,7 +769,7 @@ inline int fast_ftoi(float a) {
int b;

#if (defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0603) || (defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) // windows 8 phone?
b = (int)((a > 0.0) ? (a + 0.5) : (a - 0.5));
b = (int)((a > 0.0f) ? (a + 0.5f) : (a - 0.5f));

#elif defined(_MSC_VER) && _MSC_VER < 1800
__asm fld a __asm fistp b
Expand All @@ -788,9 +788,10 @@ inline int fast_ftoi(float a) {
return b;
}

inline double snapped(double p_value, double p_step) {
template <typename T>
inline T snapped(T p_value, T p_step) {
if (p_step != 0) {
p_value = Math::floor(p_value / p_step + 0.5) * p_step;
p_value = Math::floor(p_value / p_step + 0.5f) * p_step;
}
return p_value;
}
Expand Down
6 changes: 3 additions & 3 deletions include/godot_cpp/templates/hashfuncs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static _FORCE_INLINE_ uint32_t hash_murmur3_one_double(double p_in, uint32_t p_s
} u;

// Normalize +/- 0.0 and NaN values so they hash the same.
if (p_in == 0.0f) {
if (p_in == 0.0) {
u.d = 0.0;
} else if (Math::is_nan(p_in)) {
u.d = NAN;
Expand Down Expand Up @@ -242,7 +242,7 @@ static _FORCE_INLINE_ uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev
} u;

// Normalize +/- 0.0 and NaN values so they hash the same.
if (p_in == 0.0f) {
if (p_in == 0.0) {
u.d = 0.0;
} else if (Math::is_nan(p_in)) {
u.d = NAN;
Expand Down Expand Up @@ -271,7 +271,7 @@ static _FORCE_INLINE_ uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_pr
} u;

// Normalize +/- 0.0 and NaN values so they hash the same.
if (p_in == 0.0f) {
if (p_in == 0.0) {
u.d = 0.0;
} else if (Math::is_nan(p_in)) {
u.d = NAN;
Expand Down
10 changes: 5 additions & 5 deletions include/godot_cpp/variant/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ struct _NO_DISCARD_ Color {

float cMax = MAX(cRed, MAX(cGreen, cBlue));

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

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

float exps = expp + 1.0f;

if (0.0f <= sMax && sMax < pow2to9) {
exps = expp;
}

float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
float sRed = Math::floor((cRed / Math::pow(2.0f, exps - B - N)) + 0.5f);
float sGreen = Math::floor((cGreen / Math::pow(2.0f, exps - B - N)) + 0.5f);
float sBlue = Math::floor((cBlue / Math::pow(2.0f, exps - B - N)) + 0.5f);

return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27);
}
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct _NO_DISCARD_ Projection {
Projection jitter_offseted(const Vector2 &p_offset) const;

static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0);
return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5f)) * 2.0f);
}

real_t get_z_far() const;
Expand Down
4 changes: 2 additions & 2 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p
Vector2 res = *this;

/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt = (1.0f - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;

return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
}

Vector2 Vector2::direction_to(const Vector2 &p_to) const {
Expand Down
4 changes: 2 additions & 2 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p
Vector3 res = *this;

/* Formula from Wikipedia article on Bezier curves. */
real_t omt = (1.0 - p_t);
real_t omt = (1.0f - p_t);
real_t omt2 = omt * omt;
real_t omt3 = omt2 * omt;
real_t t2 = p_t * p_t;
real_t t3 = t2 * p_t;

return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
return res * omt3 + p_control_1 * omt2 * p_t * 3.0f + p_control_2 * omt * t2 * 3.0f + p_end * t3;
}

real_t Vector3::distance_to(const Vector3 &p_to) const {
Expand Down
44 changes: 22 additions & 22 deletions src/variant/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
// return the simplest form (human friendlier in editor and scripts)
euler.x = 0;
euler.y = atan2(rows[0][2], rows[0][0]);
euler.y = Math::atan2(rows[0][2], rows[0][0]);
euler.z = 0;
} else {
euler.x = Math::atan2(-rows[1][2], rows[2][2]);
Expand All @@ -479,12 +479,12 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
}
} else {
euler.x = Math::atan2(rows[2][1], rows[1][1]);
euler.y = -Math_PI / 2.0f;
euler.y = (real_t)(-Math_PI / 2.0);
euler.z = 0.0f;
}
} else {
euler.x = Math::atan2(rows[2][1], rows[1][1]);
euler.y = Math_PI / 2.0f;
euler.y = (real_t)(Math_PI / 2.0);
euler.z = 0.0f;
}
return euler;
Expand All @@ -508,13 +508,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// It's -1
euler.x = -Math::atan2(rows[1][2], rows[2][2]);
euler.y = 0.0f;
euler.z = Math_PI / 2.0f;
euler.z = (real_t)(Math_PI / 2.0);
}
} else {
// It's 1
euler.x = -Math::atan2(rows[1][2], rows[2][2]);
euler.y = 0.0f;
euler.z = -Math_PI / 2.0f;
euler.z = (real_t)(-Math_PI / 2.0);
}
return euler;
}
Expand All @@ -535,22 +535,22 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// is this a pure X rotation?
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
// return the simplest form (human friendlier in editor and scripts)
euler.x = atan2(-m12, rows[1][1]);
euler.x = Math::atan2(-m12, rows[1][1]);
euler.y = 0;
euler.z = 0;
} else {
euler.x = asin(-m12);
euler.y = atan2(rows[0][2], rows[2][2]);
euler.z = atan2(rows[1][0], rows[1][1]);
euler.x = Math::asin(-m12);
euler.y = Math::atan2(rows[0][2], rows[2][2]);
euler.z = Math::atan2(rows[1][0], rows[1][1]);
}
} else { // m12 == -1
euler.x = Math_PI * 0.5f;
euler.y = atan2(rows[0][1], rows[0][0]);
euler.x = (real_t)(Math_PI * 0.5);
euler.y = Math::atan2(rows[0][1], rows[0][0]);
euler.z = 0;
}
} else { // m12 == 1
euler.x = -Math_PI * 0.5f;
euler.y = -atan2(rows[0][1], rows[0][0]);
euler.x = (real_t)(-Math_PI * 0.5);
euler.y = -Math::atan2(rows[0][1], rows[0][0]);
euler.z = 0;
}

Expand All @@ -575,13 +575,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
// It's -1
euler.x = Math::atan2(rows[2][1], rows[2][2]);
euler.y = 0.0f;
euler.z = -Math_PI / 2.0f;
euler.z = (real_t)(-Math_PI / 2.0);
}
} else {
// It's 1
euler.x = Math::atan2(rows[2][1], rows[2][2]);
euler.y = 0.0f;
euler.z = Math_PI / 2.0f;
euler.z = (real_t)(Math_PI / 2.0);
}
return euler;
}
Expand All @@ -601,13 +601,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
} else {
// It's -1
euler.x = -Math_PI / 2.0f;
euler.x = (real_t)(-Math_PI / 2.0);
euler.y = Math::atan2(rows[0][2], rows[0][0]);
euler.z = 0;
}
} else {
// It's 1
euler.x = Math_PI / 2.0f;
euler.x = (real_t)(Math_PI / 2.0);
euler.y = Math::atan2(rows[0][2], rows[0][0]);
euler.z = 0;
}
Expand All @@ -630,13 +630,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
} else {
// It's -1
euler.x = 0;
euler.y = Math_PI / 2.0f;
euler.y = (real_t)(Math_PI / 2.0);
euler.z = -Math::atan2(rows[0][1], rows[1][1]);
}
} else {
// It's 1
euler.x = 0;
euler.y = -Math_PI / 2.0f;
euler.y = (real_t)(-Math_PI / 2.0);
euler.z = -Math::atan2(rows[0][1], rows[1][1]);
}
return euler;
Expand Down Expand Up @@ -816,7 +816,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
return;
}
// As we have reached here there are no singularities so we can handle normally.
double s = Math::sqrt((rows[2][1] - rows[1][2]) * (rows[2][1] - rows[1][2]) + (rows[0][2] - rows[2][0]) * (rows[0][2] - rows[2][0]) + (rows[1][0] - rows[0][1]) * (rows[1][0] - rows[0][1])); // Used to normalize.
real_t s = Math::sqrt((rows[2][1] - rows[1][2]) * (rows[2][1] - rows[1][2]) + (rows[0][2] - rows[2][0]) * (rows[0][2] - rows[2][0]) + (rows[1][0] - rows[0][1]) * (rows[1][0] - rows[0][1])); // Used to normalize.

if (Math::abs(s) < CMP_EPSILON) {
// Prevent divide by zero, should not happen if matrix is orthogonal and should be caught by singularity test above.
Expand Down Expand Up @@ -939,9 +939,9 @@ void Basis::rotate_sh(real_t *p_values) {
const static real_t s_c_scale = 1.0 / 0.91529123286551084;
const static real_t s_c_scale_inv = 0.91529123286551084;

const static real_t s_rc2 = 1.5853309190550713 * s_c_scale;
const static real_t s_rc2 = (real_t)1.5853309190550713 * s_c_scale;
const static real_t s_c4_div_c3 = s_c4 / s_c3;
const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * (real_t)2.0;

const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv;
const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv;
Expand Down
Loading

0 comments on commit 2271e34

Please sign in to comment.