Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Replace C math headers with C++ equivalents #104386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
#include "core/templates/hash_map.h"
#include "core/variant/dictionary.h"

#include <cmath>

const char *Image::format_names[Image::FORMAT_MAX] = {
"Lum8",
"LumAlpha8",
Expand Down Expand Up @@ -4383,12 +4381,12 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool
image_metric_mean = CLAMP(sum / total_values, 0.0f, 255.0f);
image_metric_mean_squared = CLAMP(sum2 / total_values, 0.0f, 255.0f * 255.0f);

image_metric_root_mean_squared = sqrt(image_metric_mean_squared);
image_metric_root_mean_squared = std::sqrt(image_metric_mean_squared);

if (!image_metric_root_mean_squared) {
image_metric_peak_snr = 1e+10f;
} else {
image_metric_peak_snr = CLAMP(log10(255.0f / image_metric_root_mean_squared) * 20.0f, 0.0f, 500.0f);
image_metric_peak_snr = CLAMP(std::log10(255.0f / image_metric_root_mean_squared) * 20.0f, 0.0f, 500.0f);
}
result["max"] = image_metric_max;
result["mean"] = image_metric_mean;
Expand Down
2 changes: 1 addition & 1 deletion core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_
return String("0.0");
}

double magnitude = log10(Math::abs(num));
double magnitude = std::log10(Math::abs(num));
int total_digits = p_full_precision ? 17 : 14;
int precision = MAX(1, total_digits - (int)Math::floor(magnitude));

Expand Down
14 changes: 7 additions & 7 deletions core/io/plist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
} break;
case 0x10: {
node->data_type = PL_NODE_TYPE_INTEGER;
node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, std::pow(2, marker_size)));
} break;
case 0x20: {
node->data_type = PL_NODE_TYPE_REAL;
node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, std::pow(2, marker_size)));
} break;
case 0x30: {
node->data_type = PL_NODE_TYPE_DATE;
Expand All @@ -539,7 +539,7 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
case 0x40: {
if (marker_size == 0x0F) {
uint8_t ext = p_file->get_8() & 0xF;
marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
marker_size = read_bplist_var_size_int(p_file, std::pow(2, ext));
}
node->data_type = PL_NODE_TYPE_DATA;
PackedByteArray buf;
Expand All @@ -550,7 +550,7 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
case 0x50: {
if (marker_size == 0x0F) {
uint8_t ext = p_file->get_8() & 0xF;
marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
marker_size = read_bplist_var_size_int(p_file, std::pow(2, ext));
}
node->data_type = PL_NODE_TYPE_STRING;
node->data_string.resize(marker_size + 1);
Expand All @@ -559,7 +559,7 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
case 0x60: {
if (marker_size == 0x0F) {
uint8_t ext = p_file->get_8() & 0xF;
marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
marker_size = read_bplist_var_size_int(p_file, std::pow(2, ext));
}
Char16String cs16;
cs16.resize(marker_size + 1);
Expand All @@ -577,7 +577,7 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
case 0xC0: {
if (marker_size == 0x0F) {
uint8_t ext = p_file->get_8() & 0xF;
marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
marker_size = read_bplist_var_size_int(p_file, std::pow(2, ext));
}
uint64_t pos = p_file->get_position();

Expand All @@ -594,7 +594,7 @@ Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_
case 0xD0: {
if (marker_size == 0x0F) {
uint8_t ext = p_file->get_8() & 0xF;
marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
marker_size = read_bplist_var_size_int(p_file, std::pow(2, ext));
}
uint64_t pos = p_file->get_position();

Expand Down
1 change: 1 addition & 0 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/io/resource_loader.h"
#include "core/math/math_funcs.h"
#include "core/math/random_pcg.h"
#include "core/os/os.h"
#include "scene/main/node.h" //only so casting works

Expand Down
20 changes: 10 additions & 10 deletions core/math/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ void Basis::scale_orthogonal(const Vector3 &p_scale) {
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
Basis m = *this;
Vector3 s = Vector3(-1, -1, -1) + p_scale;
bool sign = signbit(s.x + s.y + s.z);
bool sign = std::signbit(s.x + s.y + s.z);
Basis b = m.orthonormalized();
s = b.xform_inv(s);
Vector3 dots;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dots[j] += s[i] * abs(m.get_column(i).normalized().dot(b.get_column(j)));
dots[j] += s[i] * std::abs(m.get_column(i).normalized().dot(b.get_column(j)));
}
}
if (sign != signbit(dots.x + dots.y + dots.z)) {
if (sign != std::signbit(dots.x + dots.y + dots.z)) {
dots = -dots;
}
m.scale_local(Vector3(1, 1, 1) + dots);
Expand Down Expand Up @@ -477,7 +477,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 = std::atan2(rows[0][2], rows[0][0]);
euler.z = 0;
} else {
euler.x = Math::atan2(-rows[1][2], rows[2][2]);
Expand Down Expand Up @@ -542,22 +542,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 = std::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 = std::asin(-m12);
euler.y = std::atan2(rows[0][2], rows[2][2]);
euler.z = std::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.y = std::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.y = -std::atan2(rows[0][1], rows[0][0]);
euler.z = 0;
}

Expand Down
3 changes: 2 additions & 1 deletion core/math/math_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
#include "math_funcs.h"

#include "core/error/error_macros.h"
#include "core/math/random_pcg.h"

RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC);
static RandomPCG default_rand;

uint32_t Math::rand_from_seed(uint64_t *seed) {
RandomPCG rng = RandomPCG(*seed, RandomPCG::DEFAULT_INC);
Expand Down
Loading