From 8d83727429602d422a524d00244f16f46ea8e70e Mon Sep 17 00:00:00 2001 From: mzxrules Date: Wed, 19 Jun 2024 00:00:23 -0400 Subject: Experiment: remove global.h dependency from sys_math, sys_math3d, z_lib (#1956) * split sys_math, sys_math3d, z_lib from global.h * suggestions * forgot this * more math stuff * nit fix * re-add ichain.h * resolve tharo's comments --- src/code/code_80069420.c | 2 +- src/code/code_800FCE80.c | 176 ------------------------------------------ src/code/fault.c | 4 +- src/code/fmodf.c | 2 +- src/code/fp_math.c | 177 +++++++++++++++++++++++++++++++++++++++++++ src/code/rand.c | 2 +- src/code/sys_math.c | 2 +- src/code/sys_math3d.c | 16 ++-- src/code/sys_math3d_draw.c | 7 ++ src/code/sys_math_atan.c | 3 +- src/code/z_collision_check.c | 13 +--- src/code/z_kankyo.c | 2 +- src/code/z_lib.c | 11 ++- src/code/z_olib.c | 4 +- 14 files changed, 215 insertions(+), 206 deletions(-) delete mode 100644 src/code/code_800FCE80.c create mode 100644 src/code/fp_math.c create mode 100644 src/code/sys_math3d_draw.c (limited to 'src/code') diff --git a/src/code/code_80069420.c b/src/code/code_80069420.c index 6dd0a94f4..9a190edcf 100644 --- a/src/code/code_80069420.c +++ b/src/code/code_80069420.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "ultra64.h" /** * memcpy: copies `len` bytes from memory starting at `src` to memory starting at `dest`. Expects the memory diff --git a/src/code/code_800FCE80.c b/src/code/code_800FCE80.c deleted file mode 100644 index 8ac46833d..000000000 --- a/src/code/code_800FCE80.c +++ /dev/null @@ -1,176 +0,0 @@ -#include "global.h" - -s32 gUseAtanContFrac; - -/** - * @param angle radians - * @return tan(angle) - */ -f32 Math_FTanF(f32 angle) { - f32 sin = sinf(angle); - f32 cos = cosf(angle); - - return sin / cos; -} - -f32 Math_FFloorF(f32 x) { - return floorf(x); -} - -f32 Math_FCeilF(f32 x) { - return ceilf(x); -} - -f32 Math_FRoundF(f32 x) { - return roundf(x); -} - -f32 Math_FTruncF(f32 x) { - return truncf(x); -} - -f32 Math_FNearbyIntF(f32 x) { - return nearbyintf(x); -} - -/* Arctangent approximation using a Taylor series (one quadrant) */ -f32 Math_FAtanTaylorQF(f32 x) { - 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, - }; - - f32 poly = x; - f32 sq = SQ(x); - f32 exp = x * sq; - const f32* c = coeffs; - f32 term; - - while (true) { - term = *c++ * exp; - if (poly + term == poly) { - break; - } - poly += term; - exp *= sq; - } - - return poly; -} - -/* Ditto for two quadrants */ -f32 Math_FAtanTaylorF(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 Math_FAtanTaylorQF(x); - } - - if (t >= M_SQRT2 + 1.0f) { - q = M_PI / 2 - Math_FAtanTaylorQF(1.0f / t); - } else { - q = M_PI / 4 - Math_FAtanTaylorQF((1.0f - t) / (1.0f + t)); - } - - if (x > 0.0f) { - return q; - } else { - return -q; - } -} - -/* Arctangent approximation using a continued fraction */ -f32 Math_FAtanContFracF(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; - } - - 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; - } -} - -/** - * @return arctan(x) in radians, in (-pi/2,pi/2) range - */ -f32 Math_FAtanF(f32 x) { - if (!gUseAtanContFrac) { - return Math_FAtanTaylorF(x); - } else { - return Math_FAtanContFracF(x); - } -} - -/** - * @return angle to (x,y) from vector (1,0) around (0,0) in radians, in (-pi,pi] range - */ -f32 Math_FAtan2F(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 Math_FAtanF(y / x); - } else if (y < 0.0f) { - return Math_FAtanF(y / x) - M_PI; - } else { - return M_PI - Math_FAtanF(-(y / x)); - } -} - -/** - * @return arcsin(x) in radians, in [-pi/2,pi/2] range - */ -f32 Math_FAsinF(f32 x) { - return Math_FAtan2F(x, sqrtf(1.0f - SQ(x))); -} - -/** - * @return arccos(x) in radians, in [0,pi] range - */ -f32 Math_FAcosF(f32 x) { - return M_PI / 2 - Math_FAsinF(x); -} diff --git a/src/code/fault.c b/src/code/fault.c index 662c1a9f1..7d8e62fef 100644 --- a/src/code/fault.c +++ b/src/code/fault.c @@ -48,9 +48,9 @@ // just above (the exact upper bound depends on the block numbers assigned to // extern variables declared in headers). #if OOT_DEBUG -#pragma increment_block_number 20 +#pragma increment_block_number 0 #else -#pragma increment_block_number 25 +#pragma increment_block_number 20 #endif void FaultDrawer_Init(void); diff --git a/src/code/fmodf.c b/src/code/fmodf.c index e1e386223..4ebec43bd 100644 --- a/src/code/fmodf.c +++ b/src/code/fmodf.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "ultra64.h" /** * Computes one `x` modulo `y` for floats. diff --git a/src/code/fp_math.c b/src/code/fp_math.c new file mode 100644 index 000000000..c429b83e2 --- /dev/null +++ b/src/code/fp_math.c @@ -0,0 +1,177 @@ +#include "z64math.h" +#include "macros.h" + +s32 gUseAtanContFrac; + +/** + * @param angle radians + * @return tan(angle) + */ +f32 Math_FTanF(f32 angle) { + f32 sin = sinf(angle); + f32 cos = cosf(angle); + + return sin / cos; +} + +f32 Math_FFloorF(f32 x) { + return floorf(x); +} + +f32 Math_FCeilF(f32 x) { + return ceilf(x); +} + +f32 Math_FRoundF(f32 x) { + return roundf(x); +} + +f32 Math_FTruncF(f32 x) { + return truncf(x); +} + +f32 Math_FNearbyIntF(f32 x) { + return nearbyintf(x); +} + +/* Arctangent approximation using a Taylor series (one quadrant) */ +f32 Math_FAtanTaylorQF(f32 x) { + 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, + }; + + f32 poly = x; + f32 sq = SQ(x); + f32 exp = x * sq; + const f32* c = coeffs; + f32 term; + + while (true) { + term = *c++ * exp; + if (poly + term == poly) { + break; + } + poly += term; + exp *= sq; + } + + return poly; +} + +/* Ditto for two quadrants */ +f32 Math_FAtanTaylorF(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 Math_FAtanTaylorQF(x); + } + + if (t >= M_SQRT2 + 1.0f) { + q = M_PI / 2 - Math_FAtanTaylorQF(1.0f / t); + } else { + q = M_PI / 4 - Math_FAtanTaylorQF((1.0f - t) / (1.0f + t)); + } + + if (x > 0.0f) { + return q; + } else { + return -q; + } +} + +/* Arctangent approximation using a continued fraction */ +f32 Math_FAtanContFracF(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; + } + + 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; + } +} + +/** + * @return arctan(x) in radians, in (-pi/2,pi/2) range + */ +f32 Math_FAtanF(f32 x) { + if (!gUseAtanContFrac) { + return Math_FAtanTaylorF(x); + } else { + return Math_FAtanContFracF(x); + } +} + +/** + * @return angle to (x,y) from vector (1,0) around (0,0) in radians, in (-pi,pi] range + */ +f32 Math_FAtan2F(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 Math_FAtanF(y / x); + } else if (y < 0.0f) { + return Math_FAtanF(y / x) - M_PI; + } else { + return M_PI - Math_FAtanF(-(y / x)); + } +} + +/** + * @return arcsin(x) in radians, in [-pi/2,pi/2] range + */ +f32 Math_FAsinF(f32 x) { + return Math_FAtan2F(x, sqrtf(1.0f - SQ(x))); +} + +/** + * @return arccos(x) in radians, in [0,pi] range + */ +f32 Math_FAcosF(f32 x) { + return M_PI / 2 - Math_FAsinF(x); +} diff --git a/src/code/rand.c b/src/code/rand.c index 4a0c5dc4a..baf396542 100644 --- a/src/code/rand.c +++ b/src/code/rand.c @@ -41,7 +41,7 @@ * * @note Original name: qrand.c */ -#include "ultra64.h" +#include "rand.h" #define RAND_MULTIPLIER 1664525 #define RAND_INCREMENT 1013904223 diff --git a/src/code/sys_math.c b/src/code/sys_math.c index f19719bb9..6a7344baa 100644 --- a/src/code/sys_math.c +++ b/src/code/sys_math.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "sys_math.h" f32 sFactorialTbl[] = { 1.0f, 1.0f, 2.0f, 6.0f, 24.0f, 120.0f, 720.0f, 5040.0f, 40320.0f, 362880.0f, 3628800.0f, 39916800.0f, 479001600.0f }; diff --git a/src/code/sys_math3d.c b/src/code/sys_math3d.c index 6525f825d..f0bfb60bc 100644 --- a/src/code/sys_math3d.c +++ b/src/code/sys_math3d.c @@ -1,9 +1,13 @@ -#include "global.h" +#include "ultra64.h" +#include "z_lib.h" +#include "z64math.h" #include "terminal.h" +#include "macros.h" +#include "sys_math3d.h" // For retail BSS ordering, the block number of cbf in Math3D_CylVsCylOverlapCenterDist // must be 0. -#pragma increment_block_number 187 +#pragma increment_block_number 114 s32 Math3D_LineVsLineClosestTwoPoints(Vec3f* lineAPointA, Vec3f* lineAPointB, Vec3f* lineBPointA, Vec3f* lineBPointB, Vec3f* lineAClosestToB, Vec3f* lineBClosestToA); @@ -2147,11 +2151,3 @@ s32 Math3D_YZInSphere(Sphere16* sphere, f32 y, f32 z) { } return false; } - -#if OOT_DEBUG -void Math3D_DrawSphere(PlayState* play, Sphere16* sph) { -} - -void Math3D_DrawCylinder(PlayState* play, Cylinder16* cyl) { -} -#endif diff --git a/src/code/sys_math3d_draw.c b/src/code/sys_math3d_draw.c new file mode 100644 index 000000000..76651c825 --- /dev/null +++ b/src/code/sys_math3d_draw.c @@ -0,0 +1,7 @@ +#include "z64.h" + +void Math3D_DrawSphere(PlayState* play, Sphere16* sph) { +} + +void Math3D_DrawCylinder(PlayState* play, Cylinder16* cyl) { +} diff --git a/src/code/sys_math_atan.c b/src/code/sys_math_atan.c index efbe8a205..0647745b0 100644 --- a/src/code/sys_math_atan.c +++ b/src/code/sys_math_atan.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "z64math.h" +#include "macros.h" static u16 sAtan2Tbl[] = { 0x0000, 0x000A, 0x0014, 0x001F, 0x0029, 0x0033, 0x003D, 0x0047, 0x0051, 0x005C, 0x0066, 0x0070, 0x007A, 0x0084, diff --git a/src/code/z_collision_check.c b/src/code/z_collision_check.c index 118a445d7..bebcac08d 100644 --- a/src/code/z_collision_check.c +++ b/src/code/z_collision_check.c @@ -2360,9 +2360,6 @@ void CollisionCheck_ATTrisVsACTris(PlayState* play, CollisionCheckContext* colCh #if OOT_DEBUG static s8 sBssDummy7; -static s8 sBssDummy8; -static s8 sBssDummy9; -static s8 sBssDummy10; #endif void CollisionCheck_ATTrisVsACQuad(PlayState* play, CollisionCheckContext* colChkCtx, Collider* atCol, @@ -3329,12 +3326,10 @@ void Collider_SetTrisDim(PlayState* play, ColliderTris* tris, s32 elemIndex, Col } #if OOT_DEBUG -// Due to an unknown reason, bss ordering changed between the 2 static Vec3f variables in the function below. -// In order to reproduce this behavior, we need a specific number of bss variables in the file before that point. -// For this, we introduce a certain amount of dummy variables throughout the file, which we fit inside padding added -// by the compiler between structs like TriNorm and/or Vec3f, so they don't take space in bss. -static s8 sBssDummy11; -static s8 sBssDummy12; +// The two static Vec3f variables in the function below cross a block index rollover, causing a bss order swap. +//! In order to replicate this behavior, we declare a certain amount of sBssDummy variables throughout the file, which +//! we fit inside padding added by the compiler between structs like TriNorm and/or Vec3f, so they don't take space in +//! bss. #endif /** diff --git a/src/code/z_kankyo.c b/src/code/z_kankyo.c index ce79a7df9..aab406904 100644 --- a/src/code/z_kankyo.c +++ b/src/code/z_kankyo.c @@ -215,7 +215,7 @@ s16 sSunDepthTestY; // These variables could be moved farther down in the file to reduce the amount // of block number padding here, but currently this causes BSS ordering issues // for debug. -#pragma increment_block_number 230 +#pragma increment_block_number 227 LightNode* sNGameOverLightNode; LightInfo sNGameOverLightInfo; diff --git a/src/code/z_lib.c b/src/code/z_lib.c index 58a291162..5eba9346f 100644 --- a/src/code/z_lib.c +++ b/src/code/z_lib.c @@ -1,4 +1,11 @@ -#include "global.h" +#include "ultra64.h" +#include "z_lib.h" +#include "ichain.h" +#include "regs.h" +#include "macros.h" +#include "sys_math.h" +#include "rand.h" +#include "sfx.h" /** * memset: sets `len` bytes to `val` starting at address `dest`. @@ -334,7 +341,7 @@ void (*sInitChainHandlers[])(u8* ptr, InitChainEntry* ichain) = { IChain_Apply_Vec3f, IChain_Apply_Vec3fdiv1000, IChain_Apply_Vec3s, }; -void Actor_ProcessInitChain(Actor* actor, InitChainEntry* ichain) { +void Actor_ProcessInitChain(struct Actor* actor, InitChainEntry* ichain) { do { sInitChainHandlers[ichain->type]((u8*)actor, ichain); } while ((ichain++)->cont); diff --git a/src/code/z_olib.c b/src/code/z_olib.c index d5056e8f7..c1f93a052 100644 --- a/src/code/z_olib.c +++ b/src/code/z_olib.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "z64math.h" +#include "fp_math.h" +#include "z_lib.h" /** * Calculates the distances between `a` and `b` -- cgit v1.2.3