Skip to content

Commit 12779ed

Browse files
committed
[libc] Add performance tests for hypotf and hypot.
Add performance tests for hypotf and hypot. Reviewed By: sivachandra, michaelrj Differential Revision: https://reviews.llvm.org/D116122
1 parent e48b1c8 commit 12779ed

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//===-- Common utility class for differential analysis --------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/FPUtil/FPBits.h"
10+
#include "utils/testutils/StreamWrapper.h"
11+
#include "utils/testutils/Timer.h"
12+
13+
namespace __llvm_libc {
14+
namespace testing {
15+
16+
template <typename T> class BinaryOpSingleOutputDiff {
17+
using FPBits = fputil::FPBits<T>;
18+
using UIntType = typename FPBits::UIntType;
19+
static constexpr UIntType MSBIT = UIntType(1) << (8 * sizeof(UIntType) - 1);
20+
static constexpr UIntType UINTMAX = (MSBIT - 1) + MSBIT;
21+
22+
public:
23+
typedef T Func(T, T);
24+
25+
static void run_perf_in_range(Func myFunc, Func otherFunc,
26+
UIntType startingBit, UIntType endingBit,
27+
UIntType N, testutils::OutputFileStream &log) {
28+
auto runner = [=](Func func) {
29+
volatile T result;
30+
if (endingBit < startingBit) {
31+
return;
32+
}
33+
34+
UIntType step = (endingBit - startingBit) / N;
35+
for (UIntType bitsX = startingBit, bitsY = endingBit;;
36+
bitsX += step, bitsY -= step) {
37+
T x = T(FPBits(bitsX));
38+
T y = T(FPBits(bitsY));
39+
result = func(x, y);
40+
if (endingBit - bitsX < step) {
41+
break;
42+
}
43+
}
44+
};
45+
46+
Timer timer;
47+
timer.start();
48+
runner(myFunc);
49+
timer.stop();
50+
51+
double my_average = static_cast<double>(timer.nanoseconds()) / N;
52+
log << "-- My function --\n";
53+
log << " Total time : " << timer.nanoseconds() << " ns \n";
54+
log << " Average runtime : " << my_average << " ns/op \n";
55+
log << " Ops per second : "
56+
<< static_cast<uint64_t>(1'000'000'000.0 / my_average) << " op/s \n";
57+
58+
timer.start();
59+
runner(otherFunc);
60+
timer.stop();
61+
62+
double other_average = static_cast<double>(timer.nanoseconds()) / N;
63+
log << "-- Other function --\n";
64+
log << " Total time : " << timer.nanoseconds() << " ns \n";
65+
log << " Average runtime : " << other_average << " ns/op \n";
66+
log << " Ops per second : "
67+
<< static_cast<uint64_t>(1'000'000'000.0 / other_average) << " op/s \n";
68+
69+
log << "-- Average runtime ratio --\n";
70+
log << " Mine / Other's : " << my_average / other_average << " \n";
71+
}
72+
73+
static void run_perf(Func myFunc, Func otherFunc, const char *logFile) {
74+
testutils::OutputFileStream log(logFile);
75+
log << " Performance tests with inputs in denormal range:\n";
76+
run_perf_in_range(myFunc, otherFunc, /* startingBit= */ UIntType(0),
77+
/* endingBit= */ FPBits::MAX_SUBNORMAL, 1'000'001, log);
78+
log << "\n Performance tests with inputs in normal range:\n";
79+
run_perf_in_range(myFunc, otherFunc, /* startingBit= */ FPBits::MIN_NORMAL,
80+
/* endingBit= */ FPBits::MAX_NORMAL, 100'000'001, log);
81+
}
82+
};
83+
84+
} // namespace testing
85+
} // namespace __llvm_libc
86+
87+
#define BINARY_OP_SINGLE_OUTPUT_DIFF(T, myFunc, otherFunc, filename) \
88+
int main() { \
89+
__llvm_libc::testing::BinaryOpSingleOutputDiff<T>::run_diff( \
90+
&myFunc, &otherFunc, filename); \
91+
return 0; \
92+
}
93+
94+
#define BINARY_OP_SINGLE_OUTPUT_PERF(T, myFunc, otherFunc, filename) \
95+
int main() { \
96+
__llvm_libc::testing::BinaryOpSingleOutputDiff<T>::run_perf( \
97+
&myFunc, &otherFunc, filename); \
98+
return 0; \
99+
}

libc/test/src/math/differential_testing/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ add_header_library(
6767
SingleInputSingleOutputDiff.h
6868
)
6969

70+
add_header_library(
71+
binary_op_single_output_diff
72+
HDRS
73+
BinaryOpSingleOutputDiff.h
74+
)
75+
7076
add_diff_binary(
7177
sinf_diff
7278
SRCS
@@ -368,3 +374,25 @@ add_diff_binary(
368374
COMPILE_OPTIONS
369375
-fno-builtin
370376
)
377+
378+
add_diff_binary(
379+
hypotf_perf
380+
SRCS
381+
hypotf_perf.cpp
382+
DEPENDS
383+
.binary_op_single_output_diff
384+
libc.src.math.hypotf
385+
COMPILE_OPTIONS
386+
-fno-builtin
387+
)
388+
389+
add_diff_binary(
390+
hypot_perf
391+
SRCS
392+
hypot_perf.cpp
393+
DEPENDS
394+
.binary_op_single_output_diff
395+
libc.src.math.hypot
396+
COMPILE_OPTIONS
397+
-fno-builtin
398+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Differential test for hypot ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "BinaryOpSingleOutputDiff.h"
10+
11+
#include "src/math/hypot.h"
12+
13+
#include <math.h>
14+
15+
BINARY_OP_SINGLE_OUTPUT_PERF(double, __llvm_libc::hypot, ::hypot,
16+
"hypot_perf.log")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Differential test for hypotf --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "BinaryOpSingleOutputDiff.h"
10+
11+
#include "src/math/hypotf.h"
12+
13+
#include <math.h>
14+
15+
BINARY_OP_SINGLE_OUTPUT_PERF(float, __llvm_libc::hypotf, ::hypotf,
16+
"hypotf_perf.log")

0 commit comments

Comments
 (0)