overflow.h 474 B

12345678910111213141516171819202122
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <type_traits>
  5. #include "bit_cast.h"
  6. namespace Common {
  7. template <typename T>
  8. requires(std::is_integral_v<T> && std::is_signed_v<T>)
  9. inline T WrappingAdd(T lhs, T rhs) {
  10. using U = std::make_unsigned_t<T>;
  11. U lhs_u = BitCast<U>(lhs);
  12. U rhs_u = BitCast<U>(rhs);
  13. return BitCast<T>(lhs_u + rhs_u);
  14. }
  15. } // namespace Common