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

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


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

>From d9f0f2b1e9b70f4a62757e91a627cedf5ab82799 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Sat, 17 Jan 2026 01:17:27 +0200
Subject: [PATCH] [libc][math] Refactor llogbf to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/llogbf.h                     | 23 +++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/llogbf.h              | 28 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/llogbf.cpp              |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 ++++++++-
 9 files changed, 80 insertions(+), 6 deletions(-)
 create mode 100644 libc/shared/math/llogbf.h
 create mode 100644 libc/src/__support/math/llogbf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index de1f7b0cc0b5d..1f5fe907747d1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -66,6 +66,7 @@
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
+#include "math/llogbf.h"
 #include "math/log.h"
 #include "math/logbf.h"
 #include "math/logbf128.h"
diff --git a/libc/shared/math/llogbf.h b/libc/shared/math/llogbf.h
new file mode 100644
index 0000000000000..1b660654e3d90
--- /dev/null
+++ b/libc/shared/math/llogbf.h
@@ -0,0 +1,23 @@
+//===-- Shared llogbf 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_LLOGBF_H
+#define LLVM_LIBC_SHARED_MATH_LLOGBF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/llogbf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::llogbf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LLOGBF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9716a2a3b1ff4..e249c8e196601 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -678,6 +678,16 @@ add_header_library(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_header_library(
+  llogbf
+  HDRS
+    llogbf.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   exp_constants
   HDRS
diff --git a/libc/src/__support/math/llogbf.h b/libc/src/__support/math/llogbf.h
new file mode 100644
index 0000000000000..1dcdcd0f7311f
--- /dev/null
+++ b/libc/src/__support/math/llogbf.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for llogbf ------------------------*- 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_LLOGBF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LLOGBF_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 long llogbf(float x) {
+  return fputil::intlogb<long>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LLOGBF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 2729c987d6369..a6c2f37f084d2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1837,7 +1837,8 @@ add_entrypoint_object(
   HDRS
     ../llogbf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.llogbf
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/llogbf.cpp b/libc/src/math/generic/llogbf.cpp
index fedad95cc48ad..2b35e2c07f88f 100644
--- a/libc/src/math/generic/llogbf.cpp
+++ b/libc/src/math/generic/llogbf.cpp
@@ -7,12 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/llogbf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/llogbf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, llogbf, (float x)) { return fputil::intlogb<long>(x); }
+LLVM_LIBC_FUNCTION(long, llogbf, (float x)) { return math::llogbf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 51bf25d60451d..2aaca086ad216 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -66,6 +66,7 @@ add_fp_unittest(
     libc.src.__support.math.ldexpf
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
+    libc.src.__support.math.llogbf
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8079890357ca0..9c3d7c8cee5b3 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_EQ(long(0), LIBC_NAMESPACE::shared::llogbf(1.0f));
   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 de7aa4dfa74ab..95a4504816b21 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2889,6 +2889,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_llogbf",
+    hdrs = ["src/__support/math/llogbf.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_log",
     hdrs = ["src/__support/math/log.h"],
@@ -4415,7 +4423,10 @@ libc_math_function(
 
 libc_math_function(name = "llogb")
 
-libc_math_function(name = "llogbf")
+libc_math_function(
+    name = "llogbf",
+    additional_deps = [":__support_math_llogbf"],
+)
 
 libc_math_function(name = "llogbl")
 



More information about the libc-commits mailing list