Merge pull request #160 from rpadaki/roshan/data/document-fn-lookups

[decomp] trig functions + lookup tables
This commit is contained in:
Seth Barberee
2023-09-25 17:12:15 -07:00
committed by GitHub
12 changed files with 117 additions and 204 deletions

View File

@@ -1297,7 +1297,7 @@ _080467BE:
adds r0, r7
str r0, [r2, 0x4]
ldr r0, [sp, 0x10]
bl sub_8009C7C
bl sin_abs_4096
lsls r1, r0, 1
adds r1, r0
lsls r1, 2
@@ -1809,7 +1809,7 @@ _08046BAA:
adds r0, r4, 0
bl IncreaseEntityPixelPos
mov r0, r10
bl sub_8009C7C
bl sin_abs_4096
add r2, sp, 0x20
adds r2, r5
lsls r1, r0, 1

View File

@@ -755,7 +755,7 @@ _08047726:
adds r0, 0xFF
_08047732:
asrs r0, 8
bl sub_8009C7C
bl sin_abs_4096
ldr r2, [sp, 0x30]
adds r1, r0, 0
muls r1, r2

View File

@@ -5299,7 +5299,7 @@ _080551F2:
adds r1, r5, 0
mov r2, r8
mov r3, r9
bl EncoreMoveAction
bl EncoreMoveAction
b _080554BA
_08055200:
adds r0, r7, 0
@@ -8219,7 +8219,7 @@ _0805694A:
adds r0, 0xFF
_0805695E:
asrs r0, 8
bl sub_8009C7C
bl sin_abs_4096
ldr r2, [sp, 0x38]
adds r3, r0, 0
muls r3, r2

View File

@@ -408,7 +408,7 @@ _0807D0DE:
ldr r0, [sp, 0x10]
str r4, [r0, 0x4]
mov r0, r8
bl sub_8009C7C
bl sin_abs_4096
lsls r1, r0, 1
adds r1, r0
lsls r1, 2
@@ -1406,7 +1406,7 @@ _0807D8B0:
adds r0, r4, 0
bl IncreaseEntityPixelPos
mov r0, r10
bl sub_8009C7C
bl sin_abs_4096
add r2, sp, 0x20
adds r2, r5
lsls r1, r0, 1

View File

@@ -2173,7 +2173,7 @@ _0807F794:
cmp r0, 0
beq _0807F7E6
ldr r0, [sp, 0x100]
bl sub_8009C7C
bl sin_abs_4096
lsls r0, 5
mov r3, r8
str r0, [r3, 0x1C]

View File

