[libc-commits] [libc] [llvm] [libc][math] Refactor logf16 to header-only shared math (PR #175408)

cpist via libc-commits libc-commits at lists.llvm.org
Tue Jan 27 03:18:01 PST 2026


https://github.com/cpist updated https://github.com/llvm/llvm-project/pull/175408

>From 2bcfeb0af373e3aea7e16ed0bc1f09c5abee1a78 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Sun, 11 Jan 2026 13:08:48 +0900
Subject: [PATCH 1/9] [libc][math] Refactor logf16 t0 header-only shared math

---
 libc/shared/math.h                            |   1 +
 libc/shared/math/logf16.h                     |  23 +++
 libc/src/__support/math/CMakeLists.txt        |  20 ++
 libc/src/__support/math/logf16.h              | 171 ++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  12 +-
 libc/src/math/generic/logf16.cpp              | 148 +--------------
 libc/test/shared/shared_math_test.cpp         |   1 +
 .../llvm-project-overlay/libc/BUILD.bazel     |  23 ++-
 8 files changed, 241 insertions(+), 158 deletions(-)
 create mode 100644 libc/shared/math/logf16.h
 create mode 100644 libc/src/__support/math/logf16.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7b2a12ac2bfaa..4ab3dbf61749c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -85,6 +85,7 @@
 #include "math/logbf128.h"
 #include "math/logbf16.h"
 #include "math/logf.h"
+#include "math/logf16.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
diff --git a/libc/shared/math/logf16.h b/libc/shared/math/logf16.h
new file mode 100644
index 0000000000000..8d04d5a213fc9
--- /dev/null
+++ b/libc/shared/math/logf16.h
@@ -0,0 +1,23 @@
+//===-- Shared logf16 function ------------------------------------*- 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_SHARED_MATH_LOGF16_H
+#define LLVM_LIBC_SHARED_MATH_LOGF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/logf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::logf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0efa3dfc9c25..0b604ea0929f8 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -619,6 +619,26 @@ add_header_library(
     libc.src.__support.FPUtil.generic.sqrt
 )
 
