1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: math_Triangular.h
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 48334 $
14 *---------------------------------------------------------------------------*/
15
16 #ifndef NN_MATH_MATH_TRIANGULAR_H_
17 #define NN_MATH_MATH_TRIANGULAR_H_
18
19 #include <nn/math/math_Config.h>
20 #include <nn/math/math_Constant.h>
21 #include <nn/math/math_Arithmetic.h>
22 #include <nn/math/ARMv6/math_Triangular.h>
23
24 #include <cmath>
25
26
27 namespace nn {
28 namespace math {
29 namespace internal {
30
31 struct SinCosSample
32 {
33 f32 sin_val;
34 f32 cos_val;
35 f32 sin_delta; // Difference from next sin_val
36 f32 cos_delta; // Difference from next cos_val
37 };
38
39 extern const SinCosSample gSinCosTbl[256 + 1];
40
41 } // namespace internal
42 } // namespace math
43 } // namespace nn
44
45
46 namespace nn {
47 namespace math {
48
49
50 /* =======================================================================
51 sin/cos/tan
52 ======================================================================== */
53 /* Please see man pages for details
54
55
56 */
57
58 #define NN_MATH_RAD_TO_FIDX(rad) ((rad) * (256.f / (2.0f * ::nn::math::F_PI)))
59 #define NN_MATH_DEG_TO_FIDX(deg) ((deg) * (256.f / 360.f) )
60 #define NN_MATH_DEG_TO_RAD(deg) ((deg) * (::nn::math::F_PI / 180.0f) )
61 #define NN_MATH_RAD_TO_DEG(rad) ((rad) * (180.0f / ::nn::math::F_PI) )
62 #define NN_MATH_FIDX_TO_RAD(fidx) ((fidx) * ((2.0f * ::nn::math::F_PI) / 256.f))
63 #define NN_MATH_FIDX_TO_DEG(fidx) ((fidx) * (360.f / 256.f) )
64
65
66 /*
67
68
69
70
71
72 */
73 f32 SinFIdx(f32 fidx);
74
75 /*
76
77
78
79
80
81 */
82 f32 CosFIdx(f32 fidx);
83
84 /*
85
86
87
88
89
90
91
92 */
93 void SinCosFIdx(f32* pSin, f32* pCos, f32 fidx);
94
95 /*
96
97
98
99
100
101 */
102 NN_MATH_INLINE f32
TanFIdx(f32 fidx)103 TanFIdx(f32 fidx)
104 {
105 f32 sin, cos;
106 SinCosFIdx( &sin, &cos, fidx );
107 return sin / cos;
108 }
109
110
111 /*
112
113
114
115
116
117 */
SinRad(f32 rad)118 NN_MATH_INLINE f32 SinRad(f32 rad) { return SinFIdx(NN_MATH_RAD_TO_FIDX(rad)); }
119
120 /*
121
122
123
124
125
126 */
CosRad(f32 rad)127 NN_MATH_INLINE f32 CosRad(f32 rad) { return CosFIdx(NN_MATH_RAD_TO_FIDX(rad)); }
128
129 /*
130
131
132
133
134
135
136
137 */
SinCosRad(f32 * s,f32 * c,f32 rad)138 NN_MATH_INLINE void SinCosRad(f32* s, f32* c, f32 rad) { SinCosFIdx(s, c, NN_MATH_RAD_TO_FIDX(rad)); }
139
140 /*
141
142
143
144
145
146 */
TanRad(f32 rad)147 NN_MATH_INLINE f32 TanRad(f32 rad) { return TanFIdx(NN_MATH_RAD_TO_FIDX(rad)); }
148
149
150
151
152 /*
153
154
155
156
157
158 */
SinDeg(f32 deg)159 NN_MATH_INLINE f32 SinDeg(f32 deg) { return SinFIdx(NN_MATH_DEG_TO_FIDX(deg)); }
160
161 /*
162
163
164
165
166
167 */
CosDeg(f32 deg)168 NN_MATH_INLINE f32 CosDeg(f32 deg) { return CosFIdx(NN_MATH_DEG_TO_FIDX(deg)); }
169
170 /*
171
172
173
174
175
176
177
178 */
SinCosDeg(f32 * s,f32 * c,f32 deg)179 NN_MATH_INLINE void SinCosDeg(f32* s, f32* c, f32 deg) { SinCosFIdx(s, c, NN_MATH_DEG_TO_FIDX(deg)); }
180
181 /*
182
183
184
185
186
187 */
TanDeg(f32 deg)188 NN_MATH_INLINE f32 TanDeg(f32 deg) { return TanFIdx(NN_MATH_DEG_TO_FIDX(deg)); }
189
190 #if 1
191 /* Once we start using one with a 16-bit index, the implementation of this function will be replaced. */
192
193 /*
194
195
196
197
198
199 */
200 f32 SinIdx(u16 idx);
201
202 /*
203
204
205
206
207
208 */
209 f32 CosIdx(u16 idx);
210
211 /*
212
213
214
215
216
217
218
219 */
220 void SinCosIdx(f32* s, f32* c, u16 idx);
221
222 /*
223
224
225
226
227
228 */
TanIdx(u16 idx)229 NN_MATH_INLINE f32 TanIdx(u16 idx)
230 {
231 f32 sin, cos;
232 SinCosIdx( &sin, &cos, idx );
233 return sin / cos;
234 }
235 #endif
236
237 /*
238
239
240
241
242
243 */
NN_fAcos(f32 x)244 NN_MATH_INLINE f32 NN_fAcos(f32 x) { return ::std::acosf(x); }
245 /*
246
247
248
249
250
251 */
NN_fAsin(f32 x)252 NN_MATH_INLINE f32 NN_fAsin(f32 x) { return ::std::asinf(x); }
253 /*
254
255
256
257
258
259 */
NN_fAtan(f32 x)260 NN_MATH_INLINE f32 NN_fAtan(f32 x) { return ::std::atanf(x); }
261 /*
262
263
264
265
266
267
268 */
NN_fAtan2(f32 y,f32 x)269 NN_MATH_INLINE f32 NN_fAtan2(f32 y, f32 x) { return ::std::atan2f(y, x); }
270
271
272 /*
273
274
275
276
277
278 */
279 NN_MATH_INLINE f32
AsinFIdx(f32 x)280 AsinFIdx(f32 x)
281 {
282 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AsinFIdx: Input is out of the domain.");
283 return NN_MATH_RAD_TO_FIDX(::std::asin(x));
284 }
285
286 /*
287
288
289
290
291
292 */
293 NN_MATH_INLINE f32
AcosFIdx(f32 x)294 AcosFIdx(f32 x)
295 {
296 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AcosFIdx: Input is out of the domain.");
297 return NN_MATH_RAD_TO_FIDX(::std::acosf(x));
298 }
299
300 /*
301
302
303
304
305
306
307
308 */
309 f32 AtanFIdx(f32 x);
310 /*
311
312
313
314
315
316
317
318
319 */
320 f32 Atan2FIdx(f32 y, f32 x);
321
322
323 /*
324
325
326
327
328
329 */
AsinRad(f32 x)330 NN_MATH_INLINE f32 AsinRad(f32 x)
331 {
332 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AsinRad: Input is out of the domain.");
333 return ::std::asin(x);
334 }
335
336 /*
337
338
339
340
341
342 */
AcosRad(f32 x)343 NN_MATH_INLINE f32 AcosRad(f32 x)
344 {
345 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AcosRad: Input is out of the domain.");
346 return ::std::acos(x);
347 }
348
349 /*
350
351
352
353
354
355 */
AtanRad(f32 x)356 NN_MATH_INLINE f32 AtanRad(f32 x) { return NN_MATH_FIDX_TO_RAD(AtanFIdx(x)); }
357
358 /*
359
360
361
362
363
364
365 */
Atan2Rad(f32 y,f32 x)366 NN_MATH_INLINE f32 Atan2Rad(f32 y, f32 x) { return NN_MATH_FIDX_TO_RAD(Atan2FIdx(y, x)); }
367
368
369
370
371
372
373 /*
374
375
376
377
378
379 */
AsinDeg(f32 x)380 NN_MATH_INLINE f32 AsinDeg(f32 x)
381 {
382 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AsinDeg: Input is out of the domain.");
383 return NN_MATH_RAD_TO_DEG(::std::asin(x));
384 }
385
386 /*
387
388
389
390
391
392 */
AcosDeg(f32 x)393 NN_MATH_INLINE f32 AcosDeg(f32 x)
394 {
395 NN_MATH_WARNING(x <= 1.f && x >= -1.f, "AcosDeg: Input is out of the domain.");
396 return NN_MATH_RAD_TO_DEG(::std::acos(x));
397 }
398
399 /*
400
401
402
403
404
405 */
AtanDeg(f32 x)406 NN_MATH_INLINE f32 AtanDeg(f32 x) { return NN_MATH_FIDX_TO_DEG(AtanFIdx(x)); }
407
408 /*
409
410
411
412
413
414
415 */
Atan2Deg(f32 y,f32 x)416 NN_MATH_INLINE f32 Atan2Deg(f32 y, f32 x) { return NN_MATH_FIDX_TO_DEG(Atan2FIdx(y, x)); }
417
418 #if 1
419 /* Once we start using one with a 16-bit index, the implementation of this function will be replaced. */
420
421
422 /*
423
424
425
426
427
428 */
429 // Returns an index in the range from 0 to 90 degrees and 270 to 360 degrees.
AsinIdx(f32 x)430 NN_MATH_INLINE u16 AsinIdx(f32 x) { f32 fidx = AsinFIdx(x); return F32ToU16((fidx < 0 ? fidx + 256.f : fidx) * 256.f); }
431
432 /*
433
434
435
436
437
438 */
439 // Returns an index in the range from 0 to pi.
AcosIdx(f32 x)440 NN_MATH_INLINE u16 AcosIdx(f32 x) { return F32ToU16(AcosFIdx(x) * 256.f); }
441
442 /*
443
444
445
446
447
448 */
449 // Returns an index in the range from 0 to 90 degrees and 270 to 360 degrees.
450 u16 AtanIdx(f32 x);
451
452 /*
453
454
455
456
457
458
459 */
460 // Returns an index in the range from 0 to 360 degrees.
461 u16 Atan2Idx(f32 y, f32 x);
462 #endif
463
464 /*
465
466 */
467
468 } // namespace math
469 } // namespace nn
470
471
472 /* NN_MATH_MATH_TRIANGULAR_H_ */
473 #endif
474