summaryrefslogtreecommitdiffstats
path: root/src/common/overflow.h
diff options
context:
space:
mode:
authorFearlessTobi <thm.frey@gmail.com>2024-01-18 23:08:37 +0100
committerLiam <byteslice@airmail.cc>2024-01-25 22:42:06 +0100
commit2c049ae06dbbdfff7542c23ca4d748879f4beb71 (patch)
treef93c8be5b6d1781a69d8c28e6aa3500d34c8b55b /src/common/overflow.h
parentresult: Make fully constexpr, add ON_RESULT_INCLUDED (diff)
downloadyuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.gz
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.bz2
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.lz
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.xz
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.zst
yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.zip
Diffstat (limited to 'src/common/overflow.h')
-rw-r--r--src/common/overflow.h17
1 files changed, 17 insertions, 0 deletions
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<T>(lhs_u + rhs_u);
}
+template <typename T>
+ requires(std::is_integral_v<T> && std::is_signed_v<T>)
+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