Files
pmd-red/src/math.c
T
2024-11-05 08:26:09 -05:00

824 lines
16 KiB
C

#include "global.h"
#include "globaldata.h"
#include "math.h"
#include "data/math.h"
static u24_8 u24_8_div(u24_8, u24_8);
static u24_8 u24_8_mul(u24_8, u24_8);
static bool8 u32_pair_less_than(u32, u32, u32, u32);
static void sub_800A5A4(unkStruct_80943A8 *, unkStruct_80943A8 *, unkStruct_80943A8 *);
static void sub_800A4E4(unkStruct_80943A8 *, unkStruct_80943A8 *, unkStruct_80943A8 *);
/**
* 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[in] x The value to get modulo 3. Must be non-negative.
*
* @return The value of x modulo 3.
*/
UNUSED 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;
}
/**
* This function lexicographically compares two pairs of u32s.
*
* @note The call signature of this might change if it makes sense to pack the
* inputs into a struct representing, say, a 64-bit unsigned integer. Doing
* so does affect the generated assembly; the current approach is the simplest
* match.
*
* @param[in] x_hi The high 32 bits of the first pair.
* @param[in] x_lo The low 32 bits of the first pair.
* @param[in] y_hi The high 32 bits of the second pair.
* @param[in] y_lo The low 32 bits of the second pair.
*
* @return `TRUE` if `x < y`, `FALSE` otherwise.
*/
static bool8 u32_pair_less_than(u32 x_hi, u32 x_lo, u32 y_hi, u32 y_lo)
{
if (x_hi < y_hi)
return TRUE;
if (x_hi > y_hi)
return FALSE;
if (x_lo >= y_lo)
return FALSE;
return TRUE;
}
s24_8 s24_8_mul(s24_8 x, s24_8 y)
{
s24_8 ret;
bool8 sgn0 = x < 0;
bool8 sgn1 = y < 0;
if (x == 0) {
return 0;
}
if (y == 0) {
return 0;
}
if (sgn0) {
x = -x;
}
if (sgn1) {
y = -y;
}
ret = u24_8_mul(x, y);
if (sgn0 != sgn1) {
ret = -ret;
}
return ret;
}
/**
* This function divides two signed 24.8 fixed-point numbers.
*
* @param[in] x The dividend.
* @param[in] y The divisor.
*
* @returns The quotient `x/y` as a signed 24.8 fixed-point number.
*/
s24_8 s24_8_div(s24_8 x, s24_8 y)
{
s24_8 ret;
bool8 sgn0 = x < 0;
bool8 sgn1 = y < 0;
if (y == 0) {
return INT32_MAX;
}
if (x == 0) {
return 0;
}
if (sgn0) {
x = -x;
}
if (sgn1) {
y = -y;
}
ret = u24_8_div(x, y);
if (sgn0 != sgn1) {
ret = -ret;
}
return ret;
}
/**
* This function multiplies two unsigned 24.8 fixed-point numbers.
*
* @param[in] x The first factor.
* @param[in] y The second factor.
*
* @return The product `x*y` as an unsigned 24.8 fixed-point number.
*/
static u24_8 u24_8_mul(u24_8 x, u24_8 y)
{
// We need 64 bits for intermediate steps of the multiplication.
u32 x_h, x_l;
u32 y_h, y_l;
u32 out_h, out_l;
s32 i;
u32 high_bit_mask;
u32 round_up;
if (x == 0 || y == 0) {
return 0;
}
x_h = 0;
x_l = x;
y_h = 0;
y_l = y;
out_h = 0;
out_l = 0;
high_bit_mask = 0x80 << 0x18; // high bit of u32
for (i = 0; i < 64; ++i) {
u32 prev_out_l = out_l;
u32 y_bit = 1;
y_bit &= y_l;
if (y_bit) {
out_l += x_l;
out_h += x_h;
if (prev_out_l > out_l) {
++out_h;
}
}
y_l >>= 1;
if (y_h & 1) {
y_l |= high_bit_mask;
}
y_h >>= 1;
x_h <<= 1;
if (x_l & high_bit_mask) {
x_h |= 1;
}
do {x_l <<= 1;} while(0); // wtf moment
}
round_up = (out_l >> 0x7) & 1;
out_l = (out_l >> 0x8) | (out_h << 0x18);
if (round_up) {
++out_l;
}
return out_l;
}
/**
* This function divides two unsigned 24.8 fixed-point numbers.
*
* @param[in] x The first factor.
* @param[in] y The second factor.
*
* @return The quotient `x/y` as an unsigned 24.8 fixed-point number.
*/
static u24_8 u24_8_div(u24_8 x, u24_8 y)
{
bool8 bVar1;
u32 r9;
u32 r2;
u32 r4;
u32 r5;
u32 r6;
u32 r7;
u32 r8;
int counter;
s32 sl;
s32 flag;
s32 save;
if (y == 0) {
return INT32_MAX;
}
else if (x == 0) {
return 0;
}
else {
r7 = x >> 0x18;
r6 = x << 8;
sl = y;
r9 = 0;
r5 = 0;
r4 = 0;
r2 = 0;
r8 = 1;
for(counter = 0x3f; counter >= 0; counter--)
{
r5 <<= 1;
if ((r4 & 0x80000000) != 0) {
r5 |= r8;
}
flag = -2;
r4 <<= 1;
if ((r7 & 0x80000000) != 0) {
r4 |= r8;
}
r7 <<= 1;
if ((r6 & 0x80000000) != 0) {
r7 |= r8;
}
r6 <<= 1;
r6 &= flag;
if (u32_pair_less_than(r5,r4,0,sl) == 0) {
save = r4;
bVar1 = 1;
r4 = r4 - sl;
r5 = r5 - r2; // WHY????
if (save < r4) {
r5--;
}
}
else {
bVar1 = 0;
}
r9 <<= 1;
if (bVar1) {
r9 |= r8;
}
}
}
return r9;
}
UNUSED s32 sub_8009F68(s32 x, s32 y)
{
s32 uVar1;
s32 sVar1;
uVar1 = y;
if (y < 0) {
uVar1 = -y;
}
sVar1 = 0x100;
for (; uVar1 != 0; uVar1 >>= 1) {
if ((uVar1 & 1) != 0) {
sVar1 = s24_8_mul(sVar1, x);
}
x = s24_8_mul(x, x);
}
if (y >= 0) {
return sVar1;
}
else
return s24_8_div(0x100,sVar1);
}
s32 sub_8009FB8(s32 x, s32 y)
{
s32 r4;
s32 counter;
s32 r5;
s32 r6;
r5 = x;
r6 = y;
if (r5 < 0) {
r5 = -r5;
}
if (r6 < 0) {
r6 = -r6;
}
if (r5 < r6) {
r4 = r5;
r5 = r6;
r6 = r4;
}
if (r6 != 0) {
for(counter = 2; counter >= 0; counter--)
{
r4 = s24_8_div(r6,r5);
r4 = s24_8_mul(r4,r4);
r4 = s24_8_div(r4,r4 + 0x400);
r5 = r5 + s24_8_mul(r5,r4) * 2;
r6 = s24_8_mul(r6,r4);
}
}
return r5;
}
void sub_800A020(unkStruct_80943A8 *param_1, u32 param_2)
{
#ifndef NONMATCHING
register u32 temp asm("r4");
#else
u32 temp;
#endif
temp = 0xFFFF0000;
param_1->s0 = param_2 >> 16;
param_1->s4 = param_2 << 16;
if (param_2 & 0x8000)
param_1->s0 |= temp;
}
u32 sub_800A048(u32 *a)
{
u32 uVar1;
uVar1 = ((u16)a[0] << 16) | (a[1] >> 16);
if (a[1] & 0x8000)
uVar1++;
return uVar1;
}
UNUSED u32 sub_800A068(u32 *a)
{
u32 uVar1;
uVar1 = ((u8)a[0] << 24) | a[1] >> 8;
if (a[1] & 0x8000)
uVar1++;
return uVar1;
}
void sub_800A088(u32 *a, s32 b)
{
a[1] = b << 8;
a[0] = b >> 24;
if (a[0] & 0x80)
a[0] |= ~0x7F;
else
a[0] &= 0x7f;
}
s32 sub_800A0B0(unkStruct_80943A8 *a)
{
s32 r2;
s32 r3;
s32 idx;
s32 divi;
r2 = a->s4;
r3 = a->s0;
if (r2 == 0 && r3 == 0)
return 0;
if (r2 > 0) {
if (r3 > 0) {
if (r2 < r3) {
divi = r3 / 256;
if (divi == 0)
return 0x200;
idx = r2 / divi;
if (idx > 0xFF)
idx = 0xFF;
return gFastUnknownFn1Lookup[idx] << 4;
}
else { // r2 >= r3
divi = r2 / 256;
if (divi == 0)
return 0x200;
idx = r3 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (0x40 - gFastUnknownFn1Lookup[idx]) << 4;
}
}
else { // r3 <= 0
r3 = -r3;
if (r2 < r3) {
divi = r3 / 256;
if (divi == 0)
return 0x600;
idx = r2 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (0x80 - gFastUnknownFn1Lookup[idx]) << 4;
}
else { // r2 >= r3
divi = r2 / 256;
if (divi == 0)
return 0x600;
idx = r3 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (gFastUnknownFn1Lookup[idx] + 0x40) << 4;
}
}
}
else { // r2 <= 0
r2 = -r2;
if (r3 > 0) {
if (r2 < r3) {
divi = r3 / 256;
if (divi == 0)
return 0xE00;
idx = r2 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (0x100 - gFastUnknownFn1Lookup[idx]) << 4;
}
else { // r2 >= r3
divi = r2 / 256;
if (divi == 0)
return 0xE00;
idx = r3 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (gFastUnknownFn1Lookup[idx] + 0xC0) << 4;
}
}
else { // r3 <= 0
r3 = -r3;
if (r2 < r3) {
divi = r3 / 256;
if (divi == 0)
return 0xA00;
idx = r2 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (gFastUnknownFn1Lookup[idx] + 0x80) << 4;
}
else { // r2 >= r3
divi = r2 / 256;
if (divi == 0)
return 0xA00;
idx = r3 / divi;
if (idx > 0xFF)
idx = 0xFF;
return (0xC0 - gFastUnknownFn1Lookup[idx]) << 4;
}
}
}
}
static void sub_800A25C(unkStruct_80943A8 *a)
{
a->s0 = ~a->s0;
a->s4 = ~a->s4 + 1;
if (a->s4 == 0)
a->s0++;
}
void sub_800A27C(unkStruct_80943A8 *a)
{
if (a->s0 < 0) {
a->s0 = ~a->s0;
a->s4 = ~a->s4 + 1;
if (a->s4 == 0)
a->s0++;
}
}
bool8 sub_800A2A0(unkStruct_80943A8 *a)
{
if (a->s0 == 0 && a->s4 == 0)
return TRUE;
return FALSE;
}
UNUSED bool8 F48_16_AreEqual(unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
if (a->s0 == b->s0 && a->s4 == b->s4)
return TRUE;
return FALSE;
}
static bool8 sub_800A2DC(unkStruct_80943A8 *a)
{
if (a->s0 < 0)
return TRUE;
return FALSE;
}
bool8 sub_800A2F0(unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
s32 r1;
u32 a0;
s32 b0;
a0 = a->s0;
r1 = a0 >> 31;
b0 = b->s0;
if (b0 < 0)
r1 |= 0x2;
switch (r1) {
case 0:
default:
return u32_pair_less_than(a0, a->s4, b0, b->s4);
case 1:
return TRUE;
case 2:
return FALSE;
case 3:
return !u32_pair_less_than(a0, a->s4, b0, b->s4);
}
}
void sub_800A34C(unkStruct_80943A8 *dst, unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
bool8 aIsNegative;
bool8 bIsNegative;
unkStruct_80943A8 aa;
unkStruct_80943A8 bb;
unkStruct_80943A8 res;
aa.s0 = a->s0;
aa.s4 = a->s4;
bb.s0 = b->s0;
bb.s4 = b->s4;
aIsNegative = sub_800A2DC(&aa);
bIsNegative = sub_800A2DC(&bb);
if (sub_800A2A0(&aa)) {
dst->s0 = 0;
dst->s4 = 0;
}
else if (sub_800A2A0(&bb)) {
dst->s0 = 0;
dst->s4 = 0;
}
else {
if (aIsNegative)
sub_800A25C(&aa);
if (bIsNegative)
sub_800A25C(&bb);
sub_800A4E4(&res, &aa, &bb);
if (aIsNegative != bIsNegative)
sub_800A25C(&res);
dst->s0 = res.s0;
dst->s4 = res.s4;
}
}
void sub_800A3F0(unkStruct_80943A8 *dst, unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
bool8 aIsNegative;
bool8 bIsNegative;
unkStruct_80943A8 aa;
unkStruct_80943A8 bb;
unkStruct_80943A8 res;
aa.s0 = a->s0;
aa.s4 = a->s4;
bb.s0 = b->s0;
bb.s4 = b->s4;
aIsNegative = sub_800A2DC(&aa);
bIsNegative = sub_800A2DC(&bb);
if (sub_800A2A0(&bb)) {
dst->s0 = INT32_MAX;
dst->s4 = UINT32_MAX;
}
else if (sub_800A2A0(&aa)) {
dst->s0 = 0;
dst->s4 = 0;
}
else {
if (aIsNegative)
sub_800A25C(&aa);
if (bIsNegative)
sub_800A25C(&bb);
sub_800A5A4(&res, &aa, &bb);
if (aIsNegative != bIsNegative)
sub_800A25C(&res);
dst->s0 = res.s0;
dst->s4 = res.s4;
}
}
void sub_800A4A0(unkStruct_80943A8 *a)
{
unkStruct_80943A8 aa;
unkStruct_80943A8 res;
aa.s0 = a->s0;
aa.s4 = a->s4;
if (sub_800A2A0(&aa)) {
a->s0 = 0;
a->s4 = 0;
}
else {
sub_800A27C(&aa);
sub_800A4E4(&res, &aa, &aa);
a->s0 = res.s0;
a->s4 = res.s4;
}
}
// Regswap https://decomp.me/scratch/HNmlz
static void sub_800A4E4(unkStruct_80943A8 *dst, unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
u32 sl;
u32 r1;
u32 r2;
u32 r3;
u32 r4;
#ifdef NONMATCHING
u32 r5;
#else
register u32 r5 asm("r5");
#endif
u32 r6;
s32 i;
if (sub_800A2A0(a)) {
dst->s0 = 0;
dst->s4 = 0;
}
else if (sub_800A2A0(b)) {
dst->s0 = 0;
dst->s4 = 0;
}
else {
r1 = a->s0;
r4 = a->s4;
sl = b->s0;
r2 = b->s4;
r6 = 0;
r5 = 0;
for (i = 0; i < 64; i++) {
r3 = r5;
if (r2 & 1) {
r5 += r4;
r6 += r1;
if (r3 > r5)
r6++;
}
r2 >>= 1;
if (sl & 1)
r2 |= 0x80000000;
sl >>= 1;
r1 <<= 1;
if (r4 & 0x80000000)
r1 |= 1;
r4 <<= 1;
}
r1 = (r5 >> 15) & 1;
r5 >>= 16;
r5 |= (r6 << 16);
r6 >>= 16;
if (r1 != 0)
r5++;
dst->s0 = r6;
dst->s4 = r5;
}
}
static void sub_800A5A4(unkStruct_80943A8 *dst, unkStruct_80943A8 *a, unkStruct_80943A8 *b)
{
s32 temp;
bool8 r1;
u32 r4;
u32 r5;
u32 r6;
u32 r7;
u32 r9;
s32 sp4;
u32 sp8;
u32 spC;
s32 i;
if (sub_800A2A0(b)) {
dst->s0 = INT32_MAX;
dst->s4 = UINT32_MAX;
}
else if (sub_800A2A0(a)) {
dst->s0 = 0;
dst->s4 = 0;
}
else {
r7 = (a->s0 << 16) | (a->s4 >> 16);
r6 = (a->s4 << 16) | 0x8000;
sp4 = b->s0;
sp8 = b->s4;
spC = 0; // Effectively unused
r9 = 0;
r5 = 0;
r4 = 0;
for (i = 0; i < 64; i++) {
r5 <<= 1;
if (r4 & 0x80000000)
r5 |= 0x1;
r4 = (r4 << 1) & ~0x1;
if (r7 & 0x80000000)
r4 |= 0x1;
r7 <<= 1;
if (r6 & 0x80000000)
r7 |= 0x1;
r6 = (r6 << 1) & ~0x1;
if (!u32_pair_less_than(r5, r4, sp4, sp8)) {
temp = r4;
r1 = TRUE;
r4 -= sp8;
r5 -= sp4;
if (temp < r4)
r5--;
}
else
r1 = FALSE;
spC <<= 1;
if (r9 & 0x80000000)
spC |= 0x1;
r9 = (r9 << 1) & ~0x1;
if (r1)
r9 |= 0x1;
}
dst->s0 = spC;
dst->s4 = r9;
}
}