#include "hdr/math_macros.h"
#include "hdr/stdint_proxy.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/macros/optimization.h"
#include "src/math/log1pf.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
#define TOLERANCE 1
#else
#define TOLERANCE 0
#endif
using LlvmLibcLog1pfTest = LIBC_NAMESPACE::testing::FPTest<float>;
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
TEST_F(LlvmLibcLog1pfTest, SpecialNumbers) {
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::log1pf(aNaN));
EXPECT_FP_EQ(inf, LIBC_NAMESPACE::log1pf(inf));
EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::log1pf(neg_inf), FE_INVALID);
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::log1pf(0.0f));
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::log1pf(-0.0f));
EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1pf(-1.0f),
FE_DIVBYZERO);
}
TEST_F(LlvmLibcLog1pfTest, TrickyInputs) {
constexpr int N = 27;
constexpr uint32_t INPUTS[N] = {
0x35c00006U,
0x35400003U,
0x3640000cU,
0x36c00018U,
0x3710001bU,
0x37400030U,
0x3770004bU,
0x3b9315c8U,
0x3c6eb7afU,
0x3ddbfec3U,
0x3efd81adU,
0x41078febU,
0x4cc1c80bU,
0x5cd69e88U,
0x5ee8984eU,
0x65d890d3U,
0x665e7ca6U,
0x6f31a8ecU,
0x79e7ec37U,
0x7a17f30aU,
0xb53ffffdU,
0xb70fffe5U,
0xbb0ec8c4U,
0xbc4d092cU,
0xbc657728U,
0xbd1d20afU,
0xbf800000U,
};
for (int i = 0; i < N; ++i) {
float x = FPBits(INPUTS[i]).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
LIBC_NAMESPACE::log1pf(x), TOLERANCE + 0.5);
}
}
TEST_F(LlvmLibcLog1pfTest, 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 (FPBits(v).is_nan() || FPBits(v).is_inf())
continue;
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
LIBC_NAMESPACE::log1pf(x), 0.5);
}
}