[libc-commits] [libc] 9e7688c - [libc] Implement log1pf correctly rounded to all rounding modes.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Mon Feb 7 13:17:40 PST 2022


Author: Tue Ly
Date: 2022-02-07T16:17:18-05:00
New Revision: 9e7688c71e97b7056bf2a40c3e24ce31dc795f10

URL: https://github.com/llvm/llvm-project/commit/9e7688c71e97b7056bf2a40c3e24ce31dc795f10
DIFF: https://github.com/llvm/llvm-project/commit/9e7688c71e97b7056bf2a40c3e24ce31dc795f10.diff

LOG: [libc] Implement log1pf correctly rounded to all rounding modes.

Implement log1pf correctly rounded to all rounding modes relying on logf implementation for exponent > 2^(-8).

Reviewed By: sivachandra, zimmermann6

Differential Revision: https://reviews.llvm.org/D118962

Added: 
    libc/src/math/generic/log1pf.cpp
    libc/src/math/log1pf.h
    libc/test/src/math/differential_testing/log1pf_perf.cpp
    libc/test/src/math/exhaustive/log1pf_test.cpp
    libc/test/src/math/log1pf_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/windows/entrypoints.txt
    libc/spec/stdc.td
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/src/math/generic/common_constants.cpp
    libc/src/math/generic/common_constants.h
    libc/src/math/generic/logf.cpp
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/differential_testing/CMakeLists.txt
    libc/test/src/math/exhaustive/CMakeLists.txt
    libc/utils/MPFRWrapper/MPFRUtils.cpp
    libc/utils/MPFRWrapper/MPFRUtils.h

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f73556afc017b..8575ab8f048da 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -164,6 +164,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ldexpf
     libc.src.math.ldexpl
     libc.src.math.log10f
+    libc.src.math.log1pf
     libc.src.math.log2f
     libc.src.math.logf
     libc.src.math.logb

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index aa3d69a48a546..3de1e6ef72adc 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -163,6 +163,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.llroundf
     libc.src.math.llroundl
     libc.src.math.log10f
+    libc.src.math.log1pf
     libc.src.math.log2f
     libc.src.math.logf
     libc.src.math.logb

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 802f9963935ef..acb5f80daf516 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -118,6 +118,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.llround
     libc.src.math.llroundf
     libc.src.math.llroundl
