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

Shikhar Soni via libc-commits libc-commits at lists.llvm.org
Sun Mar 22 03:13:44 PDT 2026


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

>From 59e2833477c8f329c3f70931828da41b331d6e0f Mon Sep 17 00:00:00 2001
From: Shikhar Soni <shikharish05 at gmail.com>
Date: Mon, 16 Mar 2026 14:36:03 +0530
Subject: [PATCH 1/2] [libc][math][c23] Add log2p1f16 C23 math function

Signed-off-by: Shikhar Soni <shikharish05 at gmail.com>
---
 libc/config/baremetal/aarch64/entrypoints.txt |   1 +
 libc/config/baremetal/riscv/entrypoints.txt   |   1 +
 libc/config/darwin/aarch64/entrypoints.txt    |   1 +
 libc/config/gpu/amdgpu/entrypoints.txt        |   1 +
 libc/config/gpu/nvptx/entrypoints.txt         |   1 +
 libc/config/linux/aarch64/entrypoints.txt     |   1 +
 libc/config/linux/riscv/entrypoints.txt       |   1 +
 libc/config/linux/x86_64/entrypoints.txt      |   1 +
 libc/docs/headers/math/index.rst              |   2 +-
 libc/include/math.yaml                        |   7 +
 libc/src/__support/math/CMakeLists.txt        |  18 ++
 libc/src/__support/math/log2p1f16.h           | 205 ++++++++++++++++++
 libc/src/math/CMakeLists.txt                  |   1 +
 libc/src/math/generic/CMakeLists.txt          |  11 +
 libc/src/math/generic/log2p1f16.cpp           |  18 ++
 libc/src/math/log2p1f16.h                     |  21 ++
 libc/test/src/math/CMakeLists.txt             |  11 +
 libc/test/src/math/log2p1f16_test.cpp         |  49 +++++
 libc/test/src/math/smoke/CMakeLists.txt       |  13 ++
 libc/test/src/math/smoke/log2p1f16_test.cpp   |  48 ++++
 libc/utils/MPFRWrapper/MPCommon.cpp           |  16 ++
 libc/utils/MPFRWrapper/MPCommon.h             |   1 +
 libc/utils/MPFRWrapper/MPFRUtils.cpp          |   2 +
 libc/utils/MPFRWrapper/MPFRUtils.h            |   1 +
 24 files changed, 431 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/__support/math/log2p1f16.h
 create mode 100644 libc/src/math/generic/log2p1f16.cpp
 create mode 100644 libc/src/math/log2p1f16.h
 create mode 100644 libc/test/src/math/log2p1f16_test.cpp
 create mode 100644 libc/test/src/math/smoke/log2p1f16_test.cpp

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 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/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/math/log2p1f16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcLog2p1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+TEST_F(LlvmLibcLog2p1f16Test, SpecialNumbers) {
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log2p1f16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::log2p1f16(sNaN),
+                              FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::log2p1f16(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log2p1f16(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log2p1f16(zero));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::log2p1f16(neg_zero));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+      neg_inf,
+      LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
+      FE_DIVBYZERO);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(
+      aNaN,
+      LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-2.0)));
+  EXPECT_MATH_ERRNO(EDOM);
+}
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 47d15b3981efe..a0cdebe6e60ea 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -329,6 +329,22 @@ MPFRNumber MPFRNumber::log2() const {
   return result;
 }
 
+MPFRNumber MPFRNumber::log2p1() const {
+  // TODO: Only use mpfr_log2p1 once CI and buildbots get MPFR >= 4.2.0.
+#if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
+  MPFRNumber result(*this);
+  mpfr_log2p1(result.value, value, mpfr_rounding);
+  return result;
+#else
+  unsigned int prec = mpfr_precision * 3;
+  MPFRNumber result(*this, prec);
+  MPFRNumber one(1.0f, prec);
+  mpfr_add(result.value, value, one.value, mpfr_rounding);
+  mpfr_log2(result.value, result.value, mpfr_rounding);
+  return result;
+#endif
+}
+
 MPFRNumber MPFRNumber::log10() const {
   MPFRNumber result(*this);
   mpfr_log10(result.value, value, mpfr_rounding);
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index cb39735ca0bc6..434849f1f30a1 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -213,6 +213,7 @@ class MPFRNumber {
   MPFRNumber hypot(const MPFRNumber &b);
   MPFRNumber log() const;
   MPFRNumber log2() const;
+  MPFRNumber log2p1() const;
   MPFRNumber log10() const;
   MPFRNumber log10p1() const;
   MPFRNumber log1p() const;
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index d9f444cfa8a76..e24ae3cc521c0 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -77,6 +77,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
     return mpfrInput.log();
   case Operation::Log2:
     return mpfrInput.log2();
+  case Operation::Log2p1:
+    return mpfrInput.log2p1();
   case Operation::Log10:
     return mpfrInput.log10();
   case Operation::Log10p1:
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 6c24b66324c1a..6179a1b38954d 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -49,6 +49,7 @@ enum class Operation : int {
   Floor,
   Log,
   Log2,
+  Log2p1,
   Log10,
   Log10p1,
   Log1p,

>From 65701a344ffbd731fe911c477f08dd30f231863f Mon Sep 17 00:00:00 2001
From: Shikhar Soni <shikharish05 at gmail.com>
Date: Sun, 22 Mar 2026 15:43:29 +0530
Subject: [PATCH 2/2] minor change

Signed-off-by: Shikhar Soni <shikharish05 at gmail.com>
---
 libc/src/__support/math/log2p1f16.h | 30 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h
index 0742915a92898..9bd5fd4ab8bb9 100644
--- a/libc/src/__support/math/log2p1f16.h
+++ b/libc/src/__support/math/log2p1f16.h
@@ -39,7 +39,7 @@ LIBC_INLINE float16 log2p1f16(float16 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 x is NaN, +/-inf, or |x| <= 2^-3.
   if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
     // log2p1(NaN) = NaN
     if (x_bits.is_nan()) {
@@ -51,21 +51,6 @@ LIBC_INLINE float16 log2p1f16(float16 x) {
       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
@@ -77,6 +62,10 @@ LIBC_INLINE float16 log2p1f16(float16 x) {
     //                   [-2^-3, 2^-3]);
     //   > Q;
     if (x_abs <= 0x3000U) {
+      // log2p1(+/-0) = +/-0
+      if (x_abs == 0U)
+        return x;
+
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
       constexpr size_t N_LOG2P1F16_EXCEPTS = 11;
       constexpr fputil::ExceptValues<float16, N_LOG2P1F16_EXCEPTS>
@@ -116,6 +105,15 @@ LIBC_INLINE float16 log2p1f16(float16 x) {
                                 0x1.ec6c5p-2f, -0x1.763338p-2f,
                                 0x1.2bcd3cp-2f));
     }
+
+    // log2p1(+inf) = +inf
+    if (x_u == 0x7c00U)
+      return FPBits::inf().get_val();
+
+    // log2p1(-inf) = NaN
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
   }
 
   // log2p1(-1) = -inf



More information about the libc-commits mailing list