summaryrefslogtreecommitdiff
path: root/src/code/code_800FCE80.c
blob: 5180c24447a309b2c29edb11f049a709921ca1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <ultra64.h>
#include <global.h>
#include <fp.h>

s32 use_cfrac;

f32 Math_tanf(f32 x) {
    f32 sin = sinf(x);
    f32 cos = cosf(x);
    return sin / cos;
}

f32 Math_floorf(f32 x) {
    return floorf(x);
}

f32 Math_ceilf(f32 x) {
    return ceilf(x);
}

f32 Math_roundf(f32 x) {
    return roundf(x);
}

f32 Math_truncf(f32 x) {
    return truncf(x);
}

f32 Math_nearbyintf(f32 x) {
    return nearbyintf(x);
}

/* Arctangent approximation using a Taylor series (one quadrant) */
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,
    };

    f32 poly = x;
    f32 sq = SQ(x);
    f32 exp = x * sq;
    const f32* c = coeffs;
    f32 term;

    while (1) {
        term = *c++ * exp;
        if (poly + term == poly) {
            break;
        }
        poly = poly + term;
        exp = exp * sq;
    }

    return poly;
}

/* Ditto for two quadrants */
f32 Math_atanf_taylor(f32 x) {
    f32 t;
    f32 q;

    if (x > 0.f) {
        t = x;
    } else if (x < 0.f) {
        t = -x;
    } else if (x == 0.f) {
        return 0.f;
    } else {
        return qNaN0x10000;
    }

    if (t <= M_SQRT2 - 1.f) {
        return Math_atanf_taylor_q(x);
    }

    if (t >= M_SQRT2 + 1.f) {
        q = M_PI / 2 - Math_atanf_taylor_q(1.f / t);
    } else {
        q = M_PI / 4 - Math_atanf_taylor_q((1.f - t) / (1.f + t));
    }

    if (x > 0.f) {
        return q;
    } else {
        return -q;
    }
}

/* Arctangent approximation using a continued fraction */
f32 Math_atanf_cfrac(f32 x) {
    s32 sector;
    f32 z;
    f32 conv;
    f32 sq;
    s32 i;

    if (x >= -1.f && x <= 1.f) {
        sector = 0;
    } else if (x > 1.f) {
        sector = 1;
        x = 1.f / x;
    } else if (x < -1.f) {
        sector = -1;
        x = 1.f / x;
    } else {
        return qNaN0x10000;
    }

    sq = SQ(x);
    conv = 0.f;
    z = 8.f;
    for (i = 8; i != 0; i--) {
        conv = SQ(z) * sq / (2.f * z + 1.f + conv);
        z -= 1.f;
    }
    conv = x / (1.f + conv);

    if (sector == 0) {
        return conv;
    } else if (sector > 0) {
        return M_PI / 2 - conv;
    } else {
        return -M_PI / 2 - conv;
    }
}

f32 Math_atanf(f32 x) {
    if (use_cfrac == 0) {
        return Math_atanf_taylor(x);
    } else {
        return Math_atanf_cfrac(x);
    }
}

f32 Math_atan2f(f32 y, f32 x) {
    if (x == 0.f) {
        if (y == 0.f) {
            return 0.f;
        } else if (y > 0.f) {
            return M_PI / 2;
        } else if (y < 0.f) {
            return -M_PI / 2;
        } else {
            return qNaN0x10000;
        }
    } else if (x >= 0.f) {
        return Math_atanf(y / x);
    } else if (y < 0.f) {
        return Math_atanf(y / x) - M_PI;
    } else {
        return M_PI - Math_atanf(-(y / x));
    }
}

f32 Math_asinf(f32 x) {
    return Math_atan2f(x, sqrtf(1.f - SQ(x)));
}

f32 Math_acosf(f32 x) {
    return M_PI / 2 - Math_asinf(x);
}