[libc-commits] [libc] [llvm] [libc][math] Refactor logbf128 to Header Only. (PR #176234)

via libc-commits libc-commits at lists.llvm.org
Fri Jan 16 13:29:47 PST 2026


https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/176234

>From d1b8e033a3ee329d4fe6f792d867da83ae23b0ba Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 21:53:14 +0200
Subject: [PATCH 1/2] [libc][math] Refactor logbf128 to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/logbf128.h                   | 30 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  9 +++++
 libc/src/__support/math/logbf128.h            | 34 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/logbf128.cpp            |  9 +++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 ++++++-
 9 files changed, 95 insertions(+), 8 deletions(-)
 create mode 100644 libc/shared/math/logbf128.h
 create mode 100644 libc/src/__support/math/logbf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2e7bbe1ef13be..b75e60c4f1973 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -67,6 +67,7 @@
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
 #include "math/log.h"
+#include "math/logbf128.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
diff --git a/libc/shared/math/logbf128.h b/libc/shared/math/logbf128.h
new file mode 100644
index 0000000000000..c73725460922a
--- /dev/null
+++ b/libc/shared/math/logbf128.h
@@ -0,0 +1,30 @@
+//===-- Shared logbf128 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_LOGBF128_H
+#define LLVM_LIBC_SHARED_MATH_LOGBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/logbf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::logbf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGBF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b45ccfc0eb3cb..97852c8688182 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1061,6 +1061,15 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  logbf128
+  HDRS
+    logbf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   log_range_reduction
   HDRS
diff --git a/libc/src/__support/math/logbf128.h b/libc/src/__support/math/logbf128.h
new file mode 100644
index 0000000000000..d18efc5077ade
--- /dev/null
+++ b/libc/src/__support/math/logbf128.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for logbf128-----------------------*- 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_LOGBF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float128 logbf128(float128 x) {
+  return fputil::logb(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 009a3033083bd..6fb260a7523d4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2204,8 +2204,8 @@ add_entrypoint_object(
   HDRS
     ../logbf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.logbf128
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/logbf128.cpp b/libc/src/math/generic/logbf128.cpp
index af8348736c9f4..274c19994b3ad 100644
--- a/libc/src/math/generic/logbf128.cpp
+++ b/libc/src/math/generic/logbf128.cpp
@@ -7,12 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logbf128.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
+#include "src/__support/math/logbf128.h"
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float128, logbf128, (float128 x)) { return fputil::logb(x); }
+LLVM_LIBC_FUNCTION(float128, logbf128, (float128 x)) {
+  return math::logbf128(x);
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 58214bf6f88eb..3a9bf9f8d4cf4 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -60,6 +60,7 @@ add_fp_unittest(
     libc.src.__support.math.fsqrt
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
+    libc.src.__support.math.logbf128
     libc.src.__support.math.ldexpf
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 6b78de0281347..918411efd7096 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,6 +115,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf128(float128(-8), 5));
+
+  EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::logbf128(float128(1.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4251f20a5dfcf..f1d390207e45e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2906,6 +2906,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_logbf128",
+    hdrs = ["src/__support/math/logbf128.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_exp_constants",
     hdrs = ["src/__support/math/exp_constants.h"],
@@ -4546,7 +4554,10 @@ libc_math_function(name = "logbf")
 
 libc_math_function(name = "logbl")
 
-libc_math_function(name = "logbf128")
+libc_math_function(
+    name = "logbf128",
+    additional_deps = [":__support_math_logbf128"],
+)
 
 libc_math_function(name = "logbf16")
 

>From 8f473b198f81cd448e74fbe2116a4761cd3bf3a2 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Fri, 16 Jan 2026 23:29:40 +0200
Subject: [PATCH 2/2] Apply suggestion from @bassiounix

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/test/shared/shared_math_test.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 918411efd7096..84563d851a002 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,7 +115,6 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf128(float128(-8), 5));
-
   EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::logbf128(float128(1.0)));
 }
 



More information about the libc-commits mailing list