Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//===----------------------------------------------------------------------===//

#include "src/__support/FPUtil/FPBits.h"
#include "test/src/math/differential_testing/Timer.h"
#include "test/src/math/performance_testing/Timer.h"

#include <fstream>

namespace LIBC_NAMESPACE {
namespace testing {

template <typename T> class BinaryOpSingleOutputDiff {
template <typename T> class BinaryOpSingleOutputPerf {
using FPBits = fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
static constexpr StorageType UIntMax =
Expand All @@ -23,40 +23,6 @@ template <typename T> class BinaryOpSingleOutputDiff {
public:
typedef T Func(T, T);

static uint64_t run_diff_in_range(Func myFunc, Func otherFunc,
StorageType startingBit,
StorageType endingBit, StorageType N,
std::ofstream &log) {
uint64_t result = 0;
if (endingBit < startingBit) {
return result;
}

StorageType step = (endingBit - startingBit) / N;
for (StorageType 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,
StorageType startingBit, StorageType endingBit,
StorageType N, std::ofstream &log) {
Expand All @@ -69,8 +35,8 @@ template <typename T> class BinaryOpSingleOutputDiff {
StorageType step = (endingBit - startingBit) / N;
for (StorageType bitsX = startingBit, bitsY = endingBit;;
bitsX += step, bitsY -= step) {
T x = T(FPBits(bitsX));
T y = T(FPBits(bitsY));
T x = FPBits(bitsX).get_val();
T y = FPBits(bitsY).get_val();
result = func(x, y);
if (endingBit - bitsX < step) {
break;
Expand Down Expand Up @@ -110,12 +76,12 @@ template <typename T> class BinaryOpSingleOutputDiff {
log << " Performance tests with inputs in denormal range:\n";
run_perf_in_range(myFunc, otherFunc, /* startingBit= */ StorageType(0),
/* endingBit= */ FPBits::max_subnormal().uintval(),
1'000'001, log);
10'000'001, log);
log << "\n Performance tests with inputs in normal range:\n";
run_perf_in_range(myFunc, otherFunc,
/* startingBit= */ FPBits::min_normal().uintval(),
/* endingBit= */ FPBits::max_normal().uintval(),
100'000'001, log);
10'000'001, log);
log << "\n Performance tests with inputs in normal range with exponents "
"close to each other:\n";
run_perf_in_range(
Expand Down Expand Up @@ -148,16 +114,9 @@ template <typename T> class BinaryOpSingleOutputDiff {
} // namespace testing
} // namespace LIBC_NAMESPACE

#define BINARY_OP_SINGLE_OUTPUT_DIFF(T, myFunc, otherFunc, filename) \
int main() { \
LIBC_NAMESPACE::testing::BinaryOpSingleOutputDiff<T>::run_diff( \
&myFunc, &otherFunc, filename); \
return 0; \
}

#define BINARY_OP_SINGLE_OUTPUT_PERF(T, myFunc, otherFunc, filename) \
int main() { \
LIBC_NAMESPACE::testing::BinaryOpSingleOutputDiff<T>::run_perf( \
LIBC_NAMESPACE::testing::BinaryOpSingleOutputPerf<T>::run_perf( \
&myFunc, &otherFunc, filename); \
return 0; \
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//===----------------------------------------------------------------------===//

#include "src/__support/FPUtil/FPBits.h"
#include "test/src/math/differential_testing/Timer.h"
#include "test/src/math/performance_testing/Timer.h"

#include <fstream>

namespace LIBC_NAMESPACE {
namespace testing {

template <typename T> class SingleInputSingleOutputDiff {
template <typename T> class SingleInputSingleOutputPerf {
using FPBits = fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
static constexpr StorageType UIntMax =
Expand All @@ -23,40 +23,18 @@ template <typename T> class SingleInputSingleOutputDiff {
public:
typedef T Func(T);

static void runDiff(Func myFunc, Func otherFunc, const char *logFile) {
StorageType diffCount = 0;
std::ofstream log(logFile);
log << "Starting diff for values from 0 to " << UIntMax << '\n'
<< "Only differing results will be logged.\n\n";
for (StorageType bits = 0;; ++bits) {
T x = T(FPBits(bits));
T myResult = myFunc(x);
T otherResult = otherFunc(x);
StorageType myBits = FPBits(myResult).uintval();
StorageType otherBits = FPBits(otherResult).uintval();
if (myBits != otherBits) {
++diffCount;
log << " Input: " << bits << " (" << x << ")\n"
<< " My result: " << myBits << " (" << myResult << ")\n"
<< "Other result: " << otherBits << " (" << otherResult << ")\n"
<< '\n';
}
if (bits == UIntMax)
break;
}
log << "Total number of differing results: " << diffCount << '\n';
}

static void runPerfInRange(Func myFunc, Func otherFunc,
StorageType startingBit, StorageType endingBit,
std::ofstream &log) {
auto runner = [=](Func func) {
constexpr StorageType N = 10'010'001;
StorageType step = (endingBit - startingBit) / N;
if (step == 0)
step = 1;
volatile T result;
for (StorageType bits = startingBit;; ++bits) {
T x = T(FPBits(bits));
for (StorageType bits = startingBit; bits < endingBit; bits += step) {
T x = FPBits(bits).get_val();
result = func(x);
if (bits == endingBit)
break;
}
};

Expand Down Expand Up @@ -104,16 +82,9 @@ template <typename T> class SingleInputSingleOutputDiff {
} // namespace testing
} // namespace LIBC_NAMESPACE

#define SINGLE_INPUT_SINGLE_OUTPUT_DIFF(T, myFunc, otherFunc, filename) \
int main() { \
LIBC_NAMESPACE::testing::SingleInputSingleOutputDiff<T>::runDiff( \
&myFunc, &otherFunc, filename); \
return 0; \
}

#define SINGLE_INPUT_SINGLE_OUTPUT_PERF(T, myFunc, otherFunc, filename) \
int main() { \
LIBC_NAMESPACE::testing::SingleInputSingleOutputDiff<T>::runPerf( \
LIBC_NAMESPACE::testing::SingleInputSingleOutputPerf<T>::runPerf( \
&myFunc, &otherFunc, filename); \
return 0; \
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TEST_SRC_MATH_DIFFERENTIAL_TESTING_TIMER_H
#define LLVM_LIBC_TEST_SRC_MATH_DIFFERENTIAL_TESTING_TIMER_H
#ifndef LLVM_LIBC_TEST_SRC_MATH_PERFORMACE_TESTING_TIMER_H
#define LLVM_LIBC_TEST_SRC_MATH_PERFORMACE_TESTING_TIMER_H

#include <stdint.h>

Expand All @@ -30,4 +30,4 @@ class Timer {
} // namespace testing
} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_TEST_SRC_MATH_DIFFERENTIAL_TESTING_TIMER_H
#endif // LLVM_LIBC_TEST_SRC_MATH_PERFORMANCE_TESTING_TIMER_H
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/ceilf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/cosf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/exp2f.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/expf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/expm1f.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/fabsf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/floorf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "BinaryOpSingleOutputDiff.h"
#include "BinaryOpSingleOutputPerf.h"

#include "src/math/fmod.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "BinaryOpSingleOutputDiff.h"
#include "BinaryOpSingleOutputPerf.h"

#include "src/math/fmodf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "BinaryOpSingleOutputDiff.h"
#include "BinaryOpSingleOutputPerf.h"

#include "src/math/hypot.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "BinaryOpSingleOutputDiff.h"
#include "BinaryOpSingleOutputPerf.h"

#include "src/math/hypotf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/log10f.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/log1pf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/log2f.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/logbf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/logf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/nearbyintf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/rintf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/roundf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/sinf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/sqrtf.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "SingleInputSingleOutputDiff.h"
#include "SingleInputSingleOutputPerf.h"

#include "src/math/truncf.h"

Expand Down