[libc-commits] [libc] 76b57ef - [libc][math] Differential "diff" test for hypot/hypotf functions.
via libc-commits
libc-commits at lists.llvm.org
Fri Jun 10 01:09:21 PDT 2022
Author: Kirill Okhotnikov
Date: 2022-06-10T10:08:47+02:00
New Revision: 76b57ef88c4e79ddcfc40393a26de9185c6df9a3
URL: https://github.com/llvm/llvm-project/commit/76b57ef88c4e79ddcfc40393a26de9185c6df9a3
DIFF: https://github.com/llvm/llvm-project/commit/76b57ef88c4e79ddcfc40393a26de9185c6df9a3.diff
LOG: [libc][math] Differential "diff" test for hypot/hypotf functions.
Added test handler in preparation to fmod/fmodf commit.
Differential Revision: https://reviews.llvm.org/D127091
Added:
libc/test/src/math/differential_testing/hypot_diff.cpp
libc/test/src/math/differential_testing/hypotf_diff.cpp
Modified:
libc/test/src/math/differential_testing/BinaryOpSingleOutputDiff.h
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
index 2fa72af0795a8..62f9e7fa6da5d 100644
--- a/libc/test/src/math/
diff erential_testing/BinaryOpSingleOutputDiff.h
+++ b/libc/test/src/math/
diff erential_testing/BinaryOpSingleOutputDiff.h
@@ -22,6 +22,40 @@ template <typename T> class BinaryOpSingleOutputDiff {
public:
typedef T Func(T, T);
+ static uint64_t run_
diff _in_range(Func myFunc, Func otherFunc,
+ UIntType startingBit, UIntType endingBit,
+ UIntType N,
+ testutils::OutputFileStream &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, testutils::OutputFileStream &log) {
@@ -84,6 +118,26 @@ template <typename T> class BinaryOpSingleOutputDiff {
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
diff Count = 0;
+ testutils::OutputFileStream log(logFile);
+ log << " Diff tests with inputs in denormal range:\n";
+
diff Count += 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";
+
diff Count += 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";
+
diff Count += 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
diff ering results: " <<
diff Count << '\n';
+ }
};
} // namespace testing
diff --git a/libc/test/src/math/
diff erential_testing/CMakeLists.txt b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
index 59b6cec52a36f..208e9979a028c 100644
--- a/libc/test/src/math/
diff erential_testing/CMakeLists.txt
+++ b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
@@ -427,6 +427,17 @@ add_
diff _binary(
-fno-builtin
)
+add_
diff _binary(
+ hypotf_
diff
+ SRCS
+ hypotf_
diff .cpp
+ DEPENDS
+ .binary_op_single_output_
diff
+ libc.src.math.hypotf
+ COMPILE_OPTIONS
+ -fno-builtin
+)
+
add_
diff _binary(
hypotf_perf
SRCS
@@ -438,6 +449,17 @@ add_
diff _binary(
-fno-builtin
)
+add_
diff _binary(
+ hypot_
diff
+ SRCS
+ hypot_
diff .cpp
+ DEPENDS
+ .binary_op_single_output_
diff
+ libc.src.math.hypot
+ COMPILE_OPTIONS
+ -fno-builtin
+)
+
add_
diff _binary(
hypot_perf
SRCS
diff --git a/libc/test/src/math/
diff erential_testing/hypot_
diff .cpp b/libc/test/src/math/
diff erential_testing/hypot_
diff .cpp
new file mode 100644
index 0000000000000..28dbd2bfc35e3
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/hypot_
diff .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_DIFF(double, __llvm_libc::hypot, ::hypot,
+ "hypot_
diff .log")
diff --git a/libc/test/src/math/
diff erential_testing/hypotf_
diff .cpp b/libc/test/src/math/
diff erential_testing/hypotf_
diff .cpp
new file mode 100644
index 0000000000000..e1ee9f28cc55a
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/hypotf_
diff .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_DIFF(float, __llvm_libc::hypotf, ::hypotf,
+ "hypotf_
diff .log")
More information about the libc-commits
mailing list