[libc-commits] [libc] [llvm] [libc][math] Refactor logb implementation to header-only (PR #175719)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 19 08:34:12 PST 2026


https://github.com/dhr412 updated https://github.com/llvm/llvm-project/pull/175719

>From 7e561b2e51cada0f98f44aa112e0a7c1513d5c0b Mon Sep 17 00:00:00 2001
From: Dhruv <62135445+dhr412 at users.noreply.github.com>
Date: Tue, 13 Jan 2026 12:46:24 +0530
Subject: [PATCH 1/3] [libc][math] Refactor logb implementation to header-only

Move the implementation of logb to src/__support/math/logb.h so it can be shared and used as a header-only utility. Update the entrypoint and shared library wrapper to use the new implementation.
---
 libc/shared/math/logb.h               | 22 ++++++++++++++++++++++
 libc/src/__support/math/logb.h        | 26 ++++++++++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt  |  2 +-
 libc/src/math/generic/logb.cpp        |  6 ++----
 libc/test/shared/shared_math_test.cpp |  2 ++
 5 files changed, 53 insertions(+), 5 deletions(-)
 create mode 100644 libc/shared/math/logb.h
 create mode 100644 libc/src/__support/math/logb.h

diff --git a/libc/shared/math/logb.h b/libc/shared/math/logb.h
new file mode 100644
index 0000000000000..0e26724d70c6e
--- /dev/null
+++ b/libc/shared/math/logb.h
@@ -0,0 +1,22 @@
+//===-- Shared logb 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_LOGB_H
+#define LLVM_LIBC_SHARED_MATH_LOGB_H
+
+#include "src/__support/math/logb.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::logb;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LOGB_H
diff --git a/libc/src/__support/math/logb.h b/libc/src/__support/math/logb.h
new file mode 100644
index 0000000000000..c039026407e51
--- /dev/null
+++ b/libc/src/__support/math/logb.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for logb -------------------------*- 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_LOGB_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGB_H
+
+#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 double logb(double x) { return fputil::logb(x); }
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOGB_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b578d1805f2a8..fc423be2e858e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2165,7 +2165,7 @@ add_entrypoint_object(
   HDRS
     ../logb.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.logb
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/logb.cpp b/libc/src/math/generic/logb.cpp
index 4b8fde966aab1..f2d154eeebdce 100644
--- a/libc/src/math/generic/logb.cpp
+++ b/libc/src/math/generic/logb.cpp
@@ -7,12 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/logb.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/logb.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(double, logb, (double x)) { return fputil::logb(x); }
+LLVM_LIBC_FUNCTION(double, logb, (double x)) { return math::logb(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 88e6ecc98a0a7..7949cbe939df4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -103,6 +103,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
+
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::logb(1.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {

>From c0208d7163d9ba131e34e3cc5332c6f4c3d27dd3 Mon Sep 17 00:00:00 2001
From: Dhruv <62135445+dhr412 at users.noreply.github.com>
Date: Fri, 16 Jan 2026 20:49:12 +0530
Subject: [PATCH 2/3] [libc][math] Add static to logb and reorder entries for
 consistency

---
 libc/src/__support/math/logb.h                |   2 +-
 libc/test/shared/shared_math_test.cpp         |   3 +-
 .../llvm-project-overlay/libc/BUILD.bazel     | 150 ++----------------
 3 files changed, 19 insertions(+), 136 deletions(-)

diff --git a/libc/src/__support/math/logb.h b/libc/src/__support/math/logb.h
index c039026407e51..9b575b0beb602 100644
--- a/libc/src/__support/math/logb.h
+++ b/libc/src/__support/math/logb.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE constexpr double logb(double x) { return fputil::logb(x); }
+LIBC_INLINE static constexpr double logb(double x) { return fputil::logb(x); }
 
 } // namespace math
 
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7949cbe939df4..b76073d046ada 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -102,9 +102,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
-  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
-
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::logb(1.0));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 90b1da0e3376e..643c31340049f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,16 +2804,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_f16fma",
-    hdrs = ["src/__support/math/f16fma.h"],
-    deps = [
-        ":__support_fputil_fma",
-        ":__support_macros_config",
-        ":llvm_libc_macros_float16_macros",
-    ],
-)
-
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -2823,24 +2813,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_fsqrt",
-    hdrs = ["src/__support/math/fsqrt.h"],
-    deps = [
-        ":__support_fputil_sqrt",
-    ],
-)
-
-libc_support_library(
-    name = "__support_math_fsqrtf128",
-    hdrs = ["src/__support/math/fsqrtf128.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_sqrt",
-        ":__support_macros_config",
-    ],
-)
-
 libc_support_library(
     name = "__support_math_inv_trigf_utils",
     hdrs = ["src/__support/math/inv_trigf_utils.h"],
@@ -2881,24 +2853,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_ilogbf",
-    hdrs = ["src/__support/math/ilogbf.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_manipulation_functions",
-        ":__support_macros_config",
-    ],
-)
-
-libc_support_library(
-    name = "__support_math_ilogbl",
-    hdrs = ["src/__support/math/ilogbl.h"],
-    deps = [
-        ":__support_fputil_manipulation_functions",
-    ],
-)
-
 libc_support_library(
     name = "__support_math_ldexpf128",
     hdrs = ["src/__support/math/ldexpf128.h"],
@@ -2927,14 +2881,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_llogbf",
-    hdrs = ["src/__support/math/llogbf.h"],
-    deps = [
-        ":__support_fputil_manipulation_functions",
-    ],
-)
-
 libc_support_library(
     name = "__support_math_log",
     hdrs = ["src/__support/math/log.h"],
@@ -2953,25 +2899,8 @@ libc_support_library(
 )
 
 libc_support_library(
-    name = "__support_math_logbf",
-    hdrs = ["src/__support/math/logbf.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_manipulation_functions",
-    ],
-)
-
-libc_support_library(
-    name = "__support_math_logbf128",
-    hdrs = ["src/__support/math/logbf128.h"],
-    deps = [
-        ":__support_fputil_manipulation_functions",
-    ],
-)
-
-libc_support_library(
-    name = "__support_math_logbf16",
-    hdrs = ["src/__support/math/logbf16.h"],
+    name = "__support_math_logb",
+    hdrs = ["src/__support/math/logb.h"],
     deps = [
         ":__support_fputil_manipulation_functions",
     ],
@@ -3225,16 +3154,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_dfmal",
-    hdrs = ["src/__support/math/dfmal.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_fma",
-        ":__support_macros_config",
-    ],
-)
-
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -3330,21 +3249,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_hypotf",
-    hdrs = ["src/__support/math/hypotf.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_double_double",
-        ":__support_fputil_fenv_impl",
-        ":__support_fputil_fp_bits",
-        ":__support_fputil_multiply_add",
-        ":__support_fputil_sqrt",
-        ":__support_macros_config",
-        ":__support_macros_optimization",
-    ],
-)
-
 ############################### complex targets ################################
 
 libc_function(
@@ -3850,7 +3754,7 @@ libc_math_function(name = "ddivf128")
 libc_math_function(
     name = "dfmal",
     additional_deps = [
-        ":__support_math_dfmal",
+        ":__support_fputil_fma",
     ],
 )
 
@@ -4027,7 +3931,7 @@ libc_math_function(name = "f16divl")
 libc_math_function(
     name = "f16fma",
     additional_deps = [
-        ":__support_math_f16fma",
+        ":__support_fputil_fma",
     ],
 )
 
@@ -4382,7 +4286,7 @@ libc_math_function(name = "fromfpxf16")
 libc_math_function(
     name = "fsqrt",
     additional_deps = [
-        ":__support_math_fsqrt",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4396,8 +4300,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtf128",
     additional_deps = [
-        ":__support_math_fsqrtf128",
-        ":errno",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4422,8 +4325,8 @@ libc_math_function(name = "hypot")
 libc_math_function(
     name = "hypotf",
     additional_deps = [
-        ":__support_math_hypotf",
-        ":errno",
+        ":__support_fputil_double_double",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4437,17 +4340,9 @@ libc_math_function(
 
 libc_math_function(name = "ilogb")
 
-libc_math_function(
-    name = "ilogbf",
-    additional_deps = [
-        ":__support_math_ilogbf",
-    ],
-)
+libc_math_function(name = "ilogbf")
 
-libc_math_function(
-    name = "ilogbl",
-    additional_deps = ["__support_math_ilogbl"],
-)
+libc_math_function(name = "ilogbl")
 
 libc_math_function(name = "ilogbf128")
 
@@ -4485,10 +4380,7 @@ libc_math_function(
 
 libc_math_function(name = "llogb")
 
-libc_math_function(
-    name = "llogbf",
-    additional_deps = [":__support_math_llogbf"],
-)
+libc_math_function(name = "llogbf")
 
 libc_math_function(name = "llogbl")
 
@@ -4638,28 +4530,20 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "logb")
-
 libc_math_function(
-    name = "logbf",
+    name = "logb",
     additional_deps = [
-        ":__support_math_logbf",
+        ":__support_math_logb",
     ],
 )
 
+libc_math_function(name = "logbf")
+
 libc_math_function(name = "logbl")
 
-libc_math_function(
-    name = "logbf128",
-    additional_deps = [":__support_math_logbf128"],
-)
+libc_math_function(name = "logbf128")
 
-libc_math_function(
-    name = "logbf16",
-    additional_deps = [
-        ":__support_math_logbf16",
-    ],
-)
+libc_math_function(name = "logbf16")
 
 libc_math_function(name = "lrint")
 

>From 32302225b2fecd5b2db9249387c220496e1a717e Mon Sep 17 00:00:00 2001
From: Dhruv <62135445+dhr412 at users.noreply.github.com>
Date: Mon, 19 Jan 2026 21:37:03 +0530
Subject: [PATCH 3/3] [libc][math] Fix comment alignment

---
 libc/shared/math/logb.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/shared/math/logb.h b/libc/shared/math/logb.h
index 0e26724d70c6e..5e1dfc519b390 100644
--- a/libc/shared/math/logb.h
+++ b/libc/shared/math/logb.h
@@ -1,4 +1,4 @@
-//===-- Shared logb function --------------------------------*- C++ -*-===//
+//===-- Shared logb 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.



More information about the libc-commits mailing list