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

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 08:26:21 PST 2026


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

>From ad9ec2a20acf45185f62c3dba114c9a24a414a70 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/9] [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 |  1 +
 5 files changed, 52 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 be3f724e10a8b..55f1652ac2c4c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2069,7 +2069,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 73d6d836227b3..a662f7b524096 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -142,6 +142,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0));
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(1.0));
   EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::logb(1.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {

>From 1b2b3a328b30c4e7229cac5218437c4ed4310468 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/9] [libc][math] Add static to logb and reorder entries for
 consistency

---
 libc/src/__support/math/logb.h                |  2 +-
 .../llvm-project-overlay/libc/BUILD.bazel     | 70 +++++--------------
 2 files changed, 18 insertions(+), 54 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/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b91a13b29d3c6..da7591136baf1 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3039,24 +3039,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"],
@@ -3211,6 +3193,8 @@ libc_support_library(
 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",
     ],
@@ -4278,7 +4262,7 @@ libc_math_function(name = "ddivf128")
 libc_math_function(
     name = "dfmal",
     additional_deps = [
-        ":__support_math_dfmal",
+        ":__support_fputil_fma",
     ],
 )
 
@@ -4456,7 +4440,7 @@ libc_math_function(name = "f16divl")
 libc_math_function(
     name = "f16fma",
     additional_deps = [
-        ":__support_math_f16fma",
+        ":__support_fputil_fma",
     ],
 )
 
@@ -4812,7 +4796,7 @@ libc_math_function(name = "fromfpxf16")
 libc_math_function(
     name = "fsqrt",
     additional_deps = [
-        ":__support_math_fsqrt",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4826,8 +4810,7 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtf128",
     additional_deps = [
-        ":__support_math_fsqrtf128",
-        ":errno",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4852,8 +4835,8 @@ libc_math_function(name = "hypot")
 libc_math_function(
     name = "hypotf",
     additional_deps = [
-        ":__support_math_hypotf",
-        ":errno",
+        ":__support_fputil_double_double",
+        ":__support_fputil_sqrt",
     ],
 )
 
@@ -4872,17 +4855,9 @@ libc_math_function(
     ],
 )
 
-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",
@@ -4930,10 +4905,7 @@ libc_math_function(
     ],
 )
 
-libc_math_function(
-    name = "llogbf",
-    additional_deps = [":__support_math_llogbf"],
-)
+libc_math_function(name = "llogbf")
 
 libc_math_function(name = "llogbl")
 
@@ -5060,28 +5032,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 1c7044c29d162fcce231743e4a0ea84839d6aab9 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/9] [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.

>From d6ff6929c92f8e4c2b4be85d30a6d368456616d2 Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Fri, 6 Feb 2026 19:49:16 +0530
Subject: [PATCH 4/9] Adapt to comply with updated signatures

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

diff --git a/libc/src/__support/math/logb.h b/libc/src/__support/math/logb.h
index 9b575b0beb602..c039026407e51 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 static constexpr double logb(double x) { return fputil::logb(x); }
+LIBC_INLINE constexpr double logb(double x) { return fputil::logb(x); }
 
 } // namespace math
 

>From 145c6c8c3f2f47fbb1f699308080a942f52d1d98 Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Sun, 8 Feb 2026 11:59:06 +0530
Subject: [PATCH 5/9] [libc][math] Add logb bazel support

---
 .../llvm-project-overlay/libc/BUILD.bazel     | 79 +++++++++++++++----
 1 file changed, 65 insertions(+), 14 deletions(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index da7591136baf1..08a78e45d1ba7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3039,6 +3039,24 @@ 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"],
@@ -3173,6 +3191,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_logb",
+    hdrs = ["src/__support/math/logb.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_logbf",
     hdrs = ["src/__support/math/logbf.h"],
@@ -3193,8 +3221,6 @@ libc_support_library(
 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",
     ],
@@ -4262,7 +4288,7 @@ libc_math_function(name = "ddivf128")
 libc_math_function(
     name = "dfmal",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_dfmal",
     ],
 )
 
@@ -4440,7 +4466,7 @@ libc_math_function(name = "f16divl")
 libc_math_function(
     name = "f16fma",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_f16fma",
     ],
 )
 
@@ -4796,7 +4822,7 @@ libc_math_function(name = "fromfpxf16")
 libc_math_function(
     name = "fsqrt",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrt",
     ],
 )
 
