Skip to content

Commit 8170c03

Browse files
authored
Use standard union for Div Mod (#693)
* Cleanup ui.c div and mod * Cleanup divRem in pauseMenu.c * Add non matchings * Consistent defines * Restore comment
1 parent 1a3f9e0 commit 8170c03

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

src/menu/pauseMenu.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,26 +1232,14 @@ bool32 sub_080A5F24(void) {
12321232
return result;
12331233
}
12341234

1235-
typedef union {
1236-
struct {
1237-
s32 v1;
1238-
s32 v2;
1239-
} values;
1240-
u64 raw;
1241-
} DoubleReturnValue;
1242-
12431235
void sub_080A5F48(Item item, u32 offset) {
1244-
// this funcitons signature allows the div function to return a u64 (2x 32 bit registers)
1245-
// with the result in one register and the remainder in the other
1246-
typedef u64 DivRem(u32, u32);
1247-
12481236
s32 ammoCount;
12491237
s32 onesDigit;
12501238
s32 tensDigit;
12511239
void* dest;
12521240
u16* temp2;
12531241
u32 index;
1254-
DoubleReturnValue ret;
1242+
union SplitDWord divRem;
12551243

12561244
switch (item) {
12571245
case ITEM_BOTTLE1:
@@ -1281,9 +1269,9 @@ void sub_080A5F48(Item item, u32 offset) {
12811269
if (ammoCount < 0)
12821270
return;
12831271

1284-
ret.raw = ((DivRem*)Div)(ammoCount, 10); // by casting to DivRem, we can recover the remainder from the Div call
1285-
onesDigit = ret.values.v2;
1286-
tensDigit = ret.values.v1;
1272+
divRem = DivAndMod(ammoCount, 10);
1273+
onesDigit = divRem.HALF.HI;
1274+
tensDigit = divRem.HALF.LO;
12871275

12881276
if (tensDigit >= 10)
12891277
tensDigit = 9;

src/ui.c

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,26 @@ void sub_0801C25C(void) {
113113
}
114114
}
115115

116-
void sub_0801C2F0(u32 param_1, u32 param_2) {
117-
u32 uVar1;
118-
register u32 rem asm("r1");
119-
param_1 = param_1 * 0x20 + 0x6010000;
116+
void sub_0801C2F0(u32 dest, u32 param_2) {
117+
u32 tensDigit;
118+
#ifdef NON_MATCHING
119+
u32 onesDigit;
120+
union SplitDWord divRem;
121+
#else
122+
FORCE_REGISTER(u32 onesDigit, r1);
123+
FORCE_REGISTER(union SplitDWord divRem, r0);
124+
#endif
120125

121-
uVar1 = Div(param_2, 10);
122-
if (uVar1 > 9) {
123-
uVar1 = 9;
124-
}
126+
dest = dest * 0x20 + 0x6010000;
127+
divRem = DivAndMod(param_2, 10);
128+
onesDigit = divRem.HALF_U.HI;
129+
tensDigit = divRem.HALF_U.LO;
130+
131+
if (tensDigit >= 10)
132+
tensDigit = 9;
125133

126-
DmaCopy32(3, (gUnk_085C4620 + uVar1 * 8), param_1, 0x8 * 4);
127-
DmaCopy32(3, (gUnk_085C4620 + (rem + 10) * 8), param_1 + 0x20, 0x8 * 4);
134+
DmaCopy32(3, gUnk_085C4620 + tensDigit * 0x8, dest, 0x8 * 4);
135+
DmaCopy32(3, gUnk_085C4620 + (onesDigit + 10) * 0x8, dest + 0x20, 0x8 * 4);
128136
}
129137

130138
void DrawUI(void) {
@@ -277,7 +285,11 @@ void RenderDigits(u32 iconVramIndex, u32 count, u32 isTextYellow, u32 digits) {
277285
u32 digit;
278286
vu32* ptr;
279287
vu32* ptr2;
280-
register u32 r1 asm("r1");
288+
#ifdef NON_MATCHING
289+
union SplitDWord divRem;
290+
#else
291+
FORCE_REGISTER(union SplitDWord divRem, r0);
292+
#endif
281293

282294
puVar4 = RupeeKeyDigits;
283295
if (isTextYellow == 0) {
@@ -287,13 +299,15 @@ void RenderDigits(u32 iconVramIndex, u32 count, u32 isTextYellow, u32 digits) {
287299
iVar2 = iVar3 + 0x600c000;
288300
switch (digits) {
289301
case 3:
290-
digit = Div(count, 100);
291-
count = r1;
302+
divRem = DivAndMod(count, 100);
303+
digit = divRem.HALF_U.LO;
304+
count = divRem.HALF_U.HI;
292305
DmaCopy32(3, puVar4 + digit * 0x40, iVar2, 0x10 * 4);
293306
iVar2 += 0x40;
294307
case 2:
295-
digit = Div(count, 10);
296-
count = r1;
308+
divRem = DivAndMod(count, 10);
309+
digit = divRem.HALF_U.LO;
310+
count = divRem.HALF_U.HI;
297311
DmaCopy32(3, puVar4 + digit * 0x40, iVar2, 0x10 * 4);
298312
iVar2 += 0x40;
299313
}
@@ -440,21 +454,14 @@ void EraseChargeBar(void) {
440454
}
441455
}
442456

443-
typedef union {
444-
u32 values[2];
445-
u64 raw;
446-
} returnValues;
447-
448-
typedef u64 DivRem(u32, u32);
449-
450457
void DrawChargeBar(void) {
451458
bool32 tmp1;
452459
u16* BufferPos;
453-
returnValues ret;
454460
// these names are almost certainly inaccurate
455461
u32 chargeTime;
456462
u32 chargeState;
457463
u32 chargeFrame;
464+
union SplitDWord divRem;
458465

459466
tmp1 = FALSE;
460467
if (!(gHUD.hideFlags & HUD_HIDE_CHARGE_BAR))
@@ -476,10 +483,9 @@ void DrawChargeBar(void) {
476483
gHUD.unk_6 = 1;
477484
gHUD.unk_7 = chargeTime;
478485

479-
// this calls Div and returns the result in ret.values[0] and the remainder in ret.values[1]
480-
ret.raw = ((DivRem*)&Div)(chargeTime, 4);
481-
chargeState = ret.values[0];
482-
chargeFrame = ret.values[1];
486+
divRem = DivAndMod(chargeTime, 4);
487+
chargeState = divRem.HALF_U.LO;
488+
chargeFrame = divRem.HALF_U.HI;
483489

484490
BufferPos[0] = 0xf016;
485491
BufferPos[11] = 0xf416;

0 commit comments

Comments
 (0)