2020-10-03 15:22:44 +00:00
|
|
|
#include "global.h"
|
|
|
|
#include "fp.h"
|
2020-03-21 02:09:42 +00:00
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
s32 gUseAtanContFrac;
|
2020-03-21 02:09:42 +00:00
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* @param angle radians
|
|
|
|
* @return tan(angle)
|
|
|
|
*/
|
|
|
|
f32 Math_FTanF(f32 angle) {
|
|
|
|
f32 sin = sinf(angle);
|
|
|
|
f32 cos = cosf(angle);
|
2021-11-30 23:40:42 +00:00
|
|
|
|
2020-03-21 02:09:42 +00:00
|
|
|
return sin / cos;
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FFloorF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return floorf(x);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FCeilF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return ceilf(x);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FRoundF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return roundf(x);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FTruncF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return truncf(x);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FNearbyIntF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return nearbyintf(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arctangent approximation using a Taylor series (one quadrant) */
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAtanTaylorQF(f32 x) {
|
2020-03-22 21:19:43 +00:00
|
|
|
static const f32 coeffs[] = {
|
2020-12-26 10:44:53 +00:00
|
|
|
-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,
|
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;
|
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
while (true) {
|
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-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAtanTaylorF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
f32 t;
|
|
|
|
f32 q;
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
if (x > 0.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
t = x;
|
2020-12-26 10:44:53 +00:00
|
|
|
} else if (x < 0.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
t = -x;
|
2020-12-26 10:44:53 +00:00
|
|
|
} else if (x == 0.0f) {
|
|
|
|
return 0.0f;
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2020-03-21 02:09:42 +00:00
|
|
|
return qNaN0x10000;
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
if (t <= M_SQRT2 - 1.0f) {
|
|
|
|
return Math_FAtanTaylorQF(x);
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
if (t >= M_SQRT2 + 1.0f) {
|
|
|
|
q = M_PI / 2 - Math_FAtanTaylorQF(1.0f / t);
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2020-12-26 10:44:53 +00:00
|
|
|
q = M_PI / 4 - Math_FAtanTaylorQF((1.0f - t) / (1.0f + t));
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
if (x > 0.0f) {
|
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-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAtanContFracF(f32 x) {
|
2020-03-21 02:09:42 +00:00
|
|
|
s32 sector;
|
|
|
|
f32 z;
|
|
|
|
f32 conv;
|
|
|
|
f32 sq;
|
|
|
|
s32 i;
|
|
|
|
|
2020-12-26 10:44:53 +00:00
|
|
|
if (x >= -1.0f && x <= 1.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
sector = 0;
|
2020-12-26 10:44:53 +00:00
|
|
|
} else if (x > 1.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
sector = 1;
|
2020-12-26 10:44:53 +00:00
|
|
|
x = 1.0f / x;
|
|
|
|
} else if (x < -1.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
sector = -1;
|
2020-12-26 10:44:53 +00:00
|
|
|
x = 1.0f / x;
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2020-03-21 02:09:42 +00:00
|
|
|
return qNaN0x10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
sq = SQ(x);
|
2020-12-26 10:44:53 +00:00
|
|
|
conv = 0.0f;
|
|
|
|
z = 8.0f;
|
2020-03-22 21:19:43 +00:00
|
|
|
for (i = 8; i != 0; i--) {
|
2020-12-26 10:44:53 +00:00
|
|
|
conv = SQ(z) * sq / (2.0f * z + 1.0f + conv);
|
|
|
|
z -= 1.0f;
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
2020-12-26 10:44:53 +00:00
|
|
|
conv = x / (1.0f + conv);
|
2020-03-21 02:09:42 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* @return arctan(x) in radians, in (-pi/2,pi/2) range
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAtanF(f32 x) {
|
|
|
|
if (!gUseAtanContFrac) {
|
|
|
|
return Math_FAtanTaylorF(x);
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2020-12-26 10:44:53 +00:00
|
|
|
return Math_FAtanContFracF(x);
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* @return angle to (x,y) from vector (1,0) around (0,0) in radians, in (-pi,pi] range
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAtan2F(f32 y, f32 x) {
|
|
|
|
if (x == 0.0f) {
|
|
|
|
if (y == 0.0f) {
|
|
|
|
return 0.0f;
|
|
|
|
} else if (y > 0.0f) {
|
2020-03-21 02:09:42 +00:00
|
|
|
return M_PI / 2;
|
2020-12-26 10:44:53 +00:00
|
|
|
} else if (y < 0.0f) {
|
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-12-26 10:44:53 +00:00
|
|
|
} else if (x >= 0.0f) {
|
|
|
|
return Math_FAtanF(y / x);
|
|
|
|
} else if (y < 0.0f) {
|
|
|
|
return Math_FAtanF(y / x) - M_PI;
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2020-12-26 10:44:53 +00:00
|
|
|
return M_PI - Math_FAtanF(-(y / x));
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* @return arcsin(x) in radians, in [-pi/2,pi/2] range
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAsinF(f32 x) {
|
|
|
|
return Math_FAtan2F(x, sqrtf(1.0f - SQ(x)));
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 21:29:36 +00:00
|
|
|
/**
|
|
|
|
* @return arccos(x) in radians, in [0,pi] range
|
|
|
|
*/
|
2020-12-26 10:44:53 +00:00
|
|
|
f32 Math_FAcosF(f32 x) {
|
|
|
|
return M_PI / 2 - Math_FAsinF(x);
|
2020-03-21 02:09:42 +00:00
|
|
|
}
|