grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / classes / gadgets / colorwheel / fixmathtrans.c
blobd0383c05c75a63ae8f338dd0ddc5c36e31ba3dca
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "colorwheel_intern.h"
7 #include "fixmath.h"
9 #define MAX_TRIG 1024
11 #if FIXED_MATH
13 /****************************************************************************/
15 Fixed32 SinCosTab[MAX_TRIG] = {
16 0,402,804,1206,1608,2010,2412,2814,3216,3617,4019,4420,
17 4821,5222,5623,6023,6424,6824,7224,7623,8022,8421,8820,9218,
18 9616,10014,10411,10808,11204,11600,11996,12391,12785,13180,13573,13966,
19 14359,14751,15143,15534,15924,16314,16703,17091,17479,17867,18253,18639,
20 19024,19409,19792,20175,20557,20939,21320,21699,22078,22457,22834,23210,
21 23586,23961,24335,24708,25080,25451,25821,26190,26558,26925,27291,27656,
22 28020,28383,28745,29106,29466,29824,30182,30538,30893,31247,31600,31952,
23 32303,32652,33000,33347,33692,34036,34379,34721,35062,35401,35738,36075,
24 36410,36744,37076,37407,37736,38064,38391,38716,39040,39362,39683,40002,
25 40320,40636,40951,41264,41576,41886,42194,42501,42806,43110,43412,43713,
26 44011,44308,44604,44898,45190,45480,45769,46056,46341,46624,46906,47186,
27 47464,47741,48015,48288,48559,48828,49095,49361,49624,49886,50146,50404,
28 50660,50914,51166,51417,51665,51912,52156,52399,52639,52878,53114,53349,
29 53581,53812,54040,54267,54491,54714,54934,55152,55368,55582,55794,56004,
30 56212,56418,56621,56823,57022,57219,57414,57607,57798,57986,58172,58356,
31 58538,58718,58896,59071,59244,59415,59583,59750,59914,60076,60235,60392,
32 60547,60700,60851,60999,61145,61288,61429,61568,61705,61839,61971,62101,
33 62228,62353,62476,62596,62714,62830,62943,63054,63162,63268,63372,63473,
34 63572,63669,63763,63854,63944,64031,64115,64197,64277,64354,64429,64501,
35 64571,64639,64704,64766,64827,64884,64940,64993,65043,65091,65137,65180,
36 65220,65259,65294,65328,65358,65387,65413,65436,65457,65476,65492,65505,
37 65516,65525,65531,65535,
38 // Cosine starts here and ends above :)
39 65536,65535,65531,65525,65516,65505,65492,65476,65457,65436,65413,65387,
40 65358,65328,65294,65259,65220,65180,65137,65091,65043,64993,64940,64884,
41 64827,64766,64704,64639,64571,64501,64429,64354,64277,64197,64115,64031,
42 63944,63854,63763,63669,63572,63473,63372,63268,63162,63054,62943,62830,
43 62714,62596,62476,62353,62228,62101,61971,61839,61705,61568,61429,61288,
44 61145,60999,60851,60700,60547,60392,60235,60076,59914,59750,59583,59415,
45 59244,59071,58896,58718,58538,58356,58172,57986,57798,57607,57414,57219,
46 57022,56823,56621,56418,56212,56004,55794,55582,55368,55152,54934,54714,
47 54491,54267,54040,53812,53581,53349,53114,52878,52639,52399,52156,51912,
48 51665,51417,51166,50914,50660,50404,50146,49886,49624,49361,49095,48828,
49 48559,48288,48015,47741,47464,47186,46906,46624,46341,46056,45769,45480,
50 45190,44898,44604,44308,44011,43713,43412,43110,42806,42501,42194,41886,
51 41576,41264,40951,40636,40320,40002,39683,39362,39040,38716,38391,38064,
52 37736,37407,37076,36744,36410,36075,35738,35401,35062,34721,34379,34036,
53 33692,33347,33000,32652,32303,31952,31600,31247,30893,30538,30182,29824,
54 29466,29106,28745,28383,28020,27656,27291,26925,26558,26190,25821,25451,
55 25080,24708,24335,23961,23586,23210,22834,22457,22078,21699,21320,20939,
56 20557,20175,19792,19409,19024,18639,18253,17867,17479,17091,16703,16314,
57 15924,15534,15143,14751,14359,13966,13573,13180,12785,12391,11996,11600,
58 11204,10808,10411,10014,9616,9218,8820,8421,8022,7623,7224,6824,
59 6424,6023,5623,5222,4821,4420,4019,3617,3216,2814,2412,2010,
60 1608,1206,804,402,
61 0,-402,-804,-1206,-1608,-2010,-2412,-2814,-3216,-3617,-4019,-4420,
62 -4821,-5222,-5623,-6023,-6424,-6824,-7224,-7623,-8022,-8421,-8820,-9218,
63 -9616,-10014,-10411,-10808,-11204,-11600,-11996,-12391,-12785,-13180,-13573,-13966,
64 -14359,-14751,-15143,-15534,-15924,-16314,-16703,-17091,-17479,-17867,-18253,-18639,
65 -19024,-19409,-19792,-20175,-20557,-20939,-21320,-21699,-22078,-22457,-22834,-23210,
66 -23586,-23961,-24335,-24708,-25080,-25451,-25821,-26190,-26558,-26925,-27291,-27656,
67 -28020,-28383,-28745,-29106,-29466,-29824,-30182,-30538,-30893,-31247,-31600,-31952,
68 -32303,-32652,-33000,-33347,-33692,-34036,-34379,-34721,-35062,-35401,-35738,-36075,
69 -36410,-36744,-37076,-37407,-37736,-38064,-38391,-38716,-39040,-39362,-39683,-40002,
70 -40320,-40636,-40951,-41264,-41576,-41886,-42194,-42501,-42806,-43110,-43412,-43713,
71 -44011,-44308,-44604,-44898,-45190,-45480,-45769,-46056,-46341,-46624,-46906,-47186,
72 -47464,-47741,-48015,-48288,-48559,-48828,-49095,-49361,-49624,-49886,-50146,-50404,
73 -50660,-50914,-51166,-51417,-51665,-51912,-52156,-52399,-52639,-52878,-53114,-53349,
74 -53581,-53812,-54040,-54267,-54491,-54714,-54934,-55152,-55368,-55582,-55794,-56004,
75 -56212,-56418,-56621,-56823,-57022,-57219,-57414,-57607,-57798,-57986,-58172,-58356,
76 -58538,-58718,-58896,-59071,-59244,-59415,-59583,-59750,-59914,-60076,-60235,-60392,
77 -60547,-60700,-60851,-60999,-61145,-61288,-61429,-61568,-61705,-61839,-61971,-62101,
78 -62228,-62353,-62476,-62596,-62714,-62830,-62943,-63054,-63162,-63268,-63372,-63473,
79 -63572,-63669,-63763,-63854,-63944,-64031,-64115,-64197,-64277,-64354,-64429,-64501,
80 -64571,-64639,-64704,-64766,-64827,-64884,-64940,-64993,-65043,-65091,-65137,-65180,
81 -65220,-65259,-65294,-65328,-65358,-65387,-65413,-65436,-65457,-65476,-65492,-65505,
82 -65516,-65525,-65531,-65535,
83 -65536,-65535,-65531,-65525,-65516,-65505,-65492,-65476,-65457,-65436,-65413,-65387,
84 -65358,-65328,-65294,-65259,-65220,-65180,-65137,-65091,-65043,-64993,-64940,-64884,
85 -64827,-64766,-64704,-64639,-64571,-64501,-64429,-64354,-64277,-64197,-64115,-64031,
86 -63944,-63854,-63763,-63669,-63572,-63473,-63372,-63268,-63162,-63054,-62943,-62830,
87 -62714,-62596,-62476,-62353,-62228,-62101,-61971,-61839,-61705,-61568,-61429,-61288,
88 -61145,-60999,-60851,-60700,-60547,-60392,-60235,-60076,-59914,-59750,-59583,-59415,
89 -59244,-59071,-58896,-58718,-58538,-58356,-58172,-57986,-57798,-57607,-57414,-57219,
90 -57022,-56823,-56621,-56418,-56212,-56004,-55794,-55582,-55368,-55152,-54934,-54714,
91 -54491,-54267,-54040,-53812,-53581,-53349,-53114,-52878,-52639,-52399,-52156,-51912,
92 -51665,-51417,-51166,-50914,-50660,-50404,-50146,-49886,-49624,-49361,-49095,-48828,
93 -48559,-48288,-48015,-47741,-47464,-47186,-46906,-46624,-46341,-46056,-45769,-45480,
94 -45190,-44898,-44604,-44308,-44011,-43713,-43412,-43110,-42806,-42501,-42194,-41886,
95 -41576,-41264,-40951,-40636,-40320,-40002,-39683,-39362,-39040,-38716,-38391,-38064,
96 -37736,-37407,-37076,-36744,-36410,-36075,-35738,-35401,-35062,-34721,-34379,-34036,
97 -33692,-33347,-33000,-32652,-32303,-31952,-31600,-31247,-30893,-30538,-30182,-29824,
98 -29466,-29106,-28745,-28383,-28020,-27656,-27291,-26925,-26558,-26190,-25821,-25451,
99 -25080,-24708,-24335,-23961,-23586,-23210,-22834,-22457,-22078,-21699,-21320,-20939,
100 -20557,-20175,-19792,-19409,-19024,-18639,-18253,-17867,-17479,-17091,-16703,-16314,
101 -15924,-15534,-15143,-14751,-14359,-13966,-13573,-13180,-12785,-12391,-11996,-11600,
102 -11204,-10808,-10411,-10014,-9616,-9218,-8820,-8421,-8022,-7623,-7224,-6824,
103 -6424,-6023,-5623,-5222,-4821,-4420,-4019,-3617,-3216,-2814,-2412,-2010,
104 -1608,-1206,-804,-402
107 /****************************************************************************/
109 /* Fixed32 */
110 unsigned short TanTab[MAX_TRIG+1] = {
111 0,63,127,191,255,319,383,447,511,575,639,
112 703,767,831,895,959,1023,1087,1151,1215,1279,
113 1343,1407,1471,1535,1599,1663,1727,1791,1855,1919,
114 1983,2047,2111,2175,2239,2303,2366,2430,2494,2558,
115 2622,2686,2750,2814,2878,2942,3005,3069,3133,3197,
116 3261,3325,3388,3452,3516,3580,3644,3708,3771,3835,
117 3899,3963,4026,4090,4154,4218,4281,4345,4409,4473,
118 4536,4600,4664,4727,4791,4855,4918,4982,5046,5109,
119 5173,5236,5300,5363,5427,5491,5554,5618,5681,5745,
120 5808,5872,5935,5999,6062,6126,6189,6252,6316,6379,
121 6443,6506,6569,6633,6696,6759,6823,6886,6949,7013,
122 7076,7139,7202,7266,7329,7392,7455,7518,7581,7645,
123 7708,7771,7834,7897,7960,8023,8086,8149,8212,8275,
124 8338,8401,8464,8527,8590,8653,8716,8779,8841,8904,
125 8967,9030,9093,9155,9218,9281,9344,9406,9469,9532,
126 9594,9657,9720,9782,9845,9907,9970,10032,10095,10157,
127 10220,10282,10345,10407,10470,10532,10594,10657,10719,10781,
128 10843,10906,10968,11030,11092,11155,11217,11279,11341,11403,
129 11465,11527,11589,11651,11713,11775,11837,11899,11961,12023,
130 12085,12146,12208,12270,12332,12394,12455,12517,12579,12640,
131 12702,12764,12825,12887,12948,13010,13071,13133,13194,13256,
132 13317,13378,13440,13501,13562,13624,13685,13746,13807,13869,
133 13930,13991,14052,14113,14174,14235,14296,14357,14418,14479,
134 14540,14601,14662,14723,14784,14844,14905,14966,15026,15087,
135 15148,15208,15269,15330,15390,15451,15511,15572,15632,15693,
136 15753,15813,15874,15934,15994,16054,16115,16175,16235,16295,
137 16355,16415,16475,16535,16595,16655,16715,16775,16835,16895,
138 16955,17015,17074,17134,17194,17253,17313,17373,17432,17492,
139 17551,17611,17670,17730,17789,17849,17908,17967,18027,18086,
140 18145,18204,18264,18323,18382,18441,18500,18559,18618,18677,
141 18736,18795,18854,18912,18971,19030,19089,19147,19206,19265,
142 19323,19382,19440,19499,19557,19616,19674,19733,19791,19849,
143 19908,19966,20024,20082,20140,20199,20257,20315,20373,20431,
144 20489,20547,20605,20662,20720,20778,20836,20894,20951,21009,
145 21067,21124,21182,21239,21297,21354,21412,21469,21526,21584,
146 21641,21698,21755,21813,21870,21927,21984,22041,22098,22155,
147 22212,22269,22326,22383,22439,22496,22553,22610,22666,22723,
148 22779,22836,22893,22949,23005,23062,23118,23175,23231,23287,
149 23343,23400,23456,23512,23568,23624,23680,23736,23792,23848,
150 23904,23960,24015,24071,24127,24183,24238,24294,24349,24405,
151 24461,24516,24571,24627,24682,24738,24793,24848,24903,24958,
152 25014,25069,25124,25179,25234,25289,25344,25398,25453,25508,
153 25563,25618,25672,25727,25782,25836,25891,25945,26000,26054,
154 26108,26163,26217,26271,26326,26380,26434,26488,26542,26596,
155 26650,26704,26758,26812,26866,26920,26973,27027,27081,27135,
156 27188,27242,27295,27349,27402,27456,27509,27562,27616,27669,
157 27722,27775,27829,27882,27935,27988,28041,28094,28147,28200,
158 28252,28305,28358,28411,28463,28516,28569,28621,28674,28726,
159 28779,28831,28884,28936,28988,29040,29093,29145,29197,29249,
160 29301,29353,29405,29457,29509,29561,29613,29664,29716,29768,
161 29819,29871,29923,29974,30026,30077,30129,30180,30231,30283,
162 30334,30385,30436,30487,30539,30590,30641,30692,30743,30793,
163 30844,30895,30946,30997,31047,31098,31149,31199,31250,31300,
164 31351,31401,31451,31502,31552,31602,31653,31703,31753,31803,
165 31853,31903,31953,32003,32053,32103,32153,32202,32252,32302,
166 32351,32401,32451,32500,32550,32599,32648,32698,32747,32796,
167 32846,32895,32944,32993,33042,33091,33140,33189,33238,33287,
168 33336,33385,33433,33482,33531,33579,33628,33677,33725,33774,
169 33822,33870,33919,33967,34015,34064,34112,34160,34208,34256,
170 34304,34352,34400,34448,34496,34544,34591,34639,34687,34735,
171 34782,34830,34877,34925,34972,35020,35067,35114,35162,35209,
172 35256,35303,35351,35398,35445,35492,35539,35586,35632,35679,
173 35726,35773,35820,35866,35913,35960,36006,36053,36099,36146,
174 36192,36238,36285,36331,36377,36423,36470,36516,36562,36608,
175 36654,36700,36746,36792,36837,36883,36929,36975,37020,37066,
176 37112,37157,37203,37248,37294,37339,37384,37430,37475,37520,
177 37565,37611,37656,37701,37746,37791,37836,37881,37926,37970,
178 38015,38060,38105,38149,38194,38239,38283,38328,38372,38417,
179 38461,38505,38550,38594,38638,38682,38726,38771,38815,38859,
180 38903,38947,38991,39034,39078,39122,39166,39210,39253,39297,
181 39340,39384,39428,39471,39514,39558,39601,39645,39688,39731,
182 39774,39817,39861,39904,39947,39990,40033,40076,40118,40161,
183 40204,40247,40290,40332,40375,40418,40460,40503,40545,40588,
184 40630,40672,40715,40757,40799,40841,40884,40926,40968,41010,
185 41052,41094,41136,41178,41220,41262,41303,41345,41387,41428,
186 41470,41512,41553,41595,41636,41678,41719,41760,41802,41843,
187 41884,41926,41967,42008,42049,42090,42131,42172,42213,42254,
188 42295,42336,42376,42417,42458,42498,42539,42580,42620,42661,
189 42701,42742,42782,42822,42863,42903,42943,42984,43024,43064,
190 43104,43144,43184,43224,43264,43304,43344,43384,43423,43463,
191 43503,43543,43582,43622,43661,43701,43740,43780,43819,43859,
192 43898,43937,43977,44016,44055,44094,44133,44172,44212,44251,
193 44290,44328,44367,44406,44445,44484,44523,44561,44600,44639,
194 44677,44716,44754,44793,44831,44870,44908,44946,44985,45023,
195 45061,45099,45138,45176,45214,45252,45290,45328,45366,45404,
196 45442,45479,45517,45555,45593,45630,45668,45706,45743,45781,
197 45818,45856,45893,45931,45968,46005,46043,46080,46117,46154,
198 46192,46229,46266,46303,46340,46377,46414,46451,46487,46524,
199 46561,46598,46635,46671,46708,46745,46781,46818,46854,46891,
200 46927,46963,47000,47036,47072,47109,47145,47181,47217,47253,
201 47290,47326,47362,47398,47434,47469,47505,47541,47577,47613,
202 47649,47684,47720,47756,47791,47827,47862,47898,47933,47969,
203 48004,48039,48075,48110,48145,48181,48216,48251,48286,48321,
204 48356,48391,48426,48461,48496,48531,48566,48601,48635,48670,
205 48705,48739,48774,48809,48843,48878,48912,48947,48981,49016,
206 49050,49084,49119,49153,49187,49222,49256,49290,49324,49358,
207 49392,49426,49460,49494,49528,49562,49596,49629,49663,49697,
208 49731,49764,49798,49832,49865,49899,49932,49966,49999,50033,
209 50066,50099,50133,50166,50199,50233,50266,50299,50332,50365,
210 50398,50431,50464,50497,50530,50563,50596,50629,50662,50694,
211 50727,50760,50792,50825,50858,50890,50923,50955,50988,51020,
212 51053,51085,51117,51150,51182,51214,51247,51279,51311,51343,
213 51375,51407,51439,51471
216 /****************************************************************************/
218 Fixed32 FixSqrti(ULONG x) // 16 -> 16.16
220 if( x )
222 register unsigned long int xroot, m2, x2, dx = x;
224 xroot = 0;
225 m2 = 1 << 15 * 2;
229 x2 = xroot + m2;
230 xroot >>= 1;
232 if (x2 <= x)
234 x -= x2; xroot += m2;
237 while (m2 >>= 2);
239 dx = ( (dx) - (xroot*xroot) ) << 16;
240 dx /= ((long)xroot)<<1;
242 return (xroot<<16) + dx;
245 return 0;
248 /****************************************************************************/
249 #if 0
250 Fixed32 FixSqrt( Fixed32 ecx )
252 LONG eax, ebx, edx;
254 eax = 0;
255 ebx = 0x40000000;
256 #if 1 // low precision 8.8
257 lp1:
258 edx = ecx;
259 edx -= ebx;
260 if( edx < 0 ) goto lp2;
261 edx -= eax;
262 if( edx < 0 ) goto lp2;
263 ecx = edx;
264 eax >>= 1;
265 eax |= ebx;
266 ebx >>= 2;
267 if( ebx != 0 ) goto lp1;
268 eax <<= 8;
269 return eax;
270 lp2:
271 eax >>= 1;
272 ebx >>= 2;
273 if( ebx != 0 ) goto lp1;
274 eax <<= 8;
275 #else // high precision 8.16
276 lp1:
277 edx = ecx;
278 edx -= ebx;
279 if( edx < 0 ) goto lp2;
280 edx -= eax;
281 if( edx < 0 ) goto lp2;
282 ecx = edx;
283 eax >>= 1;
284 eax |= ebx;
285 ebx >>= 2;
286 if( ebx != 0 ) goto lp1;
287 else goto lp5;
288 lp2:
289 eax >>= 1;
290 ebx >>= 2;
291 if( ebx != 0L ) goto lp1;
292 lp5:
293 ebx = 0x4000;
294 eax <<= 16;
295 ecx <<= 16;
296 lp3:
297 edx = ecx;
298 edx -= ebx;
299 if( edx < 0 ) goto lp4;
300 edx -= eax;
301 if( edx < 0 ) goto lp4;
302 ecx = edx;
303 eax >>= 1;
304 eax |= ebx;
305 ebx >>= 2;
306 if( ebx != 0 ) goto lp3;
307 goto lp6;
308 lp4:
309 eax >>= 1;
310 ebx >>= 2;
311 if( ebx != 0L ) goto lp3;
312 lp6:
313 #endif
314 return eax;
317 /****************************************************************************/
319 Fixed32 FixSinCos( Fixed32 theta, Fixed32 *sinus )
321 theta = FIXED_TO_INT( FixMul( theta << 9, 20861 ) );
322 theta &= (MAX_TRIG - 1);
324 *sinus = SinCosTab[theta];
326 if( ( theta += 256 ) >= MAX_TRIG )
327 theta = theta - MAX_TRIG;
329 return SinCosTab[theta];
331 #endif
332 /****************************************************************************/
334 __inline static Fixed32 FixAtan( Fixed32 x )
336 return TanTab[ x >> 6 ]; /*TanTab[ FixDiv( FixMul( x, MAX_TRIG ), 65536 ) ];*/
339 /****************************************************************************/
341 #undef FixDiv
342 #define FixDiv( x, y ) (((ULONG)x<<16)/(ULONG)y)
344 Fixed32 FixAtan2( Fixed32 y, Fixed32 x )
346 if( !x && !y )
347 return 0;
349 if (x>= 0)
351 // x >=0
352 if (y>= 0)
354 // y>= 0
355 if (x>y)
357 // octant 0
358 return FixAtan( FixDiv(y,x) );
360 else
362 // octant 1
363 return ANG90-1-FixAtan( FixDiv(x,y) );
366 else
368 // y<0
369 y = -y;
371 if (x>y)
373 // octant 8
374 return -FixAtan(FixDiv(y,x) );
376 else
378 // octant 7
379 return ANG270+FixAtan( FixDiv(x,y) );
383 else
385 // x<0
386 x = -x;
388 if (y>= 0)
390 // y>= 0
391 if (x>y)
393 // octant 3
394 return ANG180-1-FixAtan( FixDiv(y,x) );
396 else
398 // octant 2
399 return ANG90+ FixAtan( FixDiv(x,y) );
402 else
404 // y<0
405 y = -y;
407 if (x>y)
409 // octant 4
410 return ANG180+FixAtan( FixDiv(y,x) );
412 else
414 // octant 5
415 return ANG270-1-FixAtan( FixDiv(x,y) );
420 return 0;
423 /****************************************************************************/
425 #endif /* FIXED_MATH */