[libc-commits] [libc] [libc][math][c23] Add log2p1f16 C23 math function (PR #186754)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 16 02:09:01 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Shikhar Soni (shikharish)

<details>
<summary>Changes</summary>



---

Patch is 23.75 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/186754.diff


24 Files Affected:

- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+1) 
- (modified) libc/config/darwin/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/gpu/amdgpu/entrypoints.txt (+1) 
- (modified) libc/config/gpu/nvptx/entrypoints.txt (+1) 
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/docs/headers/math/index.rst (+1-1) 
- (modified) libc/include/math.yaml (+7) 
- (modified) libc/src/__support/math/CMakeLists.txt (+18) 
- (added) libc/src/__support/math/log2p1f16.h (+205) 
- (modified) libc/src/math/CMakeLists.txt (+1) 
- (modified) libc/src/math/generic/CMakeLists.txt (+11) 
- (added) libc/src/math/generic/log2p1f16.cpp (+18) 
- (added) libc/src/math/log2p1f16.h (+21) 
- (modified) libc/test/src/math/CMakeLists.txt (+11) 
- (added) libc/test/src/math/log2p1f16_test.cpp (+49) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+13) 
- (added) libc/test/src/math/smoke/log2p1f16_test.cpp (+48) 
- (modified) libc/utils/MPFRWrapper/MPCommon.cpp (+16) 
- (modified) libc/utils/MPFRWrapper/MPCommon.h (+1) 
- (modified) libc/utils/MPFRWrapper/MPFRUtils.cpp (+2) 
- (modified) libc/utils/MPFRWrapper/MPFRUtils.h (+1) 


``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index c4e040e537cd0..e4589dd2cd1b6 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -656,6 +656,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index fc6201b44149f..84035c7dedd15 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -663,6 +663,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 2d26763e29d03..3f2fa54e5ddda 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -476,6 +476,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 98c27d300ad9d..65e3e7a273bfe 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -586,6 +586,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 532fcda9ab3be..3180ecce06d83 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -588,6 +588,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 380c91e1ac396..baf74b07387d5 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -738,6 +738,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.llrintf16
     libc.src.math.llroundf16
     libc.src.math.log10p1f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.lrintf16
     libc.src.math.lroundf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index ddb24c3eea178..c22e00a325ca9 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -756,6 +756,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9102de9bf0417..c0eb9c6025231 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -805,6 +805,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.log10f16
     libc.src.math.log10p1f16
     libc.src.math.log2f16
+    libc.src.math.log2p1f16
     libc.src.math.logbf16
     libc.src.math.logf16
     libc.src.math.lrintf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 8692bf37c05b1..794f1a0ddfbab 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -329,7 +329,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | log2      | |check|          | |check|         |                        | |check|              |                        |                        | 7.12.6.15              | F.10.3.15                  |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| log2p1    |                  |                 |                        |                      |                        |                        | 7.12.6.16              | F.10.3.16                  |
+| log2p1    |                  |                 |                        | |check|              |                        |                        | 7.12.6.16              | F.10.3.16                  |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | logp1     |                  |                 |                        |                      |                        |                        | 7.12.6.14              | F.10.3.14                  |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index 8cf29c1a8cff1..72fe15a5f068f 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -1791,6 +1791,13 @@ functions:
     arguments:
       - type: _Float16
     guard: LIBC_TYPES_HAS_FLOAT16
+  - name: log2p1f16
+    standards:
+      - stdc
+    return_type: _Float16
+    arguments:
+      - type: _Float16
+    guard: LIBC_TYPES_HAS_FLOAT16
   - name: log1p
     standards:
       - stdc
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 8bb935d863a9b..a3308bdf4f116 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2674,6 +2674,24 @@ add_header_library(
     libc.src.__support.macros.properties.cpu_features
 )
 
