[libc-commits] [libc] [llvm] [libc][math] Refactor ilogb implementation to header-only in src/__support/math folder. (PR #175504)
via libc-commits
libc-commits at lists.llvm.org
Mon Jan 12 01:06:20 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Dasha Buka (dvbuka)
<details>
<summary>Changes</summary>
closes #<!-- -->175348
---
Full diff: https://github.com/llvm/llvm-project/pull/175504.diff
7 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/ilogb.h (+23)
- (modified) libc/src/__support/math/CMakeLists.txt (+10)
- (added) libc/src/__support/math/ilogb.h (+27)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-1)
- (modified) libc/src/math/generic/ilogb.cpp (+2-4)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+16-1)
``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..de2bf7a5a8e06 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/ilogb.h"
#include "math/ilogbf16.h"
#include "math/ldexpf.h"
#include "math/ldexpf128.h"
diff --git a/libc/shared/math/ilogb.h b/libc/shared/math/ilogb.h
new file mode 100644
index 0000000000000..54944c20b8e4d
--- /dev/null
+++ b/libc/shared/math/ilogb.h
@@ -0,0 +1,23 @@
+//===-- Shared ilogb 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_ILOGB_H
+#define LLVM_LIBC_SHARED_MATH_ILOGB_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ilogb.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ilogb;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ILOGB_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..47645e32b4fcc 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -631,6 +631,16 @@ add_header_library(
libc.src.__support.FPUtil.manipulation_functions
)
+add_header_library(
+ ilogb
+ HDRS
+ ilogb.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
add_header_library(
ilogbf16
HDRS
diff --git a/libc/src/__support/math/ilogb.h b/libc/src/__support/math/ilogb.h
new file mode 100644
index 0000000000000..19f222831f6d9
--- /dev/null
+++ b/libc/src/__support/math/ilogb.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for ilogb -------------------------*- 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_ILOGB_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGB_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/math/ilogb.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr int ilogb(double x) { return fputil::intlogb<int>(x); }
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGB_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..b1575ae1f998c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1762,7 +1762,7 @@ add_entrypoint_object(
HDRS
../ilogb.h
DEPENDS
- libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.math.ilogb
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/ilogb.cpp b/libc/src/math/generic/ilogb.cpp
index 60f2af24dc225..74e6166a73394 100644
--- a/libc/src/math/generic/ilogb.cpp
+++ b/libc/src/math/generic/ilogb.cpp
@@ -7,12 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/ilogb.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/ilogb.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return fputil::intlogb<int>(x); }
+LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return math::ilogb(x); }
} // 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 c03c21b834895..2fbdb3848a95f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2842,6 +2842,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_ilogb",
+ hdrs = ["src/__support/math/ilogb.h"],
+ deps = [
+ ":__support_common",
+ ":__support_fputil_manipulation_functions",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_ilogbf16",
hdrs = ["src/__support/math/ilogbf16.h"],
@@ -4330,7 +4340,12 @@ libc_math_function(
],
)
-libc_math_function(name = "ilogb")
+libc_math_function(
+ name = "ilogb",
+ additional_deps = [
+ ":__support_math_ilogb",
+ ],
+)
libc_math_function(name = "ilogbf")
``````````
</details>
https://github.com/llvm/llvm-project/pull/175504
More information about the libc-commits
mailing list