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

Vedant Neve via libc-commits libc-commits at lists.llvm.org
Mon Jan 19 08:35:21 PST 2026


https://github.com/0bVdnt updated https://github.com/llvm/llvm-project/pull/175524

>From add161492abbf6b4bc63aeea0ad557d226111c6e Mon Sep 17 00:00:00 2001
From: Vedant Neve <vedantneve13 at gmail.com>
Date: Mon, 12 Jan 2026 11:46:46 +0000
Subject: [PATCH] [libc][math] Refactor llogb to Header Only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/llogb.h                      | 23 ++++++
 libc/src/__support/math/CMakeLists.txt        | 12 +++
 libc/src/__support/math/llogb.h               | 73 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/llogb.cpp               |  7 +-
 .../llvm-project-overlay/libc/BUILD.bazel     | 20 ++++-
 7 files changed, 133 insertions(+), 6 deletions(-)
 create mode 100644 libc/shared/math/llogb.h
 create mode 100644 libc/src/__support/math/llogb.h

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..12bb21a74ddb8 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -924,6 +924,18 @@ 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.FPUtil.fenv_impl
+    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..1ac41a0a7d2c1
--- /dev/null
+++ b/libc/src/__support/math/llogb.h
@@ -0,0 +1,73 @@
+//===-- 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/FEnvImpl.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())) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return -LONG_MAX - 1; // FP_LLOGB0
+  }
+
+  if (LIBC_UNLIKELY(bits.is_inf())) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return LONG_MAX;
+  }
+
+  if (LIBC_UNLIKELY(bits.is_nan())) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+#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
+  // Note: For double, the exponent range (-1022 to 1023) always fits in long,
+  // so no range check is needed here.
+  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..691a1e6062109 100644
--- a/libc/src/math/generic/llogb.cpp
+++ b/libc/src/math/generic/llogb.cpp
@@ -7,12 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/llogb.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/llogb.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..2f2b52a7448a5 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3118,6 +3118,18 @@ 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_fputil_fenv_impl",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+    ],
+)
+
 libc_support_library(
     name = "__support_range_reduction_double",
     hdrs = [
@@ -4337,7 +4349,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")
 



More information about the libc-commits mailing list