diff options
author | Morph <39850852+Morph1984@users.noreply.github.com> | 2023-03-07 22:42:32 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-07 22:42:32 -0500 |
commit | a3ffea6a646ac1945ac7f01d401c7ae7b670b5a8 (patch) | |
tree | 26997bf1cf3b42d8e187923278357c8d774b5d0c /src/common/overflow.h | |
parent | b014fdacdbb19491b9169df7e987293b4e851481 (diff) | |
parent | d45ac00d48352d86a483e7642bfccadf1eb4f4ce (diff) |
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.h | 22 |
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 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <type_traits> +#include "bit_cast.h" + +namespace Common { + +template <typename T> + requires(std::is_integral_v<T> && std::is_signed_v<T>) +inline T WrappingAdd(T lhs, T rhs) { + using U = std::make_unsigned_t<T>; + + U lhs_u = BitCast<U>(lhs); + U rhs_u = BitCast<U>(rhs); + + return BitCast<T>(lhs_u + rhs_u); +} + +} // namespace Common |