-
Notifications
You must be signed in to change notification settings - Fork 11
/
math.h
373 lines (322 loc) · 8.89 KB
/
math.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// SPDX-License-Identifier: Zlib
// SPDX-FileNotice: Modified from the original version by the BlocksDS project.
//
// Copyright (C) 2005 Michael Noland (joat)
// Copyright (C) 2005 Jason Rogers (dovoto)
// Copyright (C) 2005 Dave Murphy (WinterMute)
/// @file nds/arm9/math.h
///
/// @brief hardware coprocessor math instructions.
#ifndef LIBNDS_NDS_ARM9_MATH_H__
#define LIBNDS_NDS_ARM9_MATH_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <nds/ndstypes.h>
#define REG_DIVCNT (*(vu16 *)(0x04000280))
#define REG_DIV_NUMER (*(vs64 *)(0x04000290))
#define REG_DIV_NUMER_L (*(vs32 *)(0x04000290))
#define REG_DIV_NUMER_H (*(vs32 *)(0x04000294))
#define REG_DIV_DENOM (*(vs64 *)(0x04000298))
#define REG_DIV_DENOM_L (*(vs32 *)(0x04000298))
#define REG_DIV_DENOM_H (*(vs32 *)(0x0400029C))
#define REG_DIV_RESULT (*(vs64 *)(0x040002A0))
#define REG_DIV_RESULT_L (*(vs32 *)(0x040002A0))
#define REG_DIV_RESULT_H (*(vs32 *)(0x040002A4))
#define REG_DIVREM_RESULT (*(vs64 *)(0x040002A8))
#define REG_DIVREM_RESULT_L (*(vs32 *)(0x040002A8))
#define REG_DIVREM_RESULT_H (*(vs32 *)(0x040002AC))
#define REG_SQRTCNT (*(vu16 *)(0x040002B0))
#define REG_SQRT_PARAM (*(vs64 *)(0x040002B8))
#define REG_SQRT_PARAM_L (*(vs32 *)(0x040002B8))
#define REG_SQRT_PARAM_H (*(vs32 *)(0x040002BC))
#define REG_SQRT_RESULT (*(vu32 *)(0x040002B4))
// Math coprocessor modes
#define DIV_64_64 2
#define DIV_64_32 1
#define DIV_32_32 0
#define DIV_BUSY (1 << 15)
#define SQRT_64 1
#define SQRT_32 0
#define SQRT_BUSY (1 << 15)
// Fixed point conversion macros
#define inttof32(n) ((n) * (1 << 12)) ///< Convert int to f32
#define f32toint(n) ((n) / (1 << 12)) ///< Convert f32 to int
#define floattof32(n) ((int)((n) * (1 << 12))) ///< Convert float to f32
#define f32tofloat(n) (((float)(n)) / (float)(1 << 12)) ///< Convert f32 to float
// Fixed Point versions
/// Asynchronous fixed point divide start
///
/// @param num 20.12 numerator.
/// @param den 20.12 denominator.
static inline void divf32_asynch(int32_t num, int32_t den)
{
REG_DIVCNT = DIV_64_32;
REG_DIV_NUMER = ((int64_t)num) << 12;
REG_DIV_DENOM_L = den;
}
/// Asynchronous fixed point divide result
///
/// @return returns 20.12 result.
static inline int32_t divf32_result(void)
{
while (REG_DIVCNT & DIV_BUSY);
return REG_DIV_RESULT_L;
}
/// Fixed point divide
///
/// @param num 20.12 numerator.
/// @param den 20.12 denominator.
/// @return returns 20.12 result.
static inline int32_t divf32(int32_t num, int32_t den)
{
divf32_asynch(num, den);
return divf32_result();
}
/// Fixed point multiply.
///
/// @param a 20.12 value.
/// @param b 20.12 value.
/// @return returns 20.12 result.
static inline int32_t mulf32(int32_t a, int32_t b)
{
int64_t result = (int64_t)a * (int64_t)b;
return (int32_t)(result >> 12);
}
/// Asynchronous fixed point sqrt start.
///
/// @param a 20.12 positive value.
static inline void sqrtf32_asynch(int32_t a)
{
REG_SQRTCNT = SQRT_64;
REG_SQRT_PARAM = ((uint64_t)(uint32_t)a) << 12;
}
/// Asynchronous fixed point sqrt result.
///
/// @return 20.12 result.
static inline int32_t sqrtf32_result(void)
{
while (REG_SQRTCNT & SQRT_BUSY);
return REG_SQRT_RESULT;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
/// Fixed point sqrt.
///
/// @param a 20.12 positive value.
/// @return 20.12 result.
static inline int32_t sqrtf32(int32_t a)
{
sqrtf32_asynch(a);
return sqrtf32_result();
}
#pragma GCC diagnostic pop
/// Asynchronous integer divide start.
///
/// @param num Numerator.
/// @param den Denominator.
static inline void div32_asynch(int32_t num, int32_t den)
{
REG_DIVCNT = DIV_32_32;
REG_DIV_NUMER_L = num;
REG_DIV_DENOM_L = den;
}
/// Asynchronous integer divide result.
///
/// @return 32 bit integer result.
static inline int32_t div32_result(void)
{
while (REG_DIVCNT & DIV_BUSY);
return REG_DIV_RESULT_L;
}
/// Integer divide.
///
/// @param num Numerator.
/// @param den Denominator.
/// @return 32 bit integer result.
static inline int32_t div32(int32_t num, int32_t den)
{
div32_asynch(num, den);
return div32_result();
}
/// Asynchronous integer modulo start.
///
/// @param num Numerator.
/// @param den Denominator.
static inline void mod32_asynch(int32_t num, int32_t den)
{
REG_DIVCNT = DIV_32_32;
REG_DIV_NUMER_L = num;
REG_DIV_DENOM_L = den;
}
/// Asynchronous integer modulo result.
///
/// @return 32 bit integer remainder.
static inline int32_t mod32_result(void)
{
while (REG_DIVCNT & DIV_BUSY);
return REG_DIVREM_RESULT_L;
}
/// Integer modulo.
///
/// @param num Numerator.
/// @param den Denominator.
/// @return 32 bit integer remainder.
static inline int32_t mod32(int32_t num, int32_t den)
{
mod32_asynch(num, den);
return mod32_result();
}
/// Asynchronous integer 64 bit divide start.
///
/// @param num 64 bit numerator.
/// @param den 32 bit denominator.
static inline void div64_asynch(int64_t num, int32_t den)
{
REG_DIVCNT = DIV_64_32;
REG_DIV_NUMER = num;
REG_DIV_DENOM_L = den;
}
/// Asynchronous integer 64 bit divide result.
///
/// @return 32 bit integer result.
static inline int32_t div64_result(void)
{
while (REG_DIVCNT & DIV_BUSY);
return REG_DIV_RESULT_L;
}
/// Integer 64 bit divide.
///
/// @param num 64 bit numerator.
/// @param den 32 bit denominator.
/// @return 32 bit integer result.
static inline int32_t div64(int64_t num, int32_t den)
{
div64_asynch(num, den);
return div64_result();
}
/// Asynchronous integer 64 bit modulo start.
///
/// @param num 64 bit numerator.
/// @param den 32 bit denominator.
static inline void mod64_asynch(int64_t num, int32_t den)
{
REG_DIVCNT = DIV_64_32;
REG_DIV_NUMER = num;
REG_DIV_DENOM_L = den;
}
/// Asynchronous integer 64 bit modulo result.
///
/// @return returns 32 bit integer remainder.
static inline int32_t mod64_result(void)
{
while (REG_DIVCNT & DIV_BUSY);
return REG_DIVREM_RESULT_L;
}
/// Integer 64 bit modulo.
///
/// @param num 64 bit numerator.
/// @param den 32 bit denominator.
/// @return returns 32 bit integer remainder.
static inline int32_t mod64(int64_t num, int32_t den)
{
mod64_asynch(num, den);
return mod64_result();
}
/// Asynchronous 32-bit integer sqrt start.
///
/// @param a 32 bit positive integer value.
static inline void sqrt32_asynch(uint32_t a)
{
REG_SQRTCNT = SQRT_32;
REG_SQRT_PARAM_L = a;
}
/// Asynchronous 32-bit integer sqrt result.
///
/// @return 32 bit integer result.
static inline uint32_t sqrt32_result(void)
{
while (REG_SQRTCNT & SQRT_BUSY);
return REG_SQRT_RESULT;
}
/// 32-bit integer sqrt.
///
/// @param a 32 bit positive integer value.
/// @return 32 bit integer result.
static inline uint32_t sqrt32(uint32_t a)
{
sqrt32_asynch(a);
return sqrt32_result();
}
/// Asynchronous 64-bit integer sqrt start.
///
/// @param a 64 bit positive integer value.
static inline void sqrt64_asynch(uint64_t a)
{
REG_SQRTCNT = SQRT_64;
REG_SQRT_PARAM = a;
}
/// Asynchronous 64-bit integer sqrt result.
///
/// @return 32 bit integer result.
static inline uint32_t sqrt64_result(void)
{
while (REG_SQRTCNT & SQRT_BUSY);
return REG_SQRT_RESULT;
}
/// 64-bit integer sqrt.
///
/// @param a 64 bit positive integer value.
/// @return 32 bit integer result.
static inline uint32_t sqrt64(uint64_t a)
{
sqrt64_asynch(a);
return sqrt64_result();
}
/// 20.12 fixed point cross product.
///
/// Cross product:
///
/// result = A x B
///
/// x = Ay * Bz - By * Az
/// y = Az * Bx - Bz * Ax
/// z = Ax * By - Bx * Ay
///
/// @param a Pointer to fixed 3 dimensions vector.
/// @param b Pointer to fixed 3 dimensions vector.
/// @param result Result pointer to fixed 3x3 matrix
static inline void crossf32(int32_t *a, int32_t *b, int32_t *result)
{
result[0] = mulf32(a[1], b[2]) - mulf32(b[1], a[2]);
result[1] = mulf32(a[2], b[0]) - mulf32(b[2], a[0]);
result[2] = mulf32(a[0], b[1]) - mulf32(b[0], a[1]);
}
/// 20.12 fixed point dot product.
///
/// Dot Product:
///
/// Result = Ax * Bx + Ay * By + Az * Bz
///
/// @param a Pointer to fixed 3 dimensions vector.
/// @param b Pointer to fixed 3 dimensions vector.
/// @return 32 bit integer result
static inline int32_t dotf32(int32_t *a, int32_t *b)
{
return mulf32(a[0], b[0]) + mulf32(a[1], b[1]) + mulf32(a[2], b[2]);
}
/// 20.12 fixed point normalize (set magnitude to 1.0 and keep the direction).
///
/// @param a Pointer to the vector to normalize.
static inline void normalizef32(int32_t *a)
{
// magnitude = sqrt(Ax^2 + Ay^2 + Az^2)
int32_t magnitude = sqrtf32(mulf32(a[0], a[0]) + mulf32(a[1], a[1]) + mulf32(a[2], a[2]));
a[0] = divf32(a[0], magnitude);
a[1] = divf32(a[1], magnitude);
a[2] = divf32(a[2], magnitude);
}
#ifdef __cplusplus
}
#endif
#endif // LIBNDS_NDS_ARM9_MATH_H__