[libc-commits] [libc] [llvm] [libc][math] Refactor ilogbf128 to Header Only (PR #175396)
via libc-commits
libc-commits at lists.llvm.org
Sat Jan 10 14:51:59 PST 2026
https://github.com/Ramshankar07 created https://github.com/llvm/llvm-project/pull/175396
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
>From ac578b0c5e8a2db8d8a4e316cb8fa085b583bf7f Mon Sep 17 00:00:00 2001
From: Ramshankar07 <picographer0214 at gmail.com>
Date: Sat, 10 Jan 2026 17:38:10 -0500
Subject: [PATCH] [libc][math] Refactor ilogbf128 to Header Only. #175345
---
libc/shared/math.h | 2 ++
libc/shared/math/ilogbf128.h | 29 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 10 ++++++
libc/src/__support/math/ilogbf128.h | 34 +++++++++++++++++++
libc/src/math/generic/ilogbf128.cpp | 4 +--
libc/test/shared/shared_math_test.cpp | 4 ++-
.../llvm-project-overlay/libc/BUILD.bazel | 17 +++++++++-
7 files changed, 96 insertions(+), 4 deletions(-)
create mode 100644 libc/shared/math/ilogbf128.h
create mode 100644 libc/src/__support/math/ilogbf128.h
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")
More information about the libc-commits
mailing list