From d45ac00d48352d86a483e7642bfccadf1eb4f4ce Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 7 Mar 2023 19:46:48 -0500 Subject: kernel: avoid signed overflow UB on MSVC --- src/common/CMakeLists.txt | 1 + src/common/overflow.h | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/common/overflow.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 58ff5f2f3..61ab68864 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -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 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 +#include "bit_cast.h" + +namespace Common { + +template + requires(std::is_integral_v && std::is_signed_v) +inline T WrappingAdd(T lhs, T rhs) { + using U = std::make_unsigned_t; + + U lhs_u = BitCast(lhs); + U rhs_u = BitCast(rhs); + + return BitCast(lhs_u + rhs_u); +} + +} // namespace Common -- cgit v1.2.3