[libc][math] Refactor llogbl to be header-only and constexpr#175376
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-libc Author: Mathew Joseph (mathew1046) ChangesSummaryRefactor the Problem StatementThe existing implementation of
SolutionMove the implementation into the header as an inline Changes1. Header File:
|
| Aspect | Impact | Notes |
|---|---|---|
| ABI Compatibility | ✅ No change | Exported symbol unchanged |
| API Compatibility | ✅ No change | Function signature identical |
| Performance | ✅ Neutral/Better | Inlining opportunities, no regression |
| Maintenance | ✅ Improved | Less code duplication |
| Platform Support | ✅ All platforms | Inherits from robust fputil |
| Build Time | ✅ Neutral | static_assert caught at compile time |
Related Issues
- Fixes: #175361 (GitHub issue requesting header-only, constexpr refactor)
Full diff: https://github.com/llvm/llvm-project/pull/175376.diff
3 Files Affected:
- (modified) libc/src/math/generic/llogbl.cpp (+4-5)
- (modified) libc/src/math/llogbl.h (+6-1)
- (modified) libc/test/src/math/smoke/llogbl_test.cpp (+34)
diff --git a/libc/src/math/generic/llogbl.cpp b/libc/src/math/generic/llogbl.cpp
index 7ee3ac55653b6..73a104de57357 100644
--- a/libc/src/math/generic/llogbl.cpp
+++ b/libc/src/math/generic/llogbl.cpp
@@ -7,14 +7,13 @@
//===----------------------------------------------------------------------===//
#include "src/math/llogbl.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(long, llogbl, (long double x)) {
- return fputil::intlogb<long>(x);
-}
+// Export the public C symbol by wrapping the inline constexpr definition.
+// This maintains binary compatibility with the shipped libc while allowing
+// callers to evaluate llogbl at compile time or have it inlined.
+LLVM_LIBC_FUNCTION(long, llogbl, (long double x)) { return llogbl(x); }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/llogbl.h b/libc/src/math/llogbl.h
index bf502a1c0bc3b..4a45eb6f1d895 100644
--- a/libc/src/math/llogbl.h
+++ b/libc/src/math/llogbl.h
@@ -9,12 +9,17 @@
#ifndef LLVM_LIBC_SRC_MATH_LLOGBL_H
#define LLVM_LIBC_SRC_MATH_LLOGBL_H
+#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
-long llogbl(long double x);
+// Inline constexpr implementation: extract the unbiased exponent of a long double
+// by delegating to the existing constexpr template fputil::intlogb<long>.
+LIBC_INLINE constexpr long llogbl(long double x) {
+ return fputil::intlogb<long>(x);
+}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/llogbl_test.cpp b/libc/test/src/math/smoke/llogbl_test.cpp
index c698210fc3de1..20332bfd6776b 100644
--- a/libc/test/src/math/smoke/llogbl_test.cpp
+++ b/libc/test/src/math/smoke/llogbl_test.cpp
@@ -11,3 +11,37 @@
#include "src/math/llogbl.h"
LIST_INTLOGB_TESTS(long, long double, LIBC_NAMESPACE::llogbl);
+
+// Constexpr tests: verify that llogbl can be evaluated at compile time.
+// These static_assert cases cover normal numbers with various exponents.
+namespace {
+class LLogblConstexprTest : public LIBC_NAMESPACE::testing::Test {
+public:
+ void RunTests() {
+ // Normal numbers: 2^0 = 1.0 => exponent 0
+ static_assert(LIBC_NAMESPACE::llogbl(1.0L) == 0);
+ static_assert(LIBC_NAMESPACE::llogbl(-1.0L) == 0);
+
+ // Normal numbers: 2^1 = 2.0 => exponent 1
+ static_assert(LIBC_NAMESPACE::llogbl(2.0L) == 1);
+ static_assert(LIBC_NAMESPACE::llogbl(-2.0L) == 1);
+
+ // Normal numbers: 2^2 = 4.0 => exponent 2
+ static_assert(LIBC_NAMESPACE::llogbl(4.0L) == 2);
+ static_assert(LIBC_NAMESPACE::llogbl(-4.0L) == 2);
+
+ // Normal numbers: 2^(-1) = 0.5 => exponent -1
+ static_assert(LIBC_NAMESPACE::llogbl(0.5L) == -1);
+ static_assert(LIBC_NAMESPACE::llogbl(-0.5L) == -1);
+
+ // Normal numbers: 2^3 = 8.0 => exponent 3
+ static_assert(LIBC_NAMESPACE::llogbl(8.0L) == 3);
+
+ // Normal numbers: 2^10 = 1024.0 => exponent 10
+ static_assert(LIBC_NAMESPACE::llogbl(1024.0L) == 10);
+ }
+};
+
+// Instantiate the test to trigger static_asserts at compile time.
+LLogblConstexprTest constexpr_test;
+} // anonymous namespace
There was a problem hiding this comment.
Pull request overview
This pull request refactors the llogbl function to be header-only and constexpr-enabled, allowing compile-time evaluation of the function. The refactor moves the implementation from a separate .cpp file into the header as an inline constexpr function that delegates to fputil::intlogb<long>.
Changes:
- Moved
llogblimplementation to header as inline constexpr function - Modified
.cppfile to contain only a thin wrapper for C symbol export - Added compile-time tests using
static_assertto verify constexpr evaluation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| libc/src/math/llogbl.h | Converted from declaration-only to inline constexpr implementation delegating to fputil::intlogb |
| libc/src/math/generic/llogbl.cpp | Simplified to thin wrapper for binary compatibility, but contains a critical bug |
| libc/test/src/math/smoke/llogbl_test.cpp | Added constexpr tests, but implementation is flawed |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hello @mathew1046, |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
🐧 Linux x64 Test Results✅ The build succeeded and no tests ran. This is expected in some build configurations. |
|
Hello @mathew1046, |
|
Hello @bassiounix, just a quick update on this PR. I’m currently refining the implementation and should have the revised version ready for review shortly. I'll let you know if I have any questions in the meantime. Thank you for waiting. |
|
Are these the changes you intended @bassiounix ? Please inform me if you need additional changes. |
|
@bassiounix I am really sorry about deleting that file. i misunderstood your message. about moving the function to designated section in main, do you perhaps meant to move the test to AllDouble section? |
To the long double test section |
bassiounix
left a comment
There was a problem hiding this comment.
Hello @mathew1046
I need your PR to comply with these new changes 823e3e0
Please rebase your code onto main and modify your code accordingly, there are breaking (signature) changes which I need you to reflect and replicate in your PR.
Thank you!
Refactor the llogbl (log base 2 of absolute value for long double) function to be header-only and marked constexpr, enabling compile-time evaluation and inlining opportunities while maintaining binary compatibility. Changes: 1. Move the implementation into libc/src/math/llogbl.h as an inline constexpr function that delegates to the existing fputil::intlogb<long> template (which is already constexpr-compatible). 2. Simplify libc/src/math/generic/llogbl.cpp to be a thin wrapper that exports the public C symbol via LLVM_LIBC_FUNCTION, ensuring binary compatibility with shipped libc libraries. 3. Add constexpr compile-time tests (static_assert) in the test file to verify that llogbl can be evaluated at compile time for normal numbers with various exponents (powers of 2 from 2^-1 to 2^10). 4. The underlying fputil::intlogb<long> already handles constexpr evaluation correctly for all cases (zero, NaN, infinity, subnormals) by using is_constant_evaluated() to skip errno/FE flag operations at compile time. Benefits: - Callers can now use constexpr long result = llogbl(2.0L); at compile time - Compiler can inline the function for better optimization - No ABI changes; binary compatibility is maintained - No platform-specific issues; inherits robust handling from fputil Fixes: llvm#175361 Signed-off-by: Mathew Joseph <mathewjosephparakka@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…c/__support/math folder
Removed constexpr tests for llogbl function.
5c392f8 to
24cdf9e
Compare
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com>
bassiounix
left a comment
There was a problem hiding this comment.
LGTM.
Thank you for the refactor!
|
@mathew1046 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Summary
Refactor the
llogbl(log base 2 of absolute value for long double) functionto be header-only and marked
constexpr, enabling compile-time evaluation andinlining opportunities while maintaining full binary compatibility.
Problem Statement
The existing implementation of
llogblwas split between a header declarationand a separate
.cppimplementation file, preventing:constexpr long x = llogbl(2.0L);could not be evaluated by the compiler
full implementation
the function at compile time
Solution
Move the implementation into the header as an inline
constexprfunction thatdelegates to the existing
fputil::intlogb<long>template (already constexpr-ready).Changes
1. Header File:
libc/src/math/llogbl.h#include "src/__support/FPUtil/ManipulationFunctions.h"LIBC_INLINE constexpr long llogbl(long double x) { return fputil::intlogb<long>(x); }2. Implementation File:
libc/src/math/generic/llogbl.cppLLVM_LIBC_FUNCTION(long, llogbl, (long double x)) { return llogbl(x); }ManipulationFunctions.h(now in header)3. Test File:
libc/test/src/math/smoke/llogbl_test.cppstatic_assertstatic_assert(llogbl(1.0L) == 0); // 2^0 = 1.0static_assert(llogbl(2.0L) == 1); // 2^1 = 2.0static_assert(llogbl(0.5L) == -1); // 2^-1 = 0.5Key Design Decisions
✅ Why This Works
fputil::intlogb<long>is already markedconstexprinManipulationFunctions.hset_errno_if_required()useis_constant_evaluated()to automatically skip errno/FE flag operations atcompile time
the existing
fputilimplementation✅ Backward Compatibility
long llogbl(long double x)✅ Performance Benefits
Testing Strategy
Runtime Tests
LIST_INTLOGB_TESTS(long, long double, LIBC_NAMESPACE::llogbl)continues to cover:
FP_ILOGB0), NaN (returnsFP_ILOGBNAN),infinity (returns
LONG_MAX)Compile-Time Tests
static_assertsuite verifies constexpr evaluation for:Impact Analysis
Related Issues
llogblto Header Only. #175361 (GitHub issue requesting header-only, constexpr refactor)