summaryrefslogtreecommitdiff
path: root/src/common/overflow.h
diff options
context:
space:
mode:
authorGravatar Morph2023-03-07 22:42:32 -0500
committerGravatar GitHub2023-03-07 22:42:32 -0500
commita3ffea6a646ac1945ac7f01d401c7ae7b670b5a8 (patch)
tree26997bf1cf3b42d8e187923278357c8d774b5d0c /src/common/overflow.h
parentMerge pull request #9920 from liamwhite/constexpr-bit-cast (diff)
parentkernel: avoid signed overflow UB on MSVC (diff)
downloadyuzu-a3ffea6a646ac1945ac7f01d401c7ae7b670b5a8.tar.gz
yuzu-a3ffea6a646ac1945ac7f01d401c7ae7b670b5a8.tar.xz
yuzu-a3ffea6a646ac1945ac7f01d401c7ae7b670b5a8.zip
Merge pull request #9918 from liamwhite/fwrapv
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