[llvm] [Support] Add constexpr versions of Add/Sub/MulOverflow (PR #210404)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 18 13:08:04 PDT 2026
================
@@ -696,64 +697,85 @@ SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
LLVM_ABI extern const float huge_valf;
/// Add two signed integers, computing the two's complement truncated result,
-/// returning true if overflow occurred.
+/// returning a pair {result, overflow}, where "overflow" is a boolean value
+/// indicating whether an overflow occurred.
template <typename T>
-std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result) {
-#if __has_builtin(__builtin_add_overflow)
- return __builtin_add_overflow(X, Y, &Result);
-#else
+constexpr std::enable_if_t<std::is_signed_v<T>, std::pair<T, bool>>
+AddOverflow(T X, T Y) {
// Perform the unsigned addition.
using U = std::make_unsigned_t<T>;
const U UX = static_cast<U>(X);
const U UY = static_cast<U>(Y);
const U UResult = UX + UY;
// Convert to signed.
- Result = static_cast<T>(UResult);
+ auto Result = static_cast<T>(UResult);
// Adding two positive numbers should result in a positive number.
if (X > 0 && Y > 0)
- return Result <= 0;
+ return {Result, Result <= 0};
// Adding two negatives should result in a negative number.
if (X < 0 && Y < 0)
- return Result >= 0;
- return false;
+ return {Result, Result >= 0};
+ return {Result, false};
+}
+
+/// Add two signed integers, computing the two's complement truncated result,
+/// returning true if overflow occurred.
+template <typename T>
+std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result) {
----------------
efriedma-quic wrote:
Ping
https://github.com/llvm/llvm-project/pull/210404
More information about the llvm-commits
mailing list