-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for gamma(a, b) and distance_under_gamma(a, b, g)
These are building stones for uniform floating point distribution.
- Loading branch information
Showing
8 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/catch2/internal/catch_random_floating_point_helpers.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
// 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_GAMMA_HPP_INCLUDED | ||
#define CATCH_GAMMA_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 step 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 IntegerPicker; | ||
template <> | ||
struct IntegerPicker<float> { | ||
using type = std::uint32_t; | ||
}; | ||
template <> | ||
struct IntegerPicker<double> { | ||
using type = std::uint64_t; | ||
}; | ||
|
||
template <typename T> | ||
using PickedType = typename IntegerPicker<T>::type; | ||
|
||
#if defined( __GNUC__ ) || defined( __clang__ ) | ||
# pragma GCC diagnostic push | ||
# pragma GCC diagnostic ignored "-Wfloat-equal" | ||
#endif | ||
/** | ||
* TODO: explain | ||
*/ | ||
template <typename FloatType> | ||
PickedType<FloatType> | ||
distance_under_gamma( FloatType a, FloatType b, FloatType g ) { | ||
assert( a < b ); | ||
|
||
const auto ag = a / g; | ||
const auto bg = b / g; | ||
|
||
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<PickedType<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_GAMMA_HPP_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters