From 2c049ae06dbbdfff7542c23ca4d748879f4beb71 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Thu, 18 Jan 2024 23:08:37 +0100 Subject: fs: Add path class --- src/common/overflow.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/common/overflow.h') diff --git a/src/common/overflow.h b/src/common/overflow.h index 44d8e7e73..e765c0d89 100644 --- a/src/common/overflow.h +++ b/src/common/overflow.h @@ -19,4 +19,21 @@ inline T WrappingAdd(T lhs, T rhs) { return BitCast(lhs_u + rhs_u); } +template + requires(std::is_integral_v && std::is_signed_v) +inline bool CanAddWithoutOverflow(T lhs, T rhs) { +#ifdef _MSC_VER + if (lhs >= 0 && rhs >= 0) { + return WrappingAdd(lhs, rhs) >= std::max(lhs, rhs); + } else if (lhs < 0 && rhs < 0) { + return WrappingAdd(lhs, rhs) <= std::min(lhs, rhs); + } else { + return true; + } +#else + T res; + return !__builtin_add_overflow(lhs, rhs, &res); +#endif +} + } // namespace Common -- cgit v1.2.3 From 975deb7528cd98460528553f6a9162bfbcd6cab1 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Fri, 19 Jan 2024 00:17:28 +0100 Subject: Address review comments and fix compilation problems --- src/common/overflow.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/common/overflow.h') diff --git a/src/common/overflow.h b/src/common/overflow.h index e765c0d89..e184ead95 100644 --- a/src/common/overflow.h +++ b/src/common/overflow.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include "bit_cast.h" -- cgit v1.2.3