1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - FX -
3   File:     fx_atanidx.c
4 
5   Copyright 2003-2008 Nintendo.  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   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16 
17  *---------------------------------------------------------------------------*/
18 
19 #include <nitro/fx/fx_trig.h>
20 #include <nitro/fx/fx_cp.h>
21 #include <nitro/fx/fx_const.h>
22 
23 const fx16 FX_AtanIdxTable_[128 + 1] = {
24     (u16)0,
25     (u16)81,
26     (u16)163,
27     (u16)244,
28     (u16)326,
29     (u16)407,
30     (u16)489,
31     (u16)570,
32     (u16)651,
33     (u16)732,
34     (u16)813,
35     (u16)894,
36     (u16)975,
37     (u16)1056,
38     (u16)1136,
39     (u16)1217,
40     (u16)1297,
41     (u16)1377,
42     (u16)1457,
43     (u16)1537,
44     (u16)1617,
45     (u16)1696,
46     (u16)1775,
47     (u16)1854,
48     (u16)1933,
49     (u16)2012,
50     (u16)2090,
51     (u16)2168,
52     (u16)2246,
53     (u16)2324,
54     (u16)2401,
55     (u16)2478,
56     (u16)2555,
57     (u16)2632,
58     (u16)2708,
59     (u16)2784,
60     (u16)2860,
61     (u16)2935,
62     (u16)3010,
63     (u16)3085,
64     (u16)3159,
65     (u16)3233,
66     (u16)3307,
67     (u16)3380,
68     (u16)3453,
69     (u16)3526,
70     (u16)3599,
71     (u16)3670,
72     (u16)3742,
73     (u16)3813,
74     (u16)3884,
75     (u16)3955,
76     (u16)4025,
77     (u16)4095,
78     (u16)4164,
79     (u16)4233,
80     (u16)4302,
81     (u16)4370,
82     (u16)4438,
83     (u16)4505,
84     (u16)4572,
85     (u16)4639,
86     (u16)4705,
87     (u16)4771,
88     (u16)4836,
89     (u16)4901,
90     (u16)4966,
91     (u16)5030,
92     (u16)5094,
93     (u16)5157,
94     (u16)5220,
95     (u16)5282,
96     (u16)5344,
97     (u16)5406,
98     (u16)5467,
99     (u16)5528,
100     (u16)5589,
101     (u16)5649,
102     (u16)5708,
103     (u16)5768,
104     (u16)5826,
105     (u16)5885,
106     (u16)5943,
107     (u16)6000,
108     (u16)6058,
109     (u16)6114,
110     (u16)6171,
111     (u16)6227,
112     (u16)6282,
113     (u16)6337,
114     (u16)6392,
115     (u16)6446,
116     (u16)6500,
117     (u16)6554,
118     (u16)6607,
119     (u16)6660,
120     (u16)6712,
121     (u16)6764,
122     (u16)6815,
123     (u16)6867,
124     (u16)6917,
125     (u16)6968,
126     (u16)7018,
127     (u16)7068,
128     (u16)7117,
129     (u16)7166,
130     (u16)7214,
131     (u16)7262,
132     (u16)7310,
133     (u16)7358,
134     (u16)7405,
135     (u16)7451,
136     (u16)7498,
137     (u16)7544,
138     (u16)7589,
139     (u16)7635,
140     (u16)7679,
141     (u16)7724,
142     (u16)7768,
143     (u16)7812,
144     (u16)7856,
145     (u16)7899,
146     (u16)7942,
147     (u16)7984,
148     (u16)8026,
149     (u16)8068,
150     (u16)8110,
151     (u16)8151,
152     (u16)8192
153 };
154 
155 
156 /*---------------------------------------------------------------------------*
157   Name:         FX_AtanIdx
158 
159   Description:  Computes an arc tangent of the input value, making use of
160                 a table.
161 
162   Arguments:    x            Y/X in fx32 format
163 
164   Returns:      result in u16 format (0 - 65535)
165  *---------------------------------------------------------------------------*/
FX_AtanIdx(fx32 x)166 u16 FX_AtanIdx(fx32 x)
167 {
168     if (x >= 0)
169     {
170         if (x > FX32_ONE)
171         {
172             return (u16)(16384 - FX_AtanIdxTable_[FX_Inv(x) >> 5]);
173         }
174         else if (x < FX32_ONE)
175         {
176             return (u16)FX_AtanIdxTable_[x >> 5];
177         }
178         else
179         {
180             return (u16)8192;
181         }
182     }
183     else
184     {
185         if (x < -FX32_ONE)
186         {
187             return (u16)(-16384 + FX_AtanIdxTable_[FX_Inv(-x) >> 5]);
188         }
189         else if (x > -FX32_ONE)
190         {
191             return (u16)-FX_AtanIdxTable_[-x >> 5];
192         }
193         else
194         {
195             return (u16)-8192;
196         }
197     }
198 }
199 
200 
201 /*---------------------------------------------------------------------------*
202   Name:         FX_Atan2Idx
203 
204   Description:  Computes an arc tangent of the input value(y/x), making use of
205                 a table. Note that 'y' is the first argument.
206 
207   Arguments:    y            a value in fx32 format
208                 x            a value in fx32 format
209 
210   Returns:      result in u16 format (0 - 65535)
211  *---------------------------------------------------------------------------*/
FX_Atan2Idx(fx32 y,fx32 x)212 u16 FX_Atan2Idx(fx32 y, fx32 x)
213 {
214     fx32    a, b;
215     int     c;
216     int     sgn;
217 
218     if (y > 0)
219     {
220         if (x > 0)
221         {
222             if (x > y)
223             {
224                 a = y;
225                 b = x;
226                 c = 0;
227                 sgn = 1;
228             }
229             else if (x < y)
230             {
231                 a = x;
232                 b = y;
233                 c = 16384;
234                 sgn = 0;
235             }
236             else
237             {
238                 return (u16)8192;
239             }
240         }
241         else if (x < 0)
242         {
243             x = -x;
244             if (x < y)
245             {
246                 a = x;
247                 b = y;
248                 c = 16384;
249                 sgn = 1;
250             }
251             else if (x > y)
252             {
253                 a = y;
254                 b = x;
255                 c = 32768;
256                 sgn = 0;
257             }
258             else
259             {
260                 return (u16)24576;
261             }
262         }
263         else
264         {
265             return (u16)16384;
266         }
267     }
268     else if (y < 0)
269     {
270         y = -y;
271         if (x < 0)
272         {
273             x = -x;
274             if (x > y)
275             {
276                 a = y;
277                 b = x;
278                 c = -32768;
279                 sgn = 1;
280             }
281             else if (x < y)
282             {
283                 a = x;
284                 b = y;
285                 c = -16384;
286                 sgn = 0;
287             }
288             else
289             {
290                 return (u16)-24576;
291             }
292         }
293         else if (x > 0)
294         {
295             if (x < y)
296             {
297                 a = x;
298                 b = y;
299                 c = -16384;
300                 sgn = 1;
301             }
302             else if (x > y)
303             {
304                 a = y;
305                 b = x;
306                 c = 0;
307                 sgn = 0;
308             }
309             else
310             {
311                 return (u16)-8192;
312             }
313         }
314         else
315         {
316             return (u16)-16384;
317         }
318     }
319     else                               // y = 0
320     {
321         if (x >= 0)
322         {
323             return 0;
324         }
325         else
326         {
327             return (u16)32768;
328         }
329     }
330 
331     if (b == 0)
332         return 0;
333     if (sgn)
334         return (u16)(c + FX_AtanIdxTable_[FX_Div(a, b) >> 5]);
335     else
336         return (u16)(c - FX_AtanIdxTable_[FX_Div(a, b) >> 5]);
337 }
338