[libc-commits] [libc] [llvm] [libc][math] Refactor ilogbf implementation to header-only in src/__support/math folder. (PR #175522)

via libc-commits libc-commits at lists.llvm.org
Thu Jan 15 22:12:08 PST 2026


https://github.com/YuXuan255 updated https://github.com/llvm/llvm-project/pull/175522

>From 29ea024810902931a806f509992471ccfd927eb5 Mon Sep 17 00:00:00 2001
From: yuxuan255 <duyuxuan at ruc.edu.cn>
Date: Mon, 12 Jan 2026 19:49:37 +0800
Subject: [PATCH 1/2] [libc][math] Refactor ilogbf implementation to
 header-only in src/__support/math folder.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/ilogbf.h                     | 24 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 13 ++++++++-
 libc/src/__support/math/ilogbf.h              | 28 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/ilogbf.cpp              |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 18 +++++++++++-
 9 files changed, 88 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/ilogbf.h
 create mode 100644 libc/src/__support/math/ilogbf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..3ec41f52b51e4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,6 +60,7 @@
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
+#include "math/ilogbf.h"
 #include "math/ilogbf16.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
diff --git a/libc/shared/math/ilogbf.h b/libc/shared/math/ilogbf.h
new file mode 100644
index 0000000000000..058c4082e0a9c
--- /dev/null
+++ b/libc/shared/math/ilogbf.h
@@ -0,0 +1,24 @@
+//===-- Shared ilogbf 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_ILOGBF_H
+#define LLVM_LIBC_SHARED_MATH_ILOGBF_H
+
+#include "include/llvm-libc-macros/float-macros.h"
+
+#include "src/__support/math/ilogbf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ilogbf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ILOGBF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..1f33b16bbcd1c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -390,7 +390,6 @@ add_header_library(
     cosf.h
   DEPENDS
     .sincosf_utils
-    libc.src.errno.errno
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
@@ -642,6 +641,18 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  ilogbf
+  HDRS
+    ilogbf.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.errno.errno
+    libc.include.llvm-libc-macros.float_macros
+)
+
 add_header_library(
   ldexpf128
   HDRS
diff --git a/libc/src/__support/math/ilogbf.h b/libc/src/__support/math/ilogbf.h
new file mode 100644
index 0000000000000..5cc0a1cab3e18
--- /dev/null
+++ b/libc/src/__support/math/ilogbf.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for ilogbf ----------------------*- 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_ILOGBF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF_H
+
+#include "include/llvm-libc-macros/float-macros.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 constexpr int ilogbf(float x) { return fputil::intlogb<int>(x); }
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..1ecbb38984549 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1772,7 +1772,7 @@ add_entrypoint_object(
   HDRS
     ../ilogbf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.ilogbf
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/ilogbf.cpp b/libc/src/math/generic/ilogbf.cpp
index 7da2aff3de316..0ed2cb569fd2e 100644
--- a/libc/src/math/generic/ilogbf.cpp
+++ b/libc/src/math/generic/ilogbf.cpp
@@ -7,12 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/ilogbf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/ilogbf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(int, ilogbf, (float x)) { return fputil::intlogb<int>(x); }
+LLVM_LIBC_FUNCTION(int, ilogbf, (float x)) { return math::ilogbf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..fb713189daf4a 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -56,6 +56,7 @@ add_fp_unittest(
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
+    libc.src.__support.math.ilogbf
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
     libc.src.__support.math.ldexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..7c07b000ff4af 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -74,6 +74,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
                             LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent));
   EXPECT_EQ(exponent, 5);
 
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf(1.0f));
+
   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));
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..3008f3800cabf 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2853,6 +2853,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_ilogbf",
+    hdrs = ["src/__support/math/ilogbf.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ldexpf128",
     hdrs = ["src/__support/math/ldexpf128.h"],
@@ -4332,7 +4343,12 @@ libc_math_function(
 
 libc_math_function(name = "ilogb")
 
-libc_math_function(name = "ilogbf")
+libc_math_function(
+    name = "ilogbf",
+    additional_deps = [
+        ":__support_math_ilogbf",
+    ],
+)
 
 libc_math_function(name = "ilogbl")
 

>From 19708beb014a555af2e113dff8f24e897b637332 Mon Sep 17 00:00:00 2001
From: Anonymous <138379976+YuXuan255 at users.noreply.github.com>
Date: Fri, 16 Jan 2026 14:11:59 +0800
Subject: [PATCH 2/2] Apply suggestions from code review

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/shared/math/ilogbf.h                         | 1 -
 libc/src/__support/math/CMakeLists.txt            | 2 --
 libc/src/__support/math/ilogbf.h                  | 1 -
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
 4 files changed, 5 deletions(-)

diff --git a/libc/shared/math/ilogbf.h b/libc/shared/math/ilogbf.h
index 058c4082e0a9c..cf6da74d88df7 100644
--- a/libc/shared/math/ilogbf.h
+++ b/libc/shared/math/ilogbf.h
@@ -9,7 +9,6 @@
 #ifndef LLVM_LIBC_SHARED_MATH_ILOGBF_H
 #define LLVM_LIBC_SHARED_MATH_ILOGBF_H
 
-#include "include/llvm-libc-macros/float-macros.h"
 
 #include "src/__support/math/ilogbf.h"
 
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 1f33b16bbcd1c..d590f245550ed 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -649,8 +649,6 @@ add_header_library(
     libc.src.__support.macros.config
     libc.src.__support.macros.properties.types
     libc.src.__support.FPUtil.manipulation_functions
-    libc.src.errno.errno
-    libc.include.llvm-libc-macros.float_macros
 )
 
 add_header_library(
diff --git a/libc/src/__support/math/ilogbf.h b/libc/src/__support/math/ilogbf.h
index 5cc0a1cab3e18..1e55f55daf8b9 100644
--- a/libc/src/__support/math/ilogbf.h
+++ b/libc/src/__support/math/ilogbf.h
@@ -9,7 +9,6 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF_H
 
-#include "include/llvm-libc-macros/float-macros.h"
 
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3008f3800cabf..8a09dcc1113d6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2860,7 +2860,6 @@ libc_support_library(
         ":__support_common",
         ":__support_fputil_manipulation_functions",
         ":__support_macros_config",
-        ":llvm_libc_macros_float_macros",
     ],
 )
 



More information about the libc-commits mailing list