[libc-commits] [libc] [libc] Fix add_with_carry constexpr (PR #154282)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Tue Aug 19 01:15:48 PDT 2025
https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/154282
>From 1e57b21482e670d56049bcd3158eb79e17ffff98 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 19 Aug 2025 08:06:26 +0000
Subject: [PATCH 1/2] [libc] Fix add_with_carry constexpr
The previous version of the code would prevent the use of the add with
carry compiler builtin.
---
libc/src/__support/math_extras.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math_extras.h b/libc/src/__support/math_extras.h
index 47df2a43250c8..0a0a2a829e1bf 100644
--- a/libc/src/__support/math_extras.h
+++ b/libc/src/__support/math_extras.h
@@ -66,7 +66,7 @@ template <typename T>
#define RETURN_IF(TYPE, BUILTIN) \
if constexpr (cpp::is_same_v<T, TYPE>) \
- return BUILTIN(a, b, carry_in, carry_out);
+ return BUILTIN(a, b, carry_in, &carry_out);
// Returns the result of 'a + b' taking into account 'carry_in'.
// The carry out is stored in 'carry_out' it not 'nullptr', dropped otherwise.
@@ -74,7 +74,7 @@ template <typename T>
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
add_with_carry(T a, T b, T carry_in, T &carry_out) {
- if constexpr (!cpp::is_constant_evaluated()) {
+ if (!cpp::is_constant_evaluated()) {
#if __has_builtin(__builtin_addcb)
RETURN_IF(unsigned char, __builtin_addcb)
#elif __has_builtin(__builtin_addcs)
>From b9ff9b11266d3e7cc9542de4a30d6e90c18abd64 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 19 Aug 2025 08:15:11 +0000
Subject: [PATCH 2/2] Also fix sub_with_borrow.
---
libc/src/__support/math_extras.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math_extras.h b/libc/src/__support/math_extras.h
index 0a0a2a829e1bf..954bcb1b6ef89 100644
--- a/libc/src/__support/math_extras.h
+++ b/libc/src/__support/math_extras.h
@@ -100,7 +100,7 @@ add_with_carry(T a, T b, T carry_in, T &carry_out) {
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
sub_with_borrow(T a, T b, T carry_in, T &carry_out) {
- if constexpr (!cpp::is_constant_evaluated()) {
+ if (!cpp::is_constant_evaluated()) {
#if __has_builtin(__builtin_subcb)
RETURN_IF(unsigned char, __builtin_subcb)
#elif __has_builtin(__builtin_subcs)
More information about the libc-commits
mailing list