Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Compare floating-point spans by value

Vladislav45720c7ea769
diff --git a/core/templates/span.h b/core/templates/span.h
index 5cb850dc3ef..7695c5b872b 100644
--- a/core/templates/span.h
+++ b/core/templates/span.h
@@ -35,7 +35,7 @@
template <typename LHS, typename RHS>
bool are_spans_equal(const LHS *p_lhs, const RHS *p_rhs, size_t p_size) {
- if constexpr (std::is_same_v<LHS, RHS> && std::is_fundamental_v<LHS>) {
+ if constexpr (std::is_same_v<LHS, RHS> && std::is_integral_v<LHS> && std::has_unique_object_representations_v<LHS>) {
// Optimize trivial type comparison.
// is_trivially_equality_comparable would help, but it doesn't exist.
// memcmp requires pointer argument to be valid even on size = 0 (C11 §7.24.1(2)).
diff --git a/tests/core/templates/test_span.cpp b/tests/core/templates/test_span.cpp
index da71c9236c7..db30f1f4aef 100644
--- a/tests/core/templates/test_span.cpp
+++ b/tests/core/templates/test_span.cpp
@@ -34,6 +34,8 @@ TEST_FORCE_LINK(test_span)
#include "core/templates/span.h"
+#include <limits>
+
namespace TestSpan {
TEST_CASE("[Span] Constexpr Validators") {
@@ -71,4 +73,13 @@ TEST_CASE("[Span] Constexpr Validators") {
}
}
+TEST_CASE_TEMPLATE("[Span] Floating-point equality", T, float, double) {
+ const T positive_zero[] = { 0.0 };
+ const T negative_zero[] = { -0.0 };
+ CHECK_EQ(Span<T>(positive_zero), Span<T>(negative_zero));
+
+ const T nan[] = { std::numeric_limits<T>::quiet_NaN() };
+ CHECK_NE(Span<T>(nan), Span<T>(nan));
+}
+
} // namespace TestSpan