[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 10:26:06 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: mitchell (zeyi2)

<details>
<summary>Changes</summary>

Closes [#<!-- -->175346](https://github.com/llvm/llvm-project/issues/175346), Part of #<!-- -->175344

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


7 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/ilogbf16.h (+29) 
- (modified) libc/src/__support/math/CMakeLists.txt (+11) 
- (added) libc/src/__support/math/ilogbf16.h (+34) 
- (modified) libc/src/math/generic/CMakeLists.txt (+1) 
- (modified) libc/src/math/generic/ilogbf16.cpp (+2-4) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+17-1) 


``````````diff
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")
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/175450


More information about the libc-commits mailing list