Skip to content

[libc][math] Refactor ilogbf128 to Header Only#175396

Merged
bassiounix merged 58 commits intollvm:mainfrom
Ramshankar07:ilogbf128-Refactor
Jan 27, 2026
Merged

[libc][math] Refactor ilogbf128 to Header Only#175396
bassiounix merged 58 commits intollvm:mainfrom
Ramshankar07:ilogbf128-Refactor

Conversation

@Ramshankar07
Copy link
Copy Markdown
Contributor

Refactors the ilogbf128 implementation to be header-only and available via shared math.

This is part of the ongoing effort to make math functions constexpr-capable
in LLVM libc.

that fixes: #175345

Changes:

  1. Created src/__support/math/ilogbf128.h - header-only, constexpr implementation in math:: namespace
  2. Created shared/math/ilogbf128.h - wrapper exposing it in shared:: namespace
  3. Updated src/math/generic/ilogbf128.cpp - now calls math::ilogbf128(x) instead of direct fputil::intlogb(x)
  4. Added CMake target in src/__support/math/CMakeLists.txt with proper dependencies
  5. Added Bazel support library __support_math_ilogbf128 in BUILD.bazel
  6. Added tests in test/shared/shared_math_test.cpp - validates shared::ilogbf128() works correctly

@github-actions
Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Jan 10, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Jan 10, 2026

@llvm/pr-subscribers-libc

Author: Ramshankar (Ramshankar07)

Changes

Refactors the ilogbf128 implementation to be header-only and available via shared math.

This is part of the ongoing effort to make math functions constexpr-capable
in LLVM libc.

that fixes: #175345

Changes:

  1. Created src/__support/math/ilogbf128.h - header-only, constexpr implementation in math:: namespace
  2. Created shared/math/ilogbf128.h - wrapper exposing it in shared:: namespace
  3. Updated src/math/generic/ilogbf128.cpp - now calls math::ilogbf128(x) instead of direct fputil::intlogb<int>(x)
  4. Added CMake target in src/__support/math/CMakeLists.txt with proper dependencies
  5. Added Bazel support library __support_math_ilogbf128 in BUILD.bazel
  6. Added tests in test/shared/shared_math_test.cpp - validates shared::ilogbf128() works correctly

Full diff: https://github.com/llvm/llvm-project/pull/175396.diff

7 Files Affected:

  • (modified) libc/shared/math.h (+2)
  • (added) libc/shared/math/ilogbf128.h (+29)
  • (modified) libc/src/__support/math/CMakeLists.txt (+10)
  • (added) libc/src/__support/math/ilogbf128.h (+34)
  • (modified) libc/src/math/generic/ilogbf128.cpp (+2-2)
  • (modified) libc/test/shared/shared_math_test.cpp (+3-1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+16-1)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7fb4c43f509c4..6932de634a545 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/ilogbf128.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
@@ -67,4 +68,5 @@
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
 
+
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/ilogbf128.h b/libc/shared/math/ilogbf128.h
new file mode 100644
index 0000000000000..b58915fb432ad
--- /dev/null
+++ b/libc/shared/math/ilogbf128.h
@@ -0,0 +1,29 @@
+//===-- Shared ilogbf128 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_ILOGBF128_H
+#define LLVM_LIBC_SHARED_MATH_ILOGBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ilogbf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+    
+using math::ilogbf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_ILogBF128_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 741da7432c94f..7d4ee136c7994 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -603,6 +603,16 @@ add_header_library(
     libc.src.__support.FPUtil.manipulation_functions
 )
 
