From 7837f0e8332f3726e0322b0c48b0da4d7c2d5813 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Fri, 10 Jul 2015 14:32:35 +0200 Subject: [PATCH] BLI_math 'compare' cleanup & enhancements. This commit: * Adds a 'compare_ff' function for absolute 'almost equal' comparison of floats. * Makes 'compare_vxvx' functions use that new 'compare_ff' one. * Adds a 'compare_ff_relative' function for secured ulp-based relative comparison of floats. * Adds matching 'compare_vxvx_relative' functions. * Adds some basic tests for compare_ff_relative. See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ Note that we could replace our python/mathutils' EXPP_FloatsAreEqual() by BLI's compare_ff_relative (using a very small absolute max_diff), but these do not have exact same behavior... Left a comment there for now, we can do it later if/when we are sure it won't break anything! --- source/blender/blenlib/BLI_math_base.h | 3 + source/blender/blenlib/BLI_math_vector.h | 11 ++- .../blender/blenlib/intern/math_base_inline.c | 40 +++++++++ .../blenlib/intern/math_vector_inline.c | 57 +++++++----- source/blender/python/mathutils/mathutils.c | 9 ++ tests/gtests/blenlib/BLI_math_base_test.cc | 87 +++++++++++++++++++ tests/gtests/blenlib/CMakeLists.txt | 1 + 7 files changed, 183 insertions(+), 25 deletions(-) create mode 100644 tests/gtests/blenlib/BLI_math_base_test.cc diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 39c1b22a632..20b76354f58 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -196,6 +196,9 @@ MINLINE int max_iii(int a, int b, int c); MINLINE int min_iiii(int a, int b, int c, int d); MINLINE int max_iiii(int a, int b, int c, int d); +MINLINE int compare_ff(float a, float b, const float max_diff); +MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps); + MINLINE float signf(float f); MINLINE int signum_i_ex(float a, float eps); MINLINE int signum_i(float a); diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h index 9bc1121801e..493c285d251 100644 --- a/source/blender/blenlib/BLI_math_vector.h +++ b/source/blender/blenlib/BLI_math_vector.h @@ -240,14 +240,19 @@ MINLINE bool is_one_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT; MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT; MINLINE bool equals_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT; +MINLINE bool equals_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT; + MINLINE bool compare_v2v2(const float a[2], const float b[2], const float limit) ATTR_WARN_UNUSED_RESULT; MINLINE bool compare_v3v3(const float a[3], const float b[3], const float limit) ATTR_WARN_UNUSED_RESULT; +MINLINE bool compare_v4v4(const float a[4], const float b[4], const float limit) ATTR_WARN_UNUSED_RESULT; + +MINLINE bool compare_v2v2_relative(const float a[2], const float b[2], const float limit, const int max_ulps) ATTR_WARN_UNUSED_RESULT; +MINLINE bool compare_v3v3_relative(const float a[3], const float b[3], const float limit, const int max_ulps) ATTR_WARN_UNUSED_RESULT; +MINLINE bool compare_v4v4_relative(const float a[4], const float b[4], const float limit, const int max_ulps) ATTR_WARN_UNUSED_RESULT; + MINLINE bool compare_len_v3v3(const float a[3], const float b[3], const float limit) ATTR_WARN_UNUSED_RESULT; MINLINE bool compare_len_squared_v3v3(const float a[3], const float b[3], const float limit) ATTR_WARN_UNUSED_RESULT; -MINLINE bool compare_v4v4(const float a[4], const float b[4], const float limit) ATTR_WARN_UNUSED_RESULT; -MINLINE bool equals_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT; - MINLINE float line_point_side_v2(const float l1[2], const float l2[2], const float pt[2]) ATTR_WARN_UNUSED_RESULT; /********************************** Angles ***********************************/ diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index facee8b0a89..f70c12bd26e 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -252,6 +252,46 @@ MINLINE int max_iiii(int a, int b, int c, int d) return max_ii(max_iii(a, b, c), d); } +/** + * Almost-equal for IEEE floats, using absolute difference method. + * + * \param max_diff the maximum absolute difference. + */ +MINLINE int compare_ff(float a, float b, const float max_diff) +{ + return fabsf(a - b) <= max_diff; +} + +/** + * Almost-equal for IEEE floats, using their integer representation (mixing ULP and absolute difference methods). + * + * \param max_diff is the maximum absolute difference (allows to take care of the near-zero area, + * where relative difference methods cannot really work). + * \param max_ulps is the 'maximum number of floats + 1' allowed between \a a and \a b to consider them equal. + * + * \see https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ + */ +MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps) +{ + union {float f; int i;} ua, ub; + +#if 0 /* No BLI_assert in INLINE :/ */ + BLI_assert(sizeof(float) == sizeof(int)); + BLI_assert(max_ulps < (1 << 22)); +#endif + + if (fabsf(a - b) <= max_diff) { + return 1; + } + + ua.f = a; + ub.f = b; + + /* Important to compare sign from integers, since (-0.0f < 0) is false + * (though this shall not be an issue in common cases)... */ + return ((ua.i < 0) != (ub.i < 0)) ? 0 : (abs(ua.i - ub.i) <= max_ulps) ? 1 : 0; +} + MINLINE float signf(float f) { return (f < 0.f) ? -1.f : 1.f; diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index fb33fed33e4..c21b09748c9 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -966,23 +966,47 @@ MINLINE bool equals_v4v4(const float v1[4], const float v2[4]) MINLINE bool compare_v2v2(const float v1[2], const float v2[2], const float limit) { - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - return true; - - return false; + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit)); } MINLINE bool compare_v3v3(const float v1[3], const float v2[3], const float limit) { - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - if (fabsf(v1[2] - v2[2]) <= limit) - return true; - - return false; + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit) && + compare_ff(v1[2], v2[2], limit)); } +MINLINE bool compare_v4v4(const float v1[4], const float v2[4], const float limit) +{ + return (compare_ff(v1[0], v2[0], limit) && + compare_ff(v1[1], v2[1], limit) && + compare_ff(v1[2], v2[2], limit) && + compare_ff(v1[3], v2[3], limit)); +} + +MINLINE bool compare_v2v2_relative(const float v1[2], const float v2[2], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps)); +} + +MINLINE bool compare_v3v3_relative(const float v1[3], const float v2[3], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps) && + compare_ff_relative(v1[2], v2[2], limit, max_ulps)); +} + +MINLINE bool compare_v4v4_relative(const float v1[4], const float v2[4], const float limit, const int max_ulps) +{ + return (compare_ff_relative(v1[0], v2[0], limit, max_ulps) && + compare_ff_relative(v1[1], v2[1], limit, max_ulps) && + compare_ff_relative(v1[2], v2[2], limit, max_ulps) && + compare_ff_relative(v1[3], v2[3], limit, max_ulps)); +} + + MINLINE bool compare_len_v3v3(const float v1[3], const float v2[3], const float limit) { float x, y, z; @@ -1005,17 +1029,6 @@ MINLINE bool compare_len_squared_v3v3(const float v1[3], const float v2[3], cons return ((x * x + y * y + z * z) <= limit_sq); } -MINLINE bool compare_v4v4(const float v1[4], const float v2[4], const float limit) -{ - if (fabsf(v1[0] - v2[0]) <= limit) - if (fabsf(v1[1] - v2[1]) <= limit) - if (fabsf(v1[2] - v2[2]) <= limit) - if (fabsf(v1[3] - v2[3]) <= limit) - return true; - - return false; -} - /** *
  *        + l1
diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c
index 361fbc8a005..8e14319e258 100644
--- a/source/blender/python/mathutils/mathutils.c
+++ b/source/blender/python/mathutils/mathutils.c
@@ -348,6 +348,15 @@ int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error
 /* Utility functions */
 
 /* LomontRRDCompare4, Ever Faster Float Comparisons by Randy Dillon */
+/* XXX We may want to use 'safer' BLI's compare_ff_relative ultimately?
+ *     LomontRRDCompare4() is an optimized version of Dawson's AlmostEqual2sComplement() (see [1] and [2]).
+ *     Dawson himself now claims this is not a 'safe' thing to do (pushing ULP method beyond its limits),
+ *     an recommands using work from [3] instead, which is done in BLI func...
+ *
+ *     [1] http://www.randydillon.org/Papers/2007/everfast.htm
+ *     [2] http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
+ *     [3] https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ instead
+ */
 #define SIGNMASK(i) (-(int)(((unsigned int)(i)) >> 31))
 
 int EXPP_FloatsAreEqual(float af, float bf, int maxDiff)
diff --git a/tests/gtests/blenlib/BLI_math_base_test.cc b/tests/gtests/blenlib/BLI_math_base_test.cc
new file mode 100644
index 00000000000..0059eb54482
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_math_base_test.cc
@@ -0,0 +1,87 @@
+/* Apache License, Version 2.0 */
+
+#include "testing/testing.h"
+
+#include "BLI_math.h"
+
+/* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a pure-ULP one. */
+
+/* Put this here, since we cannot use BLI_assert() in inline math files it seems... */
+TEST(math_base, CompareFFRelativeValid)
+{
+	EXPECT_TRUE(sizeof(float) == sizeof(int));
+}
+
+TEST(math_base, CompareFFRelativeNormal)
+{
+	float f1 = 1.99999988f;  /* *(float *)&(*(int *)&f2 - 1) */
+	float f2 = 2.00000000f;
+	float f3 = 2.00000048f;  /* *(float *)&(*(int *)&f2 + 2) */
+	float f4 = 2.10000000f;  /* *(float *)&(*(int *)&f2 + 419430) */
+
+	const float max_diff = FLT_EPSILON * 0.1f;
+
+	EXPECT_TRUE(compare_ff_relative(f1, f2, max_diff, 1));
+	EXPECT_TRUE(compare_ff_relative(f2, f1, max_diff, 1));
+
+	EXPECT_TRUE(compare_ff_relative(f3, f2, max_diff, 2));
+	EXPECT_TRUE(compare_ff_relative(f2, f3, max_diff, 2));
+
+	EXPECT_FALSE(compare_ff_relative(f3, f2, max_diff, 1));
+	EXPECT_FALSE(compare_ff_relative(f2, f3, max_diff, 1));
+
+
+	EXPECT_FALSE(compare_ff_relative(f3, f2, -1.0f, 1));
+	EXPECT_FALSE(compare_ff_relative(f2, f3, -1.0f, 1));
+
+	EXPECT_TRUE(compare_ff_relative(f3, f2, -1.0f, 2));
+	EXPECT_TRUE(compare_ff_relative(f2, f3, -1.0f, 2));
+
+
+	EXPECT_FALSE(compare_ff_relative(f4, f2, max_diff, 64));
+	EXPECT_FALSE(compare_ff_relative(f2, f4, max_diff, 64));
+
+	EXPECT_TRUE(compare_ff_relative(f1, f3, max_diff, 64));
+	EXPECT_TRUE(compare_ff_relative(f3, f1, max_diff, 64));
+}
+
+TEST(math_base, CompareFFRelativeZero)
+{
+	float f0 = 0.0f;
+	float f1 = 4.2038954e-045f;  /* *(float *)&(*(int *)&f0 + 3) */
+
+	float fn0 = -0.0f;
+	float fn1 = -2.8025969e-045f;  /* *(float *)&(*(int *)&fn0 - 2) */
+
+	const float max_diff = FLT_EPSILON * 0.1f;
+
+	EXPECT_TRUE(compare_ff_relative(f0, f1, -1.0f, 3));
+	EXPECT_TRUE(compare_ff_relative(f1, f0, -1.0f, 3));
+
+	EXPECT_FALSE(compare_ff_relative(f0, f1, -1.0f, 1));
+	EXPECT_FALSE(compare_ff_relative(f1, f0, -1.0f, 1));
+
+	EXPECT_TRUE(compare_ff_relative(fn0, fn1, -1.0f, 8));
+	EXPECT_TRUE(compare_ff_relative(fn1, fn0, -1.0f, 8));
+
+
+	EXPECT_TRUE(compare_ff_relative(f0, f1, max_diff, 1));
+	EXPECT_TRUE(compare_ff_relative(f1, f0, max_diff, 1));
+
+	EXPECT_TRUE(compare_ff_relative(fn0, f0, max_diff, 1));
+	EXPECT_TRUE(compare_ff_relative(f0, fn0, max_diff, 1));
+
+	EXPECT_TRUE(compare_ff_relative(f0, fn1, max_diff, 1));
+	EXPECT_TRUE(compare_ff_relative(fn1, f0, max_diff, 1));
+
+
+	/* Note: in theory, this should return false, since 0.0f  and -0.0f have 0x80000000 diff,
+	 *       but overflow in substraction seems to break something here
+	 *       (abs(*(int *)&fn0 - *(int *)&f0) == 0x80000000 == fn0), probably because int32 cannot hold this abs value.
+     *       this is yet another illustration of why one shall never use (near-)zero floats in pure-ULP comparison. */
+//	EXPECT_FALSE(compare_ff_relative(fn0, f0, -1.0f, 1024));
+//	EXPECT_FALSE(compare_ff_relative(f0, fn0, -1.0f, 1024));
+
+	EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024));
+	EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024));
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index b4cb2b9d0ea..acd9f944e4c 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -38,6 +38,7 @@ set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LIN
 BLENDER_TEST(BLI_stack "bf_blenlib")
 BLENDER_TEST(BLI_math_color "bf_blenlib")
 BLENDER_TEST(BLI_math_geom "bf_blenlib")
+BLENDER_TEST(BLI_math_base "bf_blenlib")
 BLENDER_TEST(BLI_string "bf_blenlib")
 BLENDER_TEST(BLI_path_util "bf_blenlib;extern_wcwidth;${ZLIB_LIBRARIES}")
 BLENDER_TEST(BLI_polyfill2d "bf_blenlib")