[libc-commits] [libc] 4dd9e99 - [libc] Fix `constexpr` `add_with_carry`/`sub_with_borrow` (#154282)

via libc-commits libc-commits at lists.llvm.org
Wed Aug 20 01:41:54 PDT 2025


Author: Guillaume Chatelet
Date: 2025-08-20T10:41:51+02:00
New Revision: 4dd9e99284b58c5625fb32082dd204d8b9b56e95

URL: https://github.com/llvm/llvm-project/commit/4dd9e99284b58c5625fb32082dd204d8b9b56e95
DIFF: https://github.com/llvm/llvm-project/commit/4dd9e99284b58c5625fb32082dd204d8b9b56e95.diff

LOG: [libc] Fix `constexpr` `add_with_carry`/`sub_with_borrow` (#154282)

The previous version of the code would prevent the use of the compiler
builtins.

Added: 
    

Modified: 
    libc/src/__support/math_extras.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/math_extras.h b/libc/src/__support/math_extras.h
index 47df2a43250c8..954bcb1b6ef89 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)
@@ -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