From f9e6f7540fed38b3ee36db7aad01d65831ca6dd9 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 20 May 2020 15:23:21 -0400 Subject: [PATCH] Use fixed point macros for math util --- data/battle_anim_scripts.s | 2 +- include/global.h | 2 +- src/battle_anim_effects_2.c | 8 +++--- src/berry_crush.c | 50 ++++++++++++++++++------------------- src/frontier_pass.c | 20 +++++++-------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/data/battle_anim_scripts.s b/data/battle_anim_scripts.s index 23416bde8..cacb1c443 100644 --- a/data/battle_anim_scripts.s +++ b/data/battle_anim_scripts.s @@ -4197,7 +4197,7 @@ Move_AIR_CUTTER: monbg ANIM_DEF_PARTNER setalpha 12, 8 delay 0 - createvisualtask AnimTask_AirCutterProjectile, 2, 32, -24, 1536, 2, 128 + createvisualtask AnimTask_AirCutterProjectile, 2, 32, -24, 6 * 256, 2, 128 @ 6 * 256 == Q_8_8(6) waitforvisualfinish playsewithpan SE_W015, SOUND_PAN_TARGET createsprite gAirCutterSliceSpriteTemplate, ANIM_ATTACKER, 2, 40, -32, 0, 2 diff --git a/include/global.h b/include/global.h index bcf3e6486..2b03ebbe1 100644 --- a/include/global.h +++ b/include/global.h @@ -52,7 +52,7 @@ #define Q_4_12(n) ((s16)((n) * 4096)) // Converts a number to Q24.8 fixed-point format -#define Q_24_8(n) ((s32)((n) * 256)) +#define Q_24_8(n) ((s32)((n) << 8)) // Converts a Q8.8 fixed-point format number to a regular integer #define Q_8_8_TO_INT(n) ((int)((n) / 256)) diff --git a/src/battle_anim_effects_2.c b/src/battle_anim_effects_2.c index f98b65913..f7d1eea10 100755 --- a/src/battle_anim_effects_2.c +++ b/src/battle_anim_effects_2.c @@ -1578,7 +1578,7 @@ static void AnimAirWaveProjectile(struct Sprite *sprite) { sprite->data[0] = 8; task->data[5] = 4; - a = MathUtil_Inv16(0x1000); + a = MathUtil_Inv16(Q_8_8(16)); sprite->pos1.x += sprite->pos2.x; sprite->pos1.y += sprite->pos2.y; sprite->pos2.y = 0; @@ -1597,8 +1597,8 @@ static void AnimAirWaveProjectile(struct Sprite *sprite) sprite->data[1] = 0; sprite->data[6] = 0; sprite->data[5] = 0; - sprite->data[3] = MathUtil_Mul16(MathUtil_Mul16(b, a), MathUtil_Inv16(0x1C0)); - sprite->data[4] = MathUtil_Mul16(MathUtil_Mul16(c, a), MathUtil_Inv16(0x1C0)); + sprite->data[3] = MathUtil_Mul16(MathUtil_Mul16(b, a), MathUtil_Inv16(Q_8_8(1.75))); + sprite->data[4] = MathUtil_Mul16(MathUtil_Mul16(c, a), MathUtil_Inv16(Q_8_8(1.75))); sprite->callback = AnimAirWaveProjectile_Step1; } } @@ -1690,7 +1690,7 @@ void AnimTask_AirCutterProjectile(u8 taskId) xDiff = attackerX - targetX; gTasks[taskId].data[5] = MathUtil_Mul16(xDiff, MathUtil_Inv16(gBattleAnimArgs[2] & ~1)); - gTasks[taskId].data[6] = MathUtil_Mul16(gTasks[taskId].data[5], 0x80); + gTasks[taskId].data[6] = MathUtil_Mul16(gTasks[taskId].data[5], Q_8_8(0.5)); gTasks[taskId].data[7] = gBattleAnimArgs[2]; if (targetY >= attackerY) { diff --git a/src/berry_crush.c b/src/berry_crush.c index 721fa4729..383d273c4 100755 --- a/src/berry_crush.c +++ b/src/berry_crush.c @@ -918,10 +918,10 @@ void sub_8020E58(void) u32 var0, var1; var0 = gUnknown_02022C90->unk68.as_four_players.unk00.unk04; - var0 <<= 8; - var0 = MathUtil_Div32(var0, 60 << 8); + var0 = Q_24_8(var0); + var0 = MathUtil_Div32(var0, Q_24_8(60)); var1 = gUnknown_02022C90->unk68.as_four_players.unk00.unk0A; - var1 <<= 8; + var1 = Q_24_8(var1); var1 = MathUtil_Div32(var1, var0) & 0xFFFF; gUnknown_02022C90->unk16 = var1; switch (gUnknown_02022C90->unk9) @@ -1232,12 +1232,12 @@ void sub_80214A8(struct BerryCrushGame *arg0, struct BerryCrushGame_138 *arg1) var0 *= 128; var6 = var5 + 32; var6 = var6 / 2; - var1 = MathUtil_Div16Shift(7, 0x3F80, var6); + var1 = MathUtil_Div16Shift(7, Q_8_8(63.5), var6); data[0] = (u16)arg1->unk38[i]->pos1.x * 128; data[3] = MathUtil_Div16Shift(7, var0, var1); var1 = MathUtil_Mul16Shift(7, var1, 85); data[4] = 0; - data[5] = MathUtil_Div16Shift(7, 0x3F80, var1); + data[5] = MathUtil_Div16Shift(7, Q_8_8(63.5), var1); data[7] |= 0x8000; if (arg1->unkC[i]->unk8 < 0) StartSpriteAffineAnim(arg1->unk38[i], 1); @@ -1383,7 +1383,7 @@ void sub_8021944(struct BerryCrushGame_138 *arg0, u16 arg1) arg0->unk4 = arg1 / 3600; arg0->unk6 = (arg1 % 3600) / 60; - r3 = MathUtil_Mul16((arg1 % 60) << 8, 4); + r3 = MathUtil_Mul16(Q_8_8(arg1 % 60), 4); for (i = 0; i < 8; i++) { @@ -1944,7 +1944,7 @@ void sub_8022B28(struct Sprite *sprite) r7[3] = MathUtil_Div16Shift(7, r4, r5); r2 = MathUtil_Mul16Shift(7, r5, 85); r7[4] = r8; - r7[5] = MathUtil_Div16Shift(7, 0x3F80, r2); + r7[5] = MathUtil_Div16Shift(7, Q_8_8(63.5), r2); r7[6] = sprite->pos2.x / 4; r7[7] |= 0x8000; sprite->pos2.y = r8; @@ -2194,7 +2194,7 @@ static u32 sub_8022F1C(struct BerryCrushGame *r5, u8 *r2) } r5->unk10 = 0; ResetBlockReceivedFlags(); - r5->unk20 = MathUtil_Div32(r5->unk18 << 8, 0x2000); + r5->unk20 = MathUtil_Div32(Q_24_8(r5->unk18), Q_24_8(32)); break; case 5: ClearDialogWindowAndFrame(0, 1); @@ -2411,9 +2411,9 @@ void sub_802339C(struct BerryCrushGame *r4) r2_ = r4->unk1A; if (r0 - r2_ > 0) { - r2_ <<= 8; + r2_ = Q_24_8(r2_); r2_ = MathUtil_Div32(r2_, r4->unk20); - r2_ >>= 8; + r2_ = Q_24_8_TO_INT(r2_); r4->unk24 = r2_; } else @@ -2800,13 +2800,13 @@ static u32 sub_8023CAC(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1 ); r7->unk68.as_four_players.unk00.unk04 = r7->unk28; r7->unk68.as_four_players.unk00.unk06 = r7->unk18 / (r7->unk28 / 60); - r2 = MathUtil_Mul32(r7->unk30 << 8, 0x3200); - r2 = MathUtil_Div32(r2, r7->unk32 << 8) + 0x3200; - r2 >>= 8; + r2 = MathUtil_Mul32(Q_24_8(r7->unk30), Q_24_8(50)); + r2 = MathUtil_Div32(r2, Q_24_8(r7->unk32)) + Q_24_8(50); + r2 = Q_24_8_TO_INT(r2); r7->unk68.as_four_players.unk00.unk08 = r2 & 0x7F; - r2 <<= 8; - r2 = MathUtil_Div32(r2, 0x6400); - r4 = (r7->unk1C * r7->unk9) << 8; + r2 = Q_24_8(r2); + r2 = MathUtil_Div32(r2, Q_24_8(100)); + r4 = Q_24_8(r7->unk1C * r7->unk9); r4 = MathUtil_Mul32(r4, r2); r7->unk68.as_four_players.unk00.unk00 = r4 >> 8; r7->unk68.as_five_players.unk1C[0].unk4.as_2d_bytes[0][7] = Random() % 3; @@ -2822,10 +2822,10 @@ static u32 sub_8023CAC(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1 if (r7->unk68.as_four_players.others[r8].unk4.as_hwords[3] != 0) { r2 = r7->unk68.as_four_players.others[r8].unk4.as_hwords[2]; - r2 <<= 8; - r2 = MathUtil_Mul32(r2, 0x6400); + r2 = Q_24_8(r2); + r2 = MathUtil_Mul32(r2, Q_24_8(100)); r4 = r7->unk68.as_four_players.others[r8].unk4.as_hwords[3]; - r4 <<= 8; + r4 = Q_24_8(r4); r4 = MathUtil_Div32(r2, r4); } else @@ -2837,10 +2837,10 @@ static u32 sub_8023CAC(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1 if (r7->unk68.as_four_players.others[r8].unk4.as_hwords[3] != 0) { r2 = r7->unk68.as_four_players.others[r8].unk4.as_hwords[4]; - r2 <<= 8; - r2 = MathUtil_Mul32(r2, 0x6400); + r2 = Q_24_8(r2); + r2 = MathUtil_Mul32(r2, Q_24_8(100)); r4 = r7->unk68.as_four_players.others[r8].unk4.as_hwords[3]; - r4 <<= 8; + r4 = Q_24_8(r4); r4 = MathUtil_Div32(r2, r4); } else @@ -2860,10 +2860,10 @@ static u32 sub_8023CAC(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1 else { r2 = r7->unk68.as_four_players.others[r8].unk4.as_hwords[5]; - r2 <<= 8; - r2 = MathUtil_Mul32(r2, 0x6400); + r2 = Q_24_8(r2); + r2 = MathUtil_Mul32(r2, Q_24_8(100)); r4 = r7->unk28; - r4 <<= 8; + r4 = Q_24_8(r4); r4 = MathUtil_Div32(r2, r4); } break; diff --git a/src/frontier_pass.c b/src/frontier_pass.c index 75893322f..6b38f0607 100644 --- a/src/frontier_pass.c +++ b/src/frontier_pass.c @@ -1016,16 +1016,16 @@ static void Task_DoFadeEffect(u8 taskId) if (!data[0]) { sub_80C5F58(TRUE, FALSE); - data[1] = 0x100; - data[2] = 0x100; + data[1] = Q_8_8(1); + data[2] = Q_8_8(1); data[3] = 0x15; data[4] = 0x15; BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 0x10, RGB_WHITE); } else { - data[1] = 0x1FC; - data[2] = 0x1FC; + data[1] = Q_8_8(1.984375); // 1 and 63/64 + data[2] = Q_8_8(1.984375); data[3] = -0x15; data[4] = -0x15; SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_OBJ_1D_MAP); @@ -1049,12 +1049,12 @@ static void Task_DoFadeEffect(u8 taskId) sPassGfx->unk30 = MathUtil_Inv16(data[2]); if (!data[0]) { - if (data[1] <= 0x1FC) + if (data[1] <= Q_8_8(1.984375)) return; } else { - if (data[1] != 0x100) + if (data[1] != Q_8_8(1)) return; } break; @@ -1154,8 +1154,8 @@ static void sub_80C5F58(bool8 arg0, bool8 arg1) gUnknown_085713E0[sPassData->unkE - 1][1] << 8, gUnknown_085713E0[sPassData->unkE - 1][0], gUnknown_085713E0[sPassData->unkE - 1][1], - MathUtil_Inv16(0x1FC), - MathUtil_Inv16(0x1FC), + MathUtil_Inv16(Q_8_8(1.984375)), // 1 and 63/64 + MathUtil_Inv16(Q_8_8(1.984375)), 0); } else @@ -1165,8 +1165,8 @@ static void sub_80C5F58(bool8 arg0, bool8 arg1) gUnknown_085713E0[sPassData->unkE - 1][1] << 8, gUnknown_085713E0[sPassData->unkE - 1][0], gUnknown_085713E0[sPassData->unkE - 1][1], - MathUtil_Inv16(0x100), - MathUtil_Inv16(0x100), + MathUtil_Inv16(Q_8_8(1)), + MathUtil_Inv16(Q_8_8(1)), 0); } }