Skip to content

Commit cd1a8b3

Browse files
committed
Fix cases where floats are promoted to doubles and then converted back to floats. (-Wdouble-promotion)
1 parent a8476db commit cd1a8b3

File tree

9 files changed

+67
-66
lines changed

9 files changed

+67
-66
lines changed

include/godot_cpp/core/math.hpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ inline float sinh(float p_x) {
320320
}
321321

322322
inline float sinc(float p_x) {
323-
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
323+
return p_x == 0 ? 1 : ::sinf(p_x) / p_x;
324324
}
325325
inline double sinc(double p_x) {
326326
return p_x == 0 ? 1 : ::sin(p_x) / p_x;
327327
}
328328

329329
inline float sincn(float p_x) {
330-
return (float)sinc(Math_PI * p_x);
330+
return sinc((float)Math_PI * p_x);
331331
}
332332
inline double sincn(double p_x) {
333333
return sinc(Math_PI * p_x);
@@ -653,8 +653,8 @@ inline bool is_equal_approx(double a, double b) {
653653
return true;
654654
}
655655
// Then check for approximate equality.
656-
double tolerance = CMP_EPSILON * abs(a);
657-
if (tolerance < CMP_EPSILON) {
656+
double tolerance = (double)CMP_EPSILON * abs(a);
657+
if (tolerance < (double)CMP_EPSILON) {
658658
tolerance = CMP_EPSILON;
659659
}
660660
return abs(a - b) < tolerance;
@@ -670,7 +670,7 @@ inline bool is_equal_approx(double a, double b, double tolerance) {
670670
}
671671

672672
inline bool is_zero_approx(double s) {
673-
return abs(s) < CMP_EPSILON;
673+
return abs(s) < (double)CMP_EPSILON;
674674
}
675675

676676
inline float absf(float g) {
@@ -769,7 +769,7 @@ inline int fast_ftoi(float a) {
769769
int b;
770770

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

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

791-
inline double snapped(double p_value, double p_step) {
791+
template<typename T>
792+
inline T snapped(T p_value, T p_step) {
792793
if (p_step != 0) {
793-
p_value = Math::floor(p_value / p_step + 0.5) * p_step;
794+
p_value = Math::floor(p_value / p_step + 0.5f) * p_step;
794795
}
795796
return p_value;
796797
}

include/godot_cpp/templates/hashfuncs.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static _FORCE_INLINE_ uint32_t hash_murmur3_one_double(double p_in, uint32_t p_s
154154
} u;
155155

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

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

273273
// Normalize +/- 0.0 and NaN values so they hash the same.
274-
if (p_in == 0.0f) {
274+
if (p_in == 0.0) {
275275
u.d = 0.0;
276276
} else if (Math::is_nan(p_in)) {
277277
u.d = NAN;

include/godot_cpp/variant/color.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ struct _NO_DISCARD_ Color {
141141

142142
float cMax = MAX(cRed, 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 = MAX(-B - 1.0f, Math::floor(Math::log(cMax) / (real_t)Math_LN2)) + 1.0f + B;
145145

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

148148
float exps = expp + 1.0f;
149149

150150
if (0.0f <= sMax && sMax < pow2to9) {
151151
exps = expp;
152152
}
153153

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

158158
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);
159159
}

include/godot_cpp/variant/projection.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct _NO_DISCARD_ Projection {
9898
Projection jitter_offseted(const Vector2 &p_offset) const;
9999

100100
static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
101-
return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0);
101+
return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5f)) * 2.0f);
102102
}
103103

104104
real_t get_z_far() const;

include/godot_cpp/variant/vector2.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p
292292
Vector2 res = *this;
293293

294294
/* Formula from Wikipedia article on Bezier curves. */
295-
real_t omt = (1.0 - p_t);
295+
real_t omt = (1.0f - p_t);
296296
real_t omt2 = omt * omt;
297297
real_t omt3 = omt2 * omt;
298298
real_t t2 = p_t * p_t;
299299
real_t t3 = t2 * p_t;
300300

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

304304
Vector2 Vector2::direction_to(const Vector2 &p_to) const {

include/godot_cpp/variant/vector3.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p
277277
Vector3 res = *this;
278278

279279
/* Formula from Wikipedia article on Bezier curves. */
280-
real_t omt = (1.0 - p_t);
280+
real_t omt = (1.0f - p_t);
281281
real_t omt2 = omt * omt;
282282
real_t omt3 = omt2 * omt;
283283
real_t t2 = p_t * p_t;
284284
real_t t3 = t2 * p_t;
285285

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

289289
real_t Vector3::distance_to(const Vector3 &p_to) const {

src/variant/basis.cpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
470470
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
471471
// return the simplest form (human friendlier in editor and scripts)
472472
euler.x = 0;
473-
euler.y = atan2(rows[0][2], rows[0][0]);
473+
euler.y = Math::atan2(rows[0][2], rows[0][0]);
474474
euler.z = 0;
475475
} else {
476476
euler.x = Math::atan2(-rows[1][2], rows[2][2]);
@@ -479,12 +479,12 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
479479
}
480480
} else {
481481
euler.x = Math::atan2(rows[2][1], rows[1][1]);
482-
euler.y = -Math_PI / 2.0f;
482+
euler.y = (real_t)(-Math_PI / 2.0);
483483
euler.z = 0.0f;
484484
}
485485
} else {
486486
euler.x = Math::atan2(rows[2][1], rows[1][1]);
487-
euler.y = Math_PI / 2.0f;
487+
euler.y = (real_t)(Math_PI / 2.0);
488488
euler.z = 0.0f;
489489
}
490490
return euler;
@@ -508,13 +508,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
508508
// It's -1
509509
euler.x = -Math::atan2(rows[1][2], rows[2][2]);
510510
euler.y = 0.0f;
511-
euler.z = Math_PI / 2.0f;
511+
euler.z = (real_t)(Math_PI / 2.0);
512512
}
513513
} else {
514514
// It's 1
515515
euler.x = -Math::atan2(rows[1][2], rows[2][2]);
516516
euler.y = 0.0f;
517-
euler.z = -Math_PI / 2.0f;
517+
euler.z = (real_t)(-Math_PI / 2.0);
518518
}
519519
return euler;
520520
}
@@ -535,22 +535,22 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
535535
// is this a pure X rotation?
536536
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
537537
// return the simplest form (human friendlier in editor and scripts)
538-
euler.x = atan2(-m12, rows[1][1]);
538+
euler.x = Math::atan2(-m12, rows[1][1]);
539539
euler.y = 0;
540540
euler.z = 0;
541541
} else {
542-
euler.x = asin(-m12);
543-
euler.y = atan2(rows[0][2], rows[2][2]);
544-
euler.z = atan2(rows[1][0], rows[1][1]);
542+
euler.x = Math::asin(-m12);
543+
euler.y = Math::atan2(rows[0][2], rows[2][2]);
544+
euler.z = Math::atan2(rows[1][0], rows[1][1]);
545545
}
546546
} else { // m12 == -1
547-
euler.x = Math_PI * 0.5f;
548-
euler.y = atan2(rows[0][1], rows[0][0]);
547+
euler.x = (real_t)(Math_PI * 0.5);
548+
euler.y = Math::atan2(rows[0][1], rows[0][0]);
549549
euler.z = 0;
550550
}
551551
} else { // m12 == 1
552-
euler.x = -Math_PI * 0.5f;
553-
euler.y = -atan2(rows[0][1], rows[0][0]);
552+
euler.x = (real_t)(-Math_PI * 0.5);
553+
euler.y = -Math::atan2(rows[0][1], rows[0][0]);
554554
euler.z = 0;
555555
}
556556

