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
|
//===-- Common utility class for differential analysis --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/__support/FPUtil/FPBits.h"
#include "test/src/math/differential_testing/Timer.h"
#include <fstream>
namespace __llvm_libc {
namespace testing {
template <typename T> class BinaryOpSingleOutputDiff {
using FPBits = fputil::FPBits<T>;
using UIntType = typename FPBits::UIntType;
static constexpr UIntType MSBIT = UIntType(1) << (8 * sizeof(UIntType) - 1);
static constexpr UIntType UINTMAX = (MSBIT - 1) + MSBIT;
public:
typedef T Func(T, T);
static uint64_t run_diff_in_range(Func myFunc, Func otherFunc,
UIntType startingBit, UIntType endingBit,
UIntType N, std::ofstream &log) {
uint64_t result = 0;
if (endingBit < startingBit) {
return result;
}
UIntType step = (endingBit - startingBit) / N;
for (UIntType bitsX = startingBit, bitsY = endingBit;;
bitsX += step, bitsY -= step) {
T x = T(FPBits(bitsX));
T y = T(FPBits(bitsY));
FPBits myBits = FPBits(myFunc(x, y));
FPBits otherBits = FPBits(otherFunc(x, y));
if (myBits.uintval() != otherBits.uintval()) {
result++;
log << " Input: " << bitsX << ", " << bitsY << " (" << x << ", "
<< y << ")\n"
<< " My result: " << myBits.uintval() << " (" << myBits.get_val()
<< ")\n"
<< "Other result: " << otherBits.uintval() << " ("
<< otherBits.get_val() << ")\n"
<< '\n';
}
if (endingBit - bitsX < step) {
break;
}
}
return result;
}
static void run_perf_in_range(Func myFunc, Func otherFunc,
UIntType startingBit, UIntType endingBit,
UIntType N, std::ofstream &log) {
auto runner = [=](Func func) {
volatile T result;
if (endingBit < startingBit) {
return;
}
UIntType step = (endingBit - startingBit) / N;
for (UIntType bitsX = startingBit, bitsY = endingBit;;
bitsX += step, bitsY -= step) {
T x = T(FPBits(bitsX));
T y = T(FPBits(bitsY));
result = func(x, y);
if (endingBit - bitsX < step) {
break;
}
}
};
Timer timer;
timer.start();
runner(myFunc);
timer.stop();
double my_average = static_cast<double>(timer.nanoseconds()) / N;
log << "-- My function --\n";
log << " Total time : " << timer.nanoseconds() << " ns \n";
log << " Average runtime : " << my_average << " ns/op \n";
log << " Ops per second : "
<< static_cast<uint64_t>(1'000'000'000.0 / my_average) << " op/s \n";
timer.start();
runner(otherFunc);
timer.stop();
double other_average = static_cast<double>(timer.nanoseconds()) / N;
log << "-- Other function --\n";
log << " Total time : " << timer.nanoseconds() << " ns \n";
log << " Average runtime : " << other_average << " ns/op \n";
log << " Ops per second : "
<< static_cast<uint64_t>(1'000'000'000.0 / other_average) << " op/s \n";
log << "-- Average runtime ratio --\n";
log << " Mine / Other's : " << my_average / other_average << " \n";
}
static void run_perf(Func myFunc, Func otherFunc, const char *logFile) {
std::ofstream log(logFile);
log << " Performance tests with inputs in denormal range:\n";
run_perf_in_range(myFunc, otherFunc, /* startingBit= */ UIntType(0),
/* endingBit= */ FPBits::MAX_SUBNORMAL, 1'000'001, log);
log << "\n Performance tests with inputs in normal range:\n";
run_perf_in_range(myFunc, otherFunc, /* startingBit= */ FPBits::MIN_NORMAL,
/* endingBit= */ FPBits::MAX_NORMAL, 100'000'001, log);
log << "\n Performance tests with inputs in normal range with exponents "
"close to each other:\n";
run_perf_in_range(
myFunc, otherFunc, /* startingBit= */ FPBits(T(0x1.0p-10)).uintval(),
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(), 10'000'001, log);
}
static void run_diff(Func myFunc, Func otherFunc, const char *logFile) {
uint64_t diffCount = 0;
std::ofstream log(logFile);
log << " Diff tests with inputs in denormal range:\n";
diffCount += run_diff_in_range(
myFunc, otherFunc, /* startingBit= */ UIntType(0),
/* endingBit= */ FPBits::MAX_SUBNORMAL, 1'000'001, log);
log << "\n Diff tests with inputs in normal range:\n";
diffCount += run_diff_in_range(
myFunc, otherFunc, /* startingBit= */ FPBits::MIN_NORMAL,
/* endingBit= */ FPBits::MAX_NORMAL, 100'000'001, log);
log << "\n Diff tests with inputs in normal range with exponents "
"close to each other:\n";
diffCount += run_diff_in_range(
myFunc, otherFunc, /* startingBit= */ FPBits(T(0x1.0p-10)).uintval(),
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(), 10'000'001, log);
log << "Total number of differing results: " << diffCount << '\n';
}
};
} // namespace testing
} // namespace __llvm_libc
#define BINARY_OP_SINGLE_OUTPUT_DIFF(T, myFunc, otherFunc, filename) \
int main() { \
__llvm_libc::testing::BinaryOpSingleOutputDiff<T>::run_diff( \
&myFunc, &otherFunc, filename); \
return 0; \
}
#define BINARY_OP_SINGLE_OUTPUT_PERF(T, myFunc, otherFunc, filename) \
int main() { \
__llvm_libc::testing::BinaryOpSingleOutputDiff<T>::run_perf( \
&myFunc, &otherFunc, filename); \
return 0; \
}
|