[libc-commits] [libc] 12779ed - [libc] Add performance tests for hypotf and hypot.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Wed Dec 22 21:45:07 PST 2021


Author: Tue Ly
Date: 2021-12-23T00:44:43-05:00
New Revision: 12779edd711845eccd83ef7d5c30ac8e316a1226

URL: https://github.com/llvm/llvm-project/commit/12779edd711845eccd83ef7d5c30ac8e316a1226
DIFF: https://github.com/llvm/llvm-project/commit/12779edd711845eccd83ef7d5c30ac8e316a1226.diff

LOG: [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

Added: 
    libc/test/src/math/differential_testing/BinaryOpSingleOutputDiff.h
    libc/test/src/math/differential_testing/hypot_perf.cpp
    libc/test/src/math/differential_testing/hypotf_perf.cpp

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

Removed: 
    


################################################################################
diff  --git a/libc/test/src/math/
diff erential_testing/BinaryOpSingleOutputDiff.h b/libc/test/src/math/
diff erential_testing/BinaryOpSingleOutputDiff.h
new file mode 100644
index 0000000000000..0fdfdf069aa93
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/BinaryOpSingleOutputDiff.h
@@ -0,0 +1,99 @@
+//===-- Common utility class for 
diff erential 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 "utils/testutils/StreamWrapper.h"
+#include "utils/testutils/Timer.h"
+
+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 void run_perf_in_range(Func myFunc, Func otherFunc,
+                                UIntType startingBit, UIntType endingBit,
+                                UIntType N, testutils::OutputFileStream &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) {
+    testutils::OutputFileStream 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);
+  }
+};
+
+} // 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;                                                                  \
+  }

diff  --git a/libc/test/src/math/
diff erential_testing/CMakeLists.txt b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
index e553bf5bfbef1..d3e0e64e03fb2 100644
--- a/libc/test/src/math/
diff erential_testing/CMakeLists.txt
+++ b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
@@ -67,6 +67,12 @@ add_header_library(
     SingleInputSingleOutputDiff.h
 )
 
+add_header_library(
+  binary_op_single_output_
diff 
+  HDRS
+    BinaryOpSingleOutputDiff.h
+)
+
 add_
diff _binary(
   sinf_
diff 
   SRCS
@@ -368,3 +374,25 @@ add_
diff _binary(
   COMPILE_OPTIONS
     -fno-builtin
 )
+
+add_
diff _binary(
+  hypotf_perf
+  SRCS
+    hypotf_perf.cpp
+  DEPENDS
+    .binary_op_single_output_
diff 
+    libc.src.math.hypotf
+  COMPILE_OPTIONS
+    -fno-builtin
+)
+
+add_
diff _binary(
+  hypot_perf
+  SRCS
+    hypot_perf.cpp
+  DEPENDS
+    .binary_op_single_output_
diff 
+    libc.src.math.hypot
+  COMPILE_OPTIONS
+    -fno-builtin
+)

diff  --git a/libc/test/src/math/
diff erential_testing/hypot_perf.cpp b/libc/test/src/math/
diff erential_testing/hypot_perf.cpp
new file mode 100644
index 0000000000000..f90605455f768
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/hypot_perf.cpp
@@ -0,0 +1,16 @@
+//===-- Differential test for hypot ---------------------------------------===//
+//
+// 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 "BinaryOpSingleOutputDiff.h"
+
+#include "src/math/hypot.h"
+
+#include <math.h>
+
+BINARY_OP_SINGLE_OUTPUT_PERF(double, __llvm_libc::hypot, ::hypot,
+                             "hypot_perf.log")

diff  --git a/libc/test/src/math/
diff erential_testing/hypotf_perf.cpp b/libc/test/src/math/
diff erential_testing/hypotf_perf.cpp
new file mode 100644
index 0000000000000..75c61cb0592af
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/hypotf_perf.cpp
@@ -0,0 +1,16 @@
+//===-- Differential test for hypotf --------------------------------------===//
+//
+// 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 "BinaryOpSingleOutputDiff.h"
+
+#include "src/math/hypotf.h"
+
+#include <math.h>
+
+BINARY_OP_SINGLE_OUTPUT_PERF(float, __llvm_libc::hypotf, ::hypotf,
+                             "hypotf_perf.log")


        


More information about the libc-commits mailing list