+add_header_library(
+  ilogbf128
+  HDRS
+    ilogbf128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.include.llvm-libc-types.float128
+)
+
 add_header_library(
   inv_trigf_utils
   HDRS
diff --git a/libc/src/__support/math/ilogbf128.h b/libc/src/__support/math/ilogbf128.h
new file mode 100644
index 0000000000000..6849114933fc0
--- /dev/null
+++ b/libc/src/__support/math/ilogbf128.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for ilogbf128 ----------------------*- 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_ILOGBF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#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 int ilogbf128(float128 x) {
+  return fputil::intlogb<int>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF128_H
\ No newline at end of file
diff --git a/libc/src/math/generic/ilogbf128.cpp b/libc/src/math/generic/ilogbf128.cpp
index 4abc670188a3a..593e36b064d55 100644
--- a/libc/src/math/generic/ilogbf128.cpp
+++ b/libc/src/math/generic/ilogbf128.cpp
@@ -10,11 +10,11 @@
 #include "src/__support/FPUtil/ManipulationFunctions.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
-
+#include "lib/shared/math/ilogbf128.h"
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) {
-  return fputil::intlogb<int>(x);
+  return shared::ilogbf128(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f823d414e2afd..392dab3317725 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -103,7 +103,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_FP_EQ_ALL_ROUNDING(float128(0.75), LIBC_NAMESPACE::shared::frexpf128(
                                                 float128(24), &exponent));
   EXPECT_EQ(exponent, 5);
-
+  
+  EXPECT_EQ(3, LIBC_NAMESPACE::shared::ilogbf128(float128(8.0)));
+  EXPECT_EQ(4, LIBC_NAMESPACE::shared::ilogbf128(float128(16.0)));
   ASSERT_FP_EQ(float128(8 << 5),
                LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 210c25dddd0b9..874bbe92d02cc 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2813,6 +2813,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_ilogbf128",
+    hdrs = ["src/__support/math/ilogbf128.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_properties_types",
+        ":llvm_libc_types_float128",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_inv_trigf_utils",
     hdrs = ["src/__support/math/inv_trigf_utils.h"],
@@ -4308,7 +4318,12 @@ libc_math_function(name = "ilogbf")
 
 libc_math_function(name = "ilogbl")
 
-libc_math_function(name = "ilogbf128")
+libc_math_function(
+    name = "ilogbf128",
+    additional_deps = [
+        ":__support_math_ilogbf128",
+    ],
+)
 
 libc_math_function(name = "ilogbf16")
 

@bassiounix
Copy link
Copy Markdown
Member

When you finish working on this please give me a mention

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 11, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 11, 2026

🐧 Linux x64 Test Results

✅ The build succeeded and no tests ran. This is expected in some build configurations.

Ramshankar07 and others added 5 commits January 12, 2026 00:07
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
@Ramshankar07
Copy link
Copy Markdown
Contributor Author

Hi @bassiounix , I couldn't test it in my Mac because float128 isn't available for macOS. Let me know If I still make any

@Ramshankar07
Copy link
Copy Markdown
Contributor Author

@bassiounix Thanks for reviewing minute changes, I'm new to open source contribution.

@bassiounix
Copy link
Copy Markdown
Member

please format the code

Ramshankar07 and others added 6 commits January 26, 2026 13:52
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
@bassiounix bassiounix merged commit 54c5dcb into llvm:main Jan 27, 2026
29 checks passed
@github-actions
Copy link
Copy Markdown

@Ramshankar07 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@Ramshankar07
Copy link
Copy Markdown
Contributor Author

Thank you for helping me in code formatting and tweaks @bassiounix , I love to learn more and contribute.

Abdelrhmansersawy pushed a commit to Abdelrhmansersawy/llvm-project that referenced this pull request Jan 27, 2026
Refactors the ilogbf128 implementation to be header-only and available via shared math.

fixes: llvm#175345
stomfaig pushed a commit to stomfaig/llvm-project that referenced this pull request Jan 28, 2026
Refactors the ilogbf128 implementation to be header-only and available via shared math.

fixes: llvm#175345
sshrestha-aa pushed a commit to sshrestha-aa/llvm-project that referenced this pull request Feb 4, 2026
Refactors the ilogbf128 implementation to be header-only and available via shared math.

fixes: llvm#175345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel "Peripheral" support tier build system: utils/bazel libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc][math] Refactor ilogbf128 to Header Only.

3 participants