[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:48:51 PST 2026


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

>From 4551880a4b71d1775eca3ca243e28b6205e7011d 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 b75e60c4f1973..8102ff38584bd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -68,6 +68,7 @@
 #include "math/ldexpf16.h"
 #include "math/log.h"
 #include "math/logbf128.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 97852c8688182..5bf0648be02e2 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1070,6 +1070,15 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+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
@@ -1105,3 +1114,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 6fb260a7523d4..11f1e0dd5e8b4 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 3a9bf9f8d4cf4..cdde9887603ee 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -67,4 +67,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 84563d851a002..a71f97cba2fed 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 f1d390207e45e..d6688696236dd 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4559,7 +4559,12 @@ libc_math_function(
     additional_deps = [":__support_math_logbf128"],
 )
 
-libc_math_function(name = "logbf16")
+libc_math_function(
+    name = "logbf16",
+    additional_deps = [
+        ":__support_math_logbf16",
+    ],
+)
 
 libc_math_function(name = "lrint")
 
@@ -7889,3 +7894,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 2c346e77d9f96b272a099f5ebc18a6bb5d40b2cb 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/shared/math/logbf16.h                       |  2 +-
 libc/src/__support/math/logbf16.h                |  3 +--
 libc/test/shared/CMakeLists.txt                  |  2 +-
 libc/test/shared/shared_math_test.cpp            |  2 +-
 .../bazel/llvm-project-overlay/libc/BUILD.bazel  | 16 ++++++++--------
 5 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/libc/shared/math/logbf16.h b/libc/shared/math/logbf16.h
index a708222236921..bf1331e6daf07 100644
--- a/libc/shared/math/logbf16.h
+++ b/libc/shared/math/logbf16.h
@@ -1,4 +1,4 @@
-//===-- Shared logbf16 function-------------------------------- -*- C++ -*-===//
+//===-- 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.
diff --git a/libc/src/__support/math/logbf16.h b/libc/src/__support/math/logbf16.h
index b85300e8fa14c..f7e24c68e49ed 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/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index cdde9887603ee..a2fa0146adadb 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -61,11 +61,11 @@ add_fp_unittest(
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
     libc.src.__support.math.logbf128
+    libc.src.__support.math.logbf16
     libc.src.__support.math.ldexpf
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
     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 a71f97cba2fed..78cf06a46eae4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -42,10 +42,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(exponent, 5);
 
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logbf16(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 d6688696236dd..6d55f95b02b8f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2914,6 +2914,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"],
@@ -7894,11 +7902,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