util/types.hpp: Fix signed/unsigned comparison error
Some checks are pending
Generate Translation Template / Generate Translation Template (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (/rpcs3/.ci/build-linux-aarch64.sh, gcc, rpcs3/rpcs3-ci-jammy-aarch64:1.7, ubuntu-24.04-arm) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (/rpcs3/.ci/build-linux.sh, gcc, rpcs3/rpcs3-ci-jammy:1.7, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (a1d35836e8d45bfc6f63c26f0a3e5d46ef622fe1, rpcs3/rpcs3-binaries-linux-arm64, /rpcs3/.ci/build-linux-aarch64.sh, clang, rpcs3/rpcs3-ci-jammy-aarch64:1.7, ubuntu-24.04-arm) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (d812f1254a1157c80fd402f94446310560f54e5f, rpcs3/rpcs3-binaries-linux, /rpcs3/.ci/build-linux.sh, clang, rpcs3/rpcs3-ci-jammy:1.7, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (51ae32f468089a8169aaf1567de355ff4a3e0842, rpcs3/rpcs3-binaries-mac, .ci/build-mac.sh, Intel) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (8e21bdbc40711a3fccd18fbf17b742348b0f4281, rpcs3/rpcs3-binaries-mac-arm64, .ci/build-mac-arm64.sh, Apple Silicon) (push) Waiting to run
Build RPCS3 / RPCS3 Windows (push) Waiting to run
Build RPCS3 / RPCS3 Windows Clang (win64, clang, clang64) (push) Waiting to run
Build RPCS3 / RPCS3 FreeBSD (push) Waiting to run

This commit is contained in:
Elad 2025-11-26 08:38:58 +02:00
parent b451dfe877
commit 914b52cf4a

View file

@ -999,17 +999,18 @@ template <typename To, typename From> requires (std::is_integral_v<decltype(std:
constexpr bool is_from_signed = std::is_signed_v<CommonFrom>;
constexpr bool is_to_signed = std::is_signed_v<CommonTo>;
// For unsigned/signed mismatch, create an "unsigned" compatible mask
constexpr auto from_mask = (is_from_signed && !is_to_signed) ? UnFrom{umax} >> 1 : UnFrom{umax};
constexpr auto to_mask = (is_to_signed && !is_from_signed) ? UnTo{umax} >> 1 : UnTo{umax};
constexpr auto mask = ~(from_mask & to_mask);
constexpr auto mask = static_cast<UnFrom>(~(from_mask & to_mask));
// Signed to unsigned always require test
// Otherwise, this is bit-wise narrowing or conversion between types of different signedness of the same size
if constexpr ((is_from_signed && !is_to_signed) || to_mask < from_mask)
// If destination ("unsigned" compatible) mask is smaller than source ("unsigned" compatible) mask
// It requires narrowing.
if constexpr (!!mask)
{
// Try to optimize test if both are of the same signedness
if (is_from_signed != is_to_signed ? !!(value & mask) : static_cast<CommonTo>(value) != value) [[unlikely]]
if (is_from_signed != is_to_signed ? !!(value & mask) : static_cast<UnTo>(value) != static_cast<UnFrom>(value)) [[unlikely]]
{
fmt::raw_verify_error(src_loc, u8"Narrowing error", +value);
}