-
Notifications
You must be signed in to change notification settings - Fork 235
Faster floating point comparisons #814
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
Open
mborland
wants to merge
10
commits into
boostorg:develop
Choose a base branch
from
mborland:fast_next
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb37db7
Add specialized float distance for type float
mborland 5fddf08
Reinterpret cast breaks strict aliasing rules use memcpy
mborland 4660844
Add specialized float distance for type double
mborland cc0d6e9
Improve error handling
mborland 00d2875
Offer 128 bit support
mborland 0a333eb
Add standalone support
mborland 0a6b08c
Fix GCC 5 error for static_assert without message
mborland a481675
Fix type used in standalone mode
mborland a1fe973
Include performance file and results
mborland 73bb192
Update include/boost/math/special_functions/fast_float_distance.hpp
mborland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
include/boost/math/special_functions/fast_float_distance.hpp
This file contains hidden or 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,132 @@ | ||
// (C) Copyright Matt Borland 2022. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. (See accompanying file | ||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef BOOST_MATH_SF_FAST_FLOAT_DISTANCE | ||
#define BOOST_MATH_SF_FAST_FLOAT_DISTANCE | ||
|
||
#include <boost/math/special_functions/next.hpp> | ||
#include <boost/math/tools/throw_exception.hpp> | ||
#include <stdexcept> | ||
#include <limits> | ||
|
||
#if defined(BOOST_MATH_USE_FLOAT128) && !defined(BOOST_MATH_STANDALONE) | ||
#include <boost/multiprecision/float128.hpp> | ||
#include <boost/multiprecision/detail/standalone_config.hpp> | ||
#define BOOST_MATH_USE_FAST_FLOAT128 | ||
#elif defined(BOOST_MATH_USE_FLOAT128) && defined(BOOST_MATH_STANDALONE) | ||
# if __has_include(<quadmath.h>) | ||
# include <quadmath.h> | ||
# define BOOST_MATH_USE_FAST_STANDALONE_FLOAT128 | ||
# endif | ||
#endif | ||
|
||
namespace boost { namespace math { | ||
|
||
// https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2/ | ||
// https://blog.regehr.org/archives/959 | ||
inline std::int32_t fast_float_distance(float a, float b) | ||
{ | ||
return boost::math::float_distance(a, b); | ||
} | ||
|
||
inline std::int64_t fast_float_distance(double a, double b) | ||
{ | ||
return boost::math::float_distance(a, b); | ||
} | ||
|
||
#ifdef BOOST_MATH_USE_FAST_FLOAT128 | ||
boost::multiprecision::int128_type fast_float_distance(boost::multiprecision::float128_type a, boost::multiprecision::float128_type b) | ||
{ | ||
using std::abs; | ||
using std::isfinite; | ||
|
||
constexpr boost::multiprecision::float128_type tol = 2 * BOOST_MP_QUAD_MIN; | ||
|
||
// 0, very small, and large magnitude distances all need special handling | ||
if (abs(a) == 0 || abs(b) == 0) | ||
{ | ||
return 0; | ||
} | ||
else if (abs(a) < tol || abs(b) < tol) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("special handling is required for tiny distances. Please use boost::math::float_distance for a slower but safe solution")); | ||
} | ||
|
||
if (!(isfinite)(a)) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("Both arguments to fast_float_distance must be finite")); | ||
} | ||
else if (!(isfinite)(b)) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("Both arguments to fast_float_distnace must be finite")); | ||
} | ||
|
||
static_assert(sizeof(boost::multiprecision::int128_type) == sizeof(boost::multiprecision::float128_type), "float128 is the wrong size"); | ||
|
||
boost::multiprecision::int128_type ai; | ||
boost::multiprecision::int128_type bi; | ||
std::memcpy(&ai, &a, sizeof(boost::multiprecision::float128_type)); | ||
std::memcpy(&bi, &b, sizeof(boost::multiprecision::float128_type)); | ||
|
||
boost::multiprecision::int128_type result = bi - ai; | ||
|
||
if (ai < 0 || bi < 0) | ||
{ | ||
result = -result; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
#elif defined(BOOST_MATH_USE_FAST_STANDALONE_FLOAT128) | ||
__int128 fast_float_distance(__float128 a, __float128 b) | ||
{ | ||
constexpr __float128 tol = 2 * static_cast<__float128>(1) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * | ||
static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * | ||
static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * | ||
static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * | ||
static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) * | ||
static_cast<__float128>(DBL_MIN) * static_cast<__float128>(DBL_MIN) / 1073741824; | ||
|
||
// 0, very small, and large magnitude distances all need special handling | ||
if (::fabsq(a) == 0 || ::fabsq(b) == 0) | ||
{ | ||
return 0; | ||
} | ||
else if (::fabsq(a) < tol || ::fabsq(b) < tol) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("special handling is required for tiny distances. Please use boost::math::float_distance for a slower but safe solution")); | ||
} | ||
|
||
if (!(::isinfq)(a) && !(::isnanq)(a)) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("Both arguments to fast_float_distnace must be finite")); | ||
} | ||
else if (!(::isinfq)(b) && !(::isnanq)(b)) | ||
{ | ||
BOOST_MATH_THROW_EXCEPTION(std::domain_error("Both arguments to fast_float_distnace must be finite")); | ||
} | ||
|
||
static_assert(sizeof(__int128) == sizeof(__float128)); | ||
|
||
__int128 ai; | ||
__int128 bi; | ||
std::memcpy(&ai, &a, sizeof(__float128)); | ||
std::memcpy(&bi, &b, sizeof(__float128)); | ||
|
||
__int128 result = bi - ai; | ||
|
||
if (ai < 0 || bi < 0) | ||
{ | ||
result = -result; | ||
} | ||
|
||
return result; | ||
} | ||
#endif | ||
|
||
}} // Namespaces | ||
|
||
#endif |
This file contains hidden or 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 hidden or 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,126 @@ | ||
// (C) Copyright Matt Borland 2022. | ||
// Use, modification and distribution are subject to the | ||
// Boost Software License, Version 1.0. (See accompanying file | ||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include <boost/math/special_functions/next.hpp> | ||
#include <benchmark/benchmark.h> | ||
|
||
template <typename T> | ||
void float_distance(benchmark::State& state) | ||
{ | ||
const auto difference = static_cast<int>(state.range(0)); | ||
T left = 2; | ||
T right = boost::math::float_advance(left, difference); | ||
|
||
for (auto _ : state) | ||
{ | ||
benchmark::DoNotOptimize(boost::math::float_distance(left, right)); | ||
} | ||
state.SetComplexityN(state.range(0)); | ||
} | ||
|
||
BENCHMARK_TEMPLATE(float_distance, float)->RangeMultiplier(2)->Range(1 << 1, 1 << 14)->Complexity()->UseRealTime(); | ||
BENCHMARK_TEMPLATE(float_distance, double)->RangeMultiplier(2)->Range(1 << 1, 1 << 14)->Complexity()->UseRealTime(); | ||
|
||
BENCHMARK_MAIN(); | ||
|
||
/* | ||
Run on Apple M1 Pro Arch using Apple Clang 14.0.0 (15OCT22) | ||
|
||
Original performance (Boost 1.80.0): | ||
|
||
Unable to determine clock rate from sysctl: hw.cpufrequency: No such file or directory | ||
This does not affect benchmark measurements, only the metadata output. | ||
2022-10-15T15:24:07-07:00 | ||
Running ./new_next_performance | ||
Run on (10 X 24.0916 MHz CPU s) | ||
CPU Caches: | ||
L1 Data 64 KiB | ||
L1 Instruction 128 KiB | ||
L2 Unified 4096 KiB (x10) | ||
Load Average: 1.86, 2.53, 5.83 | ||
--------------------------------------------------------------------------------- | ||
Benchmark Time CPU Iterations | ||
--------------------------------------------------------------------------------- | ||
float_distance<float>/2/real_time 61.4 ns 61.4 ns 9074469 | ||
float_distance<float>/4/real_time 61.7 ns 61.7 ns 11384150 | ||
float_distance<float>/8/real_time 61.4 ns 61.4 ns 10814604 | ||
float_distance<float>/16/real_time 61.7 ns 61.7 ns 11348376 | ||
float_distance<float>/32/real_time 61.4 ns 61.4 ns 11387167 | ||
float_distance<float>/64/real_time 61.6 ns 61.6 ns 11131932 | ||
float_distance<float>/128/real_time 61.4 ns 61.4 ns 11382029 | ||
float_distance<float>/256/real_time 61.4 ns 61.4 ns 11307649 | ||
float_distance<float>/512/real_time 61.4 ns 61.4 ns 11376048 | ||
float_distance<float>/1024/real_time 61.4 ns 61.4 ns 11355748 | ||
float_distance<float>/2048/real_time 61.8 ns 61.8 ns 11373776 | ||
float_distance<float>/4096/real_time 61.4 ns 61.4 ns 11382368 | ||
float_distance<float>/8192/real_time 61.4 ns 61.4 ns 11353453 | ||
float_distance<float>/16384/real_time 61.4 ns 61.4 ns 11378298 | ||
float_distance<float>/real_time_BigO 61.48 (1) 61.47 (1) | ||
float_distance<float>/real_time_RMS 0 % 0 % | ||
float_distance<double>/2/real_time 55.6 ns 55.6 ns 12580218 | ||
float_distance<double>/4/real_time 55.6 ns 55.6 ns 12577835 | ||
float_distance<double>/8/real_time 55.6 ns 55.6 ns 12564909 | ||
float_distance<double>/16/real_time 56.2 ns 56.2 ns 12554909 | ||
float_distance<double>/32/real_time 56.0 ns 56.0 ns 12544381 | ||
float_distance<double>/64/real_time 55.6 ns 55.6 ns 12566488 | ||
float_distance<double>/128/real_time 55.6 ns 55.6 ns 12499581 | ||
float_distance<double>/256/real_time 55.6 ns 55.6 ns 12565661 | ||
float_distance<double>/512/real_time 56.1 ns 56.1 ns 12550023 | ||
float_distance<double>/1024/real_time 55.8 ns 55.8 ns 12568603 | ||
float_distance<double>/2048/real_time 55.6 ns 55.6 ns 12546049 | ||
float_distance<double>/4096/real_time 55.6 ns 55.6 ns 12528525 | ||
float_distance<double>/8192/real_time 55.9 ns 55.9 ns 12563030 | ||
float_distance<double>/16384/real_time 56.0 ns 56.0 ns 12447644 | ||
float_distance<double>/real_time_BigO 55.78 (1) 55.78 (1) | ||
float_distance<double>/real_time_RMS 0 % 0 % | ||
|
||
New performance: | ||
|
||
Unable to determine clock rate from sysctl: hw.cpufrequency: No such file or directory | ||
This does not affect benchmark measurements, only the metadata output. | ||
2022-10-15T15:31:37-07:00 | ||
Running ./new_next_performance | ||
Run on (10 X 24.122 MHz CPU s) | ||
CPU Caches: | ||
L1 Data 64 KiB | ||
L1 Instruction 128 KiB | ||
L2 Unified 4096 KiB (x10) | ||
Load Average: 2.12, 2.17, 4.26 | ||
--------------------------------------------------------------------------------- | ||
Benchmark Time CPU Iterations | ||
--------------------------------------------------------------------------------- | ||
float_distance<float>/2/real_time 15.8 ns 15.8 ns 42162717 | ||
float_distance<float>/4/real_time 15.9 ns 15.9 ns 44213877 | ||
float_distance<float>/8/real_time 15.8 ns 15.8 ns 43972542 | ||
float_distance<float>/16/real_time 15.8 ns 15.8 ns 44209456 | ||
float_distance<float>/32/real_time 15.8 ns 15.8 ns 44200244 | ||
float_distance<float>/64/real_time 15.8 ns 15.8 ns 44239293 | ||
float_distance<float>/128/real_time 15.8 ns 15.8 ns 44171202 | ||
float_distance<float>/256/real_time 15.8 ns 15.8 ns 44241507 | ||
float_distance<float>/512/real_time 15.9 ns 15.8 ns 44230034 | ||
float_distance<float>/1024/real_time 15.8 ns 15.8 ns 44241554 | ||
float_distance<float>/2048/real_time 15.8 ns 15.8 ns 44220802 | ||
float_distance<float>/4096/real_time 15.8 ns 15.8 ns 44220441 | ||
float_distance<float>/8192/real_time 15.9 ns 15.9 ns 44213994 | ||
float_distance<float>/16384/real_time 15.8 ns 15.8 ns 44215413 | ||
float_distance<float>/real_time_BigO 15.83 (1) 15.83 (1) | ||
float_distance<float>/real_time_RMS 0 % 0 % | ||
float_distance<double>/2/real_time 15.5 ns 15.5 ns 45098165 | ||
float_distance<double>/4/real_time 15.6 ns 15.6 ns 45065465 | ||
float_distance<double>/8/real_time 15.5 ns 15.5 ns 45058733 | ||
float_distance<double>/16/real_time 15.8 ns 15.7 ns 45078404 | ||
float_distance<double>/32/real_time 15.5 ns 15.5 ns 44832734 | ||
float_distance<double>/64/real_time 15.5 ns 15.5 ns 45077303 | ||
float_distance<double>/128/real_time 15.5 ns 15.5 ns 45067255 | ||
float_distance<double>/256/real_time 15.5 ns 15.5 ns 45073844 | ||
float_distance<double>/512/real_time 15.6 ns 15.6 ns 45109342 | ||
float_distance<double>/1024/real_time 15.5 ns 15.5 ns 44845180 | ||
float_distance<double>/2048/real_time 15.5 ns 15.5 ns 45051846 | ||
float_distance<double>/4096/real_time 15.5 ns 15.5 ns 45064317 | ||
float_distance<double>/8192/real_time 15.5 ns 15.5 ns 45115653 | ||
float_distance<double>/16384/real_time 15.5 ns 15.5 ns 45067642 | ||
float_distance<double>/real_time_BigO 15.54 (1) 15.54 (1) | ||
float_distance<double>/real_time_RMS 0 % 0 % | ||
*/ |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.