FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libspeex / fixed_debug.h
blobd5c449f4d939c831720af86533530656297174b1
1 /* Copyright (C) 2003 Jean-Marc Valin */
2 /**
3 @file fixed_debug.h
4 @brief Fixed-point operations with debugging
5 */
6 /*
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
11 - Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
18 - Neither the name of the Xiph.org Foundation nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef FIXED_DEBUG_H
36 #define FIXED_DEBUG_H
38 #include <stdio.h>
40 extern long long spx_mips;
41 #define MIPS_INC spx_mips++,
43 #define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))
44 #define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))
47 #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
48 #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
50 static inline short NEG16(int x)
52 int res;
53 if (!VERIFY_SHORT(x))
55 fprintf (stderr, "NEG16: input is not short: %d\n", (int)x);
57 res = -x;
58 if (!VERIFY_SHORT(res))
59 fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
60 spx_mips++;
61 return res;
63 static inline int NEG32(long long x)
65 long long res;
66 if (!VERIFY_INT(x))
68 fprintf (stderr, "NEG16: input is not int: %d\n", (int)x);
70 res = -x;
71 if (!VERIFY_INT(res))
72 fprintf (stderr, "NEG16: output is not int: %d\n", (int)res);
73 spx_mips++;
74 return res;
77 #define EXTRACT16(x) _EXTRACT16(x, __FILE__, __LINE__)
78 static inline short _EXTRACT16(int x, char *file, int line)
80 int res;
81 if (!VERIFY_SHORT(x))
83 fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line);
85 res = x;
86 spx_mips++;
87 return res;
90 #define EXTEND32(x) _EXTEND32(x, __FILE__, __LINE__)
91 static inline int _EXTEND32(int x, char *file, int line)
93 int res;
94 if (!VERIFY_SHORT(x))
96 fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
98 res = x;
99 spx_mips++;
100 return res;
103 #define SHR16(a, shift) _SHR16(a, shift, __FILE__, __LINE__)
104 static inline short _SHR16(int a, int shift, char *file, int line)
106 int res;
107 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
109 fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line);
111 res = a>>shift;
112 if (!VERIFY_SHORT(res))
113 fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
114 spx_mips++;
115 return res;
117 #define SHL16(a, shift) _SHL16(a, shift, __FILE__, __LINE__)
118 static inline short _SHL16(int a, int shift, char *file, int line)
120 int res;
121 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
123 fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line);
125 res = a<<shift;
126 if (!VERIFY_SHORT(res))
127 fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
128 spx_mips++;
129 return res;
132 static inline int SHR32(long long a, int shift)
134 long long res;
135 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
137 fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
139 res = a>>shift;
140 if (!VERIFY_INT(res))
142 fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
144 spx_mips++;
145 return res;
147 static inline int SHL32(long long a, int shift)
149 long long res;
150 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
152 fprintf (stderr, "SHL32: inputs are not int: %d %d\n", (int)a, shift);
154 res = a<<shift;
155 if (!VERIFY_INT(res))
157 fprintf (stderr, "SHL32: output is not int: %d\n", (int)res);
159 spx_mips++;
160 return res;
163 #define PSHR16(a,shift) (SHR16(ADD16((a),((1<<((shift))>>1))),shift))
164 #define PSHR32(a,shift) (SHR32(ADD32((a),((1<<((shift))>>1))),shift))
165 #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
167 #define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
168 #define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
170 //#define SHR(a,shift) ((a) >> (shift))
171 //#define SHL(a,shift) ((a) << (shift))
173 #define ADD16(a, b) _ADD16(a, b, __FILE__, __LINE__)
174 static inline short _ADD16(int a, int b, char *file, int line)
176 int res;
177 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
179 fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
181 res = a+b;
182 if (!VERIFY_SHORT(res))
184 fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
186 spx_mips++;
187 return res;
190 #define SUB16(a, b) _SUB16(a, b, __FILE__, __LINE__)
191 static inline short _SUB16(int a, int b, char *file, int line)
193 int res;
194 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
196 fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
198 res = a-b;
199 if (!VERIFY_SHORT(res))
200 fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
201 spx_mips++;
202 return res;
205 #define ADD32(a, b) _ADD32(a, b, __FILE__, __LINE__)
206 static inline int _ADD32(long long a, long long b, char *file, int line)
208 long long res;
209 if (!VERIFY_INT(a) || !VERIFY_INT(b))
211 fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
213 res = a+b;
214 if (!VERIFY_INT(res))
216 fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line);
218 spx_mips++;
219 return res;
222 static inline int SUB32(long long a, long long b)
224 long long res;
225 if (!VERIFY_INT(a) || !VERIFY_INT(b))
227 fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
229 res = a-b;
230 if (!VERIFY_INT(res))
231 fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
232 spx_mips++;
233 return res;
236 #define ADD64(a,b) (MIPS_INC(a)+(b))
238 /* result fits in 16 bits */
239 static inline short MULT16_16_16(int a, int b)
241 int res;
242 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
244 fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
246 res = a*b;
247 if (!VERIFY_SHORT(res))
248 fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
249 spx_mips++;
250 return res;
253 #define MULT16_16(a, b) _MULT16_16(a, b, __FILE__, __LINE__)
254 static inline int _MULT16_16(int a, int b, char *file, int line)
256 long long res;
257 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
259 fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
261 res = ((long long)a)*b;
262 if (!VERIFY_INT(res))
263 fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line);
264 spx_mips++;
265 return res;
268 #define MAC16_16(c,a,b) (spx_mips--,ADD32((c),MULT16_16((a),(b))))
269 #define MAC16_16_Q11(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11)))))
270 #define MAC16_16_Q13(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13)))))
271 #define MAC16_16_P13(c,a,b) (EXTRACT16(ADD32((c),SHR32(ADD32(4096,MULT16_16((a),(b))),13))))
274 #define MULT16_32_QX(a, b, Q) _MULT16_32_QX(a, b, Q, __FILE__, __LINE__)
275 static inline int _MULT16_32_QX(int a, long long b, int Q, char *file, int line)
277 long long res;
278 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
280 fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
282 if (ABS32(b)>=(1<<(15+Q)))
283 fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
284 res = (((long long)a)*(long long)b) >> Q;
285 if (!VERIFY_INT(res))
286 fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line);
287 spx_mips+=5;
288 return res;
291 static inline int MULT16_32_PX(int a, long long b, int Q)
293 long long res;
294 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
296 fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d\n", Q, (int)a, (int)b);
298 if (ABS32(b)>=(1<<(15+Q)))
299 fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d\n", Q, (int)a, (int)b);
300 res = ((((long long)a)*(long long)b) + ((1<<Q)>>1))>> Q;
301 if (!VERIFY_INT(res))
302 fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d\n", Q, (int)a, (int)b,(int)res);
303 spx_mips+=5;
304 return res;
308 #define MULT16_32_Q11(a,b) MULT16_32_QX(a,b,11)
309 #define MAC16_32_Q11(c,a,b) ADD32((c),MULT16_32_Q11((a),(b)))
310 #define MULT16_32_Q12(a,b) MULT16_32_QX(a,b,12)
311 #define MULT16_32_Q13(a,b) MULT16_32_QX(a,b,13)
312 #define MULT16_32_Q14(a,b) MULT16_32_QX(a,b,14)
313 #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
314 #define MULT16_32_P15(a,b) MULT16_32_PX(a,b,15)
315 #define MAC16_32_Q15(c,a,b) ADD32((c),MULT16_32_Q15((a),(b)))
317 static inline int SATURATE(int a, int b)
319 if (a>b)
320 a=b;
321 if (a<-b)
322 a = -b;
323 return a;
326 static inline int MULT16_16_Q11_32(int a, int b)
328 long long res;
329 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
331 fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
333 res = ((long long)a)*b;
334 res >>= 11;
335 if (!VERIFY_INT(res))
336 fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
337 spx_mips+=3;
338 return res;
340 static inline short MULT16_16_Q13(int a, int b)
342 long long res;
343 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
345 fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
347 res = ((long long)a)*b;
348 res >>= 13;
349 if (!VERIFY_SHORT(res))
350 fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
351 spx_mips+=3;
352 return res;
354 static inline short MULT16_16_Q14(int a, int b)
356 long long res;
357 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
359 fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
361 res = ((long long)a)*b;
362 res >>= 14;
363 if (!VERIFY_SHORT(res))
364 fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
365 spx_mips+=3;
366 return res;
368 static inline short MULT16_16_Q15(int a, int b)
370 long long res;
371 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
373 fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b);
375 res = ((long long)a)*b;
376 res >>= 15;
377 if (!VERIFY_SHORT(res))
379 fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
381 spx_mips+=3;
382 return res;
385 static inline short MULT16_16_P13(int a, int b)
387 long long res;
388 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
390 fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
392 res = ((long long)a)*b;
393 res += 4096;
394 if (!VERIFY_INT(res))
395 fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
396 res >>= 13;
397 if (!VERIFY_SHORT(res))
398 fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
399 spx_mips+=4;
400 return res;
402 static inline short MULT16_16_P14(int a, int b)
404 long long res;
405 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
407 fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
409 res = ((long long)a)*b;
410 res += 8192;
411 if (!VERIFY_INT(res))
412 fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
413 res >>= 14;
414 if (!VERIFY_SHORT(res))
415 fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
416 spx_mips+=4;
417 return res;
419 static inline short MULT16_16_P15(int a, int b)
421 long long res;
422 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
424 fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
426 res = ((long long)a)*b;
427 res += 16384;
428 if (!VERIFY_INT(res))
429 fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
430 res >>= 15;
431 if (!VERIFY_SHORT(res))
432 fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
433 spx_mips+=4;
434 return res;
437 #define DIV32_16(a, b) _DIV32_16(a, b, __FILE__, __LINE__)
439 static inline int _DIV32_16(long long a, long long b, char *file, int line)
441 long long res;
442 if (b==0)
444 fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
445 return 0;
447 if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
449 fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
451 res = a/b;
452 if (!VERIFY_SHORT(res))
454 fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line);
455 if (res>32767)
456 res = 32767;
457 if (res<-32768)
458 res = -32768;
460 spx_mips+=20;
461 return res;
464 #define DIV32(a, b) _DIV32(a, b, __FILE__, __LINE__)
465 static inline int _DIV32(long long a, long long b, char *file, int line)
467 long long res;
468 if (b==0)
470 fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
471 return 0;
474 if (!VERIFY_INT(a) || !VERIFY_INT(b))
476 fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
478 res = a/b;
479 if (!VERIFY_INT(res))
480 fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line);
481 spx_mips+=36;
482 return res;
484 #define PDIV32(a,b) DIV32(ADD32((a),(b)>>1),b)
485 #define PDIV32_16(a,b) DIV32_16(ADD32((a),(b)>>1),b)
487 #endif