add SAFE_DIV macro and usages
This commit is contained in:
@@ -73,6 +73,14 @@
|
|||||||
#define abs(x) (((x) < 0) ? -(x) : (x))
|
#define abs(x) (((x) < 0) ? -(x) : (x))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Used in cases where division by 0 can occur in the retail version.
|
||||||
|
// Avoids invalid opcodes on some emulators, and the otherwise UB.
|
||||||
|
#ifdef UBFIX
|
||||||
|
#define SAFE_DIV(a, b) ((b) ? (a) / (b) : 0)
|
||||||
|
#else
|
||||||
|
#define SAFE_DIV(a, b) ((a) / (b))
|
||||||
|
#endif
|
||||||
|
|
||||||
// There are many quirks in the source code which have overarching behavioral differences from
|
// There are many quirks in the source code which have overarching behavioral differences from
|
||||||
// a number of other files. For example, diploma.c seems to declare rodata before each use while
|
// a number of other files. For example, diploma.c seems to declare rodata before each use while
|
||||||
// other files declare out of order and must be at the beginning. There are also a number of
|
// other files declare out of order and must be at the beginning. There are also a number of
|
||||||
|
|||||||
@@ -1706,7 +1706,7 @@ void SetBattlerSpriteYOffsetFromOtherYScale(u8 spriteId, u8 otherSpriteId)
|
|||||||
{
|
{
|
||||||
s32 var = 64 - GetBattlerYDeltaFromSpriteId(otherSpriteId) * 2;
|
s32 var = 64 - GetBattlerYDeltaFromSpriteId(otherSpriteId) * 2;
|
||||||
u16 matrix = gSprites[spriteId].oam.matrixNum;
|
u16 matrix = gSprites[spriteId].oam.matrixNum;
|
||||||
s32 var2 = (var << 8) / gOamMatrices[matrix].d;
|
s32 var2 = SAFE_DIV((var << 8), gOamMatrices[matrix].d);
|
||||||
|
|
||||||
if (var2 > 128)
|
if (var2 > 128)
|
||||||
var2 = 128;
|
var2 = 128;
|
||||||
|
|||||||
@@ -3108,7 +3108,7 @@ static void atk23_getexp(void)
|
|||||||
calculatedExp = gBaseStats[gBattleMons[gBattlerFainted].species].expYield * gBattleMons[gBattlerFainted].level / 7;
|
calculatedExp = gBaseStats[gBattleMons[gBattlerFainted].species].expYield * gBattleMons[gBattlerFainted].level / 7;
|
||||||
if (viaExpShare) // at least one mon is getting exp via exp share
|
if (viaExpShare) // at least one mon is getting exp via exp share
|
||||||
{
|
{
|
||||||
*exp = calculatedExp / 2 / viaSentIn;
|
*exp = SAFE_DIV(calculatedExp / 2, viaSentIn);
|
||||||
if (*exp == 0)
|
if (*exp == 0)
|
||||||
*exp = 1;
|
*exp = 1;
|
||||||
gExpShareExp = calculatedExp / 2 / viaExpShare;
|
gExpShareExp = calculatedExp / 2 / viaExpShare;
|
||||||
@@ -3117,7 +3117,7 @@ static void atk23_getexp(void)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*exp = calculatedExp / viaSentIn;
|
*exp = SAFE_DIV(calculatedExp, viaSentIn);
|
||||||
if (*exp == 0)
|
if (*exp == 0)
|
||||||
*exp = 1;
|
*exp = 1;
|
||||||
gExpShareExp = 0;
|
gExpShareExp = 0;
|
||||||
|
|||||||
+2
-8
@@ -3668,14 +3668,8 @@ static void Task_MoveDeoxysRock_Step(u8 taskId)
|
|||||||
data[5] = sprite->pos1.y << 4;
|
data[5] = sprite->pos1.y << 4;
|
||||||
|
|
||||||
// UB: Possible divide by zero
|
// UB: Possible divide by zero
|
||||||
#ifdef UBFIX
|
data[6] = SAFE_DIV(((data[2] << 4) - data[4]), data[8]);
|
||||||
#define DIVISOR (data[8] ? data[8] : 1);
|
data[7] = SAFE_DIV(((data[3] << 4) - data[5]), data[8]);
|
||||||
#else
|
|
||||||
#define DIVISOR (data[8])
|
|
||||||
#endif
|
|
||||||
|
|
||||||
data[6] = ((data[2] << 4) - data[4]) / DIVISOR;
|
|
||||||
data[7] = ((data[3] << 4) - data[5]) / DIVISOR;
|
|
||||||
data[0]++;
|
data[0]++;
|
||||||
// fallthrough
|
// fallthrough
|
||||||
case 1:
|
case 1:
|
||||||
|
|||||||
+1
-1
@@ -1333,7 +1333,7 @@ void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnim
|
|||||||
s16 ConvertScaleParam(s16 scale)
|
s16 ConvertScaleParam(s16 scale)
|
||||||
{
|
{
|
||||||
s32 val = 0x10000;
|
s32 val = 0x10000;
|
||||||
return val / scale;
|
return SAFE_DIV(val, scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd)
|
void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd)
|
||||||
|
|||||||
Reference in New Issue
Block a user