2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/acoshf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcAcoshfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/asinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcAsinfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/asinhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcAsinhfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/atanf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcAtanfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/atanhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcAtanhfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/cosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcCosfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/coshf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcCoshfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/exp10f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcExp10fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/exp2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct LlvmLibcExp2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/expf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcExpfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/expm1f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace mpfr = __llvm_libc::testing::mpfr;
struct LlvmLibcExpm1fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
if (!r.success)
return true;
mpfr::ForceRoundingMode r(rounding);
uint32_t bits = stop;
bool result = true;
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/hypotf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct LlvmLibcHypotfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
constexpr uint32_t Y_STOP = (48U + 127U) << 23;

mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t xbits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/log10f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct LlvmLibcLog10fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/log1pf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct LlvmLibcLog1pfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/log2f_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct LlvmLibcLog2fExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/logf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct LlvmLibcLogfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/sincosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcSinCosfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/sinf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcSinfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/sinhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct LlvmLibcSinhfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/tanf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct LlvmLibcTanfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
int tol = TOLERANCE;
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/exhaustive/tanhf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct LlvmLibcTanhfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
bool check(uint32_t start, uint32_t stop,
mpfr::RoundingMode rounding) override {
mpfr::ForceRoundingMode r(rounding);
if (!r.success)
return true;
uint32_t bits = start;
bool result = true;
do {
Expand Down
2 changes: 2 additions & 0 deletions libc/test/src/math/log10_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ TEST(LlvmLibcLog10Test, InDoubleRange) {

auto test = [&](mpfr::RoundingMode rounding_mode) {
mpfr::ForceRoundingMode __r(rounding_mode);
if (!__r.success)
return;
uint64_t fails = 0;
uint64_t count = 0;
uint64_t cc = 0;
Expand Down
3 changes: 3 additions & 0 deletions libc/test/src/math/log1p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ TEST(LlvmLibcLog1pTest, InDoubleRange) {
auto test = [&](uint64_t start, uint64_t stop,
mpfr::RoundingMode rounding_mode) {
mpfr::ForceRoundingMode __r(rounding_mode);
if (!__r.success)
return;

uint64_t fails = 0;
uint64_t count = 0;
uint64_t cc = 0;
Expand Down
3 changes: 3 additions & 0 deletions libc/test/src/math/log2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ TEST(LlvmLibcLog2Test, InDoubleRange) {

auto test = [&](mpfr::RoundingMode rounding_mode) {
mpfr::ForceRoundingMode __r(rounding_mode);
if (!__r.success)
return;

uint64_t fails = 0;
uint64_t count = 0;
uint64_t cc = 0;
Expand Down
3 changes: 3 additions & 0 deletions libc/test/src/math/log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ TEST(LlvmLibcLogTest, InDoubleRange) {

auto test = [&](mpfr::RoundingMode rounding_mode) {
mpfr::ForceRoundingMode __r(rounding_mode);
if (!__r.success)
return;

uint64_t fails = 0;
uint64_t count = 0;
uint64_t cc = 0;
Expand Down
48 changes: 28 additions & 20 deletions libc/test/src/math/sincosf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,40 @@ TEST(LlvmLibcSinCosfTest, SpecialNumbers) {
namespace mpfr = __llvm_libc::testing::mpfr; \
\
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Nearest); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Nearest); \
if (__r1.success) { \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Nearest); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Nearest); \
} \
\
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Upward); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Upward); \
if (__r2.success) { \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Upward); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Upward); \
} \
\
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Downward); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Downward); \
if (__r3.success) { \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::Downward); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::Downward); \
} \
\
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::TowardZero); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::TowardZero); \
if (__r4.success) { \
__llvm_libc::sincosf(input, &sin, &cos); \
EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5, \
mpfr::RoundingMode::TowardZero); \
EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5, \
mpfr::RoundingMode::TowardZero); \
} \
}

TEST(LlvmLibcSinCosfTest, InFloatRange) {
Expand Down
95 changes: 23 additions & 72 deletions libc/test/src/stdio/sprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class LlvmLibcSPrintfTest : public __llvm_libc::testing::Test {
int written;
};

using __llvm_libc::fputil::testing::ForceRoundingMode;
using __llvm_libc::fputil::testing::RoundingMode;

// Subtract 1 from sizeof(expected_str) to account for the null byte.
#define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str) \
EXPECT_EQ(actual_written, static_cast<int>(sizeof(expected_str) - 1)); \
Expand Down Expand Up @@ -505,8 +508,7 @@ TEST(LlvmLibcSPrintfTest, OctConv) {
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT

TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);
ForceRoundingMode r(RoundingMode::Nearest);
double inf = __llvm_libc::fputil::FPBits<double>::inf().get_val();
double nan = __llvm_libc::fputil::FPBits<double>::build_nan(1);

Expand Down Expand Up @@ -705,10 +707,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {

// Rounding Mode Tests.

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);

if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
written = __llvm_libc::sprintf(buff, "%.1a", 0x1.08p0);
ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");

Expand All @@ -734,10 +733,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Upward);

