15 changes: 15 additions & 0 deletions libc/test/src/math/exhaustive/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ add_fp_unittest(
-lpthread
)

add_fp_unittest(
exp10m1f_test
NO_RUN_POSTBUILD
NEED_MPFR
SUITE
libc_math_exhaustive_tests
SRCS
exp10m1f_test.cpp
DEPENDS
.exhaustive_test
libc.src.math.exp10m1f
LINK_LIBRARIES
-lpthread
)

add_fp_unittest(
expm1f_test
NO_RUN_POSTBUILD
Expand Down
33 changes: 33 additions & 0 deletions libc/test/src/math/exhaustive/exp10m1f_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Exhaustive test for exp10m1f --------------------------------------===//
//
// 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 "exhaustive_test.h"
#include "src/math/exp10m1f.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

using LlvmLibcExp10m1fExhaustiveTest =
LlvmLibcUnaryOpExhaustiveMathTest<float, mpfr::Operation::Exp10m1,
LIBC_NAMESPACE::exp10m1f>;

// Range: [0, Inf];
static constexpr uint32_t POS_START = 0x0000'0000U;
static constexpr uint32_t POS_STOP = 0x7f80'0000U;

TEST_F(LlvmLibcExp10m1fExhaustiveTest, PostiveRange) {
test_full_range_all_roundings(POS_START, POS_STOP);
}

// Range: [-Inf, 0];
static constexpr uint32_t NEG_START = 0x8000'0000U;
static constexpr uint32_t NEG_STOP = 0xff80'0000U;

TEST_F(LlvmLibcExp10m1fExhaustiveTest, NegativeRange) {
test_full_range_all_roundings(NEG_START, NEG_STOP);
}
97 changes: 97 additions & 0 deletions libc/test/src/math/exp10m1f_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===-- Unittests for exp10m1f --------------------------------------------===//
//
// 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 "hdr/math_macros.h"
#include "src/__support/CPP/array.h"
#include "src/errno/libc_errno.h"
#include "src/math/exp10m1f.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

#include <stdint.h>

using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

TEST_F(LlvmLibcExp10m1fTest, TrickyInputs) {
constexpr LIBC_NAMESPACE::cpp::array<float, 39> INPUTS = {
// EXP10M1F_EXCEPTS_LO
0x1.0fe54ep-11f,
0x1.80e6eap-11f,
-0x1.2a33bcp-51f,
-0x0p+0f,
-0x1.b59e08p-31f,
-0x1.bf342p-12f,
-0x1.6207fp-11f,
-0x1.bd0c66p-11f,
-0x1.ffd84cp-10f,
-0x1.a74172p-9f,
-0x1.cb694cp-9f,
// EXP10M1F_EXCEPTS_HI
0x1.8d31eep-8f,
0x1.915fcep-8f,
0x1.bcf982p-8f,
0x1.99ff0ap-7f,
0x1.75ea14p-6f,
0x1.f81b64p-6f,
0x1.fafecp+3f,
-0x1.3bf094p-8f,
-0x1.4558bcp-8f,
-0x1.4bb43p-8f,
-0x1.776cc8p-8f,
-0x1.f024cp-8f,
-0x1.f510eep-8f,
-0x1.0b43c4p-7f,
-0x1.245ee4p-7f,
-0x1.f9f2dap-7f,
-0x1.08e42p-6f,
-0x1.0cdc44p-5f,
-0x1.ca4322p-5f,
// Exceptional integers.
8.0f,
9.0f,
10.0f,
// Overflow boundaries.
0x1.344134p+5f,
0x1.344136p+5f,
0x1.344138p+5f,
// Underflow boundaries.
-0x1.e1a5e0p+2f,
-0x1.e1a5e2p+2f,
-0x1.e1a5e4p+2f,
};

for (float x : INPUTS) {
LIBC_NAMESPACE::libc_errno = 0;
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10m1, x,
LIBC_NAMESPACE::exp10m1f(x), 0.5);
}
}

TEST_F(LlvmLibcExp10m1fTest, InFloatRange) {
constexpr uint32_t COUNT = 100'000;
constexpr uint32_t STEP = UINT32_MAX / COUNT;
for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
float x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
continue;
LIBC_NAMESPACE::libc_errno = 0;
float result = LIBC_NAMESPACE::exp10m1f(x);

// If the computation resulted in an error or did not produce valid result
// in the single-precision floating point range, then ignore comparing with
// MPFR result as MPFR can still produce valid results because of its
// wider precision.
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10m1, x,
LIBC_NAMESPACE::exp10m1f(x), 0.5);
}
}
11 changes: 11 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,17 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)

add_fp_unittest(
exp10m1f_test
SUITE
libc-math-smoke-tests
SRCS
exp10m1f_test.cpp
DEPENDS
libc.src.errno.errno
libc.src.math.exp10m1f
)

add_fp_unittest(
copysign_test
SUITE
Expand Down
59 changes: 59 additions & 0 deletions libc/test/src/math/smoke/exp10m1f_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===-- Unittests for exp10m1f --------------------------------------------===//
//
// 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/errno/libc_errno.h"
#include "src/math/exp10m1f.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

using LlvmLibcExp10m1fTest = LIBC_NAMESPACE::testing::FPTest<float>;

TEST_F(LlvmLibcExp10m1fTest, SpecialNumbers) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_EQ(FPBits(aNaN).uintval(),
FPBits(LIBC_NAMESPACE::exp10m1f(aNaN)).uintval());
EXPECT_EQ(FPBits(neg_aNaN).uintval(),
FPBits(LIBC_NAMESPACE::exp10m1f(neg_aNaN)).uintval());
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::exp10m1f(inf));
EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::exp10m1f(neg_inf));
EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp10m1f(zero));
EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::exp10m1f(neg_zero));

EXPECT_FP_EQ_ALL_ROUNDING(9.0f, LIBC_NAMESPACE::exp10m1f(1.0f));
EXPECT_FP_EQ_ALL_ROUNDING(99.0f, LIBC_NAMESPACE::exp10m1f(2.0f));
EXPECT_FP_EQ_ALL_ROUNDING(999.0f, LIBC_NAMESPACE::exp10m1f(3.0f));
}

TEST_F(LlvmLibcExp10m1fTest, Overflow) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.fffffep+127f),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.344136p+5),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp10m1f(0x1.344138p+5),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}

TEST_F(LlvmLibcExp10m1fTest, Underflow) {
LIBC_NAMESPACE::libc_errno = 0;

EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp10m1f(-max_normal),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f, LIBC_NAMESPACE::exp10m1f(-0x1.e1a5e4p+2f),
FE_UNDERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ unittest("InstrumentationTests") {
"//llvm/lib/Testing/Support",
"//llvm/lib/Transforms/Instrumentation",
]
sources = [ "PGOInstrumentationTest.cpp" ]
sources = [
"MemProfUseTest.cpp",
"PGOInstrumentationTest.cpp",
]
}