+    libc.src.math.log1pf
     libc.src.math.log2f
     libc.src.math.logf
     libc.src.math.logb

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 5cae1ddfe0ad0..14b2d4d9f2eb7 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -398,6 +398,8 @@ def StdC : StandardSpec<"stdc"> {
 
           FunctionSpec<"log10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
 
+          FunctionSpec<"log1pf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+
           FunctionSpec<"log2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
 
           FunctionSpec<"logf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 0ff088e6061ea..b0b27126e6383 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -118,6 +118,8 @@ add_math_entrypoint_object(ldexpl)
 
 add_math_entrypoint_object(log10f)
 
+add_math_entrypoint_object(log1pf)
+
 add_math_entrypoint_object(log2f)
 
 add_math_entrypoint_object(logf)

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 94336d9177ac1..c2cc79f2004dc 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -670,6 +670,19 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  log1pf
+  SRCS
+    log1pf.cpp
+  HDRS
+    ../log1pf.h
+  DEPENDS
+    .common_constants
+    libc.src.__support.FPUtil.fputil
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   log2f
   SRCS

diff  --git a/libc/src/math/generic/common_constants.cpp b/libc/src/math/generic/common_constants.cpp
index eb40768ff52b0..2c7db2462db7f 100644
--- a/libc/src/math/generic/common_constants.cpp
+++ b/libc/src/math/generic/common_constants.cpp
@@ -56,4 +56,50 @@ const double ONE_OVER_F[128] = {
     0x1.05197f7d73404p-1, 0x1.0410410410410p-1, 0x1.03091b51f5e1ap-1,
     0x1.0204081020408p-1, 0x1.0101010101010p-1};
 
+// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127.
+const double LOG_F[128] = {
+    0x0.0000000000000p+0, 0x1.fe02a6b106788p-8, 0x1.fc0a8b0fc03e3p-7,
+    0x1.7b91b07d5b11ap-6, 0x1.f829b0e783300p-6, 0x1.39e87b9febd5fp-5,
+    0x1.77458f632dcfcp-5, 0x1.b42dd711971bep-5, 0x1.f0a30c01162a6p-5,
+    0x1.16536eea37ae0p-4, 0x1.341d7961bd1d0p-4, 0x1.51b073f06183fp-4,
+    0x1.6f0d28ae56b4bp-4, 0x1.8c345d6319b20p-4, 0x1.a926d3a4ad563p-4,
+    0x1.c5e548f5bc743p-4, 0x1.e27076e2af2e5p-4, 0x1.fec9131dbeabap-4,
+    0x1.0d77e7cd08e59p-3, 0x1.1b72ad52f67a0p-3, 0x1.29552f81ff523p-3,
+    0x1.371fc201e8f74p-3, 0x1.44d2b6ccb7d1ep-3, 0x1.526e5e3a1b437p-3,
+    0x1.5ff3070a793d3p-3, 0x1.6d60fe719d21cp-3, 0x1.7ab890210d909p-3,
+    0x1.87fa06520c910p-3, 0x1.9525a9cf456b4p-3, 0x1.a23bc1fe2b563p-3,
+    0x1.af3c94e80bff2p-3, 0x1.bc286742d8cd6p-3, 0x1.c8ff7c79a9a21p-3,
+    0x1.d5c216b4fbb91p-3, 0x1.e27076e2af2e5p-3, 0x1.ef0adcbdc5936p-3,
+    0x1.fb9186d5e3e2ap-3, 0x1.0402594b4d040p-2, 0x1.0a324e27390e3p-2,
+    0x1.1058bf9ae4ad5p-2, 0x1.1675cababa60ep-2, 0x1.1c898c16999fap-2,
+    0x1.22941fbcf7965p-2, 0x1.2895a13de86a3p-2, 0x1.2e8e2bae11d30p-2,
+    0x1.347dd9a987d54p-2, 0x1.3a64c556945e9p-2, 0x1.404308686a7e3p-2,
+    0x1.4618bc21c5ec2p-2, 0x1.4be5f957778a0p-2, 0x1.51aad872df82dp-2,
+    0x1.5767717455a6cp-2, 0x1.5d1bdbf5809cap-2, 0x1.62c82f2b9c795p-2,
+    0x1.686c81e9b14aep-2, 0x1.6e08eaa2ba1e3p-2, 0x1.739d7f6bbd006p-2,
+    0x1.792a55fdd47a2p-2, 0x1.7eaf83b82afc3p-2, 0x1.842d1da1e8b17p-2,
+    0x1.89a3386c1425ap-2, 0x1.8f11e873662c7p-2, 0x1.947941c2116fap-2,
+    0x1.99d958117e08ap-2, 0x1.9f323ecbf984bp-2, 0x1.a484090e5bb0ap-2,
+    0x1.a9cec9a9a0849p-2, 0x1.af1293247786bp-2, 0x1.b44f77bcc8f62p-2,
+    0x1.b9858969310fbp-2, 0x1.beb4d9da71b7bp-2, 0x1.c3dd7a7cdad4dp-2,
+    0x1.c8ff7c79a9a21p-2, 0x1.ce1af0b85f3ebp-2, 0x1.d32fe7e00ebd5p-2,
+    0x1.d83e7258a2f3ep-2, 0x1.dd46a04c1c4a0p-2, 0x1.e24881a7c6c26p-2,
+    0x1.e744261d68787p-2, 0x1.ec399d2468cc0p-2, 0x1.f128f5faf06ecp-2,
+    0x1.f6123fa7028acp-2, 0x1.faf588f78f31ep-2, 0x1.ffd2e0857f498p-2,
+    0x1.02552a5a5d0fep-1, 0x1.04bdf9da926d2p-1, 0x1.0723e5c1cdf40p-1,
+    0x1.0986f4f573520p-1, 0x1.0be72e4252a82p-1, 0x1.0e44985d1cc8bp-1,
+    0x1.109f39e2d4c96p-1, 0x1.12f719593efbcp-1, 0x1.154c3d2f4d5e9p-1,
+    0x1.179eabbd899a0p-1, 0x1.19ee6b467c96ep-1, 0x1.1c3b81f713c24p-1,
+    0x1.1e85f5e7040d0p-1, 0x1.20cdcd192ab6dp-1, 0x1.23130d7bebf42p-1,
+    0x1.2555bce98f7cbp-1, 0x1.2795e1289b11ap-1, 0x1.29d37fec2b08ap-1,
+    0x1.2c0e9ed448e8bp-1, 0x1.2e47436e40268p-1, 0x1.307d7334f10bep-1,
+    0x1.32b1339121d71p-1, 0x1.34e289d9ce1d3p-1, 0x1.37117b54747b5p-1,
+    0x1.393e0d3562a19p-1, 0x1.3b68449fffc22p-1, 0x1.3d9026a7156fap-1,
+    0x1.3fb5b84d16f42p-1, 0x1.41d8fe84672aep-1, 0x1.43f9fe2f9ce67p-1,
+    0x1.4618bc21c5ec2p-1, 0x1.48353d1ea88dfp-1, 0x1.4a4f85db03ebbp-1,
+    0x1.4c679afccee39p-1, 0x1.4e7d811b75bb0p-1, 0x1.50913cc01686bp-1,
+    0x1.52a2d265bc5aap-1, 0x1.54b2467999497p-1, 0x1.56bf9d5b3f399p-1,
+    0x1.58cadb5cd7989p-1, 0x1.5ad404c359f2cp-1, 0x1.5cdb1dc6c1764p-1,
+    0x1.5ee02a9241675p-1, 0x1.60e32f44788d8p-1};
+
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/common_constants.h b/libc/src/math/generic/common_constants.h
index b68de29a60771..5f91038611992 100644
--- a/libc/src/math/generic/common_constants.h
+++ b/libc/src/math/generic/common_constants.h
@@ -14,6 +14,9 @@ namespace __llvm_libc {
 // Lookup table for (1/f) where f = 1 + n*2^(-7), n = 0..127.
 extern const double ONE_OVER_F[128];
 
+// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127.
+extern const double LOG_F[128];
+
 } // namespace __llvm_libc
 
 #endif // LLVM_LIBC_SRC_MATH_GENERIC_COMMON_CONSTANTS_H

diff  --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
new file mode 100644
index 0000000000000..25e5271266ead
--- /dev/null
+++ b/libc/src/math/generic/log1pf.cpp
@@ -0,0 +1,166 @@
+//===-- Single-precision log1p(x) function --------------------------------===//
+//
+// 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/math/log1pf.h"
+#include "common_constants.h" // Lookup table for (1/f) and log(f)
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/FEnvUtils.h"
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/common.h"
+
+// This is an algorithm for log10(x) in single precision which is
+// correctly rounded for all rounding modes.
+// - An exhaustive test show that when x >= 2^45, log1pf(x) == logf(x)
+// for all rounding modes.
+// - When 2^(-8) <= |x| < 2^45, the sum (double(x) + 1.0) is exact,
+// so we can adapt the correctly rounded algorithm of logf to compute
+// log(double(x) + 1.0) correctly.  For more information about the logf
+// algorithm, see `libc/src/math/generic/logf.cpp`.
+// - When |x| < 2^(-8), we use a degree-6 polynomial in double precision
+// generated with Sollya using the following command:
+//   fpminimax(log(1 + x)/x, 5, [|D...|], [-2^-8; 2^-8]);
+
+namespace __llvm_libc {
+
+namespace internal {
+
+// We don't need to treat denormal
+INLINE_FMA static inline float log(double x) {
+  constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
+
+  using FPBits = typename fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  if (xbits.is_zero()) {
+    return static_cast<float>(fputil::FPBits<float>::neg_inf());
+  }
+
+  if (xbits.uintval() > FPBits::MAX_NORMAL) {
+    if (xbits.get_sign() && !xbits.is_nan()) {
+      return FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1));
+    }
+    return static_cast<float>(x);
+  }
+
+  double m = static_cast<double>(xbits.get_exponent());
+
+  // Set bits to 1.m
+  xbits.set_unbiased_exponent(0x3FF);
+  // Get the 8 highest bits, use 7 bits (excluding the implicit hidden bit) for
+  // lookup tables.
+  int f_index =
+      xbits.get_mantissa() >> 45; // fputil::MantissaWidth<double>::VALUE - 7
+
+  FPBits f(xbits.val);
+  // Clear the lowest 45 bits.
+  f.bits &= ~0x0000'1FFF'FFFF'FFFFULL;
+
+  double d = static_cast<double>(xbits) - static_cast<double>(f);
+  d *= ONE_OVER_F[f_index];
+
+  double extra_factor = fputil::fma(m, LOG_2, LOG_F[f_index]);
+
+  double r = fputil::polyeval(d, extra_factor, 0x1.fffffffffffacp-1,
+                              -0x1.fffffffef9cb2p-2, 0x1.5555513bc679ap-2,
+                              -0x1.fff4805ea441p-3, 0x1.930180dbde91ap-3);
+
+  return static_cast<float>(r);
+}
+
+} // namespace internal
+
+INLINE_FMA
+LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
+  using FPBits = typename fputil::FPBits<float>;
+  FPBits xbits(x);
+  double xd = static_cast<double>(x);
+
+  if (xbits.get_exponent() >= -8) {
+    // Hard-to-round cases.
+    switch (xbits.uintval()) {
+    case 0x3b9315c8U: // x = 0x1.262b9p-8f
+      if (fputil::get_round() != FE_UPWARD)
+        return 0x1.25830cp-8f;
+      break;
+    case 0x3c6eb7afU: // x = 0x1.dd6f5ep-7f
+      if (fputil::get_round() == FE_UPWARD)
+        return 0x1.d9fd86p-7f;
+      return 0x1.d9fd84p-7f;
+    case 0x41078febU: // x = 0x1.0f1fd6p+3f
+      if (fputil::get_round() != FE_UPWARD)
+        return 0x1.1fcbcep+1f;
+      break;
+    case 0x5cd69e88U: // x = 0x1.ad3d1p+58f
+      if (fputil::get_round() != FE_UPWARD)
+        return 0x1.45c146p+5f;
+      break;
+    case 0x65d890d3U: // x = 0x1.b121a6p+76f
+      if (fputil::get_round() == FE_TONEAREST)
+        return 0x1.a9a3f2p+5f;
+      break;
+    case 0x6f31a8ecU: // x = 0x1.6351d8p+95f
+      if (fputil::get_round() == FE_TONEAREST)
+        return 0x1.08b512p+6f;
+      break;
+    case 0x7a17f30aU: // x = 0x1.2fe614p+117f
+      if (fputil::get_round() != FE_UPWARD)
+        return 0x1.451436p+6f;
+      break;
+    case 0xbc4d092cU: // x = -0x1.9a1258p-7f
+      if (fputil::get_round() == FE_TONEAREST)
+        return -0x1.9ca8bep-7f;
+      break;
+    case 0xbc657728U: // x = -0x1.caee5p-7f
+      if (fputil::get_round() != FE_DOWNWARD)
+        return -0x1.ce2cccp-7f;
+      break;
+    case 0xbd1d20afU: // x = -0x1.3a415ep-5f
+      int round_mode = fputil::get_round();
+      if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO)
+        return -0x1.40711p-5f;
+      return -0x1.407112p-5f;
+    }
+
+    return internal::log(xd + 1.0);
+  }
+
+  // Hard-to round cases.
+  switch (xbits.uintval()) {
+  case 0x35400003U: // x = 0x1.800006p-21f
+    if (fputil::get_round() == FE_TONEAREST)
+      return 0x1.7ffffep-21f;
+    break;
+  case 0x3710001bU: // x = 0x1.200036p-17f
+    if (fputil::get_round() == FE_TONEAREST)
+      return 0x1.1fffe6p-17f;
+    break;
+  case 0xb53ffffdU: // x = -0x1.7ffffap-21f
+    if (fputil::get_round() != FE_DOWNWARD)
+      return -0x1.800002p-21f;
+    break;
+  case 0xb70fffe5U: // x = -0x1.1fffcap-17f
+    if (fputil::get_round() != FE_DOWNWARD)
+      return -0x1.20001ap-17f;
+    break;
+  case 0xbb0ec8c4U: // x = -0x1.1d9188p-9f
+    if (fputil::get_round() == FE_TONEAREST)
+      return -0x1.1de14ap-9f;
+    break;
+  }
+
+  double r;
+  // Polymial generated with Sollya:
+  // > fpminimax(log(1 + x)/x, 5, [|D...|], [-2^-8; 2^-8]);
+  r = fputil::polyeval(xd, -0x1p-1, 0x1.5555555515551p-2, -0x1.ffffffff82bdap-3,
+                       0x1.999b33348d3aep-3, -0x1.5556cae3adcc3p-3);
+  return static_cast<float>(fputil::fma(r, xd * xd, xd));
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/math/generic/logf.cpp b/libc/src/math/generic/logf.cpp
index 4a35f49888207..390617bf1ea7f 100644
--- a/libc/src/math/generic/logf.cpp
+++ b/libc/src/math/generic/logf.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logf.h"
-#include "common_constants.h" // Lookup table for (1/f)
+#include "common_constants.h" // Lookup table for (1/f) and log(f)
 #include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FMA.h"
