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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
#include "DataFormats/Math/interface/approx_exp.h"
#include "DataFormats/Math/interface/approx_log.h"
#include "DataFormats/Math/interface/approx_erf.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
void printEXP(float x) {
printf("x= %a val= %a %a %a\n", x, std::exp(double(x)), unsafe_expf<6>(x), unsafe_expf<4>(x));
printf("x= %f val= %f %f %f\n", x, std::exp(double(x)), unsafe_expf<6>(x), unsafe_expf<4>(x));
printf("x= %f val= %f %f %f\n\n", x, std::exp(x), approx_expf<6>(x), approx_expf<4>(x));
}
void printLOG(float x) {
printf("x= %a val= %a %a %a\n", x, std::log(double(x)), unsafe_logf<8>(x), unsafe_logf<4>(x));
printf("x= %f val= %f %f %f\n", x, std::log(double(x)), unsafe_logf<8>(x), unsafe_logf<4>(x));
printf("x= %f val= %f %f %f\n\n", x, std::log(x), approx_logf<6>(x), approx_logf<4>(x));
}
template <typename PRINT>
void printLim(PRINT printit) {
constexpr float zero_threshold_ftz = -float(0x5.75628p4);
constexpr float inf_threshold = float(0x5.8b90cp4);
std::cout << "\nexp valid for " << zero_threshold_ftz << " < x < " << inf_threshold << std::endl;
printit(zero_threshold_ftz);
printit(zero_threshold_ftz - 1);
printit(zero_threshold_ftz + 1);
printit(4 * zero_threshold_ftz);
printit(0);
printit(1.);
printit(-1.);
printit(inf_threshold);
printit(inf_threshold + 1);
printit(inf_threshold - 1);
printit(4 * inf_threshold);
printit(std::sqrt(-1));
printit(std::numeric_limits<float>::infinity());
printit(-std::numeric_limits<float>::infinity());
std::cout << "\n" << std::endl;
}
namespace justcomp {
constexpr int NN = 1024 * 1024;
float a[NN], b[NN];
template <int DEGREE>
void bar() {
for (int i = 0; i != NN; ++i)
b[i] = approx_expf<DEGREE>(a[i]);
}
template <int DEGREE>
void foo() {
for (int i = 0; i != NN; ++i)
b[i] = unsafe_expf<DEGREE>(a[i]);
}
template <int DEGREE>
void lar() {
for (int i = 0; i != NN; ++i)
b[i] = approx_logf<DEGREE>(a[i]);
}
template <int DEGREE>
void loo() {
for (int i = 0; i != NN; ++i)
b[i] = unsafe_logf<DEGREE>(a[i]);
}
} // namespace justcomp
template <typename STD, typename APPROX>
void accTest(STD stdf, APPROX approx, int degree) {
using namespace approx_math;
std::cout << std::endl << "launching exhaustive test for degree " << degree << std::endl;
binary32 x, r, ref;
int maxdiff = 0;
int n127 = 0;
int n16393 = 0;
x.ui32 = 0; // should be 0 but
while (x.ui32 < 0xffffffff) {
x.ui32++;
// remove nans..
if ((x.ui32 & 0x7f80000) && x.ui32 & 0x7FFFFF)
continue;
r.f = approx(x.f);
ref.f = stdf(double(x.f)); // double-prec one (no hope with -fno-math-errno)
int d = abs(r.i32 - ref.i32);
if (d > maxdiff) {
// std::cout << "new maxdiff for x=" << x.f << " : " << d << std::endl;
maxdiff = d;
}
if (d > 127)
++n127;
if (d > 16393)
++n16393;
}
std::cout << "maxdiff / diff >127 / diff >16393 " << maxdiff << " / " << n127 << " / " << n16393 << std::endl;
}
template <typename STD, typename APPROX>
void accuTest(STD stdf,
APPROX approx,
const char* name,
float mm = std::numeric_limits<float>::min(),
float mx = std::numeric_limits<float>::max()) {
using namespace approx_math;
std::cout << std::endl << "launching exhaustive test for " << name << std::endl;
binary32 x, pend, r, ref;
int maxdiff = 0;
int n127 = 0;
int n16393 = 0;
float ad = 0., rd = 0;
x.f = mm;
x.ui32++;
pend.f = mx;
pend.ui32--;
std::cout << "limits " << x.f << " " << pend.f << " " << pend.ui32 - x.ui32 << std::endl;
while (x.ui32 < pend.ui32) {
x.ui32++;
r.f = approx(x.f);
ref.f = stdf(x.f); // double-prec one (no hope with -fno-math-errno)
ad = std::max(ad, std::abs(r.f - ref.f));
rd = std::max(rd, std::abs((r.f / ref.f) - 1.f));
int d = abs(r.i32 - ref.i32);
if (d > maxdiff) {
// std::cout << "new maxdiff for x=" << x.f << " : " << d << std::endl;
maxdiff = d;
}
if (d > 127)
++n127;
if (d > 16393)
++n16393;
}
std::cout << "absdiff / reldeff/ maxdiff / diff >127 / diff >16393 : " << ad << " / " << rd << " / " << maxdiff
<< " / " << n127 << " / " << n16393 << std::endl;
}
// performance test
#include "rdtscp.h"
template <int DEGREE, int WHAT>
struct Measure {
inline void operator()(unsigned long long& t) const;
};
template <int DEGREE>
struct Measure<DEGREE, 0> {
inline void operator()(unsigned long long& t) const {
t -= rdtsc();
justcomp::foo<DEGREE>();
t += rdtsc();
}
};
template <int DEGREE>
struct Measure<DEGREE, 1> {
inline void operator()(unsigned long long& t) const {
t -= rdtsc();
justcomp::bar<DEGREE>();
t += rdtsc();
}
};
template <int DEGREE>
struct Measure<DEGREE, 2> {
inline void operator()(unsigned long long& t) const {
t -= rdtsc();
justcomp::loo<DEGREE>();
t += rdtsc();
}
};
template <int DEGREE>
struct Measure<DEGREE, 3> {
inline void operator()(unsigned long long& t) const {
t -= rdtsc();
justcomp::lar<DEGREE>();
t += rdtsc();
}
};
template <int DEGREE, int WHAT = 1>
void perf() {
if (!has_rdtscp())
return;
Measure<DEGREE, WHAT> measure;
using namespace approx_math;
unsigned long long t = 0;
binary32 x;
float sum = 0;
long long ntot = 0;
x.f = 1.0; // should be 0 but
while (x.f < 32) { // this is 5*2^23 tests
++ntot;
int i = 0;
while (i < justcomp::NN) {
x.ui32++;
justcomp::a[i++] = x.f;
justcomp::a[i++] = (WHAT < 2) ? -x.f : 1.f / x.f;
}
measure(t);
// binary32 r;
// r.f=approx_expf<6>(x.f);// time 0m1.180s
// r.f=expf(x.f); // time 0m4.372s
// r.f=exp(x.f); // time 0m1.789s
for (int i = 0; i != justcomp::NN; ++i)
sum += justcomp::b[i];
}
const char* what[] = {"exp unsafe", "exp apporx", "log unsafe", "log approx"};
std::cout << "time for " << what[WHAT] << " degree " << DEGREE << " is " << double(t) / double(justcomp::NN * ntot)
<< std::endl;
std::cout << "sum= " << sum << " to prevent compiler optim." << std::endl;
;
}
int main() {
printLim(printEXP);
printLim(printLOG);
perf<2, 0>();
perf<3, 0>();
perf<3>();
perf<4>();
perf<6>();
perf<2, 2>();
perf<2, 3>();
perf<4, 2>();
perf<4, 3>();
perf<8, 2>();
perf<8, 3>();
accTest(::exp, approx_expf<2>, 2);
accTest(::exp, approx_expf<3>, 3);
accTest(::exp, approx_expf<4>, 4);
accTest(::exp, approx_expf<6>, 6);
accTest(::log, approx_logf<2>, 2);
accTest(::log, approx_logf<4>, 4);
accTest(::log, approx_logf<8>, 8);
accuTest(::erf, approx_erf, "erf", .01, 8);
return 0;
}
|