Skip to content

Commit

Permalink
[libc] Let exhaustive tests indicate each interval PASSED/FAILED.
Browse files Browse the repository at this point in the history
Let exhaustive tests indicate each interval PASSED/FAILED.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D121564
  • Loading branch information
lntue committed Mar 16, 2022
1 parent 190f385 commit 4c9bfec
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 40 deletions.
3 changes: 1 addition & 2 deletions libc/test/src/math/exhaustive/exhaustive_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void LlvmLibcExhaustiveTest<T>::test_full_range(T start, T stop, int nthreads,
std::cout << msg.str();
msg.str("");

bool result;
check(begin, end, rounding, result);
bool result = check(begin, end, rounding);

msg << "** Finished testing from " << std::dec << begin << " to " << end
<< " [0x" << std::hex << begin << ", 0x" << end
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/exhaustive/exhaustive_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ struct LlvmLibcExhaustiveTest : public __llvm_libc::testing::Test {
void test_full_range(T start, T stop, int nthreads,
mpfr::RoundingMode rounding);

virtual void check(T start, T stop, mpfr::RoundingMode rounding,
bool &result) = 0;
virtual bool check(T start, T stop, mpfr::RoundingMode rounding) = 0;
};
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/exp2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcExp2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Exp2, x, __llvm_libc::exp2f(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Exp2, x,
__llvm_libc::exp2f(x), 0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/expf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcExpfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Exp, x, __llvm_libc::expf(x),
0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
11 changes: 5 additions & 6 deletions libc/test/src/math/exhaustive/expm1f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcExpfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop, mpfr::RoundingMode rounding,
bool &result) override {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
result = false;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, x,
__llvm_libc::expm1f(x), 0.5, rounding);
} while (bits++ < stop);
result = true;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/hypotf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcHypotfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
// Range of the second input: [2^37, 2^48).
constexpr uint32_t Y_START = (37U + 127U) << 23;
constexpr uint32_t Y_STOP = (48U + 127U) << 23;

mpfr::ForceRoundingMode r(rounding);
uint32_t xbits = start;
bool result = true;
do {
float x = float(FPBits(xbits));
uint32_t ybits = Y_START;
do {
float y = float(FPBits(ybits));
EXPECT_FP_EQ(__llvm_libc::fputil::hypot(x, y),
__llvm_libc::hypotf(x, y));
result &= EXPECT_FP_EQ(__llvm_libc::fputil::hypot(x, y),
__llvm_libc::hypotf(x, y));
// Using MPFR will be much slower.
// mpfr::BinaryInput<float> input{x, y};
// EXPECT_MPFR_MATCH(mpfr::Operation::Hypot, input,
// __llvm_libc::hypotf(x, y), 0.5,
// rounding);
} while (ybits++ < Y_STOP);
} while (xbits++ < stop);
return result;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/log10f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcLog10fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Log10, x, __llvm_libc::log10f(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Log10, x,
__llvm_libc::log10f(x), 0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/log1pf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibclog1pfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Log1p, x, __llvm_libc::log1pf(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Log1p, x,
__llvm_libc::log1pf(x), 0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/log2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcLog2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Log2, x, __llvm_libc::log2f(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Log2, x,
__llvm_libc::log2f(x), 0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
8 changes: 5 additions & 3 deletions libc/test/src/math/exhaustive/logf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ using FPBits = __llvm_libc::fputil::FPBits<float>;
namespace mpfr = __llvm_libc::testing::mpfr;

struct LlvmLibcLogfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
void check(uint32_t start, uint32_t stop,
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = start;
bool result = true;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5,
rounding);
result &= EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x),
0.5, rounding);
} while (bits++ < stop);
return result;
}
};

Expand Down
15 changes: 6 additions & 9 deletions libc/utils/UnitTest/LibcTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,16 @@ template <typename... Types> using TypeList = internal::TypeList<Types...>;
#define UNIQUE_VAR(prefix) __CAT(prefix, __LINE__)

#define EXPECT_THAT(MATCH, MATCHER) \
do { \
[&]() -> bool { \
auto UNIQUE_VAR(__matcher) = (MATCHER); \
this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \
UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__, \
__LINE__); \
} while (0)
return this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \
UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__, \
__LINE__); \
}()

#define ASSERT_THAT(MATCH, MATCHER) \
do { \
auto UNIQUE_VAR(__matcher) = (MATCHER); \
if (!this->testMatch(UNIQUE_VAR(__matcher).match((MATCH)), \
UNIQUE_VAR(__matcher), #MATCH, #MATCHER, __FILE__, \
__LINE__)) \
if (!EXPECT_THAT(MATCH, MATCHER)) \
return; \
} while (0)

Expand Down

0 comments on commit 4c9bfec

Please sign in to comment.