@@ -5,177 +5,6 @@
.text
thumb_func_start FastMod3
@ return arg % 3
@ Has a lookup table up to 0x100
FastMod3:
push {lr}
adds r1, r0, 0
cmp r1, 0xFF
ble _08009C6C
movs r1, 0x3
bl __modsi3
b _08009C72
_08009C6C:
ldr r0, =gFastMod3Lookup
adds r0, r1, r0
ldrb r0, [r0]
_08009C72:
pop {r1}
bx r1
.pool
thumb_func_end FastMod3
thumb_func_start sub_8009C7C
sub_8009C7C:
push {lr}
adds r2, r0, 0
movs r3, 0xC0
lsls r3, 4
adds r1, r2, 0
ands r1, r3
movs r0, 0x80
lsls r0, 3
cmp r1, r0
beq _08009CB8
cmp r1, r0
bgt _08009C9A
cmp r1, 0
beq _08009CA8
b _08009CFC
_08009C9A:
movs r0, 0x80
lsls r0, 4
cmp r1, r0
beq _08009CD0
cmp r1, r3
beq _08009CE0
b _08009CFC
_08009CA8:
ldr r0, _08009CB0
ldr r1, _08009CB4
ands r1, r2
b _08009CBE
.align 2, 0
_08009CB0: .4byte gUnknown_80B8AE4
_08009CB4: .4byte 0x000003ff
_08009CB8:
ldr r0, _08009CC8
ldr r1, _08009CCC
bics r1, r2
_08009CBE:
lsls r1, 1
adds r1, r0
movs r2, 0
ldrsh r0, [r1, r2]
b _08009CFE
.align 2, 0
_08009CC8: .4byte gUnknown_80B8AE4
_08009CCC: .4byte 0x000003ff
_08009CD0:
ldr r0, _08009CD8
ldr r1, _08009CDC
ands r1, r2
b _08009CE6
.align 2, 0
_08009CD8: .4byte gUnknown_80B8AE4
_08009CDC: .4byte 0x000003ff
_08009CE0:
ldr r0, _08009CF4
ldr r1, _08009CF8
bics r1, r2
_08009CE6:
lsls r1, 1
adds r1, r0
movs r2, 0
ldrsh r0, [r1, r2]
negs r0, r0
b _08009CFE
.align 2, 0
_08009CF4: .4byte gUnknown_80B8AE4
_08009CF8: .4byte 0x000003ff
_08009CFC:
movs r0, 0
_08009CFE:
pop {r1}
bx r1
thumb_func_end sub_8009C7C
thumb_func_start sub_8009D04
sub_8009D04:
push {lr}
adds r2, r0, 0
movs r3, 0xC0
lsls r3, 4
adds r1, r2, 0
ands r1, r3
movs r0, 0x80
lsls r0, 3
cmp r1, r0
beq _08009D40
cmp r1, r0
bgt _08009D22
cmp r1, 0
beq _08009D30
b _08009D84
_08009D22:
movs r0, 0x80
lsls r0, 4
cmp r1, r0
beq _08009D50
cmp r1, r3
beq _08009D6C
b _08009D84
_08009D30:
ldr r0, _08009D38
ldr r1, _08009D3C
bics r1, r2
b _08009D72
.align 2, 0
_08009D38: .4byte gUnknown_80B8AE4
_08009D3C: .4byte 0x000003ff
_08009D40:
ldr r0, _08009D48
ldr r1, _08009D4C
ands r1, r2
b _08009D56
.align 2, 0
_08009D48: .4byte gUnknown_80B8AE4
_08009D4C: .4byte 0x000003ff
_08009D50:
ldr r0, _08009D64
ldr r1, _08009D68
bics r1, r2
_08009D56:
lsls r1, 1
adds r1, r0
movs r2, 0
ldrsh r0, [r1, r2]
negs r0, r0
b _08009D86
.align 2, 0
_08009D64: .4byte gUnknown_80B8AE4
_08009D68: .4byte 0x000003ff
_08009D6C:
ldr r0, _08009D7C
ldr r1, _08009D80
ands r1, r2
_08009D72:
lsls r1, 1
adds r1, r0
movs r2, 0
ldrsh r0, [r1, r2]
b _08009D86
.align 2, 0
_08009D7C: .4byte gUnknown_80B8AE4
_08009D80: .4byte 0x000003ff
_08009D84:
movs r0, 0
_08009D86:
pop {r1}
bx r1
thumb_func_end sub_8009D04
thumb_func_start sub_8009D8C
sub_8009D8C:
push {lr}
@@ -685,7 +514,7 @@ _0800A0E2:
ldr r0, [r1]
b _0800A252
.align 2, 0
_0800A0EC: .4byte gUnknown_80B92E4
_0800A0EC: .4byte gFastUnknownFn1Lookup
_0800A0F0:
adds r1, r2, 0
cmp r2, 0
@@ -714,7 +543,7 @@ _0800A112:
movs r0, 0x40
b _0800A250
.align 2, 0
_0800A120: .4byte gUnknown_80B92E4
_0800A120: .4byte gFastUnknownFn1Lookup
_0800A124:
negs r3, r3
cmp r2, r3
@@ -741,7 +570,7 @@ _0800A146:
movs r0, 0x80
b _0800A250
.align 2, 0
_0800A154: .4byte gUnknown_80B92E4
_0800A154: .4byte gFastUnknownFn1Lookup
_0800A158:
adds r1, r2, 0
cmp r2, 0
@@ -770,7 +599,7 @@ _0800A17A:
adds r0, 0x40
b _0800A252
.align 2, 0
_0800A188: .4byte gUnknown_80B92E4
_0800A188: .4byte gFastUnknownFn1Lookup
_0800A18C:
negs r2, r2
cmp r3, 0
@@ -795,7 +624,7 @@ _0800A1AA:
lsls r0, 1
b _0800A250
.align 2, 0
_0800A1B8: .4byte gUnknown_80B92E4
_0800A1B8: .4byte gFastUnknownFn1Lookup
_0800A1BC:
adds r1, r2, 0
cmp r2, 0
@@ -824,7 +653,7 @@ _0800A1DE:
adds r0, 0xC0
b _0800A252
.align 2, 0
_0800A1EC: .4byte gUnknown_80B92E4
_0800A1EC: .4byte gFastUnknownFn1Lookup
_0800A1F0:
negs r3, r3
cmp r2, r3
@@ -851,7 +680,7 @@ _0800A212:
adds r0, 0x80
b _0800A252
.align 2, 0
_0800A220: .4byte gUnknown_80B92E4
_0800A220: .4byte gFastUnknownFn1Lookup
_0800A224:
adds r0, r2, 0
cmp r0, 0
@@ -886,7 +715,7 @@ _0800A254:
pop {r1}
bx r1
.align 2, 0
_0800A258: .4byte gUnknown_80B92E4
_0800A258: .4byte gFastUnknownFn1Lookup
thumb_func_end sub_800A0B0
thumb_func_start sub_800A25C

