[libc-commits] [libc] [llvm] [libc][math] Refactor ilogbf16 implementation to header-only in src/__support/math folder. (PR #175450)
via libc-commits
libc-commits at lists.llvm.org
Sun Jan 11 15:52:38 PST 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/175450
>From 9cf9f00e3879218abc6b5c8099095c2e41587a31 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Mon, 12 Jan 2026 02:20:34 +0800
Subject: [PATCH 1/2] [libc][math] Refactor ilogbf16 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h | 1 +
libc/shared/math/ilogbf16.h | 29 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 ++++++
libc/src/__support/math/ilogbf16.h | 34 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 1 +
libc/src/math/generic/ilogbf16.cpp | 6 ++--
.../llvm-project-overlay/libc/BUILD.bazel | 18 +++++++++-
7 files changed, 95 insertions(+), 5 deletions(-)
create mode 100644 libc/shared/math/ilogbf16.h
create mode 100644 libc/src/__support/math/ilogbf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 70c6d375c22de..a504c507b4fa4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -59,6 +59,7 @@
#include "math/frexpf.h"
#include "math/frexpf128.h"
#include "math/frexpf16.h"
+#include "math/ilogbf16.h"
#include "math/ldexpf.h"
#include "math/ldexpf128.h"
#include "math/ldexpf16.h"
diff --git a/libc/shared/math/ilogbf16.h b/libc/shared/math/ilogbf16.h
new file mode 100644
index 0000000000000..0e292affa4c0f
--- /dev/null
+++ b/libc/shared/math/ilogbf16.h
@@ -0,0 +1,29 @@
+//===-- Shared ilogbf16 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_ILOGBF16_H
+#define LLVM_LIBC_SHARED_MATH_ILOGBF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/ilogbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ilogbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ILOGBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index f8622da52d983..121bb099c1199 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -631,6 +631,17 @@ add_header_library(
libc.src.__support.FPUtil.manipulation_functions
)
+add_header_library(
+ ilogbf16
+ HDRS
+ ilogbf16.h
+ DEPENDS
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.include.llvm-libc-macros.float16_macros
+)
+
add_header_library(
ldexpf128
HDRS
diff --git a/libc/src/__support/math/ilogbf16.h b/libc/src/__support/math/ilogbf16.h
new file mode 100644
index 0000000000000..db9b8494f8a15
--- /dev/null
+++ b/libc/src/__support/math/ilogbf16.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for ilogbf16 ----------------------*- 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_ILOGBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#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 constexpr int ilogbf16(float16 x) {
+ return fputil::intlogb<int>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index e343c1f15c3f1..239fdba5eae6d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1812,6 +1812,7 @@ add_entrypoint_object(
HDRS
../ilogbf16.h
DEPENDS
+ libc.src.__support.math.ilogbf16
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.manipulation_functions
)
diff --git a/libc/src/math/generic/ilogbf16.cpp b/libc/src/math/generic/ilogbf16.cpp
index 9217cdfe01651..8cb6a28e917b0 100644
--- a/libc/src/math/generic/ilogbf16.cpp
+++ b/libc/src/math/generic/ilogbf16.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/ilogbf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/math/ilogbf16.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, ilogbf16, (float16 x)) {
- return fputil::intlogb<int>(x);
-}
+LLVM_LIBC_FUNCTION(int, ilogbf16, (float16 x)) { return math::ilogbf16(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 913e913572b14..b8547d26007d1 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2842,6 +2842,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_ilogbf16",
+ hdrs = ["src/__support/math/ilogbf16.h"],
+ deps = [
+ ":__support_common",
+ ":__support_fputil_manipulation_functions",
+ ":__support_macros_config",
+ ":llvm_libc_macros_float16_macros",
+ ],
+)
+
libc_support_library(
name = "__support_math_ldexpf128",
hdrs = ["src/__support/math/ldexpf128.h"],
@@ -4282,7 +4293,12 @@ libc_math_function(name = "ilogbl")
libc_math_function(name = "ilogbf128")
-libc_math_function(name = "ilogbf16")
+libc_math_function(
+ name = "ilogbf16",
+ additional_deps = [
+ ":__support_math_ilogbf16",
+ ],
+)
libc_math_function(name = "ldexp")
>From ba4b521d3c49744bfe892c09ee4f97454c0eaf38 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Mon, 12 Jan 2026 07:51:00 +0800
Subject: [PATCH 2/2] Address review feedbacks
---
libc/shared/math/ilogbf16.h | 1 -
libc/src/math/generic/CMakeLists.txt | 2 --
libc/src/math/generic/ilogbf16.cpp | 2 --
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +++
5 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/libc/shared/math/ilogbf16.h b/libc/shared/math/ilogbf16.h
index 0e292affa4c0f..6be207b589e0c 100644
--- a/libc/shared/math/ilogbf16.h
+++ b/libc/shared/math/ilogbf16.h
@@ -10,7 +10,6 @@
#define LLVM_LIBC_SHARED_MATH_ILOGBF16_H
#include "include/llvm-libc-macros/float16-macros.h"
-#include "shared/libc_common.h"
#ifdef LIBC_TYPES_HAS_FLOAT16
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 239fdba5eae6d..d7d475cd60943 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1813,8 +1813,6 @@ add_entrypoint_object(
../ilogbf16.h
DEPENDS
libc.src.__support.math.ilogbf16
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.manipulation_functions
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/ilogbf16.cpp b/libc/src/math/generic/ilogbf16.cpp
index 8cb6a28e917b0..451b704618e44 100644
--- a/libc/src/math/generic/ilogbf16.cpp
+++ b/libc/src/math/generic/ilogbf16.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/math/ilogbf16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
#include "src/__support/math/ilogbf16.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 4482d6344ae03..cab56f5a59e7d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -55,6 +55,7 @@ add_fp_unittest(
libc.src.__support.math.frexpf
libc.src.__support.math.frexpf128
libc.src.__support.math.frexpf16
+ libc.src.__support.math.ilogbf16
libc.src.__support.math.ldexpf
libc.src.__support.math.ldexpf128
libc.src.__support.math.ldexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f09b0dcd4ef9f..d3260e1e06a78 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -40,6 +40,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
0.75f16, LIBC_NAMESPACE::shared::frexpf16(24.0f16, &exponent));
EXPECT_EQ(exponent, 5);
+ EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
+ EXPECT_EQ(3, LIBC_NAMESPACE::shared::ilogbf16(8.0f16));
+
EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
}
More information about the libc-commits
mailing list