if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1a", 0x1.08p0);
ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");

Expand All @@ -763,10 +759,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Downward);

if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1a", 0x1.08p0);
ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");

Expand All @@ -792,10 +785,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
ASSERT_STREQ_LEN(written, buff, "-0x1.2p+0");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::TowardZero);

if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
written = __llvm_libc::sprintf(buff, "%.1a", 0x1.08p0);
ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");

Expand Down Expand Up @@ -866,8 +856,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
}

TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);
ForceRoundingMode r(RoundingMode::Nearest);
double inf = __llvm_libc::fputil::FPBits<double>::inf().get_val();
double nan = __llvm_libc::fputil::FPBits<double>::build_nan(1);

Expand Down Expand Up @@ -1340,10 +1329,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {

// Rounding Mode Tests.

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);

if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
written = __llvm_libc::sprintf(buff, "%.1f", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8");

Expand Down Expand Up @@ -1381,10 +1367,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Upward);

if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1f", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8");

Expand Down Expand Up @@ -1422,10 +1405,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
ASSERT_STREQ_LEN(written, buff, "-1.8");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Downward);

if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1f", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7");

Expand Down Expand Up @@ -1463,10 +1443,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::TowardZero);

if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
written = __llvm_libc::sprintf(buff, "%.1f", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7");

Expand Down Expand Up @@ -1572,8 +1549,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
}

TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);
ForceRoundingMode r(RoundingMode::Nearest);
double inf = __llvm_libc::fputil::FPBits<double>::inf().get_val();
double nan = __llvm_libc::fputil::FPBits<double>::build_nan(1);

Expand Down Expand Up @@ -1926,10 +1902,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {

// Rounding Mode Tests.

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);

if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
written = __llvm_libc::sprintf(buff, "%.1e", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8e+00");

Expand Down Expand Up @@ -1967,10 +1940,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9e+00");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Upward);

if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1e", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8e+00");

Expand Down Expand Up @@ -2008,10 +1978,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
ASSERT_STREQ_LEN(written, buff, "-1.8e+00");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Downward);

if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
written = __llvm_libc::sprintf(buff, "%.1e", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7e+00");

Expand Down Expand Up @@ -2049,10 +2016,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9e+00");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::TowardZero);

if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
written = __llvm_libc::sprintf(buff, "%.1e", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7e+00");

Expand Down Expand Up @@ -2167,8 +2131,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
}

TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);
ForceRoundingMode r(RoundingMode::Nearest);
double inf = __llvm_libc::fputil::FPBits<double>::inf().get_val();
double nan = __llvm_libc::fputil::FPBits<double>::build_nan(1);

Expand Down Expand Up @@ -2545,10 +2508,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {

// Rounding Mode Tests.

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Nearest);

if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
written = __llvm_libc::sprintf(buff, "%.2g", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8");

Expand Down Expand Up @@ -2586,10 +2546,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Upward);

if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
written = __llvm_libc::sprintf(buff, "%.2g", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.8");

Expand Down Expand Up @@ -2627,10 +2584,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {
ASSERT_STREQ_LEN(written, buff, "-1.8");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::Downward);

if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
written = __llvm_libc::sprintf(buff, "%.2g", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7");

Expand Down Expand Up @@ -2668,10 +2622,7 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {
ASSERT_STREQ_LEN(written, buff, "-1.9");
}

{
__llvm_libc::fputil::testing::ForceRoundingMode r(
__llvm_libc::fputil::testing::RoundingMode::TowardZero);

if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
written = __llvm_libc::sprintf(buff, "%.2g", 1.75);
ASSERT_STREQ_LEN(written, buff, "1.7");

Expand Down
40 changes: 24 additions & 16 deletions libc/utils/MPFRWrapper/MPFRUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,21 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
{ \
namespace mpfr = __llvm_libc::testing::mpfr; \
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
if (__r1.success) \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
if (__r2.success) \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
if (__r3.success) \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
if (__r4.success) \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
}

#define TEST_MPFR_MATCH_ROUNDING_SILENTLY(op, input, match_value, \
Expand Down Expand Up @@ -393,17 +397,21 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
{ \
namespace mpfr = __llvm_libc::testing::mpfr; \
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
if (__r1.success) \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
if (__r2.success) \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
if (__r3.success) \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
if (__r4.success) \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
}

#endif // LLVM_LIBC_UTILS_TESTUTILS_MPFRUTILS_H
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cc_library(
"//libc:__support_cpp_type_traits",
"//libc:__support_fputil_fp_bits",
"//libc:__support_fputil_fpbits_str",
"//libc:__support_fputil_rounding_mode",
"//libc:__support_macros_properties_architectures",
"//libc:__support_stringutil",
"//libc:__support_uint128",
Expand Down Expand Up @@ -77,6 +78,7 @@ cc_library(
"//libc:__support_fputil_fenv_impl",
"//libc:__support_fputil_fp_bits",
"//libc:__support_fputil_fpbits_str",
"//libc:__support_fputil_rounding_mode",
"//libc:libc_root",
],
)
Expand Down