Skip to content

Commit

Permalink
[Soft-Float] - Fixes Itof method on Clang compiler.
Browse files Browse the repository at this point in the history
We use a faster checked method that achieve the same result.
  • Loading branch information
GitHubProUser67 committed Jan 6, 2025
1 parent dfb361d commit 99ee5d5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pcsx2/FPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void CTC1() {
void CVT_S() {
if (CHECK_FPU_SOFT_ADDSUB || CHECK_FPU_SOFT_MULDIV || CHECK_FPU_SOFT_SQRT)
{
_FdValUl_ = PS2Float::Itof(0, _FsValSl_);
_FdValUl_ = PS2Float::Itof(0, _FsValSl_).raw;
}
else
{
Expand Down
78 changes: 43 additions & 35 deletions pcsx2/PS2Float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PS2Float::PS2Float(bool sign, u8 exponent, u32 mantissa)
{
raw = 0;

Check warning on line 88 in pcsx2/PS2Float.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/PS2Float.cpp#L88

Variable 'raw' is assigned in constructor body. Consider performing initialization in initialization list.
raw |= (sign ? 1u : 0u) << 31;
raw |= (u32)(exponent << 23);
raw |= (u32)(exponent << MANTISSA_BITS);
raw |= mantissa & 0x7FFFFF;
}

Expand Down Expand Up @@ -371,7 +371,7 @@ PS2Float PS2Float::DoAdd(PS2Float other)
rawExp -= amount;
absMan <<= amount;

s32 msbIndex = Common::BitScanReverse8(absMan >> 23);
s32 msbIndex = Common::BitScanReverse8(absMan >> MANTISSA_BITS);
rawExp += msbIndex;
absMan >>= msbIndex;

Expand All @@ -388,7 +388,7 @@ PS2Float PS2Float::DoAdd(PS2Float other)
return result;
}

return PS2Float(((u32)man & SIGNMASK) | (u32)rawExp << 23 | ((u32)absMan & 0x7FFFFF));
return PS2Float(((u32)man & SIGNMASK) | (u32)rawExp << MANTISSA_BITS | ((u32)absMan & 0x7FFFFF));
}

PS2Float PS2Float::DoMul(PS2Float other)
Expand All @@ -400,7 +400,7 @@ PS2Float PS2Float::DoMul(PS2Float other)
u32 sign = (raw ^ other.raw) & SIGNMASK;

s32 resExponent = selfExponent + otherExponent - 127;
u32 resMantissa = (u32)(MulMantissa(selfMantissa, otherMantissa) >> 23);
u32 resMantissa = (u32)(MulMantissa(selfMantissa, otherMantissa) >> MANTISSA_BITS);

if (resMantissa > 0xFFFFFF)
{
Expand All @@ -421,7 +421,7 @@ PS2Float PS2Float::DoMul(PS2Float other)
return result;
}

return PS2Float(sign | (u32)(resExponent << 23) | (resMantissa & 0x7FFFFF));
return PS2Float(sign | (u32)(resExponent << MANTISSA_BITS) | (resMantissa & 0x7FFFFF));
}

PS2Float PS2Float::SolveAddSubDenormalizedOperation(PS2Float a, PS2Float b, bool add)
Expand Down Expand Up @@ -461,41 +461,49 @@ PS2Float PS2Float::SolveDivisionDenormalizedOperation(PS2Float a, PS2Float b)
return PS2Float(0);
}

u32 PS2Float::Itof(s32 complement, s32 f1)
PS2Float PS2Float::Itof(s32 complement, s32 f1)
{
u8 specialCondition;
u32 result;
s32 subExponent, newExponent, floatResult;
if (f1 == 0)
return PS2Float(0);

s32 resExponent;

if (f1 != 0)
if (f1 == -2147483648)
{
specialCondition = 0;
subExponent = 158;
if (f1 < 0)
{
f1 = ~(f1 - 1);
specialCondition = 1;
}
while (f1 >= 0)
{
f1 *= 2;
--subExponent;
}
floatResult = (2 * f1) >> 9;
newExponent = subExponent - complement;
if (newExponent >= 0)
{
floatResult = (((u8)newExponent << 7) | ((floatResult >> 16) & 0x807F)) << 16 | (floatResult & 0xFFFF);
floatResult = (((specialCondition << 7) | ((floatResult >> 24) & 0x7F)) << 24) | (floatResult & 0xFFFFFF);
result = (u32)floatResult;
}
else
result = 0;
// special case
resExponent = 158 - complement;

if (resExponent >= 0)
return PS2Float(true, (u8)resExponent, 0);

return PS2Float(0);
}

bool negative = f1 < 0;

Check notice on line 482 in pcsx2/PS2Float.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/PS2Float.cpp#L482

The scope of the variable 'negative' can be reduced.
s32 u = std::abs(f1);

s32 shifts;

s32 lzcnt = Common::CountLeadingSignBits(u);
if (lzcnt < 8)
{
s32 count = 8 - lzcnt;
u >>= count;
shifts = -count;
}
else
result = 0;
{
s32 count = lzcnt - 8;
u <<= count;
shifts = count;
}

return result;
resExponent = BIAS + MANTISSA_BITS - shifts - complement;

if (resExponent >= 0)
return PS2Float(negative, (u8)resExponent, (u32)u);

return PS2Float(0);
}