@@ -49,52 +49,6 @@
 
 namespace __llvm_libc {
 
-// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127.
-static constexpr double LOG_F[128] = {
-    0x0.0000000000000p+0, 0x1.fe02a6b106788p-8, 0x1.fc0a8b0fc03e3p-7,
-    0x1.7b91b07d5b11ap-6, 0x1.f829b0e783300p-6, 0x1.39e87b9febd5fp-5,
-    0x1.77458f632dcfcp-5, 0x1.b42dd711971bep-5, 0x1.f0a30c01162a6p-5,
-    0x1.16536eea37ae0p-4, 0x1.341d7961bd1d0p-4, 0x1.51b073f06183fp-4,
-    0x1.6f0d28ae56b4bp-4, 0x1.8c345d6319b20p-4, 0x1.a926d3a4ad563p-4,
-    0x1.c5e548f5bc743p-4, 0x1.e27076e2af2e5p-4, 0x1.fec9131dbeabap-4,
-    0x1.0d77e7cd08e59p-3, 0x1.1b72ad52f67a0p-3, 0x1.29552f81ff523p-3,
-    0x1.371fc201e8f74p-3, 0x1.44d2b6ccb7d1ep-3, 0x1.526e5e3a1b437p-3,
-    0x1.5ff3070a793d3p-3, 0x1.6d60fe719d21cp-3, 0x1.7ab890210d909p-3,
-    0x1.87fa06520c910p-3, 0x1.9525a9cf456b4p-3, 0x1.a23bc1fe2b563p-3,
-    0x1.af3c94e80bff2p-3, 0x1.bc286742d8cd6p-3, 0x1.c8ff7c79a9a21p-3,
-    0x1.d5c216b4fbb91p-3, 0x1.e27076e2af2e5p-3, 0x1.ef0adcbdc5936p-3,
-    0x1.fb9186d5e3e2ap-3, 0x1.0402594b4d040p-2, 0x1.0a324e27390e3p-2,
-    0x1.1058bf9ae4ad5p-2, 0x1.1675cababa60ep-2, 0x1.1c898c16999fap-2,
-    0x1.22941fbcf7965p-2, 0x1.2895a13de86a3p-2, 0x1.2e8e2bae11d30p-2,
-    0x1.347dd9a987d54p-2, 0x1.3a64c556945e9p-2, 0x1.404308686a7e3p-2,
-    0x1.4618bc21c5ec2p-2, 0x1.4be5f957778a0p-2, 0x1.51aad872df82dp-2,
-    0x1.5767717455a6cp-2, 0x1.5d1bdbf5809cap-2, 0x1.62c82f2b9c795p-2,
-    0x1.686c81e9b14aep-2, 0x1.6e08eaa2ba1e3p-2, 0x1.739d7f6bbd006p-2,
-    0x1.792a55fdd47a2p-2, 0x1.7eaf83b82afc3p-2, 0x1.842d1da1e8b17p-2,
-    0x1.89a3386c1425ap-2, 0x1.8f11e873662c7p-2, 0x1.947941c2116fap-2,
-    0x1.99d958117e08ap-2, 0x1.9f323ecbf984bp-2, 0x1.a484090e5bb0ap-2,
-    0x1.a9cec9a9a0849p-2, 0x1.af1293247786bp-2, 0x1.b44f77bcc8f62p-2,
-    0x1.b9858969310fbp-2, 0x1.beb4d9da71b7bp-2, 0x1.c3dd7a7cdad4dp-2,
-    0x1.c8ff7c79a9a21p-2, 0x1.ce1af0b85f3ebp-2, 0x1.d32fe7e00ebd5p-2,
-    0x1.d83e7258a2f3ep-2, 0x1.dd46a04c1c4a0p-2, 0x1.e24881a7c6c26p-2,
-    0x1.e744261d68787p-2, 0x1.ec399d2468cc0p-2, 0x1.f128f5faf06ecp-2,
-    0x1.f6123fa7028acp-2, 0x1.faf588f78f31ep-2, 0x1.ffd2e0857f498p-2,
-    0x1.02552a5a5d0fep-1, 0x1.04bdf9da926d2p-1, 0x1.0723e5c1cdf40p-1,
-    0x1.0986f4f573520p-1, 0x1.0be72e4252a82p-1, 0x1.0e44985d1cc8bp-1,
-    0x1.109f39e2d4c96p-1, 0x1.12f719593efbcp-1, 0x1.154c3d2f4d5e9p-1,
-    0x1.179eabbd899a0p-1, 0x1.19ee6b467c96ep-1, 0x1.1c3b81f713c24p-1,
-    0x1.1e85f5e7040d0p-1, 0x1.20cdcd192ab6dp-1, 0x1.23130d7bebf42p-1,
-    0x1.2555bce98f7cbp-1, 0x1.2795e1289b11ap-1, 0x1.29d37fec2b08ap-1,
-    0x1.2c0e9ed448e8bp-1, 0x1.2e47436e40268p-1, 0x1.307d7334f10bep-1,
-    0x1.32b1339121d71p-1, 0x1.34e289d9ce1d3p-1, 0x1.37117b54747b5p-1,
-    0x1.393e0d3562a19p-1, 0x1.3b68449fffc22p-1, 0x1.3d9026a7156fap-1,
-    0x1.3fb5b84d16f42p-1, 0x1.41d8fe84672aep-1, 0x1.43f9fe2f9ce67p-1,
-    0x1.4618bc21c5ec2p-1, 0x1.48353d1ea88dfp-1, 0x1.4a4f85db03ebbp-1,
-    0x1.4c679afccee39p-1, 0x1.4e7d811b75bb0p-1, 0x1.50913cc01686bp-1,
-    0x1.52a2d265bc5aap-1, 0x1.54b2467999497p-1, 0x1.56bf9d5b3f399p-1,
-    0x1.58cadb5cd7989p-1, 0x1.5ad404c359f2cp-1, 0x1.5cdb1dc6c1764p-1,
-    0x1.5ee02a9241675p-1, 0x1.60e32f44788d8p-1};
-
 INLINE_FMA
 LLVM_LIBC_FUNCTION(float, logf, (float x)) {
   constexpr double LOG_2 = 0x1.62e42fefa39efp-1;

diff  --git a/libc/src/math/log1pf.h b/libc/src/math/log1pf.h
new file mode 100644
index 0000000000000..ea00377f8e2ad
--- /dev/null
+++ b/libc/src/math/log1pf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for log1pf ------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_LOG1PF_H
+#define LLVM_LIBC_SRC_MATH_LOG1PF_H
+
+namespace __llvm_libc {
+
+float log1pf(float x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_LOG1PF_H

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index fa2a336e75d17..0b7ce71d0095c 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1254,6 +1254,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fputil
 )
 
+add_fp_unittest(
+  log1pf_test
+  NEED_MPFR
+  SUITE
+    libc_math_unittests
+  SRCS
+    log1pf_test.cpp
+  DEPENDS
+    libc.include.math
+    libc.src.math.log1pf
+    libc.src.__support.FPUtil.fputil
+)
+
 add_subdirectory(generic)
 add_subdirectory(exhaustive)
 add_subdirectory(
diff erential_testing)

diff  --git a/libc/test/src/math/
diff erential_testing/CMakeLists.txt b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
index 79aca4b3a0808..59b6cec52a36f 100644
--- a/libc/test/src/math/
diff erential_testing/CMakeLists.txt
+++ b/libc/test/src/math/
diff erential_testing/CMakeLists.txt
@@ -252,6 +252,17 @@ add_
diff _binary(
     -fno-builtin
 )
 
+add_
diff _binary(
+  log1pf_perf
+  SRCS
+    log1pf_perf.cpp
+  DEPENDS
+    .single_input_single_output_
diff 
+    libc.src.math.log1pf
+  COMPILE_OPTIONS
+    -fno-builtin
+)
+
 add_
diff _binary(
   log2f_
diff 
   SRCS

diff  --git a/libc/test/src/math/
diff erential_testing/log1pf_perf.cpp b/libc/test/src/math/
diff erential_testing/log1pf_perf.cpp
new file mode 100644
index 0000000000000..9904aba10d35e
--- /dev/null
+++ b/libc/test/src/math/
diff erential_testing/log1pf_perf.cpp
@@ -0,0 +1,16 @@
+//===-- Differential test for log1pf --------------------------------------===//
+//
+// 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 "SingleInputSingleOutputDiff.h"
+
+#include "src/math/log1pf.h"
+
+#include <math.h>
+
+SINGLE_INPUT_SINGLE_OUTPUT_PERF(float, __llvm_libc::log1pf, ::log1pf,
+                                "log1pf_perf.log")

diff  --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index f089bfb112e1a..56931a0340656 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -96,6 +96,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  log1pf_test
+  NO_RUN_POSTBUILD
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    log1pf_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.include.math
+    libc.src.math.log1pf
+    libc.src.__support.FPUtil.fputil
+  LINK_OPTIONS
+    -lpthread
+)
+
 add_fp_unittest(
   log2f_test
   NO_RUN_POSTBUILD

diff  --git a/libc/test/src/math/exhaustive/log1pf_test.cpp b/libc/test/src/math/exhaustive/log1pf_test.cpp
new file mode 100644
index 0000000000000..5f2705a522dea
--- /dev/null
+++ b/libc/test/src/math/exhaustive/log1pf_test.cpp
@@ -0,0 +1,55 @@
+//===-- Exhaustive test for log1pf ----------------------------------------===//
+//
+// 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/__support/FPUtil/FPBits.h"
+#include "src/math/log1pf.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include "utils/UnitTest/FPMatcher.h"
+
+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,
+             mpfr::RoundingMode rounding) override {
+    mpfr::ForceRoundingMode r(rounding);
+    uint32_t bits = start;
+    do {
+      FPBits xbits(bits);
+      float x = float(xbits);
+      EXPECT_MPFR_MATCH(mpfr::Operation::Log1p, x, __llvm_libc::log1pf(x), 0.5,
+                        rounding);
+    } while (bits++ < stop);
+  }
+};
+
+// Range: All non-negative;
+static constexpr uint32_t START = 0x0000'0000U;
+static constexpr uint32_t STOP = 0x7f80'0000U;
+// Range: [-1, 0];
+// static constexpr uint32_t START = 0x8000'0000U;
+// static constexpr uint32_t STOP  = 0xbf80'0000U;
+static constexpr int NUM_THREADS = 16;
+
+TEST_F(LlvmLibclog1pfExhaustiveTest, RoundNearestTieToEven) {
+  test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Nearest);
+}
+
+TEST_F(LlvmLibclog1pfExhaustiveTest, RoundUp) {
+  test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
+}
+
+TEST_F(LlvmLibclog1pfExhaustiveTest, RoundDown) {
+  test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Downward);
+}
+
+TEST_F(LlvmLibclog1pfExhaustiveTest, RoundTowardZero) {
+  test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::TowardZero);
+}

