This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vultin.cpp
217 lines (184 loc) · 6.22 KB
/
vultin.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
The MIT License (MIT)
Copyright (c) 2015 Leonardo Laguna Ruiz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
NOTE: The code for the fixed-point operations is based on the project:
https://code.google.com/p/libfixmath/
*/
#include "vultin.h"
#include "stdio.h"
fix16_t fix_exp(fix16_t inValue) {
if (inValue == 0)
return 0x00010000;
if (inValue == 0x00010000)
return 178145;
if (inValue >= 681391)
return 0x7FFFFFFF;
if (inValue <= -772243)
return 0;
// The power-series converges much faster on positive values
// and exp(-x) = 1/exp(x).
int neg = (inValue < 0);
if (neg)
inValue = -inValue;
fix16_t result = inValue + 0x00010000;
fix16_t term = inValue;
uint_fast8_t i;
for (i = 2; i < 30; i++) {
term = fix_mul(term, fix_div(inValue, int_to_fix(i)));
result += term;
if ((term < 500) && ((i > 15) || (term < 20)))
break;
}
if (neg)
result = fix_div(0x00010000, result);
return result;
}
fix16_t fix_sin(fix16_t x0) {
fix16_t x1 = (x0 % 0x6487e /* 6.283185 */);
uint8_t sign = (x1 > 0x3243f /* 3.141593 */);
fix16_t x2 = (x1 % 0x3243f /* 3.141593 */);
fix16_t x3;
if (x2 > 0x1921f /* 1.570796 */)
x3 = fix_add(0x3243f /* 3.141593 */, (-x2));
else
x3 = x2;
fix16_t xp2 = fix_mul(x3, x3);
fix16_t acc =
fix_mul(x3, fix_add(0x10000 /* 1.000000 */,
fix_mul(fix_add((0xffffd556 /* -0.166667 */),
fix_mul(0x222 /* 0.008333 */, xp2)),
xp2)));
return (sign ? (-acc) : acc);
}
fix16_t fix_cos(fix16_t inAngle) { return fix_sin(inAngle + (fix_pi() >> 1)); }
fix16_t fix_tan(fix16_t inAngle) {
return fix_div(fix_sin(inAngle), fix_cos(inAngle));
}
fix16_t fix_sinh(fix16_t inAngle) {
return fix_mul(fix_exp(inAngle) - fix_exp(-inAngle), 0x8000);
}
fix16_t fix_cosh(fix16_t inAngle) {
return fix_mul(fix_exp(inAngle) + fix_exp(-inAngle), 0x8000);
}
fix16_t fix_tanh(fix16_t inAngle) {
fix16_t e_x = fix_exp(inAngle);
fix16_t m_e_x = fix_exp(-inAngle);
return fix_div(e_x - m_e_x, e_x + m_e_x);
}
fix16_t fix_sqrt(fix16_t inValue) {
uint8_t neg = (inValue < 0);
uint32_t num = (neg ? -inValue : inValue);
uint32_t result = 0;
uint32_t bit;
uint8_t n;
// Many numbers will be less than 15, so
// this gives a good balance between time spent
// in if vs. time spent in the while loop
// when searching for the starting value.
if (num & 0xFFF00000)
bit = (uint32_t)1 << 30;
else
bit = (uint32_t)1 << 18;
while (bit > num)
bit >>= 2;
// The main part is executed twice, in order to avoid
// using 64 bit values in computations.
for (n = 0; n < 2; n++) {
// First we get the top 24 bits of the answer.
while (bit) {
if (num >= result + bit) {
num -= result + bit;
result = (result >> 1) + bit;
} else {
result = (result >> 1);
}
bit >>= 2;
}
if (n == 0) {
// Then process it again to get the lowest 8 bits.
if (num > 65535) {
// The remainder 'num' is too large to be shifted left
// by 16, so we have to add 1 to result manually and
// adjust 'num' accordingly.
// num = a - (result + 0.5)^2
// = num + result^2 - (result + 0.5)^2
// = num - result - 0.5
num -= result;
num = (num << 16) - 0x8000;
result = (result << 16) + 0x8000;
} else {
num <<= 16;
result <<= 16;
}
bit = 1 << 14;
}
}
return (neg ? -(int32_t)result : (int32_t)result);
}
/* Array initialization */
void float_init_array(size_t size, float value, float data[]) {
size_t i;
for (i = 0; i < size; i++)
data[i] = value;
}
void int_init_array(size_t size, int value, int data[]) {
size_t i;
for (i = 0; i < size; i++)
data[i] = value;
}
void bool_init_array(size_t size, uint8_t value, uint8_t data[]) {
size_t i;
for (i = 0; i < size; i++)
data[i] = value;
}
void fix_init_array(size_t size, fix16_t value, fix16_t data[]) {
size_t i;
for (i = 0; i < size; i++)
data[i] = value;
}
void float_copy_array(size_t size, float *dest, float *src) {
size_t i;
for (i = 0; i < size; i++)
dest[i] = src[i];
}
void int_copy_array(size_t size, int *dest, int *src) {
size_t i;
for (i = 0; i < size; i++)
dest[i] = src[i];
}
void bool_copy_array(size_t size, uint8_t *dest, uint8_t *src) {
size_t i;
for (i = 0; i < size; i++)
dest[i] = src[i];
}
void fix_copy_array(size_t size, fix16_t *dest, fix16_t *src) {
size_t i;
for (i = 0; i < size; i++)
dest[i] = src[i];
}
float float_random() { return (float)rand() / RAND_MAX; }
fix16_t fix_random() {
float temp = ((float)rand() / RAND_MAX) * 0x00010000;
return (fix16_t)temp;
}
int irandom() { return (int)rand(); }
void float_print(float value) { printf("%f\n", value); }
void fix_print(fix16_t value) { printf("%f\n", fix_to_float(value)); }
void int_print(int value) { printf("%i\n", value); }
void string_print(char *value) { printf("%s\n", value); }
void bool_print(uint8_t value) { printf("%s\n", value ? "true" : "false"); }