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

via libc-commits libc-commits at lists.llvm.org
Fri Jan 16 14:09:15 PST 2026


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

>From 01397fcd392af6e37436218013fc74e31c299c3a Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 20:47:03 +0200
Subject: [PATCH 1/3] [libc][math] Refactor logbf to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/logbf.h                      | 24 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  8 ++++++
 libc/src/__support/math/logbf.h               | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 ++-
 libc/src/math/generic/logbf.cpp               |  7 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 16 +++++++++++-
 9 files changed, 80 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/logbf.h
 create mode 100644 libc/src/__support/math/logbf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8102ff38584bd..de1f7b0cc0b5d 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/logbf.h"
 #include "math/logbf128.h"
 #include "math/logbf16.h"
 #include "math/rsqrtf.h"
diff --git a/libc/shared/math/logbf.h b/libc/shared/math/logbf.h
new file mode 100644
index 0000000000000..7a778d59fcead
--- /dev/null
+++ b/libc/shared/math/logbf.h
@@ -0,0 +1,24 @@
+//===-- Shared logbf 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_LOGBF_H
+#define LLVM_LIBC_SHARED_MATH_LOGBF_H
+
+#include "shared/libc_common.h"
+
+#include "src/__support/math/logbf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::logbf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGBF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 5bf0648be02e2..9716a2a3b1ff4 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1079,6 +1079,14 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  logbf
+  HDRS
+    logbf.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_header_library(
   log_range_reduction
   HDRS
diff --git a/libc/src/__support/math/logbf.h b/libc/src/__support/math/logbf.h
new file mode 100644
index 0000000000000..3d9f6494d2d2d
--- /dev/null
+++ b/libc/src/__support/math/logbf.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for logbf--------------------------*- 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_LOGBF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF_H
+
+#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 float logbf(float x) { return fputil::logb(x); }
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 11f1e0dd5e8b4..2729c987d6369 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2173,7 +2173,8 @@ add_entrypoint_object(
   HDRS
     ../logbf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.logbf
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/logbf.cpp b/libc/src/math/generic/logbf.cpp
index 0dc0251afe290..a2cf5830cee93 100644
--- a/libc/src/math/generic/logbf.cpp
+++ b/libc/src/math/generic/logbf.cpp
@@ -7,12 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logbf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
+#include "src/__support/math/logbf.h"
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, logbf, (float x)) { return fputil::logb(x); }
+LLVM_LIBC_FUNCTION(float, logbf, (float x)) { return math::logbf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index a2fa0146adadb..51bf25d60451d 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.logbf
     libc.src.__support.math.logbf128
     libc.src.__support.math.logbf16
     libc.src.__support.math.ldexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index baf725b1accc3..8079890357ca0 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -78,6 +78,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   ASSERT_FP_EQ(float(8 << 5), LIBC_NAMESPACE::shared::ldexpf(8.0f, 5));
   ASSERT_FP_EQ(float(-1 * (8 << 5)), LIBC_NAMESPACE::shared::ldexpf(-8.0f, 5));
 
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::logbf(1.0f));
   EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::rsqrtf(1.0f));
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 6d55f95b02b8f..de7aa4dfa74ab 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2906,6 +2906,15 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_logbf",
+    hdrs = ["src/__support/math/logbf.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_manipulation_functions",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_logbf128",
     hdrs = ["src/__support/math/logbf128.h"],
@@ -4558,7 +4567,12 @@ libc_math_function(
 
 libc_math_function(name = "logb")
 
-libc_math_function(name = "logbf")
+libc_math_function(
+    name = "logbf",
+    additional_deps = [
+        ":__support_math_logbf",
+    ],
+)
 
 libc_math_function(name = "logbl")
 

>From 9e7a30ecd566f3716391db42b591b410981137b3 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Sat, 17 Jan 2026 00:04:19 +0200
Subject: [PATCH 2/3] Apply suggestions from code review

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/shared/math/logbf.h        | 2 +-
 libc/src/__support/math/logbf.h | 2 +-
 libc/src/math/generic/logbf.cpp | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math/logbf.h b/libc/shared/math/logbf.h
index 7a778d59fcead..559f4bbc327f8 100644
--- a/libc/shared/math/logbf.h
+++ b/libc/shared/math/logbf.h
@@ -1,4 +1,4 @@
-//===-- Shared logbf function------------------------------------*- C++ -*-===//
+//===-- Shared logbf 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/logbf.h b/libc/src/__support/math/logbf.h
index 3d9f6494d2d2d..1b0daf92784ed 100644
--- a/libc/src/__support/math/logbf.h
+++ b/libc/src/__support/math/logbf.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for logbf--------------------------*- C++ -*-===//
+//===-- Implementation header for logbf -------------------------*- 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/math/generic/logbf.cpp b/libc/src/math/generic/logbf.cpp
index a2cf5830cee93..e319736adc71e 100644
--- a/libc/src/math/generic/logbf.cpp
+++ b/libc/src/math/generic/logbf.cpp
@@ -8,6 +8,7 @@
 
 #include "src/math/logbf.h"
 #include "src/__support/math/logbf.h"
+
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, logbf, (float x)) { return math::logbf(x); }

>From 62f9f76ac1fd70ad618955d9feb2e763b029b8b4 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Sat, 17 Jan 2026 00:09:06 +0200
Subject: [PATCH 3/3] Apply suggestion from @bassiounix

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/shared/math/logbf.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/shared/math/logbf.h b/libc/shared/math/logbf.h
index 559f4bbc327f8..32665a0f9070b 100644
--- a/libc/shared/math/logbf.h
+++ b/libc/shared/math/logbf.h
@@ -9,8 +9,6 @@
 #ifndef LLVM_LIBC_SHARED_MATH_LOGBF_H
 #define LLVM_LIBC_SHARED_MATH_LOGBF_H
 
-#include "shared/libc_common.h"
-
 #include "src/__support/math/logbf.h"
 
 namespace LIBC_NAMESPACE_DECL {



More information about the libc-commits mailing list