[libc-commits] [libc] [llvm] [libc][math] Refactor ilogbf128 to Header Only (PR #175396)
via libc-commits
libc-commits at lists.llvm.org
Tue Jan 13 19:11:01 PST 2026
https://github.com/Ramshankar07 updated https://github.com/llvm/llvm-project/pull/175396
>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 1/9] [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")
>From 8e2edfe7f1a5788ddee7a896baf35963d171634e Mon Sep 17 00:00:00 2001
From: Ramshankar <87872158+Ramshankar07 at users.noreply.github.com>
Date: Mon, 12 Jan 2026 00:07:08 -0500
Subject: [PATCH 2/9] Update libc/src/math/generic/ilogbf128.cpp
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/math/generic/ilogbf128.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/math/generic/ilogbf128.cpp b/libc/src/math/generic/ilogbf128.cpp
index 593e36b064d55..8342e732a9aaa 100644
--- a/libc/src/math/generic/ilogbf128.cpp
+++ b/libc/src/math/generic/ilogbf128.cpp
@@ -10,7 +10,7 @@
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "lib/shared/math/ilogbf128.h"
+#include "src/__support/math/ilogbf128.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) {
>From 0817f9cff83e42c41c2e51c451f792e3a4a65b68 Mon Sep 17 00:00:00 2001
From: Ramshankar <87872158+Ramshankar07 at users.noreply.github.com>
Date: Mon, 12 Jan 2026 00:07:28 -0500
Subject: [PATCH 3/9] Update libc/src/__support/math/ilogbf128.h
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/__support/math/ilogbf128.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/ilogbf128.h b/libc/src/__support/math/ilogbf128.h
index 6849114933fc0..1ba787d0b3745 100644
--- a/libc/src/__support/math/ilogbf128.h
+++ b/libc/src/__support/math/ilogbf128.h
@@ -31,4 +31,4 @@ LIBC_INLINE static constexpr int ilogbf128(float128 x) {
#endif // LIBC_TYPES_HAS_FLOAT128
-#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF128_H
\ No newline at end of file
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF128_H
>From 10b0c4786f5be0fcde14eddfdf6be5e1e9d751bb Mon Sep 17 00:00:00 2001
From: Ramshankar <87872158+Ramshankar07 at users.noreply.github.com>
Date: Mon, 12 Jan 2026 00:07:46 -0500
Subject: [PATCH 4/9] Update libc/src/math/generic/ilogbf128.cpp
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/src/math/generic/ilogbf128.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/math/generic/ilogbf128.cpp b/libc/src/math/generic/ilogbf128.cpp
index 8342e732a9aaa..09f074fb8af6c 100644
--- a/libc/src/math/generic/ilogbf128.cpp
+++ b/libc/src/math/generic/ilogbf128.cpp
@@ -14,7 +14,7 @@
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) {
- return shared::ilogbf128(x);
+ return math::ilogbf128(x);
}
} // namespace LIBC_NAMESPACE_DECL
>From cf4f76ae635a3dac294da2bfe95cc26ce49ed6ed Mon Sep 17 00:00:00 2001
From: Ramshankar <87872158+Ramshankar07 at users.noreply.github.com>
Date: Mon, 12 Jan 2026 00:07:53 -0500
Subject: [PATCH 5/9] Update libc/test/shared/shared_math_test.cpp
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
libc/test/shared/shared_math_test.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 392dab3317725..e8b35278effae 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -104,8 +104,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
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)));
+ EXPECT_EQ(float128(3), LIBC_NAMESPACE::shared::ilogbf128(float128(8.0)));
ASSERT_FP_EQ(float128(8 << 5),
LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
ASSERT_FP_EQ(float128(-1 * (8 << 5)),
>From c8f5c23c5a644485ffa2df416b9c05896b4860c0 Mon Sep 17 00:00:00 2001
From: Ramshankar07 <picographer0214 at gmail.com>
Date: Mon, 12 Jan 2026 00:16:37 -0500
Subject: [PATCH 6/9] adding in test's CMakeLists.txt
---
libc/test/shared/CMakeLists.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..7549adbbfd9c1 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -57,6 +57,7 @@ add_fp_unittest(
libc.src.__support.math.frexpf128
libc.src.__support.math.frexpf16
libc.src.__support.math.ilogbf16
+ libc.src.__support.math.ilogbf128
libc.src.__support.math.log
libc.src.__support.math.ldexpf
libc.src.__support.math.ldexpf128
>From 82c03665d80f8de19059f8a4cc47e739d61bee61 Mon Sep 17 00:00:00 2001
From: Ramshankar07 <picographer0214 at gmail.com>
Date: Mon, 12 Jan 2026 01:18:07 -0500
Subject: [PATCH 7/9] adding in CMakeLists.txt
---
libc/shared/math/ilogbf128.h | 1 -
libc/src/__support/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 3 +--
libc/src/math/generic/ilogbf128.cpp | 3 ---
4 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/libc/shared/math/ilogbf128.h b/libc/shared/math/ilogbf128.h
index b58915fb432ad..b9a5ba874603b 100644
--- a/libc/shared/math/ilogbf128.h
+++ b/libc/shared/math/ilogbf128.h
@@ -13,7 +13,6 @@
#ifdef LIBC_TYPES_HAS_FLOAT128
-#include "shared/libc_common.h"
#include "src/__support/math/ilogbf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4d4479cdb0d4d..5a44b7af9e16d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -608,6 +608,7 @@ add_header_library(
HDRS
ilogbf128.h
DEPENDS
+ libc.src.__support.macros.config
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.manipulation_functions
libc.include.llvm-libc-types.float128
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..aa94b3458b42e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1802,8 +1802,7 @@ add_entrypoint_object(
HDRS
../ilogbf128.h
DEPENDS
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.manipulation_functions
+ libc.src.__support.math.ilogbf128
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/ilogbf128.cpp b/libc/src/math/generic/ilogbf128.cpp
index 09f074fb8af6c..9bf366eae4932 100644
--- a/libc/src/math/generic/ilogbf128.cpp
+++ b/libc/src/math/generic/ilogbf128.cpp
@@ -7,9 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/math/ilogbf128.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
#include "src/__support/math/ilogbf128.h"
namespace LIBC_NAMESPACE_DECL {
>From b4b1235a3d50ffcc63a30eeea9ca9c6d20a21551 Mon Sep 17 00:00:00 2001
From: Ramshankar07 <picographer0214 at gmail.com>
Date: Tue, 13 Jan 2026 18:26:12 -0500
Subject: [PATCH 8/9] fixing-type in test to int as int ilogbf128(float128 x)
// Returns int, not float!
---
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 e7617309089d6..b688ab36ad226 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -107,7 +107,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
float128(24), &exponent));
EXPECT_EQ(exponent, 5);
- EXPECT_EQ(float128(3), LIBC_NAMESPACE::shared::ilogbf128(float128(8.0)));
+ EXPECT_EQ(3, LIBC_NAMESPACE::shared::ilogbf128(float128(8.0)));
ASSERT_FP_EQ(float128(8 << 5),
LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
ASSERT_FP_EQ(float128(-1 * (8 << 5)),
>From f51ca6065cb12e3c0072be039b73b29f95e5e4d2 Mon Sep 17 00:00:00 2001
From: Ramshankar07 <picographer0214 at gmail.com>
Date: Tue, 13 Jan 2026 22:08:44 -0500
Subject: [PATCH 9/9] Fix clang-format issues in ilogbf128 refactoring
---
libc/shared/math.h | 1 -
libc/shared/math/ilogbf128.h | 2 +-
libc/src/__support/math/ilogbf128.h | 3 ++-
libc/src/math/generic/ilogbf128.cpp | 6 +++---
libc/test/shared/shared_math_test.cpp | 2 +-
5 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a0db3224abc77..936005842fb3b 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -70,5 +70,4 @@
#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
index b9a5ba874603b..418c28f2a7b39 100644
--- a/libc/shared/math/ilogbf128.h
+++ b/libc/shared/math/ilogbf128.h
@@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace shared {
-
+
using math::ilogbf128;
} // namespace shared
diff --git a/libc/src/__support/math/ilogbf128.h b/libc/src/__support/math/ilogbf128.h
index 1ba787d0b3745..da1b9ac30c16c 100644
--- a/libc/src/__support/math/ilogbf128.h
+++ b/libc/src/__support/math/ilogbf128.h
@@ -1,4 +1,5 @@
-//===-- Implementation header for ilogbf128 ----------------------*- C++ -*-===//
+//===-- 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.
diff --git a/libc/src/math/generic/ilogbf128.cpp b/libc/src/math/generic/ilogbf128.cpp
index 9bf366eae4932..5ca536138fca0 100644
--- a/libc/src/math/generic/ilogbf128.cpp
+++ b/libc/src/math/generic/ilogbf128.cpp
@@ -7,11 +7,11 @@
//===----------------------------------------------------------------------===//
#include "src/math/ilogbf128.h"
+
#include "src/__support/math/ilogbf128.h"
+
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) {
- return math::ilogbf128(x);
-}
+LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) { return math::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 b688ab36ad226..30de9abe74f22 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -106,7 +106,7 @@ 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)));
ASSERT_FP_EQ(float128(8 << 5),
LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
More information about the libc-commits
mailing list