View File

@@ -1960,8 +1960,9 @@ x = 0
x = x + 1
.endr
.global gUnknown_80B8AE4
gUnknown_80B8AE4: @ 80B8AE4
@ gFastSinLookup[i] = floor(256 * sin(iπ / 2048))
.global gFastSinLookup
gFastSinLookup: @ 80B8AE4
.2byte 0, 0, 0, 1, 1, 1, 2, 2
.2byte 3, 3, 3, 4, 4, 5, 5, 5
.2byte 6, 6, 7, 7, 7, 8, 8, 9
@@ -2091,8 +2092,9 @@ gUnknown_80B8AE4: @ 80B8AE4
.2byte 255, 255, 255, 255, 255, 255, 255, 255
.2byte 255, 255, 255, 255, 255, 255, 255, 255
.global gUnknown_80B92E4
gUnknown_80B92E4: @ 80B92E4
@ Need to figure out what function this actually is.
.global gFastUnknownFn1Lookup
gFastUnknownFn1Lookup: @ 80B92E4
.4byte 0, 0, 0, 0, 0, 0, 0, 1
.4byte 1, 1, 1, 1, 1, 2, 2, 2
.4byte 2, 2, 2, 3, 3, 3, 3, 3
@@ -2126,4 +2128,4 @@ gUnknown_80B92E4: @ 80B92E4
.4byte 30, 30, 30, 30, 31, 31, 31, 31
.4byte 31, 31, 31, 31, 31, 31, 31, 31
@ END file_system
@ END file_system

View File

