Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-11-19 21:00:53 +0100
committerMartin Hořeňovský2023-12-10 18:44:10 +0100
commitd139b4ff7cd8823923e8616e8fef954e09e46322 (patch)
treecd0e03dde2d2b3e72911d27182355b63c6540d70 /src/catch2/internal
parentbfd9f0f5a67c0127b25ac82eb828f6ab71aaae1e (diff)
downloadcatch3-d139b4ff7cd8823923e8616e8fef954e09e46322.tar.gz
Add implementation of helpers for uniform float distribution
Specifically we add * `gamma(a, b)`, which returns the magnitude of largest 1-ULP step in range [a, b]. * `count_equidistant_float(a, b, distance)`, which returns the number of equi-distant floats in range [a, b].
Diffstat (limited to 'src/catch2/internal')
-rw-r--r--src/catch2/internal/catch_floating_point_helpers.cpp11
-rw-r--r--src/catch2/internal/catch_floating_point_helpers.hpp5
-rw-r--r--src/catch2/internal/catch_random_floating_point_helpers.hpp94
3 files changed, 110 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_floating_point_helpers.cpp b/src/catch2/internal/catch_floating_point_helpers.cpp
index e30ee434..9631ed6d 100644
--- a/src/catch2/internal/catch_floating_point_helpers.cpp
+++ b/src/catch2/internal/catch_floating_point_helpers.cpp
@@ -27,6 +27,17 @@ namespace Catch {
return i;
}
+#if defined( __GNUC__ ) || defined( __clang__ )
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
+ bool directCompare( float lhs, float rhs ) { return lhs == rhs; }
+ bool directCompare( double lhs, double rhs ) { return lhs == rhs; }
+#if defined( __GNUC__ ) || defined( __clang__ )
+# pragma GCC diagnostic pop
+#endif
+
+
} // end namespace Detail
} // end namespace Catch
diff --git a/src/catch2/internal/catch_floating_point_helpers.hpp b/src/catch2/internal/catch_floating_point_helpers.hpp
index ca883c61..b2143726 100644
--- a/src/catch2/internal/catch_floating_point_helpers.hpp
+++ b/src/catch2/internal/catch_floating_point_helpers.hpp
@@ -22,6 +22,11 @@ namespace Catch {
uint32_t convertToBits(float f);
uint64_t convertToBits(double d);
+ // Used when we know we want == comparison of two doubles
+ // to centralize warning suppression
+ bool directCompare( float lhs, float rhs );
+ bool directCompare( double lhs, double rhs );
+
} // end namespace Detail
diff --git a/src/catch2/internal/catch_random_floating_point_helpers.hpp b/src/catch2/internal/catch_random_floating_point_helpers.hpp
new file mode 100644
index 00000000..d75b4218
--- /dev/null
+++ b/src/catch2/internal/catch_random_floating_point_helpers.hpp
@@ -0,0 +1,94 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+
+#ifndef CATCH_RANDOM_FLOATING_POINT_HELPERS_HPP_INCLUDED
+#define CATCH_RANDOM_FLOATING_POINT_HELPERS_HPP_INCLUDED
+
+#include <catch2/internal/catch_polyfills.hpp>
+
+#include <cassert>
+#include <cmath>
+#include <cstdint>
+#include <limits>
+#include <type_traits>
+
+namespace Catch {
+
+ namespace Detail {
+ /**
+ * Returns the largest magnitude of 1-ULP distance inside the [a, b] range.
+ *
+ * Assumes `a < b`.
+ */
+ template <typename FloatType>
+ FloatType gamma(FloatType a, FloatType b) {
+ static_assert( std::is_floating_point<FloatType>::value,
+ "gamma returns the largest ULP magnitude within "
+ "floating point range [a, b]. This only makes sense "
+ "for floating point types" );
+ assert( a < b );
+
+ const auto gamma_up = Catch::nextafter( a, std::numeric_limits<FloatType>::infinity() ) - a;
+ const auto gamma_down = b - Catch::nextafter( b, -std::numeric_limits<FloatType>::infinity() );
+
+ return gamma_up < gamma_down ? gamma_down : gamma_up;
+ }
+
+ template <typename FloatingPoint>
+ struct DistanceTypePicker;
+ template <>
+ struct DistanceTypePicker<float> {
+ using type = std::uint32_t;
+ };
+ template <>
+ struct DistanceTypePicker<double> {
+ using type = std::uint64_t;
+ };
+
+ template <typename T>
+ using DistanceType = typename DistanceTypePicker<T>::type;
+
+#if defined( __GNUC__ ) || defined( __clang__ )
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
+ /**
+ * Computes the number of equi-distant floats in [a, b]
+ *
+ * Since not every range can be split into equidistant floats
+ * exactly, we actually compute ceil(b/distance - a/distance),
+ * because in those cases we want to overcount.
+ *
+ * Uses modified Dekker's FastTwoSum algorithm to handle rounding.
+ */
+ template <typename FloatType>
+ DistanceType<FloatType>
+ count_equidistant_floats( FloatType a, FloatType b, FloatType distance ) {
+ assert( a < b );
+ // We get distance as gamma for our uniform float distribution,
+ // so this will round perfectly.
+ const auto ag = a / distance;
+ const auto bg = b / distance;
+
+ const auto s = bg - ag;
+ const auto err = ( std::fabs( a ) <= std::fabs( b ) )
+ ? -ag - ( s - bg )
+ : bg - ( s + ag );
+ const auto ceil_s = static_cast<DistanceType<FloatType>>( std::ceil( s ) );
+
+ return ( ceil_s != s ) ? ceil_s : ceil_s + ( err > 0 );
+ }
+#if defined( __GNUC__ ) || defined( __clang__ )
+# pragma GCC diagnostic pop
+#endif
+
+ }
+
+} // end namespace Catch
+
+#endif // CATCH_RANDOM_FLOATING_POINT_HELPERS_HPP_INCLUDED