forked from ckormanyos/real-time-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_benchmark_cnl_scaled_integer.cpp
118 lines (90 loc) · 3.65 KB
/
app_benchmark_cnl_scaled_integer.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2021.
// 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)
//
#include <algorithm>
#include <app/benchmark/app_benchmark.h>
#include <app/benchmark/app_benchmark_detail.h>
#if(APP_BENCHMARK_TYPE == APP_BENCHMARK_TYPE_CNL_SCALED_INTEGER)
#include <cnl/scaled_integer.h>
namespace
{
template<typename NumericType>
NumericType quadratic(const NumericType a,
const NumericType b,
const NumericType c,
const NumericType x)
{
return NumericType((NumericType(a * x) + b) * x) + c;
}
template<typename NumericType>
bool is_close_fraction(const NumericType a,
const NumericType b,
const NumericType tol = NumericType(std::numeric_limits<NumericType>::epsilon() * 100))
{
using std::abs;
const NumericType ratio = abs(NumericType((NumericType(1) * a) / b));
const NumericType closeness = abs(NumericType(1 - ratio));
return (closeness < tol);
}
using fixed_point_bin_type = cnl::scaled_integer<std::int32_t, cnl::power<-10>>;
using fixed_point_dec_type = cnl::scaled_integer<std::int32_t, cnl::power<-3, 10>>;
}
extern fixed_point_bin_type a_bin;
extern fixed_point_bin_type b_bin;
extern fixed_point_bin_type c_bin;
extern fixed_point_bin_type x_bin;
extern fixed_point_bin_type r_bin;
extern fixed_point_dec_type a_dec;
extern fixed_point_dec_type b_dec;
extern fixed_point_dec_type c_dec;
extern fixed_point_dec_type x_dec;
extern fixed_point_dec_type r_dec;
bool app::benchmark::run_cnl_scaled_integer()
{
static std::uint_fast8_t app_benchmark_cnl_scaled_integer_type_toggler;
bool result_is_ok;
if((app_benchmark_cnl_scaled_integer_type_toggler % 2U) == 0U)
{
const fixed_point_bin_type f = quadratic(a_bin, b_bin, c_bin, x_bin);
result_is_ok = is_close_fraction(f, r_bin, fixed_point_bin_type(fixed_point_bin_type(1) / 100));
}
else
{
const fixed_point_dec_type f = quadratic(a_dec, b_dec, c_dec, x_dec);
result_is_ok = is_close_fraction(f, r_dec, fixed_point_dec_type(fixed_point_dec_type(1) / 100));
}
++app_benchmark_cnl_scaled_integer_type_toggler;
return result_is_ok;
}
// Consider the result of the quadratic:
// = ((12/10) (1/2)^2) + (34/10) (1/2) + (56/10)
// = (12 + (34*2) + (56*4)) / 40
// = 304 / 40
// = 38/5
// = 7.6
fixed_point_bin_type a_bin(fixed_point_bin_type(12) / 10);
fixed_point_bin_type b_bin(fixed_point_bin_type(34) / 10);
fixed_point_bin_type c_bin(fixed_point_bin_type(56) / 10);
fixed_point_bin_type x_bin(fixed_point_bin_type( 1) / 2);
fixed_point_bin_type r_bin(fixed_point_bin_type(76) / 10);
fixed_point_dec_type a_dec(fixed_point_dec_type(12) / 10);
fixed_point_dec_type b_dec(fixed_point_dec_type(34) / 10);
fixed_point_dec_type c_dec(fixed_point_dec_type(56) / 10);
fixed_point_dec_type x_dec(fixed_point_dec_type( 1) / 2);
fixed_point_dec_type r_dec(fixed_point_dec_type(76) / 10);
#if defined(APP_BENCHMARK_STANDALONE_MAIN)
int main()
{
// g++ -Wall -O3 -march=native -I./ref_app/src/mcal/host -I./ref_app/src -DAPP_BENCHMARK_TYPE=APP_BENCHMARK_TYPE_CNL_SCALED_INTEGER -DAPP_BENCHMARK_STANDALONE_MAIN ./ref_app/src/app/benchmark/app_benchmark_cnl_scaled_integer.cpp -o ./ref_app/bin/app_benchmark_cnl_scaled_integer.exe
bool result_is_ok = true;
for(unsigned i = 0U; i < 64U; ++i)
{
result_is_ok &= app::benchmark::run_cnl_scaled_integer();
}
return result_is_ok ? 0 : -1;
}
#endif
#endif // APP_BENCHMARK_TYPE_CNL_SCALED_INTEGER