@@ -4810,7 +4836,8 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtf128",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtf128",
+        ":errno",
     ],
 )
 
@@ -4835,8 +4862,8 @@ libc_math_function(name = "hypot")
 libc_math_function(
     name = "hypotf",
     additional_deps = [
-        ":__support_fputil_double_double",
-        ":__support_fputil_sqrt",
+        ":__support_math_hypotf",
+        ":errno",
     ],
 )
 
@@ -4855,9 +4882,17 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "ilogbf")
+libc_math_function(
+    name = "ilogbf",
+    additional_deps = [
+        ":__support_math_ilogbf",
+    ],
+)
 
-libc_math_function(name = "ilogbl")
+libc_math_function(
+    name = "ilogbl",
+    additional_deps = ["__support_math_ilogbl"],
+)
 
 libc_math_function(
     name = "ilogbf128",
@@ -4905,7 +4940,10 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "llogbf")
+libc_math_function(
+    name = "llogbf",
+    additional_deps = [":__support_math_llogbf"],
+)
 
 libc_math_function(name = "llogbl")
 
@@ -5039,13 +5077,26 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "logbf")
+libc_math_function(
+    name = "logbf",
+    additional_deps = [
+        ":__support_math_logbf",
+    ],
+)
 
 libc_math_function(name = "logbl")
 
-libc_math_function(name = "logbf128")
+libc_math_function(
+    name = "logbf128",
+    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")
 

>From 5e35db010f2f2d0c8fb5897888d3bdb04641dcd7 Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Wed, 11 Feb 2026 20:16:08 +0530
Subject: [PATCH 6/9] Add logb header in shared math header and shared tests
 CMakeLists

---
 libc/shared/math.h              | 1 +
 libc/test/shared/CMakeLists.txt | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 110b4bfe81073..c8647808a25c0 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -88,6 +88,7 @@
 #include "math/log10.h"
 #include "math/log1p.h"
 #include "math/log2.h"
+#include "math/logb.h"
 #include "math/logbf.h"
 #include "math/logbf128.h"
 #include "math/logbf16.h"
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7143ce6e84458..c878778fc9266 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -79,6 +79,7 @@ add_fp_unittest(
     libc.src.__support.math.log10
     libc.src.__support.math.log1p
     libc.src.__support.math.log2
+    libc.src.__support.math.logb
     libc.src.__support.math.logbf
     libc.src.__support.math.logbf128
     libc.src.__support.math.logbf16

>From 47aec69403407b474162b35c5f79c04d40cf6ac1 Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Wed, 11 Feb 2026 20:24:37 +0530
Subject: [PATCH 7/9] Add logb header in math CMakeLists

---
 libc/src/__support/math/CMakeLists.txt | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 3d43eccfdbe2c..b636409852409 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1292,6 +1292,14 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  logb
+  HDRS
+    logb.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+)
+
 add_header_library(
   log10
   HDRS

>From ffccd76d12840d56ae81f89de74251d2bc43f2c6 Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Thu, 12 Feb 2026 21:14:34 +0530
Subject: [PATCH 8/9] Add missing headers

---
 libc/src/__support/math/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b636409852409..ea410c12a35a2 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1298,6 +1298,8 @@ add_header_library(
     logb.h
   DEPENDS
     libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.common
+    libc.src.__support.macros.config
 )
 
 add_header_library(

>From 0bbfaf1f4859b8ba721e877245dfd5bb240aacef Mon Sep 17 00:00:00 2001
From: Dhruv <dhruv.baweja4 at gmail.com>
Date: Fri, 13 Feb 2026 21:55:59 +0530
Subject: [PATCH 9/9] Fix BUILD.bazel syntax error

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 958c0d11f846e..ab33dffb7c70a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3219,6 +3219,10 @@ libc_support_library(
         ":__support_common",
         ":__support_fputil_manipulation_functions",
         ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
     name = "__support_math_log10f16",
     hdrs = ["src/__support/math/log10f16.h"],
     deps = [



More information about the llvm-commits mailing list