forked from ckormanyos/real-time-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_benchmark_detail.h
More file actions
64 lines (49 loc) · 1.89 KB
/
app_benchmark_detail.h
File metadata and controls
64 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2025.
// Distributed under 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 APP_BENCHMARK_DETAIL_2018_10_02_H
#define APP_BENCHMARK_DETAIL_2018_10_02_H
#include <cmath>
#include <cstdint>
#include <limits>
namespace app { namespace benchmark { namespace detail {
template<typename NumericType>
constexpr auto default_tol() noexcept -> NumericType
{
return NumericType { std::numeric_limits<NumericType>::epsilon() * static_cast<NumericType>(UINT8_C(100)) }; // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
}
template<typename NumericType>
constexpr auto is_close_fraction(const NumericType a, // NOLINT(bugprone-easily-swappable-parameters)
const NumericType b, // NOLINT(bugprone-easily-swappable-parameters)
const NumericType tol = default_tol<NumericType>()) noexcept -> bool
{
using std::fabs;
using std::fpclassify;
using numeric_type = NumericType;
const int fpc_a { fpclassify(a) };
const int fpc_b { fpclassify(b) };
bool result_is_ok { };
if(fpc_b == FP_ZERO)
{
const numeric_type closeness { (fpc_a == FP_ZERO) ? numeric_type { 0 } : fabs(a - b) };
result_is_ok = (closeness < tol);
}
else if((fpc_a != FP_NORMAL) || (fpc_b != FP_NORMAL))
{
result_is_ok = false;
}
else
{
const numeric_type ratio { a / b };
const numeric_type closeness { fabs(static_cast<numeric_type>(1 - ratio )) };
result_is_ok = (closeness < tol);
}
return result_is_ok;
}
} // namespace detail
} // namespace benchmark
} // namespace app
#endif // APP_BENCHMARK_DETAIL_2018_10_02_H