s32 PS2Float::Ftoi(s32 complement, u32 f1)
Expand All @@ -507,7 +515,7 @@ s32 PS2Float::Ftoi(s32 complement, u32 f1)
result = 0;
else
{
complement = (s32)(f1 >> 23 & 0xFF) + complement;
complement = (s32)(f1 >> MANTISSA_BITS & 0xFF) + complement;
f1 &= 0x7FFFFF;
f1 |= 0x800000;
if (complement < 158)
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/PS2Float.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class PS2Float
public:

static constexpr u8 BIAS = 127;
static constexpr u8 MANTISSA_BITS = 23;
static constexpr u32 SIGNMASK = 0x80000000;
static constexpr u32 MAX_FLOATING_POINT_VALUE = 0x7FFFFFFF;
static constexpr u32 MIN_FLOATING_POINT_VALUE = 0xFFFFFFFF;
static constexpr u32 ONE = 0x3F800000;
static constexpr u32 MIN_ONE = 0xBF800000;
static constexpr int IMPLICIT_LEADING_BIT_POS = 23;

bool dz = false;
bool iv = false;
Expand Down Expand Up @@ -68,7 +68,7 @@ class PS2Float

static PS2Float SolveDivisionDenormalizedOperation(PS2Float a, PS2Float b);

static u32 Itof(s32 complement, s32 f1);
static PS2Float Itof(s32 complement, s32 f1);

static s32 Ftoi(s32 complement, u32 f1);

Expand Down
32 changes: 16 additions & 16 deletions pcsx2/VUops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,10 +1696,10 @@ static __fi void _vuITOF0(VURegs* VU)

if (CHECK_VU_SOFT_ADDSUB((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_MULDIV((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_SQRT((VU == &VU1) ? 1 : 0))
{
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(0, VU->VF[_Fs_].SL[0]);
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(0, VU->VF[_Fs_].SL[1]);
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(0, VU->VF[_Fs_].SL[2]);
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(0, VU->VF[_Fs_].SL[3]);
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(0, VU->VF[_Fs_].SL[0]).raw;
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(0, VU->VF[_Fs_].SL[1]).raw;
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(0, VU->VF[_Fs_].SL[2]).raw;
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(0, VU->VF[_Fs_].SL[3]).raw;
}
else
{
Expand All @@ -1717,10 +1717,10 @@ static __fi void _vuITOF4(VURegs* VU)

if (CHECK_VU_SOFT_ADDSUB((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_MULDIV((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_SQRT((VU == &VU1) ? 1 : 0))
{
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(4, VU->VF[_Fs_].SL[0]);
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(4, VU->VF[_Fs_].SL[1]);
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(4, VU->VF[_Fs_].SL[2]);
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(4, VU->VF[_Fs_].SL[3]);
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(4, VU->VF[_Fs_].SL[0]).raw;
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(4, VU->VF[_Fs_].SL[1]).raw;
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(4, VU->VF[_Fs_].SL[2]).raw;
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(4, VU->VF[_Fs_].SL[3]).raw;
}
else
{
Expand All @@ -1738,10 +1738,10 @@ static __fi void _vuITOF12(VURegs* VU)

if (CHECK_VU_SOFT_ADDSUB((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_MULDIV((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_SQRT((VU == &VU1) ? 1 : 0))
{
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(12, VU->VF[_Fs_].SL[0]);
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(12, VU->VF[_Fs_].SL[1]);
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(12, VU->VF[_Fs_].SL[2]);
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(12, VU->VF[_Fs_].SL[3]);
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(12, VU->VF[_Fs_].SL[0]).raw;
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(12, VU->VF[_Fs_].SL[1]).raw;
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(12, VU->VF[_Fs_].SL[2]).raw;
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(12, VU->VF[_Fs_].SL[3]).raw;
}
else
{
Expand All @@ -1759,10 +1759,10 @@ static __fi void _vuITOF15(VURegs* VU)

if (CHECK_VU_SOFT_ADDSUB((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_MULDIV((VU == &VU1) ? 1 : 0) || CHECK_VU_SOFT_SQRT((VU == &VU1) ? 1 : 0))
{
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(15, VU->VF[_Fs_].SL[0]);
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(15, VU->VF[_Fs_].SL[1]);
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(15, VU->VF[_Fs_].SL[2]);
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(15, VU->VF[_Fs_].SL[3]);
if (_X) VU->VF[_Ft_].i.x = PS2Float::Itof(15, VU->VF[_Fs_].SL[0]).raw;
if (_Y) VU->VF[_Ft_].i.y = PS2Float::Itof(15, VU->VF[_Fs_].SL[1]).raw;
if (_Z) VU->VF[_Ft_].i.z = PS2Float::Itof(15, VU->VF[_Fs_].SL[2]).raw;
if (_W) VU->VF[_Ft_].i.w = PS2Float::Itof(15, VU->VF[_Fs_].SL[3]).raw;
}
else
{
Expand Down

0 comments on commit 99ee5d5

Please sign in to comment.