2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2003 Underbit Technologies, Inc.
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.
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.
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
21 # ifndef LIBMAD_FIXED_H
22 # define LIBMAD_FIXED_H
27 typedef s32 mad_fixed_t
;
29 typedef s32 mad_fixed64hi_t
;
30 typedef u32 mad_fixed64lo_t
;
32 typedef s32 mad_fixed_t
;
34 typedef s32 mad_fixed64hi_t
;
35 typedef u32 mad_fixed64lo_t
;
38 # if defined(_MSC_VER)
39 # define mad_fixed64_t signed __int64
40 # elif 1 || defined(__GNUC__)
41 # define mad_fixed64_t signed long long
44 # if defined(FPM_FLOAT)
45 typedef double mad_sample_t
;
47 typedef mad_fixed_t mad_sample_t
;
51 * Fixed-point format: 0xABBBBBBB
52 * A == whole part (sign + 3 bits)
53 * B == fractional part (28 bits)
55 * Values are signed two's complement, so the effective range is:
56 * 0x80000000 to 0x7fffffff
57 * -8.0 to +7.9999999962747097015380859375
59 * The smallest representable value is:
60 * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
62 * 28 bits of fractional accuracy represent about
63 * 8.6 digits of decimal accuracy.
65 * Fixed-point numbers can be added or subtracted as normal
66 * integers, but multiplication requires shifting the 64-bit result
67 * from 56 fractional bits back to 28 (and rounding.)
69 * Changing the definition of MAD_F_FRACBITS is only partially
70 * supported, and must be done with care.
73 # define MAD_F_FRACBITS 28
75 # if MAD_F_FRACBITS == 28
76 # define MAD_F(x) ((mad_fixed_t) (x##L))
78 # if MAD_F_FRACBITS < 28
79 # warning "MAD_F_FRACBITS < 28"
80 # define MAD_F(x) ((mad_fixed_t) \
82 (1L << (28 - MAD_F_FRACBITS - 1))) >> \
83 (28 - MAD_F_FRACBITS)))
84 # elif MAD_F_FRACBITS > 28
85 # error "MAD_F_FRACBITS > 28 not currently supported"
86 # define MAD_F(x) ((mad_fixed_t) \
87 ((x##L) << (MAD_F_FRACBITS - 28)))
91 # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
92 # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
94 # define MAD_F_ONE MAD_F(0x10000000)
96 # define mad_f_tofixed(x) ((mad_fixed_t) \
97 ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
98 # define mad_f_todouble(x) ((double) \
99 ((x) / (double) (1L << MAD_F_FRACBITS)))
101 # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
102 # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
103 /* (x should be positive) */
105 # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
107 # define mad_f_add(x, y) ((x) + (y))
108 # define mad_f_sub(x, y) ((x) - (y))
110 # if defined(FPM_FLOAT)
111 # error "FPM_FLOAT not yet supported"
114 # define MAD_F(x) mad_f_todouble(x)
116 # define mad_f_mul(x, y) ((x) * (y))
117 # define mad_f_scale64
119 # undef ASO_ZEROCHECK
121 # elif defined(FPM_64BIT)
124 * This version should be the most accurate if 64-bit types are supported by
125 * the compiler, although it may not be the most efficient.
127 # if defined(OPT_ACCURACY)
128 # define mad_f_mul(x, y) \
130 ((((mad_fixed64_t) (x) * (y)) + \
131 (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
133 # define mad_f_mul(x, y) \
134 ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
137 # define MAD_F_SCALEBITS MAD_F_FRACBITS
139 /* --- Intel --------------------------------------------------------------- */
141 # elif defined(FPM_INTEL)
143 # if defined(_MSC_VER)
144 # pragma warning(push)
145 # pragma warning(disable: 4035) /* no return value */
147 mad_fixed_t
mad_f_mul_inline(mad_fixed_t x
, mad_fixed_t y
)
150 fracbits
= MAD_F_FRACBITS
156 shrd eax
, edx
, fracbits
159 /* implicit return of eax */
161 # pragma warning(pop)
163 # define mad_f_mul mad_f_mul_inline
164 # define mad_f_scale64
167 * This Intel version is fast and accurate; the disposition of the least
168 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
170 # define MAD_F_MLX(hi, lo, x, y) \
172 : "=a" (lo), "=d" (hi) \
173 : "%a" (x), "rm" (y) \
176 # if defined(OPT_ACCURACY)
178 * This gives best accuracy but is not very fast.
180 # define MAD_F_MLA(hi, lo, x, y) \
181 ({ mad_fixed64hi_t __hi; \
182 mad_fixed64lo_t __lo; \
183 MAD_F_MLX(__hi, __lo, (x), (y)); \
184 asm ("addl %2,%0\n\t" \
186 : "=rm" (lo), "=rm" (hi) \
187 : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
190 # endif /* OPT_ACCURACY */
192 # if defined(OPT_ACCURACY)
194 * Surprisingly, this is faster than SHRD followed by ADC.
196 # define mad_f_scale64(hi, lo) \
197 ({ mad_fixed64hi_t __hi_; \
198 mad_fixed64lo_t __lo_; \
199 mad_fixed_t __result; \
200 asm ("addl %4,%2\n\t" \
202 : "=rm" (__lo_), "=rm" (__hi_) \
203 : "0" (lo), "1" (hi), \
204 "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
206 asm ("shrdl %3,%2,%1" \
208 : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
213 # define mad_f_scale64(hi, lo) \
214 ({ mad_fixed_t __result; \
215 asm ("shrdl %3,%2,%1" \
217 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
221 # endif /* OPT_ACCURACY */
223 # define MAD_F_SCALEBITS MAD_F_FRACBITS
226 /* --- ARM ----------------------------------------------------------------- */
228 # elif defined(FPM_ARM)
231 * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
232 * least significant bit is properly rounded at no CPU cycle cost!
236 * This is faster than the default implementation via MAD_F_MLX() and
239 # define mad_f_mul(x, y) \
240 ({ mad_fixed64hi_t __hi; \
241 mad_fixed64lo_t __lo; \
242 mad_fixed_t __result; \
243 asm ("smull %0, %1, %3, %4\n\t" \
244 "movs %0, %0, lsr %5\n\t" \
245 "adc %2, %0, %1, lsl %6" \
246 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
247 : "%r" (x), "r" (y), \
248 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
254 # define MAD_F_MLX(hi, lo, x, y) \
255 asm ("smull %0, %1, %2, %3" \
256 : "=&r" (lo), "=&r" (hi) \
259 # define MAD_F_MLA(hi, lo, x, y) \
260 asm ("smlal %0, %1, %2, %3" \
261 : "+r" (lo), "+r" (hi) \
264 # define MAD_F_MLN(hi, lo) \
265 asm ("rsbs %0, %2, #0\n\t" \
267 : "=r" (lo), "=r" (hi) \
268 : "0" (lo), "1" (hi) \
271 # define mad_f_scale64(hi, lo) \
272 ({ mad_fixed_t __result; \
273 asm ("movs %0, %1, lsr %3\n\t" \
274 "adc %0, %0, %2, lsl %4" \
276 : "r" (lo), "r" (hi), \
277 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
282 # define MAD_F_SCALEBITS MAD_F_FRACBITS
284 /* --- MIPS ---------------------------------------------------------------- */
286 # elif defined(FPM_MIPS)
289 * This MIPS version is fast and accurate; the disposition of the least
290 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
292 # define MAD_F_MLX(hi, lo, x, y) \
294 : "=l" (lo), "=h" (hi) \
297 # if defined(HAVE_MADD_ASM)
298 # define MAD_F_MLA(hi, lo, x, y) \
300 : "+l" (lo), "+h" (hi) \
302 # elif defined(HAVE_MADD16_ASM)
304 * This loses significant accuracy due to the 16-bit integer limit in the
305 * multiply/accumulate instruction.
307 # define MAD_F_ML0(hi, lo, x, y) \
309 : "=l" (lo), "=h" (hi) \
310 : "%r" ((x) >> 12), "r" ((y) >> 16))
311 # define MAD_F_MLA(hi, lo, x, y) \
312 asm ("madd16 %2,%3" \
313 : "+l" (lo), "+h" (hi) \
314 : "%r" ((x) >> 12), "r" ((y) >> 16))
315 # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
318 # if defined(OPT_SPEED)
319 # define mad_f_scale64(hi, lo) \
320 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
321 # define MAD_F_SCALEBITS MAD_F_FRACBITS
324 /* --- SPARC --------------------------------------------------------------- */
326 # elif defined(FPM_SPARC)
329 * This SPARC V8 version is fast and accurate; the disposition of the least
330 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
332 # define MAD_F_MLX(hi, lo, x, y) \
333 asm ("smul %2, %3, %0\n\t" \
335 : "=r" (lo), "=r" (hi) \
336 : "%r" (x), "rI" (y))
338 /* --- PowerPC ------------------------------------------------------------- */
340 # elif defined(FPM_PPC)
343 * This PowerPC version is fast and accurate; the disposition of the least
344 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
346 # define MAD_F_MLX(hi, lo, x, y) \
348 asm ("mullw %0,%1,%2" \
350 : "%r" (x), "r" (y)); \
351 asm ("mulhw %0,%1,%2" \
353 : "%r" (x), "r" (y)); \
357 # if defined(OPT_ACCURACY)
359 * This gives best accuracy but is not very fast.
361 # define MAD_F_MLA(hi, lo, x, y) \
362 ({ mad_fixed64hi_t __hi; \
363 mad_fixed64lo_t __lo; \
364 MAD_F_MLX(__hi, __lo, (x), (y)); \
365 asm ("addc %0,%2,%3\n\t" \
367 : "=r" (lo), "=r" (hi) \
368 : "%r" (lo), "r" (__lo), \
369 "%r" (hi), "r" (__hi) \
374 # if defined(OPT_ACCURACY)
376 * This is slower than the truncating version below it.
378 # define mad_f_scale64(hi, lo) \
379 ({ mad_fixed_t __result, __round; \
380 asm ("rotrwi %0,%1,%2" \
382 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
383 asm ("extrwi %0,%1,1,0" \
386 asm ("insrwi %0,%1,%2,0" \
388 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
389 asm ("add %0,%1,%2" \
391 : "%r" (__result), "r" (__round)); \
395 # define mad_f_scale64(hi, lo) \
396 ({ mad_fixed_t __result; \
397 asm ("rotrwi %0,%1,%2" \
399 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
400 asm ("insrwi %0,%1,%2,0" \
402 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
407 # define MAD_F_SCALEBITS MAD_F_FRACBITS
409 /* --- Default ------------------------------------------------------------- */
411 # elif defined(FPM_DEFAULT)
414 * This version is the most portable but it loses significant accuracy.
415 * Furthermore, accuracy is biased against the second argument, so care
416 * should be taken when ordering operands.
418 * The scale factors are constant as this is not used with SSO.
420 * Pre-rounding is required to stay within the limits of compliance.
422 # if defined(OPT_SPEED)
423 # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
425 # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
426 (((y) + (1L << 15)) >> 16))
429 /* ------------------------------------------------------------------------- */
432 # error "no FPM selected"
435 /* default implementations */
437 # if !defined(mad_f_mul)
438 # define mad_f_mul(x, y) \
439 ({ register mad_fixed64hi_t __hi; \
440 register mad_fixed64lo_t __lo; \
441 MAD_F_MLX(__hi, __lo, (x), (y)); \
442 mad_f_scale64(__hi, __lo); \
446 # if !defined(MAD_F_MLA)
447 # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
448 # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
449 # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
450 # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
453 # if !defined(MAD_F_ML0)
454 # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
457 # if !defined(MAD_F_MLN)
458 # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
461 # if !defined(MAD_F_MLZ)
462 # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
465 # if !defined(mad_f_scale64)
466 # if defined(OPT_ACCURACY)
467 # define mad_f_scale64(hi, lo) \
469 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
470 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
472 # define mad_f_scale64(hi, lo) \
474 (((hi) << (32 - MAD_F_SCALEBITS)) | \
475 ((lo) >> MAD_F_SCALEBITS)))
477 # define MAD_F_SCALEBITS MAD_F_FRACBITS
482 mad_fixed_t
mad_f_abs(mad_fixed_t
);
483 mad_fixed_t
mad_f_div(mad_fixed_t
, mad_fixed_t
);