summaryrefslogtreecommitdiff
path: root/src/common/overflow.h
diff options
context:
space:
mode:
authorGravatar Liam2023-03-07 19:46:48 -0500
committerGravatar Liam2023-03-07 19:46:48 -0500
commitd45ac00d48352d86a483e7642bfccadf1eb4f4ce (patch)
tree9c8327fa45f820e586835f8547fb4b976bbca500 /src/common/overflow.h
parentMerge pull request #9889 from Morph1984/time-is-ticking (diff)
downloadyuzu-d45ac00d48352d86a483e7642bfccadf1eb4f4ce.tar.gz
yuzu-d45ac00d48352d86a483e7642bfccadf1eb4f4ce.tar.xz
yuzu-d45ac00d48352d86a483e7642bfccadf1eb4f4ce.zip
kernel: avoid signed overflow UB on MSVC
Diffstat (limited to 'src/common/overflow.h')
-rw-r--r--src/common/overflow.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/common/overflow.h b/src/common/overflow.h
new file mode 100644
index 000000000..44d8e7e73
--- /dev/null
+++ b/src/common/overflow.h
@@ -0,0 +1,22 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <type_traits>
7#include "bit_cast.h"
8
9namespace Common {
10
11template <typename T>
12 requires(std::is_integral_v<T> && std::is_signed_v<T>)
13inline T WrappingAdd(T lhs, T rhs) {
14 using U = std::make_unsigned_t<T>;
15
16 U lhs_u = BitCast<U>(lhs);
17 U rhs_u = BitCast<U>(rhs);
18
19 return BitCast<T>(lhs_u + rhs_u);
20}
21
22} // namespace Common