From d0274f412a67ca7718c35cab2c57db58a6a56022 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Jul 2018 14:39:07 -0400 Subject: [PATCH] safe_ops: Avoid signed overflow in Negate() Negation of values such as -9223372036854775808 can't be represented in signed equivalents (such as long long), leading to signed overflow. Therefore, we can just invert bits and add 1 to perform this behavior with unsigned arithmetic. --- src/common/safe_ops.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/safe_ops.h b/src/common/safe_ops.h index bc6d7b88..eb833ebb 100644 --- a/src/common/safe_ops.h +++ b/src/common/safe_ops.h @@ -103,7 +103,7 @@ T ArithmeticShiftRightDouble(T top, T bottom, int shift_amount) { template T Negate(T value) { - return static_cast(-static_cast>(value)); + return static_cast(~static_cast(value) + 1); } } // namespace Dynarmic::Safe