[libc-commits] [libc] [llvm] [libc][math] Refactor logbf16 to Header Only. (PR #176231)

via libc-commits libc-commits at lists.llvm.org
Fri Jan 16 13:12:13 PST 2026


https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/176231

>From aaf28b94a517cc08978a043c66ae882616016393 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 21:35:52 +0200
Subject: [PATCH 1/2] [libc][math] Refactor logbf16 to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/logbf16.h                    | 29 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/logbf16.h             | 34 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/logbf16.cpp             |  7 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 +++++++-
 9 files changed, 95 insertions(+), 8 deletions(-)
 create mode 100644 libc/shared/math/logbf16.h
 create mode 100644 libc/src/__support/math/logbf16.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2e7bbe1ef13be..7ad814d0513af 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -67,6 +67,7 @@
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
 #include "math/log.h"
+#include "math/logbf16.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
diff --git a/libc/shared/math/logbf16.h b/libc/shared/math/logbf16.h
new file mode 100644
index 0000000000000..a708222236921
--- /dev/null
+++ b/libc/shared/math/logbf16.h
@@ -0,0 +1,29 @@
+//===-- Shared logbf16 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_LOGBF16_H
+#define LLVM_LIBC_SHARED_MATH_LOGBF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/logbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::logbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b45ccfc0eb3cb..8d964be14ff47 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1061,6 +1061,15 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  logbf16
+  HDRS
+    logbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   log_range_reduction
   HDRS
@@ -1096,3 +1105,4 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.macros.config
 )
+
diff --git a/libc/src/__support/math/logbf16.h b/libc/src/__support/math/logbf16.h
new file mode 100644
index 0000000000000..b85300e8fa14c
--- /dev/null
+++ b/libc/src/__support/math/logbf16.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for logbf16----------------------- -*- 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_LOGBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF16_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 float16 logbf16(float16 x) {
+  return fputil::logb(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 009a3033083bd..3c6600cdf1000 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2193,8 +2193,8 @@ add_entrypoint_object(
   HDRS
     ../logbf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.logbf16
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/logbf16.cpp b/libc/src/math/generic/logbf16.cpp
index 6e286a266a137..ec6a3ff636692 100644
--- a/libc/src/math/generic/logbf16.cpp
+++ b/libc/src/math/generic/logbf16.cpp
@@ -7,12 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logbf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
+#include "src/__support/math/logbf16.h"
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, logbf16, (float16 x)) { return fputil::logb(x); }
+LLVM_LIBC_FUNCTION(float16, logbf16, (float16 x)) { return math::logbf16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 58214bf6f88eb..6854bf25c9882 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -66,4 +66,5 @@ add_fp_unittest(
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
+    libc.src.__support.math.logbf16
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 6b78de0281347..4807fb32e2226 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -44,6 +44,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
 
   EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
+
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logbf16(1.0f16));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4251f20a5dfcf..432abca4f1f0c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4548,7 +4548,12 @@ libc_math_function(name = "logbl")
 
 libc_math_function(name = "logbf128")
 
-libc_math_function(name = "logbf16")
+libc_math_function(
+    name = "logbf16",
+    additional_deps = [
+        ":__support_math_logbf16",
+    ],
+)
 
 libc_math_function(name = "lrint")
 
@@ -7878,3 +7883,11 @@ libc_function(
         ":types_wchar_t",
     ],
 )
+
+libc_support_library(
+    name = "__support_math_logbf16",
+    hdrs = ["src/__support/math/logbf16.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+    ],
+)

>From df55a6cf6d6e313cfbb55c75cb0d9432346dbcfc Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 23:25:23 +0200
Subject: [PATCH 2/2] fix formatting

---
 libc/src/__support/math/logbf16.h                |  3 +--
 .../bazel/llvm-project-overlay/libc/BUILD.bazel  | 16 ++++++++--------
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/libc/src/__support/math/logbf16.h b/libc/src/__support/math/logbf16.h
index b85300e8fa14c..6ca4af7b0d560 100644
--- a/libc/src/__support/math/logbf16.h
+++ b/libc/src/__support/math/logbf16.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for logbf16----------------------- -*- C++
-//-*-===//
+//===-- Implementation header for logbf16------------------------*- 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/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 432abca4f1f0c..5fb1449ce6083 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2906,6 +2906,14 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_logbf16",
+    hdrs = ["src/__support/math/logbf16.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_exp_constants",
     hdrs = ["src/__support/math/exp_constants.h"],
@@ -7883,11 +7891,3 @@ libc_function(
         ":types_wchar_t",
     ],
 )
-
-libc_support_library(
-    name = "__support_math_logbf16",
-    hdrs = ["src/__support/math/logbf16.h"],
-    deps = [
-        ":__support_fputil_manipulation_functions",
-    ],
-)



More information about the libc-commits mailing list