-
Notifications
You must be signed in to change notification settings - Fork 0
/
trickmath.c
138 lines (119 loc) · 3.52 KB
/
trickmath.c
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
/**
* @file trickmath.c
* @brief Tricky math routines.
*/
/************************************************************************
* ___ _ _
* B / __\__ _ _ __ _ __ (_)_ ____ _| |
* O / / / _` | '__| '_ \| \ \ / / _` | |
* O / /__| (_| | | | | | | |\ V / (_| | |
* M \____/\__,_|_| |_| |_|_| \_/ \__,_|_|
*
* $Id: $
*
* Authors
* - Albert Veli
*
* Copyright (C) 2007 Albert Veli
*
* ------------------------------
*
* This file is part of Carnival
*
* Carnival is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Carnival is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
************************************************************************/
/* ----------------------------------------------
* Local variables
* ----------------------------------------------
*/
/* Calculated with tools/sin.c */
static float sintab[65] = {
0.000000, 0.024541, 0.049068, 0.073565,
0.098017, 0.122411, 0.146730, 0.170962,
0.195090, 0.219101, 0.242980, 0.266713,
0.290285, 0.313682, 0.336890, 0.359895,
0.382683, 0.405241, 0.427555, 0.449611,
0.471397, 0.492898, 0.514103, 0.534998,
0.555570, 0.575808, 0.595699, 0.615232,
0.634393, 0.653173, 0.671559, 0.689541,
0.707107, 0.724247, 0.740951, 0.757209,
0.773010, 0.788346, 0.803208, 0.817585,
0.831470, 0.844854, 0.857729, 0.870087,
0.881921, 0.893224, 0.903989, 0.914210,
0.923880, 0.932993, 0.941544, 0.949528,
0.956940, 0.963776, 0.970031, 0.975702,
0.980785, 0.985278, 0.989177, 0.992480,
0.995185, 0.997290, 0.998795, 0.999699,
1.000000
};
/* ----------------------------------------------
* Exported functions
* ----------------------------------------------
*/
/* #if this out because it causes compiler warnings */
#if 0
/* Taken from Quake III source code */
float Q_rsqrt(float number)
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = *(long *)&y; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
y = * (float *)&i;
y = y * (threehalfs - (x2 * y * y)); // 1st iteration
return y;
}
#endif
/* Lookup in sintab (taken from Rockbox source code) */
inline float u8sin(unsigned char v)
{
if (v < 65) {
return sintab[v];
} else if (v < 129) {
return sintab[128 - v];
} else if (v < 193) {
return -sintab[v - 128];
} else {
return -sintab[256 - v];
}
}
inline float u8cos(unsigned char v)
{
return u8sin(v - 64);
}
/* interpolate from sintab */
inline float u8sinf(float v)
{
float s1 = u8sin((unsigned char)v);
float s2 = u8sin((unsigned char)v + 1);
float d = s2 - s1;
return s1 + ((float)v - ((int)v)) * d;
}
inline float u8cosf(float v)
{
return u8sinf(v - 64);
}
/**
* GNU Emacs settings: K&R with 3 spaces indent.
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 3
* indent-tabs-mode: nil
* End:
*/