Skip to content

Commit d24c715

Browse files
committed
Float literals - fix math classes to allow 32 bit calculations
Converts float literals from double format (e.g. 0.0) to float format (e.g. 0.0f) where appropriate for 32 bit calculations, and cast to (real_t) or (float) as appropriate. This ensures that appropriate calculations will be done at 32 bits when real_t is compiled as float, rather than promoted to 64 bits.
1 parent ae9fa90 commit d24c715

25 files changed

+264
-264
lines changed

core/math/aabb.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class _NO_DISCARD_CLASS_ AABB {
5858
void set_position(const Vector3 &p_pos) { position = p_pos; }
5959
const Vector3 &get_size() const { return size; }
6060
void set_size(const Vector3 &p_size) { size = p_size; }
61-
Vector3 get_center() const { return position + (size * 0.5); }
61+
Vector3 get_center() const { return position + (size * 0.5f); }
6262

6363
bool operator==(const AABB &p_rval) const;
6464
bool operator!=(const AABB &p_rval) const;
@@ -176,7 +176,7 @@ inline bool AABB::encloses(const AABB &p_aabb) const {
176176
}
177177

178178
Vector3 AABB::get_support(const Vector3 &p_normal) const {
179-
Vector3 half_extents = size * 0.5;
179+
Vector3 half_extents = size * 0.5f;
180180
Vector3 ofs = position + half_extents;
181181

182182
return Vector3(
@@ -210,7 +210,7 @@ Vector3 AABB::get_endpoint(int p_point) const {
210210
}
211211

212212
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
213-
Vector3 half_extents = size * 0.5;
213+
Vector3 half_extents = size * 0.5f;
214214
Vector3 ofs = position + half_extents;
215215

216216
for (int i = 0; i < p_plane_count; i++) {
@@ -252,7 +252,7 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con
252252
}
253253

254254
bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
255-
Vector3 half_extents = size * 0.5;
255+
Vector3 half_extents = size * 0.5f;
256256
Vector3 ofs = position + half_extents;
257257

258258
for (int i = 0; i < p_plane_count; i++) {
@@ -322,7 +322,7 @@ inline void AABB::expand_to(const Vector3 &p_vector) {
322322
}
323323

324324
void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
325-
Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
325+
Vector3 half_extents = size * 0.5f;
326326
Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
327327

328328
real_t length = p_plane.normal.abs().dot(half_extents);
@@ -360,9 +360,9 @@ inline real_t AABB::get_shortest_axis_size() const {
360360
}
361361

362362
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
363-
real_t divx = 1.0 / p_dir.x;
364-
real_t divy = 1.0 / p_dir.y;
365-
real_t divz = 1.0 / p_dir.z;
363+
real_t divx = 1 / p_dir.x;
364+
real_t divy = 1 / p_dir.y;
365+
real_t divz = 1 / p_dir.z;
366366

367367
Vector3 upbound = position + size;
368368
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
@@ -412,9 +412,9 @@ void AABB::grow_by(real_t p_amount) {
412412
position.x -= p_amount;
413413
position.y -= p_amount;
414414
position.z -= p_amount;
415-
size.x += 2.0 * p_amount;
416-
size.y += 2.0 * p_amount;
417-
size.z += 2.0 * p_amount;
415+
size.x += 2 * p_amount;
416+
size.y += 2 * p_amount;
417+
size.z += 2 * p_amount;
418418
}
419419

420420
#endif // AABB_H

core/math/basis.cpp

+58-58
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
(elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
3838

3939
void Basis::from_z(const Vector3 &p_z) {
40-
if (Math::abs(p_z.z) > Math_SQRT12) {
40+
if (Math::abs(p_z.z) > (real_t)Math_SQRT12) {
4141
// choose p in y-z plane
4242
real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
43-
real_t k = 1.0 / Math::sqrt(a);
43+
real_t k = 1 / Math::sqrt(a);
4444
elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
4545
elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
4646
} else {
4747
// choose p in x-y plane
4848
real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
49-
real_t k = 1.0 / Math::sqrt(a);
49+
real_t k = 1 / Math::sqrt(a);
5050
elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
5151
elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
5252
}
@@ -63,7 +63,7 @@ void Basis::invert() {
6363
#ifdef MATH_CHECKS
6464
ERR_FAIL_COND(det == 0);
6565
#endif
66-
real_t s = 1.0 / det;
66+
real_t s = 1 / det;
6767

6868
set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
6969
co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
@@ -113,13 +113,13 @@ bool Basis::is_rotation() const {
113113
}
114114

115115
bool Basis::is_symmetric() const {
116-
if (!Math::is_equal_approx_ratio(elements[0][1], elements[1][0], UNIT_EPSILON)) {
116+
if (!Math::is_equal_approx_ratio(elements[0][1], elements[1][0], (real_t)UNIT_EPSILON)) {
117117
return false;
118118
}
119-
if (!Math::is_equal_approx_ratio(elements[0][2], elements[2][0], UNIT_EPSILON)) {
119+
if (!Math::is_equal_approx_ratio(elements[0][2], elements[2][0], (real_t)UNIT_EPSILON)) {
120120
return false;
121121
}
122-
if (!Math::is_equal_approx_ratio(elements[1][2], elements[2][1], UNIT_EPSILON)) {
122+
if (!Math::is_equal_approx_ratio(elements[1][2], elements[2][1], (real_t)UNIT_EPSILON)) {
123123
return false;
124124
}
125125

@@ -138,7 +138,7 @@ Basis Basis::diagonalize() {
138138

139139
int ite = 0;
140140
Basis acc_rot;
141-
while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
141+
while (off_matrix_norm_2 > (real_t)CMP_EPSILON2 && ite++ < ite_max) {
142142
real_t el01_2 = elements[0][1] * elements[0][1];
143143
real_t el02_2 = elements[0][2] * elements[0][2];
144144
real_t el12_2 = elements[1][2] * elements[1][2];
@@ -167,7 +167,7 @@ Basis Basis::diagonalize() {
167167
if (Math::is_equal_approx(elements[j][j], elements[i][i])) {
168168
angle = Math_PI / 4;
169169
} else {
170-
angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
170+
angle = 0.5f * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
171171
}
172172

173173
// Compute the rotation matrix
@@ -412,10 +412,10 @@ Vector3 Basis::get_euler_xyz() const {
412412

413413
Vector3 euler;
414414
real_t sy = elements[0][2];
415-
if (sy < (1.0 - CMP_EPSILON)) {
416-
if (sy > -(1.0 - CMP_EPSILON)) {
415+
if (sy < (1 - (real_t)CMP_EPSILON)) {
416+
if (sy > -(1 - (real_t)CMP_EPSILON)) {
417417
// is this a pure Y rotation?
418-
if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
418+
if (elements[1][0] == 0 && elements[0][1] == 0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
419419
// return the simplest form (human friendlier in editor and scripts)
420420
euler.x = 0;
421421
euler.y = atan2(elements[0][2], elements[0][0]);
@@ -447,15 +447,15 @@ void Basis::set_euler_xyz(const Vector3 &p_euler) {
447447

448448
c = Math::cos(p_euler.x);
449449
s = Math::sin(p_euler.x);
450-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
450+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
451451

452452
c = Math::cos(p_euler.y);
453453
s = Math::sin(p_euler.y);
454-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
454+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
455455

456456
c = Math::cos(p_euler.z);
457457
s = Math::sin(p_euler.z);
458-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
458+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
459459

460460
//optimizer will optimize away all this anyway
461461
*this = xmat * (ymat * zmat);
@@ -471,8 +471,8 @@ Vector3 Basis::get_euler_xzy() const {
471471

472472
Vector3 euler;
473473
real_t sz = elements[0][1];
474-
if (sz < (1.0 - CMP_EPSILON)) {
475-
if (sz > -(1.0 - CMP_EPSILON)) {
474+
if (sz < (1 - (real_t)CMP_EPSILON)) {
475+
if (sz > -(1 - (real_t)CMP_EPSILON)) {
476476
euler.x = Math::atan2(elements[2][1], elements[1][1]);
477477
euler.y = Math::atan2(elements[0][2], elements[0][0]);
478478
euler.z = Math::asin(-sz);
@@ -496,15 +496,15 @@ void Basis::set_euler_xzy(const Vector3 &p_euler) {
496496

497497
c = Math::cos(p_euler.x);
498498
s = Math::sin(p_euler.x);
499-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
499+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
500500

501501
c = Math::cos(p_euler.y);
502502
s = Math::sin(p_euler.y);
503-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
503+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
504504

505505
c = Math::cos(p_euler.z);
506506
s = Math::sin(p_euler.z);
507-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
507+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
508508

509509
*this = xmat * zmat * ymat;
510510
}
@@ -519,8 +519,8 @@ Vector3 Basis::get_euler_yzx() const {
519519

520520
Vector3 euler;
521521
real_t sz = elements[1][0];
522-
if (sz < (1.0 - CMP_EPSILON)) {
523-
if (sz > -(1.0 - CMP_EPSILON)) {
522+
if (sz < (1 - (real_t)CMP_EPSILON)) {
523+
if (sz > -(1 - (real_t)CMP_EPSILON)) {
524524
euler.x = Math::atan2(-elements[1][2], elements[1][1]);
525525
euler.y = Math::atan2(-elements[2][0], elements[0][0]);
526526
euler.z = Math::asin(sz);
@@ -544,15 +544,15 @@ void Basis::set_euler_yzx(const Vector3 &p_euler) {
544544

545545
c = Math::cos(p_euler.x);
546546
s = Math::sin(p_euler.x);
547-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
547+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
548548

549549
c = Math::cos(p_euler.y);
550550
s = Math::sin(p_euler.y);
551-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
551+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
552552

553553
c = Math::cos(p_euler.z);
554554
s = Math::sin(p_euler.z);
555-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
555+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
556556

557557
*this = ymat * zmat * xmat;
558558
}
@@ -572,8 +572,8 @@ Vector3 Basis::get_euler_yxz() const {
572572

573573
real_t m12 = elements[1][2];
574574

575-
if (m12 < (1 - CMP_EPSILON)) {
576-
if (m12 > -(1 - CMP_EPSILON)) {
575+
if (m12 < (1 - (real_t)CMP_EPSILON)) {
576+
if (m12 > -(1 - (real_t)CMP_EPSILON)) {
577577
// is this a pure X rotation?
578578
if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) {
579579
// return the simplest form (human friendlier in editor and scripts)
@@ -608,15 +608,15 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) {
608608

609609
c = Math::cos(p_euler.x);
610610
s = Math::sin(p_euler.x);
611-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
611+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
612612

613613
c = Math::cos(p_euler.y);
614614
s = Math::sin(p_euler.y);
615-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
615+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
616616

617617
c = Math::cos(p_euler.z);
618618
s = Math::sin(p_euler.z);
619-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
619+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
620620

621621
//optimizer will optimize away all this anyway
622622
*this = ymat * xmat * zmat;
@@ -631,8 +631,8 @@ Vector3 Basis::get_euler_zxy() const {
631631
// -cx*sy sx cx*cy
632632
Vector3 euler;
633633
real_t sx = elements[2][1];
634-
if (sx < (1.0 - CMP_EPSILON)) {
635-
if (sx > -(1.0 - CMP_EPSILON)) {
634+
if (sx < (1 - (real_t)CMP_EPSILON)) {
635+
if (sx > -(1 - (real_t)CMP_EPSILON)) {
636636
euler.x = Math::asin(sx);
637637
euler.y = Math::atan2(-elements[2][0], elements[2][2]);
638638
euler.z = Math::atan2(-elements[0][1], elements[1][1]);
@@ -656,15 +656,15 @@ void Basis::set_euler_zxy(const Vector3 &p_euler) {
656656

657657
c = Math::cos(p_euler.x);
658658
s = Math::sin(p_euler.x);
659-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
659+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
660660

661661
c = Math::cos(p_euler.y);
662662
s = Math::sin(p_euler.y);
663-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
663+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
664664

665665
c = Math::cos(p_euler.z);
666666
s = Math::sin(p_euler.z);
667-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
667+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
668668

669669
*this = zmat * xmat * ymat;
670670
}
@@ -678,8 +678,8 @@ Vector3 Basis::get_euler_zyx() const {
678678
// -sy cy*sx cy*cx
679679
Vector3 euler;
680680
real_t sy = elements[2][0];
681-
if (sy < (1.0 - CMP_EPSILON)) {
682-
if (sy > -(1.0 - CMP_EPSILON)) {
681+
if (sy < (1 - (real_t)CMP_EPSILON)) {
682+
if (sy > -(1 - (real_t)CMP_EPSILON)) {
683683
euler.x = Math::atan2(elements[2][1], elements[2][2]);
684684
euler.y = Math::asin(-sy);
685685
euler.z = Math::atan2(elements[1][0], elements[0][0]);
@@ -703,15 +703,15 @@ void Basis::set_euler_zyx(const Vector3 &p_euler) {
703703

704704
c = Math::cos(p_euler.x);
705705
s = Math::sin(p_euler.x);
706-
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
706+
Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
707707

708708
c = Math::cos(p_euler.y);
709709
s = Math::sin(p_euler.y);
710-
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
710+
Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
711711

712712
c = Math::cos(p_euler.z);
713713
s = Math::sin(p_euler.z);
714-
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
714+
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
715715

716716
*this = zmat * ymat * xmat;
717717
}
@@ -772,10 +772,10 @@ Quat Basis::get_quat() const {
772772
real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
773773
real_t temp[4];
774774

775-
if (trace > 0.0) {
776-
real_t s = Math::sqrt(trace + 1.0);
777-
temp[3] = (s * 0.5);
778-
s = 0.5 / s;
775+
if (trace > 0) {
776+
real_t s = Math::sqrt(trace + 1);
777+
temp[3] = (s * 0.5f);
778+
s = 0.5f / s;
779779

780780
temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s);
781781
temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s);
@@ -787,9 +787,9 @@ Quat Basis::get_quat() const {
787787
int j = (i + 1) % 3;
788788
int k = (i + 2) % 3;
789789

790-
real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0);
791-
temp[i] = s * 0.5;
792-
s = 0.5 / s;
790+
real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1);
791+
temp[i] = s * 0.5f;
792+
s = 0.5f / s;
793793

794794
temp[3] = (m.elements[k][j] - m.elements[j][k]) * s;
795795
temp[j] = (m.elements[j][i] + m.elements[i][j]) * s;
@@ -832,10 +832,10 @@ int Basis::get_orthogonal_index() const {
832832
for (int i = 0; i < 3; i++) {
833833
for (int j = 0; j < 3; j++) {
834834
real_t v = orth[i][j];
835-
if (v > 0.5) {
836-
v = 1.0;
837-
} else if (v < -0.5) {
838-
v = -1.0;
835+
if (v > 0.5f) {
836+
v = 1;
837+
} else if (v < -0.5f) {
838+
v = -1;
839839
} else {
840840
v = 0;
841841
}
@@ -940,14 +940,14 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
940940

941941
void Basis::set_quat(const Quat &p_quat) {
942942
real_t d = p_quat.length_squared();
943-
real_t s = 2.0 / d;
943+
real_t s = 2 / d;
944944
real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
945945
real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
946946
real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
947947
real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
948-
set(1.0 - (yy + zz), xy - wz, xz + wy,
949-
xy + wz, 1.0 - (xx + zz), yz - wx,
950-
xz - wy, yz + wx, 1.0 - (xx + yy));
948+
set(1 - (yy + zz), xy - wz, xz + wy,
949+
xy + wz, 1 - (xx + zz), yz - wx,
950+
xz - wy, yz + wx, 1 - (xx + yy));
951951
}
952952

953953
void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
@@ -957,9 +957,9 @@ void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
957957
#endif
958958
Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
959959
real_t cosine = Math::cos(p_phi);
960-
elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
961-
elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
962-
elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
960+
elements[0][0] = axis_sq.x + cosine * (1 - axis_sq.x);
961+
elements[1][1] = axis_sq.y + cosine * (1 - axis_sq.y);
962+
elements[2][2] = axis_sq.z + cosine * (1 - axis_sq.z);
963963

964964
real_t sine = Math::sin(p_phi);
965965
real_t t = 1 - cosine;

0 commit comments

Comments
 (0)