Merge pull request #9918 from liamwhite/fwrapv

kernel: avoid signed overflow UB on MSVC
This commit is contained in:
Morph 2023-03-07 22:42:32 -05:00 committed by GitHub
commit a3ffea6a64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View file

@ -91,6 +91,7 @@ add_library(common STATIC
multi_level_page_table.h
nvidia_flags.cpp
nvidia_flags.h
overflow.h
page_table.cpp
page_table.h
param_package.cpp

22
src/common/overflow.h Normal file
View file

@ -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