+add_header_library(
+  log2p1f16
+  HDRS
+    log2p1f16.h
+  DEPENDS
+    .expxf16_utils
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.cpu_features
+)
+
 add_header_library(
   log10f
   HDRS
diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h
new file mode 100644
index 0000000000000..0742915a92898
--- /dev/null
+++ b/libc/src/__support/math/log2p1f16.h
@@ -0,0 +1,205 @@
+//===-- Implementation header for log2p1f16 ---------------------*- 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___SUPPORT_MATH_LOG2P1F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "expxf16_utils.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 log2p1f16(float16 x) {
+  using namespace math::expxf16_internal;
+  using FPBits = fputil::FPBits<float16>;
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  // If x is +-0, NaN, +/-inf, or |x| <= 2^-3.
+  if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
+    // log2p1(NaN) = NaN
+    if (x_bits.is_nan()) {
+      if (x_bits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
+      return x;
+    }
+
+    // log2p1(+/-0) = +/-0
+    if (x_abs == 0U)
+      return x;
+
+    // log2p1(+inf) = +inf
+    if (x_u == 0x7c00U)
+      return FPBits::inf().get_val();
+
+    // log2p1(-inf) = NaN
+    if (x_abs >= 0x7c00U) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
+    // When |x| <= 2^-3, use a degree-5 minimax polynomial on x.
+    // For y = 1+x near 1, the table-based range reduction suffers from
+    // catastrophic cancellation (m + log2(f) nearly cancel).  Computing
+    // log2(1+x) directly via polynomial avoids this.
+    //
+    // Generated by Sollya with:
+    //   > display = hexadecimal;
+    //   > Q = fpminimax(log2(1 + x), [|1, 2, 3, 4, 5|], [|SG...|],
+    //                   [-2^-3, 2^-3]);
+    //   > Q;
+    if (x_abs <= 0x3000U) {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+      constexpr size_t N_LOG2P1F16_EXCEPTS = 11;
+      constexpr fputil::ExceptValues<float16, N_LOG2P1F16_EXCEPTS>
+          LOG2P1F16_EXCEPTS = {{
+              // (input, RZ output, RU offset, RD offset, RN offset)
+              // x = 0x1.ec4p-12
+              {0x0FB1U, 0x118BU, 1U, 0U, 1U},
+              // x = 0x1.c04p-6
+              {0x2701U, 0x28FBU, 1U, 0U, 1U},
+              // x = -0x1.824p-15
+              {0x8309U, 0x8461U, 0U, 1U, 0U},
+              // x = -0x1.414p-10
+              {0x9505U, 0x973EU, 0U, 1U, 1U},
+              // x = -0x1.cb8p-10
+              {0x972EU, 0x992FU, 0U, 1U, 0U},
+              // x = -0x1.99cp-8
+              {0x9E67U, 0xA0A2U, 0U, 1U, 0U},
+              // x = -0x1.ce8p-7
+              {0xA33AU, 0xA540U, 0U, 1U, 0U},
+              // x = -0x1.73cp-6
+              {0xA5CFU, 0xA83DU, 0U, 1U, 0U},
+              // x = -0x1.87p-5
+              {0xAA1CU, 0xAC84U, 0U, 1U, 0U},
+              // x = -0x1.d48p-5
+              {0xAB52U, 0xAD70U, 0U, 1U, 0U},
+              // x = -0x1.da0p-4
+              {0xAF68U, 0xB1ADU, 0U, 1U, 0U},
+          }};
+
+      if (auto r = LOG2P1F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+        return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+      float xf = x;
+      return fputil::cast<float16>(
+          xf * fputil::polyeval(xf, 0x1.715476p+0f, -0x1.715204p-1f,
+                                0x1.ec6c5p-2f, -0x1.763338p-2f,
+                                0x1.2bcd3cp-2f));
+    }
+  }
+
+  // log2p1(-1) = -inf
+  if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
+    fputil::raise_except_if_required(FE_DIVBYZERO);
+    return FPBits::inf(Sign::NEG).get_val();
+  }
+
+  // log2p1(x) = NaN for x < -1
+  if (LIBC_UNLIKELY(x_u > 0xbc00U)) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
+  }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+  constexpr size_t N_LOG2P1F16_EXCEPTS_HI = 2;
+#else
+  constexpr size_t N_LOG2P1F16_EXCEPTS_HI = 6;
+#endif
+  constexpr fputil::ExceptValues<float16, N_LOG2P1F16_EXCEPTS_HI>
+      LOG2P1F16_EXCEPTS_HI = {{
+  // (input, RZ output, RU offset, RD offset, RN offset)
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+          // x = 0x1.12p-3
+          {0x3048U, 0x31CBU, 1U, 0U, 1U},
+          // x = 0x1.598p-2
+          {0x3566U, 0x36B5U, 1U, 0U, 1U},
+#endif
+          // x = 0x1.23cp-3
+          {0x308FU, 0x3226U, 1U, 0U, 0U},
+          // x = 0x1.accp+0
+          {0x3EB3U, 0x3DADU, 1U, 0U, 0U},
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+          // x = -0x1.534p-2
+          {0xB54DU, 0xB8A5U, 0U, 1U, 0U},
+          // x = -0x1.bb8p-2
+          {0xB6EEU, 0xBA8DU, 0U, 1U, 0U},
+#endif
+      }};
+
+  if (auto r = LOG2P1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+    return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+  // For the range reduction, we compute y = 1 + x in float.  For |x| > 2^-3,
+  // the addition 1.0f + xf is exact in float, and the result y is bounded away
+  // from 1.0, avoiding catastrophic cancellation in the table decomposition.
+  float xf = x;
+  float y = 1.0f + xf;
+
+  using FPBitsFloat = fputil::FPBits<float>;
+  FPBitsFloat y_bits(y);
+
+  // y = 1 + x should stay positive for x > -1, but keep this guard for
+  // completeness in case of unexpected intermediate behavior.
+  if (LIBC_UNLIKELY(y_bits.is_zero()))
+    return FPBits::inf(Sign::NEG).get_val();
+
+  int m = y_bits.get_exponent();
+  y_bits.set_biased_exponent(FPBitsFloat::EXP_BIAS);
+  float mant_f = y_bits.get_val();
+
+  // Leading 23 - 5 = 18, so the top 5 mantissa bits give the index in [0, 31].
+  int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5);
+
+  // v = 1.mant * 1/f - 1 = d/f
+  float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
+
+  // Degree-3 minimax polynomial generated by Sollya with the following
+  // commands:
+  //   > display = hexadecimal;
+  //   > P = fpminimax(log2(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+  //   > x * P;
+  float log2p1_d_over_f =
+      v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f);
+  float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f;
+  return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 98dba11f34c86..59291b1811c78 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -396,6 +396,7 @@ add_math_entrypoint_object(log1pf)
 add_math_entrypoint_object(log2)
 add_math_entrypoint_object(log2f)
 add_math_entrypoint_object(log2f16)
