26 changes: 26 additions & 0 deletions libc/test/src/math/exhaustive/logf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Exhaustive test for logf ------------------------------------------===//
//
// 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/__support/FPUtil/FPBits.h"
#include "src/math/logf.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/FPMatcher.h"
#include "utils/UnitTest/Test.h"

using FPBits = __llvm_libc::fputil::FPBits<float>;

namespace mpfr = __llvm_libc::testing::mpfr;

TEST(LlvmLibcLogfExhaustiveTest, AllValues) {
uint32_t bits = 0U;
do {
FPBits xbits(bits);
float x = float(xbits);
EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
} while (bits++ < 0x7f7f'ffffU);
}
64 changes: 64 additions & 0 deletions libc/test/src/math/logf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===-- Unittests for logf-----------------------------------------------===//
//
// 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/__support/FPUtil/FPBits.h"
#include "src/math/logf.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/FPMatcher.h"
#include "utils/UnitTest/Test.h"
#include <math.h>

#include <errno.h>
#include <stdint.h>

namespace mpfr = __llvm_libc::testing::mpfr;

DECLARE_SPECIAL_CONSTANTS(float)

TEST(LlvmLibcLogfTest, SpecialNumbers) {
EXPECT_FP_EQ(aNaN, __llvm_libc::logf(aNaN));
EXPECT_FP_EQ(inf, __llvm_libc::logf(inf));
EXPECT_TRUE(isnan(__llvm_libc::logf(neg_inf)));
EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(0.0f));
EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(-0.0f));
EXPECT_TRUE(isnan(__llvm_libc::logf(-1.0f)));
EXPECT_FP_EQ(zero, __llvm_libc::logf(1.0f));
}

TEST(LlvmLibcLogfTest, TrickyInputs) {
constexpr int N = 24;
constexpr uint32_t INPUTS[N] = {
0x3509dcf6U, 0x3bf86ef0U, 0x3ca1c99fU, 0x3d13e105U, 0x3f7ff1f2U,
0x3f7fffffU, 0x3f800006U, 0x3f800014U, 0x3f80001cU, 0x3f80c777U,
0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, 0x3f81feb4U,
0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, 0x41178febU,
0x4c5d65a5U, 0x4e85f412U, 0x65d890d3U, 0x6f31a8ecU};
for (int i = 0; i < N; ++i) {
float x = float(FPBits(INPUTS[i]));
EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
}
}

TEST(LlvmLibcLogfTest, InFloatRange) {
constexpr uint32_t count = 1000000;
constexpr uint32_t step = UINT32_MAX / count;
for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
float x = float(FPBits(v));
if (isnan(x) || isinf(x))
continue;
errno = 0;
float result = __llvm_libc::logf(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) || errno != 0)
continue;
ASSERT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);
}
}
8 changes: 8 additions & 0 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ class MPFRNumber {
return result;
}

MPFRNumber log() const {
MPFRNumber result;
mpfr_log(result.value, value, MPFR_RNDN);
return result;
}

MPFRNumber remquo(const MPFRNumber &divisor, int &quotient) {
MPFRNumber remainder;
long q;
Expand Down Expand Up @@ -385,6 +391,8 @@ unaryOperation(Operation op, InputType input) {
return mpfrInput.expm1();
case Operation::Floor:
return mpfrInput.floor();
case Operation::Log:
return mpfrInput.log();
case Operation::Mod2PI:
return mpfrInput.mod_2pi();
case Operation::ModPIOver2:
Expand Down
1 change: 1 addition & 0 deletions libc/utils/MPFRWrapper/MPFRUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum class Operation : int {
Exp2,
Expm1,
Floor,
Log,
Mod2PI,
ModPIOver2,
ModPIOver4,
Expand Down