diff  --git a/libc/test/src/math/log1pf_test.cpp b/libc/test/src/math/log1pf_test.cpp
new file mode 100644
index 0000000000000..785d77f317df6
--- /dev/null
+++ b/libc/test/src/math/log1pf_test.cpp
@@ -0,0 +1,81 @@
+//===-- Unittests for log1pf ----------------------------------------------===//
+//
+// 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/log1pf.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(LlvmLibclog1pfTest, SpecialNumbers) {
+  EXPECT_FP_EQ(aNaN, __llvm_libc::log1pf(aNaN));
+  EXPECT_FP_EQ(inf, __llvm_libc::log1pf(inf));
+  EXPECT_TRUE(FPBits((__llvm_libc::log1pf(neg_inf))).is_nan());
+  EXPECT_FP_EQ(zero, __llvm_libc::log1pf(0.0f));
+  EXPECT_FP_EQ(neg_zero, __llvm_libc::log1pf(-0.0f));
+  EXPECT_FP_EQ(neg_inf, __llvm_libc::log1pf(-1.0f));
+}
+
+TEST(LlvmLibclog1pfTest, TrickyInputs) {
+  constexpr int N = 20;
+  constexpr uint32_t INPUTS[N] = {
+      0x35c00006U, /*0x1.80000cp-20f*/
+      0x35400003U, /*0x1.800006p-21f*/
+      0x3640000cU, /*0x1.800018p-19f*/
+      0x36c00018U, /*0x1.80003p-18f*/
+      0x3710001bU, /*0x1.200036p-17f*/
+      0x37400030U, /*0x1.80006p-17f*/
+      0x3770004bU, /*0x1.e00096p-17f*/
+      0x3b9315c8U, /*0x1.262b9p-8f*/
+      0x3c6eb7afU, /*0x1.dd6f5ep-7f*/
+      0x41078febU, /*0x1.0f1fd6p+3f*/
+      0x5cd69e88U, /*0x1.ad3d1p+58f*/
+      0x65d890d3U, /*0x1.b121a6p+76f*/
+      0x6f31a8ecU, /*0x1.6351d8p+95f*/
+      0x7a17f30aU, /*0x1.2fe614p+117f*/
+      0xb53ffffdU, /*-0x1.7ffffap-21f*/
+      0xb70fffe5U, /*-0x1.1fffcap-17f*/
+      0xbb0ec8c4U, /*-0x1.1d9188p-9f*/
+      0xbc4d092cU, /*-0x1.9a1258p-7f*/
+      0xbc657728U, /*-0x1.caee5p-7f*/
+      0xbd1d20afU, /*-0x1.3a415ep-5f*/
+  };
+  for (int i = 0; i < N; ++i) {
+    float x = float(FPBits(INPUTS[i]));
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log1p, x,
+                                   __llvm_libc::log1pf(x), 0.5);
+  }
+}
+
+TEST(LlvmLibclog1pfTest, 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::log1pf(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_ALL_ROUNDING(mpfr::Operation::Log1p, x,
+                                   __llvm_libc::log1pf(x), 0.5);
+  }
+}

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index c6470e23451e0..3c57211bc35a4 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -279,6 +279,12 @@ class MPFRNumber {
     return result;
   }
 
+  MPFRNumber log1p() const {
+    MPFRNumber result(*this);
+    mpfr_log1p(result.value, value, mpfr_rounding);
+    return result;
+  }
+
   MPFRNumber remquo(const MPFRNumber &divisor, int &quotient) {
     MPFRNumber remainder(*this);
     long q;
@@ -510,6 +516,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
     return mpfrInput.log2();
   case Operation::Log10:
     return mpfrInput.log10();
+  case Operation::Log1p:
+    return mpfrInput.log1p();
   case Operation::Mod2PI:
     return mpfrInput.mod_2pi();
   case Operation::ModPIOver2:

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 2a13ab1d11608..2896973a33c2f 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -33,6 +33,7 @@ enum class Operation : int {
   Log,
   Log2,
   Log10,
+  Log1p,
   Mod2PI,
   ModPIOver2,
   ModPIOver4,


        


More information about the libc-commits mailing list