[libc-commits] [libc] [llvm] [libc][math] Refactor llogb to Header Only (PR #175524)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 12 04:05:41 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Vedant Neve (0bVdnt)

<details>
<summary>Changes</summary>

Refactors the llogb math function to be header-only and constexpr.
This moves the implementation to src/__support/math/llogb.h and updates the entrypoint to be a wrapper.

Fixes #<!-- -->175360 

---
Full diff: https://github.com/llvm/llvm-project/pull/175524.diff


7 Files Affected:

- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/llogb.h (+23) 
- (modified) libc/src/__support/math/CMakeLists.txt (+11) 
- (added) libc/src/__support/math/llogb.h (+64) 
- (modified) libc/src/math/generic/CMakeLists.txt (+2-1) 
- (modified) libc/src/math/generic/llogb.cpp (+3-3) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+18-1) 


``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7fb4c43f509c4..49dbe5ff9027e 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -63,6 +63,7 @@
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
+#include "math/llogb.h"
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
diff --git a/libc/shared/math/llogb.h b/libc/shared/math/llogb.h
new file mode 100644
index 0000000000000..9d650ff370539
--- /dev/null
+++ b/libc/shared/math/llogb.h
@@ -0,0 +1,23 @@
+//===-- Shared llogb 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_LLOGB_H
+#define LLVM_LIBC_SHARED_MATH_LLOGB_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/llogb.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::llogb;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LLOGB_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 741da7432c94f..f2e344a19e75d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -924,6 +924,17 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  llogb
+  HDRS
+    llogb.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+)
+
 add_header_library(
   range_reduction_double
   HDRS
diff --git a/libc/src/__support/math/llogb.h b/libc/src/__support/math/llogb.h
new file mode 100644
index 0000000000000..b77ee11f9a411
--- /dev/null
+++ b/libc/src/__support/math/llogb.h
@@ -0,0 +1,64 @@
+//===-- Implementation header for llogb --------------------------*- 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_LLOGB_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LLOGB_H
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"  // LIBC_UNLIKELY
+#include <limits.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr long llogb(double x) {
+  using FPBits = fputil::FPBits<double>;
+  FPBits bits(x);
+
+  if (LIBC_UNLIKELY(bits.is_zero())) {
+    return -LONG_MAX - 1; // FP_LLOGB0
+  }
+
+  if (LIBC_UNLIKELY(bits.is_inf())) {
+    return LONG_MAX;
+  }
+
+  if (LIBC_UNLIKELY(bits.is_nan())) {
+#ifdef __FP_LOGBNAN_MIN
+    return -LONG_MAX - 1;
+#else
+    return LONG_MAX; // FP_LLOGBNAN
+#endif
+  }
+
+  int biased_exp = bits.get_biased_exponent();
+
+  // Handle normal numbers
+  if (biased_exp > 0) {
+    return biased_exp - FPBits::EXP_BIAS;
+  }
+
+  // Handle subnormal numbers
+  // Formula: log2(mantissa * 2^-1074) = log2(mantissa) - 1074
+  uint64_t mantissa = bits.get_mantissa();
+
+  // Define constants
+  constexpr int EXP_ADJUSTMENT = FPBits::EXP_BIAS + FPBits::FRACTION_LEN - 1;
+  constexpr int STORAGE_MSB = (sizeof(uint64_t) * CHAR_BIT) - 1;
+
+  // log2(mantissa) = STORAGE_MSB - countl_zero(mantissa)
+  return (STORAGE_MSB - cpp::countl_zero(mantissa)) - EXP_ADJUSTMENT;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LLOGB_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 9c0da076b6cf0..aabd6899ffb02 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1828,7 +1828,8 @@ add_entrypoint_object(
   HDRS
     ../llogb.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.llogb
+    libc.src.__support.macros.config
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/llogb.cpp b/libc/src/math/generic/llogb.cpp
index 3850ac00d98d9..edca94e1e734c 100644
--- a/libc/src/math/generic/llogb.cpp
+++ b/libc/src/math/generic/llogb.cpp
@@ -7,12 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/llogb.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
+#include "src/__support/math/llogb.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, llogb, (double x)) { return fputil::intlogb<long>(x); }
+LLVM_LIBC_FUNCTION(long, llogb, (double x)) { return math::llogb(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 210c25dddd0b9..af88b034db423 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3118,6 +3118,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_llogb",
+    hdrs = ["src/__support/math/llogb.h"],
+    deps = [
+        ":__support_cpp_bit",
+        ":__support_fputil_fp_bits",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+    ],
+)
+
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -4337,7 +4348,13 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "llogb")
+libc_math_function(
+    name = "llogb",
+    additional_deps = [
+        ":__support_math_llogb",
+        ":__support_fputil_fp_bits",
+    ],
+)
 
 libc_math_function(name = "llogbf")
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/175524


More information about the libc-commits mailing list