+add_header_library(
+  logf16
+  HDRS
+    logf16.h
+  DEPENDS
+.expxf16_utils
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.common
+    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.poly_eval
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.cpu_features
+)
+
 add_header_library(
   inv_trigf_utils
   HDRS
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
new file mode 100644
index 0000000000000..ef88febecac2c
--- /dev/null
+++ b/libc/src/__support/math/logf16.h
@@ -0,0 +1,171 @@
+//===-- Implementation header for logf16 ----------------------*- 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_LOGF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_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"
+#include "src/__support/math/expxf16_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+static constexpr size_t N_LOGF16_EXCEPTS = 5;
+#else
+static constexpr size_t N_LOGF16_EXCEPTS = 11;
+#endif
+
+static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
+    LOGF16_EXCEPTS = {{
+// (input, RZ output, RU offset, RD offset, RN offset)
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ)
+        {0x0987U, 0xc858U, 0U, 1U, 0U},
+        // x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ)
+        {0x0fc8U, 0xc7a6U, 0U, 1U, 1U},
+#endif
+        // x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ)
+        {0x1935U, 0xc5f9U, 0U, 1U, 0U},
+        // x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ)
+        {0x1d78U, 0xc53bU, 0U, 1U, 0U},
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ)
+        {0x3bf4U, 0x9e04U, 0U, 1U, 1U},
+        // x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ)
+        {0x3bf8U, 0x9c02U, 0U, 1U, 0U},
+#endif
+        // x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ)
+        {0x3bfcU, 0x9801U, 0U, 1U, 0U},
+        // x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ)
+        {0x3bfeU, 0x9400U, 0U, 1U, 1U},
+#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ)
+        {0x4131U, 0x3ba1U, 1U, 0U, 1U},
+#else
+        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
+        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
+        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
+        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
+        // x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ)
+        {0x6354U, 0x46d7U, 1U, 0U, 1U},
+#endif
+    }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+LIBC_INLINE float16 logf16(float16 x) {
+  using namespace math::expxf16_internal;
+  using FPBits = fputil::FPBits<float16>;
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+
+  // If x <= 0, or x is 1, or x is +inf, or x is NaN.
+  if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
+    // log(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;
+    }
+
+    // log(+/-0) = −inf
+    if ((x_u & 0x7fffU) == 0U) {
+      fputil::raise_except_if_required(FE_DIVBYZERO);
+      return FPBits::inf(Sign::NEG).get_val();
+    }
+
+    if (x_u == 0x3c00U)
+      return FPBits::zero().get_val();
+
+    // When x < 0.
+    if (x_u > 0x8000U) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
+    // log(+inf) = +inf
+    return FPBits::inf().get_val();
+  }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  if (auto r = LOGF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+    return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+  // To compute log(x), we perform the following range reduction:
+  //   x = 2^m * 1.mant,
+  //   log(x) = m * log(2) + log(1.mant).
+  // To compute log(1.mant), let f be the highest 6 bits including the hidden
+  // bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of
+  // the mantissa, then:
+  //   log(1.mant) = log(f) + log(1.mant / f)
+  //                = log(f) + log(1 + d/f)
+  // since d/f is sufficiently small.
+  // We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F_F
+  // respectively.
+
+  int m = -FPBits::EXP_BIAS;
+
+  // When x is subnormal, normalize it.
+  if ((x_u & FPBits::EXP_MASK) == 0U) {
+    // Can't pass an integer to fputil::cast directly.
+    constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
+    x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
+    x_u = x_bits.uintval();
+    m -= FPBits::FRACTION_LEN;
+  }
+
+  uint16_t mant = x_bits.get_mantissa();
+  // Leading 10 - 5 = 5 bits of the mantissa.
+  int f = mant >> 5;
+  // Unbiased exponent.
+  m += x_u >> FPBits::FRACTION_LEN;
+
+  // Set bits to 1.mant instead of 2^m * 1.mant.
+  x_bits.set_biased_exponent(FPBits::EXP_BIAS);
+  float mant_f = x_bits.get_val();
+  // 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(log(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+  //   > x * P;
+  float log1p_d_over_f =
+      v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f);
+  // log(1.mant) = log(f) + log(1 + d/f)
+  float log_1_mant = LOGF_F[f] + log1p_d_over_f;
+  return fputil::cast<float16>(
+      fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index f369206c39a16..bc992a99cf9e4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2069,17 +2069,7 @@ add_entrypoint_object(
   HDRS
     ../logf16.h
   DEPENDS
-    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
-    libc.src.__support.math.expxf16_utils
+    libc.src.__support.math.logf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/logf16.cpp b/libc/src/math/generic/logf16.cpp
index 22e0dc8be34bf..1ae287fc9b1b5 100644
--- a/libc/src/math/generic/logf16.cpp
+++ b/libc/src/math/generic/logf16.cpp
@@ -7,156 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logf16.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"
-#include "src/__support/math/expxf16_utils.h"
+#include "src/__support/math/logf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-static constexpr size_t N_LOGF16_EXCEPTS = 5;
-#else
-static constexpr size_t N_LOGF16_EXCEPTS = 11;
-#endif
-
-static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
-    LOGF16_EXCEPTS = {{
-// (input, RZ output, RU offset, RD offset, RN offset)
-#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-        // x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ)
-        {0x0987U, 0xc858U, 0U, 1U, 0U},
-        // x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ)
-        {0x0fc8U, 0xc7a6U, 0U, 1U, 1U},
-#endif
-        // x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ)
-        {0x1935U, 0xc5f9U, 0U, 1U, 0U},
-        // x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ)
-        {0x1d78U, 0xc53bU, 0U, 1U, 0U},
-#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-        // x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ)
-        {0x3bf4U, 0x9e04U, 0U, 1U, 1U},
-        // x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ)
-        {0x3bf8U, 0x9c02U, 0U, 1U, 0U},
-#endif
-        // x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ)
-        {0x3bfcU, 0x9801U, 0U, 1U, 0U},
-        // x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ)
-        {0x3bfeU, 0x9400U, 0U, 1U, 1U},
-#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-        // x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ)
-        {0x4131U, 0x3ba1U, 1U, 0U, 1U},
-#else
-        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
-        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
-        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
-        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
-        // x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ)
-        {0x6354U, 0x46d7U, 1U, 0U, 1U},
-#endif
-    }};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
 LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
-  using namespace math::expxf16_internal;
-  using FPBits = fputil::FPBits<float16>;
-  FPBits x_bits(x);
-
-  uint16_t x_u = x_bits.uintval();
-
-  // If x <= 0, or x is 1, or x is +inf, or x is NaN.
-  if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
-    // log(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;
-    }
-
-    // log(+/-0) = −inf
-    if ((x_u & 0x7fffU) == 0U) {
-      fputil::raise_except_if_required(FE_DIVBYZERO);
-      return FPBits::inf(Sign::NEG).get_val();
-    }
-
-    if (x_u == 0x3c00U)
-      return FPBits::zero().get_val();
-
-    // When x < 0.
-    if (x_u > 0x8000U) {
-      fputil::set_errno_if_required(EDOM);
-      fputil::raise_except_if_required(FE_INVALID);
-      return FPBits::quiet_nan().get_val();
-    }
-
-    // log(+inf) = +inf
-    return FPBits::inf().get_val();
-  }
-
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-  if (auto r = LOGF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
-    return r.value();
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-  // To compute log(x), we perform the following range reduction:
-  //   x = 2^m * 1.mant,
-  //   log(x) = m * log(2) + log(1.mant).
-  // To compute log(1.mant), let f be the highest 6 bits including the hidden
-  // bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of
-  // the mantissa, then:
-  //   log(1.mant) = log(f) + log(1.mant / f)
-  //               = log(f) + log(1 + d/f)
-  // since d/f is sufficiently small.
-  // We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F_F
-  // respectively.
-
-  int m = -FPBits::EXP_BIAS;
-
-  // When x is subnormal, normalize it.
-  if ((x_u & FPBits::EXP_MASK) == 0U) {
-    // Can't pass an integer to fputil::cast directly.
-    constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
-    x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
-    x_u = x_bits.uintval();
-    m -= FPBits::FRACTION_LEN;
-  }
-
-  uint16_t mant = x_bits.get_mantissa();
-  // Leading 10 - 5 = 5 bits of the mantissa.
-  int f = mant >> 5;
-  // Unbiased exponent.
-  m += x_u >> FPBits::FRACTION_LEN;
-
-  // Set bits to 1.mant instead of 2^m * 1.mant.
-  x_bits.set_biased_exponent(FPBits::EXP_BIAS);
-  float mant_f = x_bits.get_val();
-  // 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(log(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
-  //   > x * P;
-  float log1p_d_over_f =
-      v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f);
-  // log(1.mant) = log(f) + log(1 + d/f)
-  float log_1_mant = LOGF_F[f] + log1p_d_over_f;
-  return fputil::cast<float16>(
-      fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
+  return math::logf16(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index da1c62520042d..23c89b13f738c 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -32,6 +32,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logf16(1.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::sinhf16(0.0f16));
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 9d6641a5847f1..44b82db64eeaf 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3417,6 +3417,26 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_logf16",
+    hdrs = ["src/__support/math/logf16.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_cast",
+        ":__support_fputil_except_value_utils",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_poly_eval",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_macros_properties_cpu_features",
+        ":__support_math_expxf16_utils",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -4800,7 +4820,8 @@ libc_math_function(
 libc_math_function(
     name = "logf16",
     additional_deps = [
-        ":__support_math_expxf16_utils",
+        ":__support_math_logf16",
+        ":errno",
     ],
 )
 

>From e25cdcdec525e7138f73d6463e2eba7115b33dc8 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Sun, 11 Jan 2026 17:33:55 +0900
Subject: [PATCH 2/9] Add the function dependency and Check format

---
 libc/shared/math/logf16.h              |  3 ++-
 libc/src/__support/math/CMakeLists.txt | 11 ++++++++++-
 libc/src/__support/math/logf16.h       |  1 -
 libc/src/math/generic/logf16.cpp       |  4 +---
 libc/test/shared/CMakeLists.txt        |  1 +
 5 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/libc/shared/math/logf16.h b/libc/shared/math/logf16.h
index 8d04d5a213fc9..d7170b52eb661 100644
--- a/libc/shared/math/logf16.h
+++ b/libc/shared/math/logf16.h
@@ -1,4 +1,5 @@
-//===-- Shared logf16 function ------------------------------------*- C++ -*-===//
+//===-- Shared logf16 function ------------------------------------*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0b604ea0929f8..5b46beec16004 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -624,7 +624,7 @@ add_header_library(
   HDRS
     logf16.h
   DEPENDS
-.expxf16_utils
+    .expxf16_utils
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.common
@@ -639,6 +639,15 @@ add_header_library(
     libc.src.__support.macros.properties.cpu_features
 )
 
+add_header_library(
+  frexpf128
+  HDRS
+    frexpf128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_header_library(
   inv_trigf_utils
   HDRS
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index ef88febecac2c..86bb586c8820e 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -167,5 +167,4 @@ LIBC_INLINE float16 logf16(float16 x) {
 
 } // namespace LIBC_NAMESPACE_DECL
 
-
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
diff --git a/libc/src/math/generic/logf16.cpp b/libc/src/math/generic/logf16.cpp
index 1ae287fc9b1b5..7149081a87669 100644
--- a/libc/src/math/generic/logf16.cpp
+++ b/libc/src/math/generic/logf16.cpp
@@ -11,8 +11,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
-  return math::logf16(x);
-}
+LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) { return math::logf16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 0a19dfd9dbba9..9a07bce5deb0c 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -90,4 +90,5 @@ add_fp_unittest(
     libc.src.__support.math.sinhf16
     libc.src.__support.math.tan
     libc.src.__support.math.tanf
+    libc.src.__support.math.logf16
 )

>From a2416dea5fc34b704e6c503e43634a1937d0218b Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Mon, 12 Jan 2026 20:06:57 +0900
Subject: [PATCH 3/9] Add LIBC_TYPES_HAS_FLOAT16 guard and fix order

---
 libc/shared/math/logf16.h        |  9 ++++++---
 libc/src/__support/math/logf16.h | 10 +++++++++-
 libc/test/shared/CMakeLists.txt  |  1 +
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/libc/shared/math/logf16.h b/libc/shared/math/logf16.h
index d7170b52eb661..13a8673f4c7d0 100644
--- a/libc/shared/math/logf16.h
+++ b/libc/shared/math/logf16.h
@@ -1,5 +1,4 @@
-//===-- Shared logf16 function ------------------------------------*- C++
-//-*-===//
+//===-- Shared logf16 function --------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,6 +10,8 @@
 #define LLVM_LIBC_SHARED_MATH_LOGF16_H
 
 #include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
 #include "src/__support/math/logf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -21,4 +22,6 @@ using math::logf16;
 } // namespace shared
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LLVM_LIBC_SHARED_MATH_LOGF16_H
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGF16_H
\ No newline at end of file
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index 86bb586c8820e..16d9dd2dbc98d 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
 
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
@@ -27,13 +29,14 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
+namespace logf16_internal {
+
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
 static constexpr size_t N_LOGF16_EXCEPTS = 5;
 #else
 static constexpr size_t N_LOGF16_EXCEPTS = 11;
 #endif
-
 static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
     LOGF16_EXCEPTS = {{
 // (input, RZ output, RU offset, RD offset, RN offset)
@@ -71,8 +74,11 @@ static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
     }};
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
+} // namespace logf16_internal
+
 LIBC_INLINE float16 logf16(float16 x) {
   using namespace math::expxf16_internal;
+  using namespace math::logf16_internal;
   using FPBits = fputil::FPBits<float16>;
   FPBits x_bits(x);
 
@@ -167,4 +173,6 @@ LIBC_INLINE float16 logf16(float16 x) {
 
 } // namespace LIBC_NAMESPACE_DECL
 
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9a07bce5deb0c..6c2c98c43f62f 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -82,6 +82,7 @@ add_fp_unittest(
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
     libc.src.__support.math.llogbf
+    libc.src.__support.math.logf16
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin

>From 111bc090399082c956943e6a869a693e6de28932 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Wed, 14 Jan 2026 21:25:48 +0900
Subject: [PATCH 4/9] Add PolyEval dependency

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
 1 file changed, 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 44b82db64eeaf..4182262886675 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4821,7 +4821,6 @@ libc_math_function(
     name = "logf16",
     additional_deps = [
         ":__support_math_logf16",
-        ":errno",
     ],
 )
 

>From 67664095a501f78170bd9be44362174d2dfb5cd0 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Fri, 16 Jan 2026 20:34:22 +0900
Subject: [PATCH 5/9] Add related include for guard

---
 libc/shared/math/logf16.h        | 3 ++-
 libc/src/__support/math/logf16.h | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math/logf16.h b/libc/shared/math/logf16.h
index 13a8673f4c7d0..add986f5d8fcf 100644
--- a/libc/shared/math/logf16.h
+++ b/libc/shared/math/logf16.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_MATH_LOGF16_H
 #define LLVM_LIBC_SHARED_MATH_LOGF16_H
 
+#include "include/llvm-libc-macros/float16-macros.h"
 #include "shared/libc_common.h"
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
@@ -24,4 +25,4 @@ using math::logf16;
 
 #endif // LIBC_TYPES_HAS_FLOAT16
 
-#endif // LLVM_LIBC_SHARED_MATH_LOGF16_H
\ No newline at end of file
+#endif // LLVM_LIBC_SHARED_MATH_LOGF16_H
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index 16d9dd2dbc98d..5e3942b0e94aa 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_H
 
+#include "include/llvm-libc-macros/float16-macros.h"
+
 #ifdef LIBC_TYPES_HAS_FLOAT16
 
 #include "hdr/errno_macros.h"
@@ -76,7 +78,7 @@ static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
 
 } // namespace logf16_internal
 
-LIBC_INLINE float16 logf16(float16 x) {
+LIBC_INLINE static float16 logf16(float16 x) {
   using namespace math::expxf16_internal;
   using namespace math::logf16_internal;
   using FPBits = fputil::FPBits<float16>;

>From c1e5273e091fe1adc131cdf66aa00a93669f76fc Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Tue, 20 Jan 2026 21:00:11 +0900
Subject: [PATCH 6/9] Adding the correct deps

---
 libc/shared/math/logf16.h                         | 2 +-
 libc/src/__support/math/logf16.h                  | 4 ++--
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/shared/math/logf16.h b/libc/shared/math/logf16.h
index add986f5d8fcf..64cfda4fb9f77 100644
--- a/libc/shared/math/logf16.h
+++ b/libc/shared/math/logf16.h
@@ -1,4 +1,4 @@
-//===-- Shared logf16 function --------------------*- C++ -*-===//
+//===-- Shared logf16 function ----------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index 5e3942b0e94aa..fc3be581660b8 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for logf16 ----------------------*- C++ -*-===//
+//===-- Implementation header for logf16 ------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -13,6 +13,7 @@
 
 #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"
@@ -25,7 +26,6 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 #include "src/__support/macros/properties/cpu_features.h"
-#include "src/__support/math/expxf16_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4182262886675..59ddcbbc0025b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3427,7 +3427,7 @@ libc_support_library(
         ":__support_fputil_fenv_impl",
         ":__support_fputil_fp_bits",
         ":__support_fputil_multiply_add",
-        ":__support_fputil_poly_eval",
+        ":__support_fputil_polyeval",
         ":__support_macros_config",
         ":__support_macros_optimization",
         ":__support_macros_properties_cpu_features",

>From d025fb22e822c73936c617f9cd774e801e3d00d5 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Sun, 25 Jan 2026 17:01:40 +0900
Subject: [PATCH 7/9] Modify return types and maintain alphabetic order in
 CMakeLists.txt

---
 libc/src/__support/math/CMakeLists.txt | 52 +++++++++++++++++---------
 libc/src/__support/math/logf16.h       |  6 +--
 libc/test/shared/CMakeLists.txt        |  1 -
 3 files changed, 38 insertions(+), 21 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 5b46beec16004..ca1aca0841f67 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -612,31 +612,21 @@ add_header_library(
 )
 
 add_header_library(
-  fsqrt
+  f16sqrtl
   HDRS
-    fsqrt.h
+    f16sqrtl.h
   DEPENDS
     libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.macros.properties.types
 )
 
 add_header_library(
-  logf16
+  frexpf128
   HDRS
-    logf16.h
+    frexpf128.h
   DEPENDS
-    .expxf16_utils
-    libc.hdr.errno_macros
-    libc.hdr.fenv_macros
-    libc.src.__support.common
-    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.poly_eval
-    libc.src.__support.macros.config
-    libc.src.__support.macros.optimization
-    libc.src.__support.macros.properties.cpu_features
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.manipulation_functions
 )
 
 add_header_library(
@@ -648,6 +638,14 @@ add_header_library(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_header_library(
+  fsqrt
+  HDRS
+    fsqrt.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+)
+
 add_header_library(
   inv_trigf_utils
   HDRS
@@ -765,6 +763,26 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  logf16
+  HDRS
+    logf16.h
+  DEPENDS
+    .expxf16_utils
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.common
+    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.poly_eval
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.cpu_features
+)
+
 add_header_library(
   exp_constants
   HDRS
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index fc3be581660b8..c123eb09b9665 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -35,11 +35,11 @@ namespace logf16_internal {
 
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-static constexpr size_t N_LOGF16_EXCEPTS = 5;
+LIBC_INLINE_VAR constexpr size_t N_LOGF16_EXCEPTS = 5;
 #else
-static constexpr size_t N_LOGF16_EXCEPTS = 11;
+LIBC_INLINE_VAR constexpr size_t N_LOGF16_EXCEPTS = 11;
 #endif
-static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
     LOGF16_EXCEPTS = {{
 // (input, RZ output, RU offset, RD offset, RN offset)
 #ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 6c2c98c43f62f..a0372a19ac445 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -91,5 +91,4 @@ add_fp_unittest(
     libc.src.__support.math.sinhf16
     libc.src.__support.math.tan
     libc.src.__support.math.tanf
-    libc.src.__support.math.logf16
 )

>From 0b9197a76bc8a890fc216542427e8920fdd71ad2 Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Tue, 27 Jan 2026 07:35:57 +0900
Subject: [PATCH 8/9] Add libc macro infos

---
 libc/src/__support/math/CMakeLists.txt            | 1 +
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ca1aca0841f67..49958b3c9c590 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -781,6 +781,7 @@ add_header_library(
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
+    libc.include.llvm-libc-macros.float16_macros
 )
 
 add_header_library(
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 59ddcbbc0025b..b9fa34cc3bc1b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3434,6 +3434,7 @@ libc_support_library(
         ":__support_math_expxf16_utils",
         ":hdr_errno_macros",
         ":hdr_fenv_macros",
+        ":llvm_libc_macros_float16_macros",
     ],
 )
 

>From ef050323e92f0545ea0a11d378fb1be1284cfbbe Mon Sep 17 00:00:00 2001
From: cpist <tinyfrog12 at gmail.com>
Date: Tue, 27 Jan 2026 20:17:00 +0900
Subject: [PATCH 9/9] Rebase CMakeLists.txt

---
 libc/src/__support/math/CMakeLists.txt | 27 --------------------------
 1 file changed, 27 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 49958b3c9c590..807407ac6b38c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -611,33 +611,6 @@ add_header_library(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
-add_header_library(
-  f16sqrtl
-  HDRS
-    f16sqrtl.h
-  DEPENDS
-    libc.src.__support.FPUtil.generic.sqrt
-    libc.src.__support.macros.properties.types
-)
-
-add_header_library(
-  frexpf128
-  HDRS
-    frexpf128.h
-  DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
-)
-
-add_header_library(
-  frexpf128
-  HDRS
-    frexpf128.h
-  DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
-)
-
 add_header_library(
   fsqrt
   HDRS



More information about the libc-commits mailing list