[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:07:20 PST 2026
https://github.com/dhr412 updated https://github.com/llvm/llvm-project/pull/175719
>From 33936ef2ba47e786363a575ef0f32cc955c0827d 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.h | 1 +
libc/shared/math/logb.h | 22 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 8 ++++++
libc/src/__support/math/logb.h | 26 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/logb.cpp | 6 ++---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 2 ++
.../llvm-project-overlay/libc/BUILD.bazel | 15 ++++++++++-
9 files changed, 77 insertions(+), 6 deletions(-)
create mode 100644 libc/shared/math/logb.h
create mode 100644 libc/src/__support/math/logb.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..83da725c9cf42 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -65,6 +65,7 @@
#include "math/ldexpf128.h"
#include "math/ldexpf16.h"
#include "math/log.h"
+#include "math/logb.h"
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.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/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..495b80afcb8cb 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -670,6 +670,14 @@ add_header_library(
libc.src.__support.FPUtil.manipulation_functions
)
+add_header_library(
+ logb
+ HDRS
+ logb.h
+ DEPENDS
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
add_header_library(
exp_constants
HDRS
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 6de4cf376bf02..d72ad59b7c1e6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2163,7 +2163,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/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..fddc5eb3228ec 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -58,6 +58,7 @@ add_fp_unittest(
libc.src.__support.math.frexpf16
libc.src.__support.math.ilogbf16
libc.src.__support.math.log
+ libc.src.__support.math.logb
libc.src.__support.math.ldexpf
libc.src.__support.math.ldexpf128
libc.src.__support.math.ldexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..8a317b186f6c7 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -94,6 +94,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(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));
}
#ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..902830a1fb6ee 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2881,6 +2881,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_logb",
+ hdrs = ["src/__support/math/logb.h"],
+ deps = [
+ ":__support_fputil_manipulation_functions",
+ ],
+)
+
libc_support_library(
name = "__support_math_log",
hdrs = ["src/__support/math/log.h"],
@@ -4522,7 +4530,12 @@ libc_math_function(
],
)
-libc_math_function(name = "logb")
+libc_math_function(
+ name = "logb",
+ additional_deps = [
+ ":__support_math_logb",
+ ],
+)
libc_math_function(name = "logbf")
>From f5a9c78d0caa2cb3612aed0ec0a65b849dc200cb 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 +--
.../bazel/llvm-project-overlay/libc/BUILD.bazel | 16 ++++++++--------
3 files changed, 10 insertions(+), 11 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 8a317b186f6c7..c92a1c1f16ee9 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -93,9 +93,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(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));
}
#ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 902830a1fb6ee..643c31340049f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2881,14 +2881,6 @@ libc_support_library(
],
)
-libc_support_library(
- name = "__support_math_logb",
- hdrs = ["src/__support/math/logb.h"],
- deps = [
- ":__support_fputil_manipulation_functions",
- ],
-)
-
libc_support_library(
name = "__support_math_log",
hdrs = ["src/__support/math/log.h"],
@@ -2906,6 +2898,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_logb",
+ hdrs = ["src/__support/math/logb.h"],
+ deps = [
+ ":__support_fputil_manipulation_functions",
+ ],
+)
+
libc_support_library(
name = "__support_math_exp_constants",
hdrs = ["src/__support/math/exp_constants.h"],
>From 97cc62d471c109fdfcb0b707e835da9198eb272e 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