From f642ed6eef04369fca72f106d87ff2c6f6475066 Mon Sep 17 00:00:00 2001 From: EllipticEllipsis <73679967+EllipticEllipsis@users.noreply.github.com> Date: Tue, 28 Sep 2021 01:07:09 +0100 Subject: [PATCH] boot_0x80086760 OK and documented (#288) * boot_80086760 OK, added fp header, resplit data * Some more documentation, data imported with unions * Sweep data under the rug * Move the fp header properly * Format * include/fp.h header guards Co-authored-by: Anghelo Carvajal * Engineer's review * Fix typo Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * Rename header * while (true) Co-authored-by: Anghelo Carvajal Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> --- include/fixed_point.h | 34 ++++++ spec | 2 +- src/boot_O2/boot_80086760.c | 209 +++++++++++++++++++++++++++++++++--- tools/disasm/files.txt | 1 + tools/disasm/variables.txt | 4 +- 5 files changed, 235 insertions(+), 15 deletions(-) create mode 100644 include/fixed_point.h diff --git a/include/fixed_point.h b/include/fixed_point.h new file mode 100644 index 0000000000..bb7e964946 --- /dev/null +++ b/include/fixed_point.h @@ -0,0 +1,34 @@ +#ifndef FIXED_POINT_H +#define FIXED_POINT_H +#include "ultra64.h" + +extern f32 qNaN0x3FFFFF; +extern f32 qNaN0x10000; +extern f32 sNaN0x3FFFFF; + +f32 floorf(f32 x); +f64 floor(f64 x); +s32 lfloorf(f32 x); +s32 lfloor(f64 x); + +f32 ceilf(f32 x); +f64 ceil(f64 x); +s32 lceilf(f32 x); +s32 lceil(f64 x); + +f32 truncf(f32 x); +f64 trunc(f64 x); +s32 ltruncf(f32 x); +s32 ltrunc(f64 x); + +f32 nearbyintf(f32 x); +f64 nearbyint(f64 x); +s32 lnearbyintf(f32 x); +s32 lnearbyint(f64 x); + +f32 roundf(f32 x); +f64 round(f64 x); +s32 lroundf(f32 x); +s32 lround(f64 x); + +#endif diff --git a/spec b/spec index 6b028302d3..41c7d61f81 100644 --- a/spec +++ b/spec @@ -43,8 +43,8 @@ beginseg include "build/data/boot/boot_800862E0.data.o" include "build/src/boot_O2/padsetup.o" include "build/src/boot_O2/boot_80086760.o" - include "build/data/boot/boot_80086760.bss.o" include "build/asm/boot/fp.text.o" + include "build/data/boot/fp.data.o" include "build/src/boot_O2/system_malloc.o" include "build/src/boot_O2/rand.o" include "build/src/boot_O2/__osMalloc.o" diff --git a/src/boot_O2/boot_80086760.c b/src/boot_O2/boot_80086760.c index b680133704..1a9b374538 100644 --- a/src/boot_O2/boot_80086760.c +++ b/src/boot_O2/boot_80086760.c @@ -1,27 +1,210 @@ +/** + * MathF library + * Contains tangent function, wrappers for a number of the handwritten functions in fp, and a suite of arctangents + */ #include "global.h" +#include "fixed_point.h" -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086760.s") +extern f32 func_80086C70(f32 x); +extern f32 func_80086CA8(f32 x); +extern f32 func_80086D50(f32 x); +extern f32 func_80086CE0(f32 x); +extern f32 func_80086D18(f32 x); -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086794.s") +s32 gUseAtanContFrac; -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_800867B4.s") +/** + * Tangent function computed using libultra __sinf and __cosf + */ +// Math_FTanF +f32 func_80086760(f32 x) { + return __sinf(x) / __cosf(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_800867D4.s") +// Unused +// Math_FFloorF +f32 func_80086794(f32 x) { + func_80086C70(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_800867F4.s") +// Unused +// Math_FCeilF +f32 func_800867B4(f32 x) { + func_80086CA8(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086814.s") +// Unused +// Math_FRoundF +f32 func_800867D4(f32 x) { + func_80086D50(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086834.s") +// Unused +// Math_FTruncF +f32 func_800867F4(f32 x) { + func_80086CE0(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086880.s") +// Math_FNearbyIntF +f32 func_80086814(f32 x) { + func_80086D18(x); +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_800869A4.s") +/** + * Arctangent approximation using a Maclaurin series [https://mathworld.wolfram.com/MaclaurinSeries.html] + * (one quadrant, i.e. |x| < 1) + */ +// Math_FAtanTaylorQF +f32 func_80086834(f32 x) { + // Coefficients of Maclaurin series of arctangent + static const f32 coeffs[] = { + -1.0f / 3, +1.0f / 5, -1.0f / 7, +1.0f / 9, -1.0f / 11, +1.0f / 13, -1.0f / 15, +1.0f / 17, 0.0f, + }; -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086AF0.s") + f32 poly = x; + f32 sq = SQ(x); + f32 exp = x * sq; + const f32* c = coeffs; + f32 term; -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086B30.s") + // Calculate the series until adding more terms does not change the float + while (true) { + term = *c++ * exp; + if (poly + term == poly) { + break; + } + poly += term; + exp *= sq; + } -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086C18.s") + return poly; +} -#pragma GLOBAL_ASM("asm/non_matchings/boot/boot_80086760/func_80086C48.s") +/** + * Extends previous arctangent function to the rest of the real numbers. + * Uses the formulae arctan(x) = pi/2 - arctan(1/x) + * and arctan(x) = pi/4 - arctan( (1-x)/(1+x) ) + * to extend the range in which the series computed by func_80086834 is a good approximation + */ +// Math_FAtanTaylorF +f32 func_80086880(f32 x) { + f32 t; + f32 q; + + if (x > 0.0f) { + t = x; + } else if (x < 0.0f) { + t = -x; + } else if (x == 0.0f) { + return 0.0f; + } else { + return qNaN0x10000; + } + + if (t <= M_SQRT2 - 1.0f) { + return func_80086834(x); + } + + if (t >= M_SQRT2 + 1.0f) { + q = M_PI / 2 - func_80086834(1.0f / t); + } else { // in the interval (\sqrt{2} - 1, \sqrt{2} + 1) + q = M_PI / 4 - func_80086834((1.0f - t) / (1.0f + t)); + } + + if (x > 0.0f) { + return q; + } else { + return -q; + } +} + +/** + * Arctangent approximation using a continued fraction + * Cf. https://en.wikipedia.org/wiki/Gauss%27s_continued_fraction#The_series_2F1_2 , + * https://dlmf.nist.gov/4.25#E4 + */ +// Math_FAtanContFracF +f32 func_800869A4(f32 x) { + s32 sector; + f32 z; + f32 conv; + f32 sq; + s32 i; + + if (x >= -1.0f && x <= 1.0f) { + sector = 0; + } else if (x > 1.0f) { + sector = 1; + x = 1.0f / x; + } else if (x < -1.0f) { + sector = -1; + x = 1.0f / x; + } else { + return qNaN0x10000; + } + + // Builds the continued fraction from the innermost fraction out + sq = SQ(x); + conv = 0.0f; + z = 8.0f; + for (i = 8; i != 0; i--) { + conv = SQ(z) * sq / (2.0f * z + 1.0f + conv); + z -= 1.0f; + } + conv = x / (1.0f + conv); + + if (sector == 0) { + return conv; + } else if (sector > 0) { + return M_PI / 2 - conv; + } else { + return -M_PI / 2 - conv; + } +} + +// Math_FAtanF +/** + * Single-argument arctangent, only used by the two-argument function. + * Nothing else sets the bss variable gUseAtanContFrac, so the Maclaurin series is always used + */ +f32 func_80086AF0(f32 x) { + if (!gUseAtanContFrac) { + return func_80086880(x); + } else { + return func_800869A4(x); + } +} + +// Math_FAtan2F +/** + * Main two-argument arctangent function + */ +f32 func_80086B30(f32 y, f32 x) { + if (x == 0.0f) { + if (y == 0.0f) { + return 0.0f; + } else if (y > 0.0f) { + return M_PI / 2; + } else if (y < 0.0f) { + return -M_PI / 2; + } else { + return qNaN0x10000; + } + } else if (x >= 0.0f) { + return func_80086AF0(y / x); + } else if (y < 0.0f) { + return func_80086AF0(y / x) - M_PI; + } else { + return M_PI - func_80086AF0(-(y / x)); + } +} + +// Math_FAsinF +f32 func_80086C18(f32 x) { + return func_80086B30(x, sqrtf(1.0f - SQ(x))); +} + +// Math_FAcosF +f32 func_80086C48(f32 x) { + return M_PI / 2 - func_80086C18(x); +} diff --git a/tools/disasm/files.txt b/tools/disasm/files.txt index eb4be6eb39..ed536b3188 100644 --- a/tools/disasm/files.txt +++ b/tools/disasm/files.txt @@ -257,6 +257,7 @@ 0x80096C40 : "stackcheck", 0x80096C50 : "gfxprint", 0x80097500 : "boot_800862E0", + 0x80097520 : "fp", 0x80097530 : "rand", 0x80097540 : "vimodeHpf", 0x800975E0 : "sins", diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 91bfbab479..cc79d6fe8a 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -36,7 +36,9 @@ 0x80096CF0:("sGfxPrintUnkData","u8","[8]",0x8), 0x80096CF8:("sGfxPrintFontData","u8","[0x800]",0x800), 0x80097500:("D_80097500","UNK_TYPE4","",0x4), - 0x80097524:("D_80097524","UNK_TYPE4","",0x4), + 0x80097520:("qNaN0x3FFFFF","f32","",0x4), + 0x80097524:("qNaN0x10000","f32","",0x4), + 0x80097528:("sNaN0x3FFFFF","f32","",0x4), 0x80097530:("sRandInt","u32","",0x4), 0x80097540:("osViModeNtscHpf1","OSViMode","",0x50), 0x80097590:("osViModePalLan1","OSViMode","",0x50),