[libc-commits] [libc] [llvm] [libc][math] Refactor ilogb implementation to header-only in src/__support/math folder. (PR #175504)
Dasha Buka via libc-commits
libc-commits at lists.llvm.org
Mon Jan 12 01:05:45 PST 2026
https://github.com/dvbuka created https://github.com/llvm/llvm-project/pull/175504
closes #175348
>From a67187241566bbd55b3c35fbd312f588b3698d17 Mon Sep 17 00:00:00 2001
From: Dasha Buka <102774272+dvbuka at users.noreply.github.com>
Date: Mon, 12 Jan 2026 00:55:01 -0800
Subject: [PATCH 1/3] Refactor ilogb to header-only
---
libc/shared/math.h | 1 +
libc/shared/math/ilogb.h | 23 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 10 +++++++
libc/src/__support/math/ilogb.h | 27 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/ilogb.cpp | 6 ++---
.../llvm-project-overlay/libc/BUILD.bazel | 18 +++++++++++--
7 files changed, 80 insertions(+), 7 deletions(-)
create mode 100644 libc/shared/math/ilogb.h
create mode 100644 libc/src/__support/math/ilogb.h
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..ab2958af93da7
--- /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..b2f06adeeb530 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,8 +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")
libc_math_function(name = "ilogbl")
>From a6434bb03c438d96ae16b2aa1624fb5d25706123 Mon Sep 17 00:00:00 2001
From: Dasha Buka <102774272+dvbuka at users.noreply.github.com>
Date: Mon, 12 Jan 2026 01:02:41 -0800
Subject: [PATCH 2/3] Fix spacing in comment
---
libc/shared/math/ilogb.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/shared/math/ilogb.h b/libc/shared/math/ilogb.h
index ab2958af93da7..54944c20b8e4d 100644
--- a/libc/shared/math/ilogb.h
+++ b/libc/shared/math/ilogb.h
@@ -1,4 +1,4 @@
-//===-- Shared ilogb function ------------------------------------*- C++-*-===//
+//===-- 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.
>From 0936e0321b04233abcea1fc0d1310da4ddb3db32 Mon Sep 17 00:00:00 2001
From: Dasha Buka <102774272+dvbuka at users.noreply.github.com>
Date: Mon, 12 Jan 2026 01:04:08 -0800
Subject: [PATCH 3/3] Fix formatting
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
1 file changed, 1 insertion(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b2f06adeeb530..2fbdb3848a95f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4346,6 +4346,7 @@ libc_math_function(
":__support_math_ilogb",
],
)
+
libc_math_function(name = "ilogbf")
libc_math_function(name = "ilogbl")
More information about the libc-commits
mailing list