@@ -575,13 +575,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
575575
// It's -1
576576
euler.x = Math::atan2(rows[2][1], rows[2][2]);
577577
euler.y = 0.0f;
578-
euler.z = -Math_PI / 2.0f;
578+
euler.z = (real_t)(-Math_PI / 2.0);
579579
}
580580
} else {
581581
// It's 1
582582
euler.x = Math::atan2(rows[2][1], rows[2][2]);
583583
euler.y = 0.0f;
584-
euler.z = Math_PI / 2.0f;
584+
euler.z = (real_t)(Math_PI / 2.0);
585585
}
586586
return euler;
587587
}
@@ -601,13 +601,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
601601
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
602602
} else {
603603
// It's -1
604-
euler.x = -Math_PI / 2.0f;
604+
euler.x = (real_t)(-Math_PI / 2.0);
605605
euler.y = Math::atan2(rows[0][2], rows[0][0]);
606606
euler.z = 0;
607607
}
608608
} else {
609609
// It's 1
610-
euler.x = Math_PI / 2.0f;
610+
euler.x = (real_t)(Math_PI / 2.0);
611611
euler.y = Math::atan2(rows[0][2], rows[0][0]);
612612
euler.z = 0;
613613
}
@@ -630,13 +630,13 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
630630
} else {
631631
// It's -1
632632
euler.x = 0;
633-
euler.y = Math_PI / 2.0f;
633+
euler.y = (real_t)(Math_PI / 2.0);
634634
euler.z = -Math::atan2(rows[0][1], rows[1][1]);
635635
}
636636
} else {
637637
// It's 1
638638
euler.x = 0;
639-
euler.y = -Math_PI / 2.0f;
639+
euler.y = (real_t)(-Math_PI / 2.0);
640640
euler.z = -Math::atan2(rows[0][1], rows[1][1]);
641641
}
642642
return euler;
@@ -816,7 +816,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
816816
return;
817817
}
818818
// As we have reached here there are no singularities so we can handle normally.
819-
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.
819+
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.
820820

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

942-
const static real_t s_rc2 = 1.5853309190550713 * s_c_scale;
942+
const static real_t s_rc2 = (real_t)1.5853309190550713 * s_c_scale;
943943
const static real_t s_c4_div_c3 = s_c4 / s_c3;
944-
const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
944+
const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * (real_t)2.0;
945945

946946
const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv;
947947
const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv;

0 commit comments

Comments
 (0)