[libc-commits] [libc] [llvm] [libc][math] Refactor llogbf16 to header-only (PR #178078)

via libc-commits libc-commits at lists.llvm.org
Tue Jan 27 02:47:55 PST 2026


https://github.com/Abdelrhmansersawy updated https://github.com/llvm/llvm-project/pull/178078

>From a9c1a49d12723c14be84c579bdd1906b87d07bd3 Mon Sep 17 00:00:00 2001
From: abdelrhmansersawy <abdelrhmansersawy at gmail.com>
Date: Tue, 27 Jan 2026 01:13:00 +0200
Subject: [PATCH] [libc][math] Refactor llogbf16 to header-only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/llogbf16.h                   | 29 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/llogbf16.h            | 34 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/llogbf16.cpp            |  8 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 16 ++++++++-
 9 files changed, 94 insertions(+), 9 deletions(-)
 create mode 100644 libc/shared/math/llogbf16.h
 create mode 100644 libc/src/__support/math/llogbf16.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7b2a12ac2bfaa..20eecb730c86c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -77,6 +77,7 @@
 #include "math/ldexpf16.h"
 #include "math/llogb.h"
 #include "math/llogbf.h"
+#include "math/llogbf16.h"
 #include "math/log.h"
 #include "math/log10.h"
 #include "math/log1p.h"
diff --git a/libc/shared/math/llogbf16.h b/libc/shared/math/llogbf16.h
new file mode 100644
index 0000000000000..5d5e2b400e19b
--- /dev/null
+++ b/libc/shared/math/llogbf16.h
@@ -0,0 +1,29 @@
+//===-- Shared llogbf16 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_LLOGBF16_H
+#define LLVM_LIBC_SHARED_MATH_LLOGBF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/llogbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::llogbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_LLOGBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0efa3dfc9c25..d984508dee21e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -688,6 +688,16 @@ add_header_library(
     libc.include.llvm-libc-types.float128
 )
 
+add_header_library(
+  llogbf16
+  HDRS
+    llogbf16.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   ilogbf
   HDRS
diff --git a/libc/src/__support/math/llogbf16.h b/libc/src/__support/math/llogbf16.h
new file mode 100644
index 0000000000000..55ddd3b9e2569
--- /dev/null
+++ b/libc/src/__support/math/llogbf16.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for llogbf16 ----------------------*- 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_LLOGBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LLOGBF16_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 static constexpr long llogbf16(float16 x) {
+  return fputil::intlogb<long>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LLOGBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index f369206c39a16..c4aa6a02a37a6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1832,8 +1832,7 @@ add_entrypoint_object(
   HDRS
     ../llogbf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.llogbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/llogbf16.cpp b/libc/src/math/generic/llogbf16.cpp
index c792e9034d88f..916a5b9d46077 100644
--- a/libc/src/math/generic/llogbf16.cpp
+++ b/libc/src/math/generic/llogbf16.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/llogbf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/llogbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, llogbf16, (float16 x)) {
-  return fputil::intlogb<long>(x);
-}
+LLVM_LIBC_FUNCTION(long, llogbf16, (float16 x)) { return math::llogbf16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 0a19dfd9dbba9..da364665df829 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -82,6 +82,7 @@ add_fp_unittest(
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
     libc.src.__support.math.llogbf
+    libc.src.__support.math.llogbf16
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index da1c62520042d..8179c0fbb482f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -49,6 +49,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logbf16(1.0f16));
+  EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbf16(1.0f16));
 
   EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 9d6641a5847f1..226e1ce60d488 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3040,6 +3040,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_llogbf16",
+    hdrs = ["src/__support/math/llogbf16.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_log",
     hdrs = ["src/__support/math/log.h"],
@@ -4761,7 +4772,10 @@ libc_math_function(name = "llogbl")
 
 libc_math_function(name = "llogbf128")
 
-libc_math_function(name = "llogbf16")
+libc_math_function(
+    name = "llogbf16",
+    additional_deps = [":__support_math_llogbf16"],
+)
 
 libc_math_function(name = "llrint")
 



More information about the libc-commits mailing list