+add_math_entrypoint_object(log2p1f16)
 
 add_math_entrypoint_object(log)
 add_math_entrypoint_object(logf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 07c48c9a04c98..a305bc2ef5334 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1859,6 +1859,17 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  log2p1f16
+  SRCS
+    log2p1f16.cpp
+  HDRS
+    ../log2p1f16.h
+  DEPENDS
+    libc.src.__support.math.log2p1f16
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   log1p
   SRCS
diff --git a/libc/src/math/generic/log2p1f16.cpp b/libc/src/math/generic/log2p1f16.cpp
new file mode 100644
index 0000000000000..a4382d9d1e2c7
--- /dev/null
+++ b/libc/src/math/generic/log2p1f16.cpp
@@ -0,0 +1,18 @@
+//===-- Half-precision log2(1+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/log2p1f16.h"
+#include "src/__support/math/log2p1f16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, log2p1f16, (float16 x)) {
+  return math::log2p1f16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/log2p1f16.h b/libc/src/math/log2p1f16.h
new file mode 100644
index 0000000000000..88b2510572c3d
--- /dev/null
+++ b/libc/src/math/log2p1f16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for log2p1f16 ---------------------*- 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_LOG2P1F16_H
+#define LLVM_LIBC_SRC_MATH_LOG2P1F16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 log2p1f16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOG2P1F16_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2710f4cbdbe31..430bea81d3d81 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2215,6 +2215,17 @@ add_fp_unittest(
     libc.src.math.log10p1f16
 )
 
+add_fp_unittest(
+  log2p1f16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    log2p1f16_test.cpp
+  DEPENDS
+    libc.src.math.log2p1f16
+)
+
 add_fp_unittest(
 log1p_test
  NEED_MPFR
diff --git a/libc/test/src/math/log2p1f16_test.cpp b/libc/test/src/math/log2p1f16_test.cpp
new file mode 100644
index 0000000000000..98cf1dfbf0ea4
--- /dev/null
+++ b/libc/test/src/math/log2p1f16_test.cpp
@@ -0,0 +1,49 @@
+//===-- Exhaustive test for log2p1f16 -------------------------------------===//
+//
+// 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/macros/optimization.h"
+#include "src/math/log2p1f16.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 // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+using LlvmLibcLog2p1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf];
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+// Range: [-1, 0];
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xbc00U;
+
+TEST_F(LlvmLibcLog2p1f16Test, PositiveRange) {
+  for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2p1, x,
+                                   LIBC_NAMESPACE::log2p1f16(x),
+                                   TOLERANCE + 0.5);
+  }
+}
+
+TEST_F(LlvmLibcLog2p1f16Test, NegativeRange) {
+  for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2p1, x,
+                                   LIBC_NAMESPACE::log2p1f16(x),
+                                   TOLERANCE + 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index d92e6b728b63e..9bb38d96df50a 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4483,6 +4483,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.cast
 )
 
+add_fp_unittest(
+  log2p1f16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    log2p1f16_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.math.log2p1f16
+    libc.src.__support.FPUtil.cast
+)
+
 add_fp_unittest(
   log1p_test
   SUITE
diff --git a/libc/test/src/math/smoke/log2p1f16_test.cpp b/libc/test/src/math/smoke/log2p1f16_test.cpp
new file mode 100644
index 0000000000000..d7b8826552e23
--- /dev/null
+++ b/libc/test/src/math/smoke/log2p1f16_test.cpp
@@ -0,0 +1,48 @@
+//===-- Unittests for log2p1f16 -------------------------------------------===//
+//
+// Part of th...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/186754


More information about the libc-commits mailing list