[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
Fri Jan 16 22:56:04 PST 2026


https://github.com/dvbuka updated https://github.com/llvm/llvm-project/pull/175504

>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/8] 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/8] 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/8] 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")

>From 3c0ab8c73865d0fafd5b144b274a73b55b8ce671 Mon Sep 17 00:00:00 2001
From: Dasha Buka <dvbuka at proton.me>
Date: Tue, 13 Jan 2026 23:31:43 -0800
Subject: [PATCH 4/8] Remove unnecessary header

---
 libc/src/__support/math/ilogb.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/__support/math/ilogb.h b/libc/src/__support/math/ilogb.h
index 19f222831f6d9..00810679360c1 100644
--- a/libc/src/__support/math/ilogb.h
+++ b/libc/src/__support/math/ilogb.h
@@ -12,7 +12,6 @@
 #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 {
 

>From 8063da1c2b5db34b2aa5290760fa7591c18e5f35 Mon Sep 17 00:00:00 2001
From: Dasha Buka <dvbuka at proton.me>
Date: Fri, 16 Jan 2026 20:57:42 -0800
Subject: [PATCH 5/8] Update libc/src/__support/math/ilogb.h

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/__support/math/ilogb.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/ilogb.h b/libc/src/__support/math/ilogb.h
index 00810679360c1..1da81c34af2b0 100644
--- a/libc/src/__support/math/ilogb.h
+++ b/libc/src/__support/math/ilogb.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE constexpr int ilogb(double x) { return fputil::intlogb<int>(x); }
+LIBC_INLINE static constexpr int ilogb(double x) { return fputil::intlogb<int>(x); }
 
 } // namespace math
 

>From 1d199e9c2692220f1f619554b3f5e3a2fd849b4b Mon Sep 17 00:00:00 2001
From: Dasha Buka <dvbuka at proton.me>
Date: Fri, 16 Jan 2026 21:27:18 -0800
Subject: [PATCH 6/8] Add shared test

---
 libc/test/shared/CMakeLists.txt       | 1 +
 libc/test/shared/shared_math_test.cpp | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..7124a147e2f8c 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.ilogb
     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..75f86dfdae8b9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -94,6 +94,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(0.0));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128

>From b9d09ceff5a3bd25d875f5516120ce9ec5ec927b Mon Sep 17 00:00:00 2001
From: Dasha Buka <dvbuka at proton.me>
Date: Fri, 16 Jan 2026 21:43:29 -0800
Subject: [PATCH 7/8] Formatting

---
 libc/shared/math.h              | 1 -
 libc/src/__support/math/ilogb.h | 4 +++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 05710749c3ff9..1eb62494220c2 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -61,7 +61,6 @@
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
-#include "math/ilogb.h"
 #include "math/fsqrt.h"
 #include "math/fsqrtf128.h"
 #include "math/hypotf.h"
diff --git a/libc/src/__support/math/ilogb.h b/libc/src/__support/math/ilogb.h
index 1da81c34af2b0..89b90f1927fbc 100644
--- a/libc/src/__support/math/ilogb.h
+++ b/libc/src/__support/math/ilogb.h
@@ -17,7 +17,9 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static constexpr int ilogb(double x) { return fputil::intlogb<int>(x); }
+LIBC_INLINE static constexpr int ilogb(double x) {
+  return fputil::intlogb<int>(x);
+}
 
 } // namespace math
 

>From 551a217ad317d4b9f0d7d0b8af573700459e0599 Mon Sep 17 00:00:00 2001
From: Dasha Buka <dvbuka at proton.me>
Date: Fri, 16 Jan 2026 22:55:49 -0800
Subject: [PATCH 8/8] Fix test

---
 libc/test/shared/shared_math_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index fa5fb53d2b8ef..4388fbbd8ee8f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -99,7 +99,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
-  EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(0.0));
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(1.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {



More information about the libc-commits mailing list