1
0
mirror of https://github.com/zeldaret/oot.git synced 2024-09-22 21:35:27 +00:00
oot/src/code/code_800FCE80.c

162 lines
2.9 KiB
C
Raw Normal View History

2020-03-21 02:09:42 +00:00
#include <ultra64.h>
#include <global.h>
#include <fp.h>
s32 use_cfrac;
2020-03-22 21:19:43 +00:00
f32 Math_tanf(f32 x) {
2020-03-21 02:09:42 +00:00
f32 sin = sinf(x);
f32 cos = cosf(x);
return sin / cos;
}
2020-03-22 21:19:43 +00:00
f32 Math_floorf(f32 x) {
2020-03-21 02:09:42 +00:00
return floorf(x);
}
2020-03-22 21:19:43 +00:00
f32 Math_ceilf(f32 x) {
2020-03-21 02:09:42 +00:00
return ceilf(x);
}
2020-03-22 21:19:43 +00:00
f32 Math_roundf(f32 x) {
2020-03-21 02:09:42 +00:00
return roundf(x);
}
2020-03-22 21:19:43 +00:00
f32 Math_truncf(f32 x) {
2020-03-21 02:09:42 +00:00
return truncf(x);
}
2020-03-22 21:19:43 +00:00
f32 Math_nearbyintf(f32 x) {
2020-03-21 02:09:42 +00:00
return nearbyintf(x);
}
/* Arctangent approximation using a Taylor series (one quadrant) */
2020-03-22 21:19:43 +00:00
f32 Math_atanf_taylor_q(f32 x) {
static const f32 coeffs[] = {
-1.f / 3, +1.f / 5, -1.f / 7, +1.f / 9, -1.f / 11, +1.f / 13, -1.f / 15, +1.f / 17, 0.f,
2020-03-21 02:09:42 +00:00
};
f32 poly = x;
f32 sq = SQ(x);
f32 exp = x * sq;
2020-03-22 21:19:43 +00:00
const f32* c = coeffs;
2020-03-21 02:09:42 +00:00
f32 term;
2020-03-22 21:19:43 +00:00
while (1) {
2020-03-21 02:09:42 +00:00
term = *c++ * exp;
2020-03-22 21:19:43 +00:00
if (poly + term == poly) {
2020-03-21 02:09:42 +00:00
break;
}
poly = poly + term;
exp = exp * sq;
}
return poly;
}
/* Ditto for two quadrants */
2020-03-22 21:19:43 +00:00
f32 Math_atanf_taylor(f32 x) {
2020-03-21 02:09:42 +00:00
f32 t;
f32 q;
2020-03-22 21:19:43 +00:00
if (x > 0.f) {
2020-03-21 02:09:42 +00:00
t = x;
2020-03-22 21:19:43 +00:00
} else if (x < 0.f) {
2020-03-21 02:09:42 +00:00
t = -x;
2020-03-22 21:19:43 +00:00
} else if (x == 0.f) {
2020-03-21 02:09:42 +00:00
return 0.f;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return qNaN0x10000;
}
2020-03-22 21:19:43 +00:00
if (t <= M_SQRT2 - 1.f) {
2020-03-21 02:09:42 +00:00
return Math_atanf_taylor_q(x);
}
2020-03-22 21:19:43 +00:00
if (t >= M_SQRT2 + 1.f) {
2020-03-21 02:09:42 +00:00
q = M_PI / 2 - Math_atanf_taylor_q(1.f / t);
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
q = M_PI / 4 - Math_atanf_taylor_q((1.f - t) / (1.f + t));
}
2020-03-22 21:19:43 +00:00
if (x > 0.f) {
2020-03-21 02:09:42 +00:00
return q;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return -q;
}
}
/* Arctangent approximation using a continued fraction */
2020-03-22 21:19:43 +00:00
f32 Math_atanf_cfrac(f32 x) {
2020-03-21 02:09:42 +00:00
s32 sector;
f32 z;
f32 conv;
f32 sq;
s32 i;
2020-03-22 21:19:43 +00:00
if (x >= -1.f && x <= 1.f) {
2020-03-21 02:09:42 +00:00
sector = 0;
2020-03-22 21:19:43 +00:00
} else if (x > 1.f) {
2020-03-21 02:09:42 +00:00
sector = 1;
x = 1.f / x;
2020-03-22 21:19:43 +00:00
} else if (x < -1.f) {
2020-03-21 02:09:42 +00:00
sector = -1;
x = 1.f / x;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return qNaN0x10000;
}
sq = SQ(x);
conv = 0.f;
z = 8.f;
2020-03-22 21:19:43 +00:00
for (i = 8; i != 0; i--) {
2020-03-21 02:09:42 +00:00
conv = SQ(z) * sq / (2.f * z + 1.f + conv);
z -= 1.f;
}
conv = x / (1.f + conv);
2020-03-22 21:19:43 +00:00
if (sector == 0) {
2020-03-21 02:09:42 +00:00
return conv;
2020-03-22 21:19:43 +00:00
} else if (sector > 0) {
2020-03-21 02:09:42 +00:00
return M_PI / 2 - conv;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return -M_PI / 2 - conv;
}
}
2020-03-22 21:19:43 +00:00
f32 Math_atanf(f32 x) {
if (use_cfrac == 0) {
2020-03-21 02:09:42 +00:00
return Math_atanf_taylor(x);
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return Math_atanf_cfrac(x);
}
}
2020-03-22 21:19:43 +00:00
f32 Math_atan2f(f32 y, f32 x) {
if (x == 0.f) {
if (y == 0.f) {
2020-03-21 02:09:42 +00:00
return 0.f;
2020-03-22 21:19:43 +00:00
} else if (y > 0.f) {
2020-03-21 02:09:42 +00:00
return M_PI / 2;
2020-03-22 21:19:43 +00:00
} else if (y < 0.f) {
2020-03-21 02:09:42 +00:00
return -M_PI / 2;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return qNaN0x10000;
}
2020-03-22 21:19:43 +00:00
} else if (x >= 0.f) {
2020-03-21 02:09:42 +00:00
return Math_atanf(y / x);
2020-03-22 21:19:43 +00:00
} else if (y < 0.f) {
2020-03-21 02:09:42 +00:00
return Math_atanf(y / x) - M_PI;
2020-03-22 21:19:43 +00:00
} else {
2020-03-21 02:09:42 +00:00
return M_PI - Math_atanf(-(y / x));
}
}
2020-03-22 21:19:43 +00:00
f32 Math_asinf(f32 x) {
2020-03-21 02:09:42 +00:00
return Math_atan2f(x, sqrtf(1.f - SQ(x)));
}
2020-03-22 21:19:43 +00:00
f32 Math_acosf(f32 x) {
2020-03-21 02:09:42 +00:00
return M_PI / 2 - Math_asinf(x);
}