@@ -68,6 +68,7 @@ SECTIONS {
asm/text.o(.text);
src/code_8009804.o(.text);
asm/code_8009804.o(.text);
src/math.o(.text);
asm/file_system.o(.text);
src/file_system.o(.text);
src/decompress.o(.text);
@@ -404,6 +405,7 @@ SECTIONS {
{
data/data_code_2.o(.rodata);
data/data.o(.rodata);
src/math.o(.rodata);
src/file_system.o(.rodata);
src/decompress.o(.rodata);
data/data_80B9BB8.o(.rodata);

View File

@@ -28,8 +28,8 @@ extern const s16 gUnknown_80B82AA[16 * 10];
extern const s16 gUnknown_80B83EA[16 * 10];
// code_8009804.s
extern s32 sub_8009C7C(s32);
extern s32 sub_8009D04(s32);
extern s32 sin_abs_4096(s32);
extern s32 cos_4096(s32);
static void sub_800561C(struct unkStructFor800561C *, s32, s32, u8 *);
s32 *sub_8005674(struct unkStructFor800561C *, s32);
@@ -527,7 +527,7 @@ void sub_8005838(s32 *a0, u8 kind)
*r3++ = *r2++;
*r3++ = 256;
}
{
{
s32 r8;
s32 iVar3;
s32 sp14;
@@ -550,20 +550,20 @@ void sub_8005838(s32 *a0, u8 kind)
val1 = gUnknown_2026E40;
val2 = gUnknown_2026E44;
val3 = gUnknown_2026E48;
spC = 0x400 / (val3 / 256 + 1);
iVar11 = val2 / 256;
j = iVar11;
k = iVar11;
for (sp10 = 0; sp10 < 0x400; sp10 += spC) {
tmp1 = val3 * sub_8009C7C(sp10) / 256;
tmp1 = val3 * sin_abs_4096(sp10) / 256;
sp14 = (val2 + tmp1) / 256;
r8 = (val2 - tmp1) / 256;
if (j < sp14 || k > r8) {
tmp2 = val3 * sub_8009D04(sp10) / 256;
tmp2 = val3 * cos_4096(sp10) / 256;
iVar5 = (val1 - tmp2) / 256;
iVar3 = (val1 + tmp2) / 256;
@@ -1558,7 +1558,7 @@ NAKED void sub_8005838(s32 *a0, u8 kind)
"_08005F62:\n"
"\tadds r0, r3, 0\n"
"\tstr r3, [sp, 0x10]\n"
"\tbl sub_8009C7C\n"
"\tbl sin_abs_4096\n"
"\tldr r1, [sp, 0x8]\n"
"\tmuls r0, r1\n"
"\tldr r3, [sp, 0x10]\n"
@@ -1594,7 +1594,7 @@ NAKED void sub_8005838(s32 *a0, u8 kind)
"_08005FA4:\n"
"\tadds r0, r3, 0\n"
"\tstr r3, [sp, 0x10]\n"
"\tbl sub_8009D04\n"
"\tbl cos_4096\n"
"\tldr r2, [sp, 0x8]\n"
"\tmuls r0, r2\n"
"\tldr r3, [sp, 0x10]\n"
@@ -1750,4 +1750,4 @@ void sub_80060EC(void)
gUnknown_2026E50 = !gUnknown_2026E50;
SetBldAlphaReg((gUnknown_2026E4E & 0x1F00) >> 8, gUnknown_2026E4E & 0x1F);
gUnknown_2026E38 = TRUE;
}
}

View File

@@ -2,8 +2,8 @@
// gFastMod3Lookup
// gFastDiv3Lookup
// gUnknown_80B8AE4
// gUnknown_80B92E4
// gFastSinLookup
// gFastUnknownFn1Lookup
static const u32 gUnknown_80B96E4[300] = {
0, 0, 45426, 71998,
@@ -84,4 +84,4 @@ static const u32 gUnknown_80B96E4[300] = {
};
ALIGNED(4) static const u8 gUnknown_80B9B94[] = _("pksdir0");
ALIGNED(4) static const u8 gUnknown_80B9B9C[] = _("not find file [%s]\n");
ALIGNED(4) static const u8 gUnknown_80B9B9C[] = _("not find file [%s]\n");

41
src/math.c Normal file
View File

@@ -0,0 +1,41 @@
#include "global.h"
#include "math.h"
extern u8 gFastMod3Lookup[];
extern s16 gFastSinLookup[];
u32 fast_mod_3(s32 x) {
if (x < 0x100) {
return gFastMod3Lookup[x];
}
return x % 3;
}
s32 sin_abs_4096(s32 x) {
switch (x & 0xc00) {
case 0x000:
return gFastSinLookup[x & 0x3ff];
case 0x400:
return gFastSinLookup[0x3ff - (x & 0x3ff)];
case 0x800:
return -gFastSinLookup[x & 0x3ff];
case 0xc00:
return -gFastSinLookup[0x3ff - (x & 0x3ff)];
}
return 0;
}
s32 cos_4096(s32 x) {
switch (x & 0xc00) {
case 0x000:
return gFastSinLookup[0x3ff - (x & 0x3ff)];
case 0x400:
return -gFastSinLookup[x & 0x3ff];
case 0x800:
return -gFastSinLookup[0x3ff - (x & 0x3ff)];
case 0xc00:
return gFastSinLookup[x & 0x3ff];
}
return 0;
}

39
src/math.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef GUARD_MATH_H
#define GUARD_MATH_H
/**
* This function computes a value modulo 3, using a lookup table for values less
* than 0x100.
*
* @warning This function performs an invalid memory access if x < 0.
* Hopefully it's never actually used.
*
* @param s32 x The value to get modulo 3. Must be non-negative.
*
* @return u32 The value of x modulo 3.
*/
u32 fast_mod_3(s32 x);
/**
* This function computes the sine of the absolute value of x using a lookup
* table. The period of the function is 4096, and the range is [-256, 256].
*
* @param s32 x The value to get the sine of.
*
* @return s32 `floor(256 * sin(pi * abs(x) / 2048))`
*/
s32 sin_abs_4096(s32 x);
/**
* This function computes the cosine of the absolute value of x using a lookup
* table. The period of the function is 4096, and the range is [-256, 256].
*
* @note Mathematically, `cos(abs(t)) = cos(t)`, unlike the case in `sin_abs_4096()` above.
*
* @param s32 x The value to get the cosine of.
*
* @return s32 `floor(256 * cos(pi * x / 2048))`
*/
s32 cos_4096(s32 x);
#endif // GUARD_MATH_H