From d7e6ef4ce6aa7f7ed220de66baef9b3d7413a049 Mon Sep 17 00:00:00 2001 From: refractionpcsx2 Date: Sun, 5 Sep 2021 00:49:13 +0100 Subject: [PATCH] VU Int: Properly limit ints when converting from float --- pcsx2/VUops.cpp | 45 +++++++++++++++++++++++++++------------------ pcsx2/VUops.h | 6 +++--- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/pcsx2/VUops.cpp b/pcsx2/VUops.cpp index f622350885..4411ae074b 100644 --- a/pcsx2/VUops.cpp +++ b/pcsx2/VUops.cpp @@ -1450,40 +1450,49 @@ static __fi void _vuOPMSUB(VURegs * VU) { static __fi void _vuNOP(VURegs * VU) { } +static __fi s32 float_to_int(float value) +{ + if (value >= 2147483647.0) + return 2147483647LL; + if (value <= -2147483648.0) + return -2147483648LL; + return value; +} + static __fi void _vuFTOI0(VURegs * VU) { if (_Ft_ == 0) return; - if (_X) VU->VF[_Ft_].SL[0] = (s32)vuDouble(VU->VF[_Fs_].i.x); - if (_Y) VU->VF[_Ft_].SL[1] = (s32)vuDouble(VU->VF[_Fs_].i.y); - if (_Z) VU->VF[_Ft_].SL[2] = (s32)vuDouble(VU->VF[_Fs_].i.z); - if (_W) VU->VF[_Ft_].SL[3] = (s32)vuDouble(VU->VF[_Fs_].i.w); + if (_X) VU->VF[_Ft_].SL[0] = float_to_int(vuDouble(VU->VF[_Fs_].i.x)); + if (_Y) VU->VF[_Ft_].SL[1] = float_to_int(vuDouble(VU->VF[_Fs_].i.y)); + if (_Z) VU->VF[_Ft_].SL[2] = float_to_int(vuDouble(VU->VF[_Fs_].i.z)); + if (_W) VU->VF[_Ft_].SL[3] = float_to_int(vuDouble(VU->VF[_Fs_].i.w)); } static __fi void _vuFTOI4(VURegs * VU) { if (_Ft_ == 0) return; - if (_X) VU->VF[_Ft_].SL[0] = float_to_int4(vuDouble(VU->VF[_Fs_].i.x)); - if (_Y) VU->VF[_Ft_].SL[1] = float_to_int4(vuDouble(VU->VF[_Fs_].i.y)); - if (_Z) VU->VF[_Ft_].SL[2] = float_to_int4(vuDouble(VU->VF[_Fs_].i.z)); - if (_W) VU->VF[_Ft_].SL[3] = float_to_int4(vuDouble(VU->VF[_Fs_].i.w)); -} + if (_X) VU->VF[_Ft_].SL[0] = float_to_int(float_to_int4(vuDouble(VU->VF[_Fs_].i.x))); + if (_Y) VU->VF[_Ft_].SL[1] = float_to_int(float_to_int4(vuDouble(VU->VF[_Fs_].i.y))); + if (_Z) VU->VF[_Ft_].SL[2] = float_to_int(float_to_int4(vuDouble(VU->VF[_Fs_].i.z))); + if (_W) VU->VF[_Ft_].SL[3] = float_to_int(float_to_int4(vuDouble(VU->VF[_Fs_].i.w))); +} static __fi void _vuFTOI12(VURegs * VU) { if (_Ft_ == 0) return; - if (_X) VU->VF[_Ft_].SL[0] = float_to_int12(vuDouble(VU->VF[_Fs_].i.x)); - if (_Y) VU->VF[_Ft_].SL[1] = float_to_int12(vuDouble(VU->VF[_Fs_].i.y)); - if (_Z) VU->VF[_Ft_].SL[2] = float_to_int12(vuDouble(VU->VF[_Fs_].i.z)); - if (_W) VU->VF[_Ft_].SL[3] = float_to_int12(vuDouble(VU->VF[_Fs_].i.w)); + if (_X) VU->VF[_Ft_].SL[0] = float_to_int(float_to_int12(vuDouble(VU->VF[_Fs_].i.x))); + if (_Y) VU->VF[_Ft_].SL[1] = float_to_int(float_to_int12(vuDouble(VU->VF[_Fs_].i.y))); + if (_Z) VU->VF[_Ft_].SL[2] = float_to_int(float_to_int12(vuDouble(VU->VF[_Fs_].i.z))); + if (_W) VU->VF[_Ft_].SL[3] = float_to_int(float_to_int12(vuDouble(VU->VF[_Fs_].i.w))); } static __fi void _vuFTOI15(VURegs * VU) { if (_Ft_ == 0) return; - if (_X) VU->VF[_Ft_].SL[0] = float_to_int15(vuDouble(VU->VF[_Fs_].i.x)); - if (_Y) VU->VF[_Ft_].SL[1] = float_to_int15(vuDouble(VU->VF[_Fs_].i.y)); - if (_Z) VU->VF[_Ft_].SL[2] = float_to_int15(vuDouble(VU->VF[_Fs_].i.z)); - if (_W) VU->VF[_Ft_].SL[3] = float_to_int15(vuDouble(VU->VF[_Fs_].i.w)); + if (_X) VU->VF[_Ft_].SL[0] = float_to_int(float_to_int15(vuDouble(VU->VF[_Fs_].i.x))); + if (_Y) VU->VF[_Ft_].SL[1] = float_to_int(float_to_int15(vuDouble(VU->VF[_Fs_].i.y))); + if (_Z) VU->VF[_Ft_].SL[2] = float_to_int(float_to_int15(vuDouble(VU->VF[_Fs_].i.z))); + if (_W) VU->VF[_Ft_].SL[3] = float_to_int(float_to_int15(vuDouble(VU->VF[_Fs_].i.w))); } static __fi void _vuITOF0(VURegs * VU) { @@ -1694,7 +1703,7 @@ static __fi void _vuMTIR(VURegs * VU) { _vuBackupVI(VU, _It_); - VU->VI[_It_].US[0] = *(u16*)&VU->VF[_Fs_].F[_Fsf_]; + VU->VI[_It_].US[0] = *(u16*)&VU->VF[_Fs_].F[_Fsf_]; } static __fi void _vuMR32(VURegs * VU) { diff --git a/pcsx2/VUops.h b/pcsx2/VUops.h index fdbd623fe4..bf6216dd5a 100644 --- a/pcsx2/VUops.h +++ b/pcsx2/VUops.h @@ -17,9 +17,9 @@ #include "VU.h" #include "VUflags.h" -#define float_to_int4(x) (s32)((float)x * (1.0f / 0.0625f)) -#define float_to_int12(x) (s32)((float)x * (1.0f / 0.000244140625f)) -#define float_to_int15(x) (s32)((float)x * (1.0f / 0.000030517578125)) +#define float_to_int4(x) ((float)x * (1.0f / 0.0625f)) +#define float_to_int12(x) ((float)x * (1.0f / 0.000244140625f)) +#define float_to_int15(x) ((float)x * (1.0f / 0.000030517578125)) #define int4_to_float(x) (float)((float)x * 0.0625f) #define int12_to_float(x) (float)((float)x * 0.000244140625f)