summaryrefslogtreecommitdiff
path: root/uisimulator/common/libmad/fixed.h
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator/common/libmad/fixed.h')
-rw-r--r--uisimulator/common/libmad/fixed.h476
1 files changed, 0 insertions, 476 deletions
diff --git a/uisimulator/common/libmad/fixed.h b/uisimulator/common/libmad/fixed.h
deleted file mode 100644
index 17db8789ee..0000000000
--- a/uisimulator/common/libmad/fixed.h
+++ /dev/null
@@ -1,476 +0,0 @@
1/*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2001 Robert Leslie
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * $Id$
20 */
21
22# ifndef LIBMAD_FIXED_H
23# define LIBMAD_FIXED_H
24
25# if SIZEOF_INT >= 4
26typedef signed int mad_fixed_t;
27
28typedef signed int mad_fixed64hi_t;
29typedef unsigned int mad_fixed64lo_t;
30# else
31typedef signed long mad_fixed_t;
32
33typedef signed long mad_fixed64hi_t;
34typedef unsigned long mad_fixed64lo_t;
35# endif
36
37# if defined(_MSC_VER)
38# define mad_fixed64_t signed __int64
39# elif 1 || defined(__GNUC__)
40# define mad_fixed64_t signed long long
41# endif
42
43# if defined(FPM_FLOAT)
44typedef double mad_sample_t;
45# else
46typedef mad_fixed_t mad_sample_t;
47# endif
48
49/*
50 * Fixed-point format: 0xABBBBBBB
51 * A == whole part (sign + 3 bits)
52 * B == fractional part (28 bits)
53 *
54 * Values are signed two's complement, so the effective range is:
55 * 0x80000000 to 0x7fffffff
56 * -8.0 to +7.9999999962747097015380859375
57 *
58 * The smallest representable value is:
59 * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
60 *
61 * 28 bits of fractional accuracy represent about
62 * 8.6 digits of decimal accuracy.
63 *
64 * Fixed-point numbers can be added or subtracted as normal
65 * integers, but multiplication requires shifting the 64-bit result
66 * from 56 fractional bits back to 28 (and rounding.)
67 *
68 * Changing the definition of MAD_F_FRACBITS is only partially
69 * supported, and must be done with care.
70 */
71
72# define MAD_F_FRACBITS 28
73
74# if MAD_F_FRACBITS == 28
75# define MAD_F(x) ((mad_fixed_t) (x##L))
76# else
77# if MAD_F_FRACBITS < 28
78# warning "MAD_F_FRACBITS < 28"
79# define MAD_F(x) ((mad_fixed_t) \
80 (((x##L) + \
81 (1L << (28 - MAD_F_FRACBITS - 1))) >> \
82 (28 - MAD_F_FRACBITS)))
83# elif MAD_F_FRACBITS > 28
84# error "MAD_F_FRACBITS > 28 not currently supported"
85# define MAD_F(x) ((mad_fixed_t) \
86 ((x##L) << (MAD_F_FRACBITS - 28)))
87# endif
88# endif
89
90# define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
91# define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
92
93# define MAD_F_ONE MAD_F(0x10000000)
94
95# define mad_f_tofixed(x) ((mad_fixed_t) \
96 ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
97# define mad_f_todouble(x) ((double) \
98 ((x) / (double) (1L << MAD_F_FRACBITS)))
99
100# define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
101# define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
102 /* (x should be positive) */
103
104# define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
105
106# define mad_f_add(x, y) ((x) + (y))
107# define mad_f_sub(x, y) ((x) - (y))
108
109# if defined(FPM_FLOAT)
110# error "FPM_FLOAT not yet supported"
111
112# undef MAD_F
113# define MAD_F(x) mad_f_todouble(x)
114
115# define mad_f_mul(x, y) ((x) * (y))
116# define mad_f_scale64
117
118# undef ASO_ZEROCHECK
119
120# elif defined(FPM_64BIT)
121
122/*
123 * This version should be the most accurate if 64-bit types are supported by
124 * the compiler, although it may not be the most efficient.
125 */
126# if defined(OPT_ACCURACY)
127# define mad_f_mul(x, y) \
128 ((mad_fixed_t) \
129 ((((mad_fixed64_t) (x) * (y)) + \
130 (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
131# else
132# define mad_f_mul(x, y) \
133 ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
134# endif
135
136# define MAD_F_SCALEBITS MAD_F_FRACBITS
137
138/* --- Intel --------------------------------------------------------------- */
139
140# elif defined(FPM_INTEL)
141
142# if defined(_MSC_VER)
143# pragma warning(push)
144# pragma warning(disable: 4035) /* no return value */
145static __forceinline
146mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
147{
148 enum {
149 fracbits = MAD_F_FRACBITS
150 };
151
152 __asm {
153 mov eax, x
154 imul y
155 shrd eax, edx, fracbits
156 }
157
158 /* implicit return of eax */
159}
160# pragma warning(pop)
161
162# define mad_f_mul mad_f_mul_inline
163# define mad_f_scale64
164# else
165/*
166 * This Intel version is fast and accurate; the disposition of the least
167 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
168 */
169# define MAD_F_MLX(hi, lo, x, y) \
170 asm ("imull %3" \
171 : "=a" (lo), "=d" (hi) \
172 : "%a" (x), "rm" (y) \
173 : "cc")
174
175# if defined(OPT_ACCURACY)
176/*
177 * This gives best accuracy but is not very fast.
178 */
179# define MAD_F_MLA(hi, lo, x, y) \
180 ({ mad_fixed64hi_t __hi; \
181 mad_fixed64lo_t __lo; \
182 MAD_F_MLX(__hi, __lo, (x), (y)); \
183 asm ("addl %2,%0\n\t" \
184 "adcl %3,%1" \
185 : "=rm" (lo), "=rm" (hi) \
186 : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
187 : "cc"); \
188 })
189# endif /* OPT_ACCURACY */
190
191# if defined(OPT_ACCURACY)
192/*
193 * Surprisingly, this is faster than SHRD followed by ADC.
194 */
195# define mad_f_scale64(hi, lo) \
196 ({ mad_fixed64hi_t __hi_; \
197 mad_fixed64lo_t __lo_; \
198 mad_fixed_t __result; \
199 asm ("addl %4,%2\n\t" \
200 "adcl %5,%3" \
201 : "=rm" (__lo_), "=rm" (__hi_) \
202 : "0" (lo), "1" (hi), \
203 "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
204 : "cc"); \
205 asm ("shrdl %3,%2,%1" \
206 : "=rm" (__result) \
207 : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
208 : "cc"); \
209 __result; \
210 })
211# else
212# define mad_f_scale64(hi, lo) \
213 ({ mad_fixed_t __result; \
214 asm ("shrdl %3,%2,%1" \
215 : "=rm" (__result) \
216 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
217 : "cc"); \
218 __result; \
219 })
220# endif /* OPT_ACCURACY */
221
222# define MAD_F_SCALEBITS MAD_F_FRACBITS
223# endif
224
225/* --- ARM ----------------------------------------------------------------- */
226
227# elif defined(FPM_ARM)
228
229/*
230 * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
231 * least significant bit is properly rounded at no CPU cycle cost!
232 */
233# if 1
234/*
235 * There's a bug somewhere, possibly in the compiler, that sometimes makes
236 * this necessary instead of the default implementation via MAD_F_MLX and
237 * mad_f_scale64. It may be related to the use (or lack) of
238 * -finline-functions and/or -fstrength-reduce.
239 *
240 * This is also apparently faster than MAD_F_MLX/mad_f_scale64.
241 */
242# define mad_f_mul(x, y) \
243 ({ mad_fixed64hi_t __hi; \
244 mad_fixed64lo_t __lo; \
245 mad_fixed_t __result; \
246 asm ("smull %0, %1, %3, %4\n\t" \
247 "movs %0, %0, lsr %5\n\t" \
248 "adc %2, %0, %1, lsl %6" \
249 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
250 : "%r" (x), "r" (y), \
251 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
252 : "cc"); \
253 __result; \
254 })
255# endif
256
257# define MAD_F_MLX(hi, lo, x, y) \
258 asm ("smull %0, %1, %2, %3" \
259 : "=&r" (lo), "=&r" (hi) \
260 : "%r" (x), "r" (y))
261
262# define MAD_F_MLA(hi, lo, x, y) \
263 asm ("smlal %0, %1, %2, %3" \
264 : "+r" (lo), "+r" (hi) \
265 : "%r" (x), "r" (y))
266
267# define MAD_F_MLN(hi, lo) \
268 asm ("rsbs %0, %2, #0\n\t" \
269 "rsc %1, %3, #0" \
270 : "=r" (lo), "=r" (hi) \
271 : "0" (lo), "1" (hi) \
272 : "cc")
273
274# define mad_f_scale64(hi, lo) \
275 ({ mad_fixed_t __result; \
276 asm ("movs %0, %1, lsr %3\n\t" \
277 "adc %0, %0, %2, lsl %4" \
278 : "=r" (__result) \
279 : "r" (lo), "r" (hi), \
280 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
281 : "cc"); \
282 __result; \
283 })
284
285# define MAD_F_SCALEBITS MAD_F_FRACBITS
286
287/* --- MIPS ---------------------------------------------------------------- */
288
289# elif defined(FPM_MIPS)
290
291/*
292 * This MIPS version is fast and accurate; the disposition of the least
293 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
294 */
295# define MAD_F_MLX(hi, lo, x, y) \
296 asm ("mult %2,%3" \
297 : "=l" (lo), "=h" (hi) \
298 : "%r" (x), "r" (y))
299
300# if defined(HAVE_MADD_ASM)
301# define MAD_F_MLA(hi, lo, x, y) \
302 asm ("madd %2,%3" \
303 : "+l" (lo), "+h" (hi) \
304 : "%r" (x), "r" (y))
305# elif defined(HAVE_MADD16_ASM)
306/*
307 * This loses significant accuracy due to the 16-bit integer limit in the
308 * multiply/accumulate instruction.
309 */
310# define MAD_F_ML0(hi, lo, x, y) \
311 asm ("mult %2,%3" \
312 : "=l" (lo), "=h" (hi) \
313 : "%r" ((x) >> 12), "r" ((y) >> 16))
314# define MAD_F_MLA(hi, lo, x, y) \
315 asm ("madd16 %2,%3" \
316 : "+l" (lo), "+h" (hi) \
317 : "%r" ((x) >> 12), "r" ((y) >> 16))
318# define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
319# endif
320
321# if defined(OPT_SPEED)
322# define mad_f_scale64(hi, lo) \
323 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
324# define MAD_F_SCALEBITS MAD_F_FRACBITS
325# endif
326
327/* --- SPARC --------------------------------------------------------------- */
328
329# elif defined(FPM_SPARC)
330
331/*
332 * This SPARC V8 version is fast and accurate; the disposition of the least
333 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
334 */
335# define MAD_F_MLX(hi, lo, x, y) \
336 asm ("smul %2, %3, %0\n\t" \
337 "rd %%y, %1" \
338 : "=r" (lo), "=r" (hi) \
339 : "%r" (x), "rI" (y))
340
341/* --- PowerPC ------------------------------------------------------------- */
342
343# elif defined(FPM_PPC)
344
345/*
346 * This PowerPC version is tuned for the 4xx embedded processors. It is
347 * effectively a tuned version of FPM_64BIT. It is a little faster and just
348 * as accurate. The disposition of the least significant bit depends on
349 * OPT_ACCURACY via mad_f_scale64().
350 */
351# define MAD_F_MLX(hi, lo, x, y) \
352 asm ("mulhw %1, %2, %3\n\t" \
353 "mullw %0, %2, %3" \
354 : "=&r" (lo), "=&r" (hi) \
355 : "%r" (x), "r" (y))
356
357# define MAD_F_MLA(hi, lo, x, y) \
358 ({ mad_fixed64hi_t __hi; \
359 mad_fixed64lo_t __lo; \
360 MAD_F_MLX(__hi, __lo, (x), (y)); \
361 asm ("addc %0, %2, %3\n\t" \
362 "adde %1, %4, %5" \
363 : "=r" (lo), "=r" (hi) \
364 : "%r" (__lo), "0" (lo), "%r" (__hi), "1" (hi)); \
365 })
366
367# if defined(OPT_ACCURACY)
368/*
369 * This is accurate and ~2 - 2.5 times slower than the unrounded version.
370 *
371 * The __volatile__ improves the generated code by another 5% (fewer spills
372 * to memory); eventually they should be removed.
373 */
374# define mad_f_scale64(hi, lo) \
375 ({ mad_fixed_t __result; \
376 mad_fixed64hi_t __hi_; \
377 mad_fixed64lo_t __lo_; \
378 asm __volatile__ ("addc %0, %2, %4\n\t" \
379 "addze %1, %3" \
380 : "=r" (__lo_), "=r" (__hi_) \
381 : "r" (lo), "r" (hi), "r" (1 << (MAD_F_SCALEBITS - 1))); \
382 asm __volatile__ ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
383 "rlwimi %0, %1,32-%3,%3,31" \
384 : "=&r" (__result) \
385 : "r" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)); \
386 __result; \
387 })
388# else
389# define mad_f_scale64(hi, lo) \
390 ({ mad_fixed_t __result; \
391 asm ("rlwinm %0, %2,32-%3,0,%3-1\n\t" \
392 "rlwimi %0, %1,32-%3,%3,31" \
393 : "=r" (__result) \
394 : "r" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)); \
395 __result; \
396 })
397# endif /* OPT_ACCURACY */
398
399# define MAD_F_SCALEBITS MAD_F_FRACBITS
400
401/* --- Default ------------------------------------------------------------- */
402
403# elif defined(FPM_DEFAULT)
404
405/*
406 * This version is the most portable but it loses significant accuracy.
407 * Furthermore, accuracy is biased against the second argument, so care
408 * should be taken when ordering operands.
409 *
410 * The scale factors are constant as this is not used with SSO.
411 *
412 * Pre-rounding is required to stay within the limits of compliance.
413 */
414# if defined(OPT_SPEED)
415# define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
416# else
417# define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
418 (((y) + (1L << 15)) >> 16))
419# endif
420
421/* ------------------------------------------------------------------------- */
422
423# else
424# error "no FPM selected"
425# endif
426
427/* default implementations */
428
429# if !defined(mad_f_mul)
430# define mad_f_mul(x, y) \
431 ({ mad_fixed64hi_t __hi; \
432 mad_fixed64lo_t __lo; \
433 MAD_F_MLX(__hi, __lo, (x), (y)); \
434 mad_f_scale64(__hi, __lo); \
435 })
436# endif
437
438# if !defined(MAD_F_MLA)
439# define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
440# define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
441# define MAD_F_MLN(hi, lo) ((lo) = -(lo))
442# define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
443# endif
444
445# if !defined(MAD_F_ML0)
446# define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
447# endif
448
449# if !defined(MAD_F_MLN)
450# define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
451# endif
452
453# if !defined(MAD_F_MLZ)
454# define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
455# endif
456
457# if !defined(mad_f_scale64)
458# if defined(OPT_ACCURACY)
459# define mad_f_scale64(hi, lo) \
460 ((((mad_fixed_t) \
461 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
462 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
463# else
464# define mad_f_scale64(hi, lo) \
465 ((mad_fixed_t) \
466 (((hi) << (32 - MAD_F_SCALEBITS)) | \
467 ((lo) >> MAD_F_SCALEBITS)))
468# endif
469# define MAD_F_SCALEBITS MAD_F_FRACBITS
470# endif
471
472/* miscellaneous C routines */
473
474mad_fixed_t mad_f_abs(mad_fixed_t);
475
476# endif