Skip to content

Commit 1ace91f

Browse files
author
Job Henandez Lara
authored
[libc][math] Add performance tests for fmul and fmull. (llvm#106262)
1 parent fd0dbc7 commit 1ace91f

File tree

11 files changed

+121
-47
lines changed

11 files changed

+121
-47
lines changed

libc/test/src/math/performance_testing/BinaryOpSingleOutputPerf.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace testing {
19-
20-
template <typename T> class BinaryOpSingleOutputPerf {
21-
using FPBits = fputil::FPBits<T>;
19+
template <typename OutputType, typename InputType>
20+
class BinaryOpSingleOutputPerf {
21+
using FPBits = fputil::FPBits<OutputType>;
2222
using StorageType = typename FPBits::StorageType;
2323
static constexpr StorageType UIntMax =
2424
cpp::numeric_limits<StorageType>::max();
2525

2626
public:
27-
typedef T Func(T, T);
27+
typedef OutputType Func(InputType, InputType);
2828

2929
static void run_perf_in_range(Func myFunc, Func otherFunc,
3030
StorageType startingBit, StorageType endingBit,
@@ -33,7 +33,7 @@ template <typename T> class BinaryOpSingleOutputPerf {
3333
N = cpp::min(N, static_cast<size_t>(endingBit - startingBit));
3434

3535
auto runner = [=](Func func) {
36-
[[maybe_unused]] volatile T result;
36+
[[maybe_unused]] volatile OutputType result;
3737
if (endingBit < startingBit) {
3838
return;
3939
}
@@ -42,8 +42,8 @@ template <typename T> class BinaryOpSingleOutputPerf {
4242
for (size_t i = 0; i < rounds; i++) {
4343
for (StorageType bitsX = startingBit, bitsY = endingBit;;
4444
bitsX += step, bitsY -= step) {
45-
T x = FPBits(bitsX).get_val();
46-
T y = FPBits(bitsY).get_val();
45+
InputType x = FPBits(bitsX).get_val();
46+
InputType y = FPBits(bitsY).get_val();
4747
result = func(x, y);
4848
if (endingBit - bitsX < step) {
4949
break;
@@ -94,10 +94,11 @@ template <typename T> class BinaryOpSingleOutputPerf {
9494
1'000'001, rounds, log);
9595
log << "\n Performance tests with inputs in normal range with exponents "
9696
"close to each other:\n";
97-
run_perf_in_range(myFunc, otherFunc,
98-
/* startingBit= */ FPBits(T(0x1.0p-10)).uintval(),
99-
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(),
100-
1'000'001, rounds, log);
97+
run_perf_in_range(
98+
myFunc, otherFunc,
99+
/* startingBit= */ FPBits(OutputType(0x1.0p-10)).uintval(),
100+
/* endingBit= */ FPBits(OutputType(0x1.0p+10)).uintval(), 1'000'001,
101+
rounds, log);
101102
}
102103

103104
static void run_diff(Func myFunc, Func otherFunc, const char *logFile) {
@@ -115,8 +116,10 @@ template <typename T> class BinaryOpSingleOutputPerf {
115116
log << "\n Diff tests with inputs in normal range with exponents "
116117
"close to each other:\n";
117118
diffCount += run_diff_in_range(
118-
myFunc, otherFunc, /* startingBit= */ FPBits(T(0x1.0p-10)).uintval(),
119-
/* endingBit= */ FPBits(T(0x1.0p+10)).uintval(), 10'000'001, log);
119+
myFunc, otherFunc,
120+
/* startingBit= */ FPBits(OutputType(0x1.0p-10)).uintval(),
121+
/* endingBit= */ FPBits(OutputType(0x1.0p+10)).uintval(), 10'000'001,
122+
log);
120123

121124
log << "Total number of differing results: " << diffCount << '\n';
122125
}
@@ -125,18 +128,21 @@ template <typename T> class BinaryOpSingleOutputPerf {
125128
} // namespace testing
126129
} // namespace LIBC_NAMESPACE_DECL
127130

128-
#define BINARY_OP_SINGLE_OUTPUT_PERF(T, myFunc, otherFunc, filename) \
131+
#define BINARY_OP_SINGLE_OUTPUT_PERF(OutputType, InputType, myFunc, otherFunc, \
132+
filename) \
129133
int main() { \
130-
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf<T>::run_perf( \
131-
&myFunc, &otherFunc, 1, filename); \
134+
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf< \
135+
OutputType, InputType>::run_perf(&myFunc, &otherFunc, 1, filename); \
132136
return 0; \
133137
}
134138

135-
#define BINARY_OP_SINGLE_OUTPUT_PERF_EX(T, myFunc, otherFunc, rounds, \
136-
filename) \
139+
#define BINARY_OP_SINGLE_OUTPUT_PERF_EX(OutputType, InputType, myFunc, \
140+
otherFunc, rounds, filename) \
137141
{ \
138-
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf<T>::run_perf( \
139-
&myFunc, &otherFunc, rounds, filename); \
140-
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf<T>::run_perf( \
141-
&myFunc, &otherFunc, rounds, filename); \
142+
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf< \
143+
OutputType, InputType>::run_perf(&myFunc, &otherFunc, rounds, \
144+
filename); \
145+
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf< \
146+
OutputType, InputType>::run_perf(&myFunc, &otherFunc, rounds, \
147+
filename); \
142148
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,25 @@ add_perf_binary(
476476
COMPILE_OPTIONS
477477
-fno-builtin
478478
)
479+
480+
add_perf_binary(
481+
fmul_perf
482+
SRCS
483+
fmul_perf.cpp
484+
DEPENDS
485+
.binary_op_single_output_diff
486+
libc.src.math.fmul
487+
COMPILE_OPTIONS
488+
-fno-builtin
489+
)
490+
491+
add_perf_binary(
492+
fmull_perf
493+
SRCS
494+
fmull_perf.cpp
495+
DEPENDS
496+
.binary_op_single_output_diff
497+
libc.src.math.fmull
498+
COMPILE_OPTIONS
499+
-fno-builtin
500+
)

libc/test/src/math/performance_testing/fmod_perf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
#include <math.h>
1414

15-
BINARY_OP_SINGLE_OUTPUT_PERF(double, LIBC_NAMESPACE::fmod, ::fmod,
15+
BINARY_OP_SINGLE_OUTPUT_PERF(double, double, LIBC_NAMESPACE::fmod, ::fmod,
1616
"fmod_perf.log")

libc/test/src/math/performance_testing/fmodf16_perf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#define FMOD_FUNC(U) (LIBC_NAMESPACE::fputil::generic::FMod<float16, U>::eval)
1717

1818
int main() {
19-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, FMOD_FUNC(uint16_t),
19+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, FMOD_FUNC(uint16_t),
2020
FMOD_FUNC(uint32_t), 5000,
2121
"fmodf16_u16_vs_u32_perf.log")
2222

23-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, FMOD_FUNC(uint16_t),
23+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, FMOD_FUNC(uint16_t),
2424
FMOD_FUNC(uint64_t), 5000,
2525
"fmodf16_u16_vs_u64_perf.log")
2626
return 0;

libc/test/src/math/performance_testing/fmodf_perf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
#include <math.h>
1414

15-
BINARY_OP_SINGLE_OUTPUT_PERF(float, LIBC_NAMESPACE::fmodf, ::fmodf,
15+
BINARY_OP_SINGLE_OUTPUT_PERF(float, float, LIBC_NAMESPACE::fmodf, ::fmodf,
1616
"fmodf_perf.log")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Performance test for the fmul function ----------------------------===//
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 "BinaryOpSingleOutputPerf.h"
10+
#include "src/math/fmul.h"
11+
12+
static constexpr size_t DOUBLE_ROUNDS = 40;
13+
14+
float fmul_placeholder_binary(double x, double y) {
15+
return static_cast<float>(x * y);
16+
}
17+
18+
int main() {
19+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, double, LIBC_NAMESPACE::fmul,
20+
fmul_placeholder_binary, DOUBLE_ROUNDS,
21+
"fmul_perf.log")
22+
return 0;
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Performance test for the fmull function ---------------------------===//
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 "BinaryOpSingleOutputPerf.h"
10+
#include "src/math/fmull.h"
11+
12+
static constexpr size_t LONG_DOUBLE_ROUNDS = 40;
13+
14+
float fmull_placeholder_binary(long double x, long double y) {
15+
return static_cast<float>(x * y);
16+
}
17+
18+
int main() {
19+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, long double, LIBC_NAMESPACE::fmull,
20+
fmull_placeholder_binary, LONG_DOUBLE_ROUNDS,
21+
"fmull_perf.log")
22+
return 0;
23+
}

libc/test/src/math/performance_testing/hypot_perf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
#include <math.h>
1414

15-
BINARY_OP_SINGLE_OUTPUT_PERF(double, LIBC_NAMESPACE::hypot, ::hypot,
15+
BINARY_OP_SINGLE_OUTPUT_PERF(double, double, LIBC_NAMESPACE::hypot, ::hypot,
1616
"hypot_perf.log")

libc/test/src/math/performance_testing/hypotf_perf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
#include <math.h>
1414

15-
BINARY_OP_SINGLE_OUTPUT_PERF(float, LIBC_NAMESPACE::hypotf, ::hypotf,
15+
BINARY_OP_SINGLE_OUTPUT_PERF(float, float, LIBC_NAMESPACE::hypotf, ::hypotf,
1616
"hypotf_perf.log")

libc/test/src/math/performance_testing/max_min_funcs_perf.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,39 @@ float16 placeholder_binaryf16(float16 x, float16 y) { return x; }
3535
float placeholder_binaryf(float x, float y) { return x; }
3636

3737
int main() {
38-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fmaxf16,
38+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, LIBC_NAMESPACE::fmaxf16,
3939
placeholder_binaryf16, FLOAT16_ROUNDS,
4040
"fmaxf16_perf.log")
41-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fminf16,
41+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, LIBC_NAMESPACE::fminf16,
4242
placeholder_binaryf16, FLOAT16_ROUNDS,
4343
"fminf16_perf.log")
44-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fmaximumf16,
44+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, LIBC_NAMESPACE::fmaximumf16,
4545
placeholder_binaryf16, FLOAT16_ROUNDS,
4646
"fmaximumf16_perf.log")
47-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fminimumf16,
47+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, LIBC_NAMESPACE::fminimumf16,
4848
placeholder_binaryf16, FLOAT16_ROUNDS,
4949
"fminimumf16_perf.log")
50-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fmaximum_numf16,
51-
placeholder_binaryf16, FLOAT16_ROUNDS,
52-
"fmaximum_numf16_perf.log")
53-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fminimum_numf16,
54-
placeholder_binaryf16, FLOAT16_ROUNDS,
55-
"fminimum_numf16_perf.log")
50+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(
51+
float16, float16, LIBC_NAMESPACE::fmaximum_numf16, placeholder_binaryf16,
52+
FLOAT16_ROUNDS, "fmaximum_numf16_perf.log")
53+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(
54+
float16, float16, LIBC_NAMESPACE::fminimum_numf16, placeholder_binaryf16,
55+
FLOAT16_ROUNDS, "fminimum_numf16_perf.log")
5656

57-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fmaxf, ::fmaxf,
57+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fmaxf, ::fmaxf,
5858
FLOAT_ROUNDS, "fmaxf_perf.log")
59-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fminf, ::fminf,
59+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fminf, ::fminf,
6060
FLOAT_ROUNDS, "fminf_perf.log")
61-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fmaximumf,
61+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fmaximumf,
6262
placeholder_binaryf, FLOAT_ROUNDS,
6363
"fmaximumf_perf.log")
64-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fminimumf,
64+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fminimumf,
6565
placeholder_binaryf, FLOAT_ROUNDS,
6666
"fminimumf_perf.log")
67-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fmaximum_numf,
67+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fmaximum_numf,
6868
placeholder_binaryf, FLOAT_ROUNDS,
6969
"fmaximum_numf_perf.log")
70-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fminimum_numf,
70+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::fminimum_numf,
7171
placeholder_binaryf, FLOAT_ROUNDS,
7272
"fminimum_numf_perf.log")
7373

libc/test/src/math/performance_testing/misc_basic_ops_perf.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ int main() {
2828
SINGLE_INPUT_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::fabsf16,
2929
placeholder_unaryf16, FLOAT16_ROUNDS,
3030
"fabsf16_perf.log")
31-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::copysignf16,
31+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float16, float16, LIBC_NAMESPACE::copysignf16,
3232
placeholder_binaryf16, FLOAT16_ROUNDS,
3333
"copysignf16_perf.log")
3434

3535
SINGLE_INPUT_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::fabsf, fabsf,
3636
FLOAT_ROUNDS, "fabsf_perf.log")
37-
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, LIBC_NAMESPACE::copysignf, copysignf,
38-
FLOAT_ROUNDS, "copysignf_perf.log")
37+
BINARY_OP_SINGLE_OUTPUT_PERF_EX(float, float, LIBC_NAMESPACE::copysignf,
38+
copysignf, FLOAT_ROUNDS, "copysignf_perf.log")
3939

4040
return 0;
4141
}

0 commit comments

Comments
 (0)