DEADSOFTWARE

Update readme
[mp3cc.git] / mps / src / F.java
1 /*
2 * Fixed-point aritmetika za MIDletPascal
3 * */
5 public class F
6 {
8 private F()
9 {
10 }
12 // fromInt
13 public static int fI(int i)
14 {
15 return i << 12;
16 }
18 // fromint
19 public static int fI(int a, int b)
20 {
21 StringBuffer s = new StringBuffer();
22 s.append(a);
23 s.append('.');
24 s.append(b);
25 return fI(s.toString(),10);
26 }
28 // fromInt
29 public static int fI(String s, int radix)
30 {
31 int i = 0;
32 if(s.charAt(0) == '-')
33 i = 1;
34 String s1 = "-1";
35 int j = s.indexOf('.');
36 if(j >= 0)
37 {
38 for(s1 = s.substring(j + 1, s.length()); s1.length() < 4; s1 = s1 + "0");
39 if(s1.length() > 4)
40 s1 = s1.substring(0, 4);
41 } else
42 {
43 j = s.length();
44 }
45 int k = Integer.parseInt(s.substring(i, j), radix);
46 int l = Integer.parseInt(s1, radix) + 1;
47 int i1 = (k << 12) + (l << 12) / 10000;
48 if(i == 1)
49 i1 = -i1;
50 return i1;
51 }
53 // toString
54 public static String tS(int i)
55 {
56 boolean flag = false;
57 if(i < 0)
58 {
59 flag = true;
60 i = -i;
61 }
62 int j = i >> 12;
63 int k = 10000 * (i & 0xfff) >> 12;
64 String s;
65 for(s = Integer.toString(k); s.length() < 4; s = "0" + s);
66 return (flag ? "-" : "") + Integer.toString(j) + "." + s;
67 }
69 // toInt
70 public static int tI(int i)
71 {
72 if(i >= 0)
73 i += 2048;
74 else
75 i -= 2048;
76 return i >> 12;
77 }
79 // div
80 public static int D(int i, int j)
81 {
82 boolean flag = false;
83 if(j == 4096)
84 return i;
85 if((j & 0xfff) == 0)
86 return i / (j >> 12);
87 if(i < 0)
88 {
89 i = -i;
90 flag = true;
91 }
92 if(j < 0)
93 {
94 j = -j;
95 if(flag)
96 flag = false;
97 else
98 flag = true;
99 }
100 byte byte0 = 0;
101 if(i > 0x64fff)
102 byte0 = 3;
103 if(i > 0x3e8fff)
104 byte0 = 4;
105 if(i > 0x7dffff)
106 byte0 = 6;
107 if(i > 0x1f4ffff)
108 byte0 = 8;
109 if(i > 0x7dfffff)
110 byte0 = 10;
111 if(byte0 > 0)
113 int k = 2 << byte0 - 1;
114 i += k;
115 j += k;
117 int l = (i << 12 - byte0) / (j >> byte0);
118 return flag ? -l : l;
121 // mult
122 public static int M(int i, int j)
124 boolean flag = false;
125 if((i & 0xfff) == 0)
126 return (i >> 12) * j;
127 if((j & 0xfff) == 0)
128 return i * (j >> 12);
129 if(i < 0 && j > 0 || i > 0 && j < 0)
130 flag = true;
131 if(i < 0)
132 i = -i;
133 if(j < 0)
134 j = -j;
135 byte byte0 = 0;
136 if(i > 0x64fff || j > 0x64fff)
137 byte0 = 2;
138 if(i > 0x3e8fff || j > 0x3e8fff)
139 byte0 = 4;
140 if(i > 0x271ffff || j > 0x271ffff)
141 byte0 = 6;
142 if(byte0 > 0)
144 int k = 2 << byte0 - 1;
145 i += k;
146 j += k;
148 int l = (i >> 12) * (j >> 12) << 12;
149 int i1 = (i & 0xfff) * (j & 0xfff) >> 12;
150 i1 += ((i & 0xfffff000) >> byte0) * ((j & 0xfff) >> byte0) >> 12 - (byte0 << 1);
151 l = l + i1 + (((i & 0xfff) >> byte0) * ((j & 0xfffff000) >> byte0) >> 12 - (byte0 << 1));
152 if(l < 0)
153 return 0;
154 else
155 return flag ? -l : l;
158 // abs
159 public static int A(int i)
161 if(i < 0)
162 return -i;
163 else
164 return i;
167 // sqrt
168 public static int S(int i, int j)
170 if(i < 0)
171 return 0;
172 if(i == 0)
173 return 0;
174 int k = i + 4096 >> 1;
175 for(int l = 0; l < j; l++)
176 k = k + D(i, k) >> 1;
178 if(k < 0)
179 return 0;
180 else
181 return k;
184 // sqrt
185 public static int S(int i)
187 byte byte0 = 8;
188 if(i > 0x64000)
189 byte0 = 12;
190 if(i > 0x3e8000)
191 byte0 = 16;
192 return S(i, byte0);
195 // sin
196 public static int s(int i)
198 int j = 0;
199 for(; i < 0; i += 25736);
200 if(i > 25736)
201 i %= 25736;
202 int k = (i * 10) / 714;
203 if(i != 0 && i != 6434 && i != 12868 && i != 19302 && i != 25736)
204 j = (i * 100) / 714 - k * 10;
205 if(k <= 90)
206 return sin_lookup(k, j);
207 if(k <= 180)
208 return sin_lookup(180 - k, j);
209 if(k <= 270)
210 return -sin_lookup(k - 180, j);
211 else
212 return -sin_lookup(360 - k, j);
215 private static int sin_lookup(int i, int j)
217 if(j > 0 && j < 10 && i < 90)
218 return SIN_TABLE[i] + ((SIN_TABLE[i + 1] - SIN_TABLE[i]) / 10) * j;
219 else
220 return SIN_TABLE[i];
223 // cos
224 public static int C(int i)
226 return s(i + 6435);
229 public static int tan(int i)
231 int j = D(s(i), C(i));
232 return j;
235 public static int cot(int i)
237 int j = D(fI(1), tan(i));
238 return j;
241 // asin
242 public static int AS(int i)
244 boolean flag = false;
245 if(i < 0)
246 flag = true;
247 if(A(i) > 4096)
248 return 0;
249 i = A(i);
250 int j = 0;
251 int k = SIN_TABLE.length;
252 for(int l = 0; l < SIN_TABLE.length; l++)
254 int i1 = A(i - SIN_TABLE[l]);
255 if(i1 < k)
257 k = i1;
258 j = l;
262 if(flag)
263 return -(j * 72);
264 else
265 return j * 72;
268 // acos
269 public static int AC(int i)
271 return 6434 - AS(i);
274 // exp
275 public static int e(int i)
277 int j = A(i) >> 12;
278 if(j > 13)
279 return 0;
280 int ai[] = {
281 4096, 11134, 30266, 0x1415e, 0x36992, 0x9469c, 0x1936dc, 0x448a21, 0xba4f54, 0x1fa7158,
282 0x560a774, 0xe9e2244, 0x27bc2ca9, 0x6c02d646
283 };
284 int k = ai[j];
285 int l = A(i) & 0xfff;
286 if(l > 0)
288 int i1 = 0;
289 int j1 = 4096;
290 int k1 = 1;
291 for(int l1 = 0; l1 < 6; l1++)
293 i1 += j1 / k1;
294 j1 = M(j1, l);
295 k1 *= l1 + 1;
298 k = M(k, i1);
300 if(i < 0)
301 return D(4096, k);
302 else
303 return k;
306 public static int log(int i)
308 if(i <= 0)
309 return 0;
310 int j = 0;
311 boolean flag = false;
312 int l;
313 for(l = 0; i > 8192; l++)
314 i >>= 1;
316 int i1 = l * 2839;
317 int j1 = 0;
318 i -= 4096;
319 for(int k1 = 1; k1 < 11; k1++)
321 int k;
322 if(j == 0)
323 k = i;
324 else
325 k = M(j, i);
326 if(k == 0)
327 break;
328 j1 += ((k1 % 2 != 0 ? 1 : -1) * k) / k1;
329 j = k;
332 return i1 + j1;
335 public static int log10(int i)
337 return D(log(i), log(2));
340 // pow
341 public static int p(int i, int j)
343 boolean flag = false;
344 int k = 1;
345 if(j < 0)
347 flag = true;
348 j = -j;
350 if(A(i) < 4096 && j > 12288)
352 k = e(M(log(i), j));
353 } else
355 k = pi(i, j >> 12);
356 if((j & 0xfff) != 0)
357 k = M(k, e(M(log(i), j & 0xfff)));
359 if(flag)
360 return D(4096, k);
361 else
362 return k;
365 private static int pi(int i, int j)
367 int k = 4096;
368 if(i == 0)
369 return 0;
370 for(int l = 0; l < j; l++)
371 k = M(k, i);
373 if(k < 0)
374 return 0;
375 else
376 return k;
379 // to degrees
380 public static int tD(int i)
382 return 180*D(i, PI);
385 // to radians
386 public static int tR(int i)
388 return M(i/180, PI);
391 public static int atan(int i)
393 return AC(D(fI(1), S(1+M(i, i))));
396 public static int atan2(int y, int x)
398 int res;
399 if (x == 0)
401 if (y == 0)
402 return 0;
403 if (y > 0)
404 return PI/2;
405 return -PI/2;
408 if (x>0)
409 return atan(D(y,x));
411 res = PI - atan(A(D(y,x)));
413 if (y>=0)
414 return res;
415 return -res;
418 public static final int MAX_VALUE = 0x7fffffff;
419 public static final int MIN_VALUE = 0x80000001;
420 public static final int E = 11134;
421 public static final int PI = 12868;
422 private static final int SIN_TABLE[] = {
423 0, 71, 142, 214, 285, 357, 428, 499, 570, 641,
424 711, 781, 851, 921, 990, 1060, 1128, 1197, 1265, 1333,
425 1400, 1468, 1534, 1600, 1665, 1730, 1795, 1859, 1922, 1985,
426 2048, 2109, 2170, 2230, 2290, 2349, 2407, 2464, 2521, 2577,
427 2632, 2686, 2740, 2793, 2845, 2896, 2946, 2995, 3043, 3091,
428 3137, 3183, 3227, 3271, 3313, 3355, 3395, 3434, 3473, 3510,
429 3547, 3582, 3616, 3649, 3681, 3712, 3741, 3770, 3797, 3823,
430 3849, 3872, 3895, 3917, 3937, 3956, 3974, 3991, 4006, 4020,
431 4033, 4045, 4056, 4065, 4073, 4080, 4086, 4090, 4093, 4095,
432 4096
433 };