Chris@0: /* Chris@0: * libmad - MPEG audio decoder library Chris@0: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU General Public License as published by Chris@0: * the Free Software Foundation; either version 2 of the License, or Chris@0: * (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU General Public License Chris@0: * along with this program; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: * Chris@0: * $Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp $ Chris@0: */ Chris@0: Chris@0: # ifndef LIBMAD_FIXED_H Chris@0: # define LIBMAD_FIXED_H Chris@0: Chris@0: # if SIZEOF_INT >= 4 Chris@0: typedef signed int mad_fixed_t; Chris@0: Chris@0: typedef signed int mad_fixed64hi_t; Chris@0: typedef unsigned int mad_fixed64lo_t; Chris@0: # else Chris@0: typedef signed long mad_fixed_t; Chris@0: Chris@0: typedef signed long mad_fixed64hi_t; Chris@0: typedef unsigned long mad_fixed64lo_t; Chris@0: # endif Chris@0: Chris@0: # if defined(_MSC_VER) Chris@0: # define mad_fixed64_t signed __int64 Chris@0: # elif 1 || defined(__GNUC__) Chris@0: # define mad_fixed64_t signed long long Chris@0: # endif Chris@0: Chris@0: # if defined(FPM_FLOAT) Chris@0: typedef double mad_sample_t; Chris@0: # else Chris@0: typedef mad_fixed_t mad_sample_t; Chris@0: # endif Chris@0: Chris@0: /* Chris@0: * Fixed-point format: 0xABBBBBBB Chris@0: * A == whole part (sign + 3 bits) Chris@0: * B == fractional part (28 bits) Chris@0: * Chris@0: * Values are signed two's complement, so the effective range is: Chris@0: * 0x80000000 to 0x7fffffff Chris@0: * -8.0 to +7.9999999962747097015380859375 Chris@0: * Chris@0: * The smallest representable value is: Chris@0: * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9) Chris@0: * Chris@0: * 28 bits of fractional accuracy represent about Chris@0: * 8.6 digits of decimal accuracy. Chris@0: * Chris@0: * Fixed-point numbers can be added or subtracted as normal Chris@0: * integers, but multiplication requires shifting the 64-bit result Chris@0: * from 56 fractional bits back to 28 (and rounding.) Chris@0: * Chris@0: * Changing the definition of MAD_F_FRACBITS is only partially Chris@0: * supported, and must be done with care. Chris@0: */ Chris@0: Chris@0: # define MAD_F_FRACBITS 28 Chris@0: Chris@0: # if MAD_F_FRACBITS == 28 Chris@0: # define MAD_F(x) ((mad_fixed_t) (x##L)) Chris@0: # else Chris@0: # if MAD_F_FRACBITS < 28 Chris@0: # warning "MAD_F_FRACBITS < 28" Chris@0: # define MAD_F(x) ((mad_fixed_t) \ Chris@0: (((x##L) + \ Chris@0: (1L << (28 - MAD_F_FRACBITS - 1))) >> \ Chris@0: (28 - MAD_F_FRACBITS))) Chris@0: # elif MAD_F_FRACBITS > 28 Chris@0: # error "MAD_F_FRACBITS > 28 not currently supported" Chris@0: # define MAD_F(x) ((mad_fixed_t) \ Chris@0: ((x##L) << (MAD_F_FRACBITS - 28))) Chris@0: # endif Chris@0: # endif Chris@0: Chris@0: # define MAD_F_MIN ((mad_fixed_t) -0x80000000L) Chris@0: # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL) Chris@0: Chris@0: # define MAD_F_ONE MAD_F(0x10000000) Chris@0: Chris@0: # define mad_f_tofixed(x) ((mad_fixed_t) \ Chris@0: ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5)) Chris@0: # define mad_f_todouble(x) ((double) \ Chris@0: ((x) / (double) (1L << MAD_F_FRACBITS))) Chris@0: Chris@0: # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS) Chris@0: # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1)) Chris@0: /* (x should be positive) */ Chris@0: Chris@0: # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS) Chris@0: Chris@0: # define mad_f_add(x, y) ((x) + (y)) Chris@0: # define mad_f_sub(x, y) ((x) - (y)) Chris@0: Chris@0: # if defined(FPM_FLOAT) Chris@0: # error "FPM_FLOAT not yet supported" Chris@0: Chris@0: # undef MAD_F Chris@0: # define MAD_F(x) mad_f_todouble(x) Chris@0: Chris@0: # define mad_f_mul(x, y) ((x) * (y)) Chris@0: # define mad_f_scale64 Chris@0: Chris@0: # undef ASO_ZEROCHECK Chris@0: Chris@0: # elif defined(FPM_64BIT) Chris@0: Chris@0: /* Chris@0: * This version should be the most accurate if 64-bit types are supported by Chris@0: * the compiler, although it may not be the most efficient. Chris@0: */ Chris@0: # if defined(OPT_ACCURACY) Chris@0: # define mad_f_mul(x, y) \ Chris@0: ((mad_fixed_t) \ Chris@0: ((((mad_fixed64_t) (x) * (y)) + \ Chris@0: (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS)) Chris@0: # else Chris@0: # define mad_f_mul(x, y) \ Chris@0: ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS)) Chris@0: # endif Chris@0: Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: Chris@0: /* --- Intel --------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_INTEL) Chris@0: Chris@0: # if defined(_MSC_VER) Chris@0: # pragma warning(push) Chris@0: # pragma warning(disable: 4035) /* no return value */ Chris@0: static __forceinline Chris@0: mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y) Chris@0: { Chris@0: enum { Chris@0: fracbits = MAD_F_FRACBITS Chris@0: }; Chris@0: Chris@0: __asm { Chris@0: mov eax, x Chris@0: imul y Chris@0: shrd eax, edx, fracbits Chris@0: } Chris@0: Chris@0: /* implicit return of eax */ Chris@0: } Chris@0: # pragma warning(pop) Chris@0: Chris@0: # define mad_f_mul mad_f_mul_inline Chris@0: # define mad_f_scale64 Chris@0: # else Chris@0: /* Chris@0: * This Intel version is fast and accurate; the disposition of the least Chris@0: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@0: */ Chris@0: # define MAD_F_MLX(hi, lo, x, y) \ Chris@0: asm ("imull %3" \ Chris@0: : "=a" (lo), "=d" (hi) \ Chris@0: : "%a" (x), "rm" (y) \ Chris@0: : "cc") Chris@0: Chris@0: # if defined(OPT_ACCURACY) Chris@0: /* Chris@0: * This gives best accuracy but is not very fast. Chris@0: */ Chris@0: # define MAD_F_MLA(hi, lo, x, y) \ Chris@0: ({ mad_fixed64hi_t __hi; \ Chris@0: mad_fixed64lo_t __lo; \ Chris@0: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@0: asm ("addl %2,%0\n\t" \ Chris@0: "adcl %3,%1" \ Chris@0: : "=rm" (lo), "=rm" (hi) \ Chris@0: : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \ Chris@0: : "cc"); \ Chris@0: }) Chris@0: # endif /* OPT_ACCURACY */ Chris@0: Chris@0: # if defined(OPT_ACCURACY) Chris@0: /* Chris@0: * Surprisingly, this is faster than SHRD followed by ADC. Chris@0: */ Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed64hi_t __hi_; \ Chris@0: mad_fixed64lo_t __lo_; \ Chris@0: mad_fixed_t __result; \ Chris@0: asm ("addl %4,%2\n\t" \ Chris@0: "adcl %5,%3" \ Chris@0: : "=rm" (__lo_), "=rm" (__hi_) \ Chris@0: : "0" (lo), "1" (hi), \ Chris@0: "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \ Chris@0: : "cc"); \ Chris@0: asm ("shrdl %3,%2,%1" \ Chris@0: : "=rm" (__result) \ Chris@0: : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \ Chris@0: : "cc"); \ Chris@0: __result; \ Chris@0: }) Chris@0: # elif defined(OPT_INTEL) Chris@0: /* Chris@0: * Alternate Intel scaling that may or may not perform better. Chris@0: */ Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed_t __result; \ Chris@0: asm ("shrl %3,%1\n\t" \ Chris@0: "shll %4,%2\n\t" \ Chris@0: "orl %2,%1" \ Chris@0: : "=rm" (__result) \ Chris@0: : "0" (lo), "r" (hi), \ Chris@0: "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \ Chris@0: : "cc"); \ Chris@0: __result; \ Chris@0: }) Chris@0: # else Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed_t __result; \ Chris@0: asm ("shrdl %3,%2,%1" \ Chris@0: : "=rm" (__result) \ Chris@0: : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \ Chris@0: : "cc"); \ Chris@0: __result; \ Chris@0: }) Chris@0: # endif /* OPT_ACCURACY */ Chris@0: Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: # endif Chris@0: Chris@0: /* --- ARM ----------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_ARM) Chris@0: Chris@0: /* Chris@0: * This ARM V4 version is as accurate as FPM_64BIT but much faster. The Chris@0: * least significant bit is properly rounded at no CPU cycle cost! Chris@0: */ Chris@0: # if 1 Chris@0: /* Chris@0: * This is faster than the default implementation via MAD_F_MLX() and Chris@0: * mad_f_scale64(). Chris@0: */ Chris@0: # define mad_f_mul(x, y) \ Chris@0: ({ mad_fixed64hi_t __hi; \ Chris@0: mad_fixed64lo_t __lo; \ Chris@0: mad_fixed_t __result; \ Chris@0: asm ("smull %0, %1, %3, %4\n\t" \ Chris@0: "movs %0, %0, lsr %5\n\t" \ Chris@0: "adc %2, %0, %1, lsl %6" \ Chris@0: : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \ Chris@0: : "%r" (x), "r" (y), \ Chris@0: "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ Chris@0: : "cc"); \ Chris@0: __result; \ Chris@0: }) Chris@0: # endif Chris@0: Chris@0: # define MAD_F_MLX(hi, lo, x, y) \ Chris@0: asm ("smull %0, %1, %2, %3" \ Chris@0: : "=&r" (lo), "=&r" (hi) \ Chris@0: : "%r" (x), "r" (y)) Chris@0: Chris@0: # define MAD_F_MLA(hi, lo, x, y) \ Chris@0: asm ("smlal %0, %1, %2, %3" \ Chris@0: : "+r" (lo), "+r" (hi) \ Chris@0: : "%r" (x), "r" (y)) Chris@0: Chris@0: # define MAD_F_MLN(hi, lo) \ Chris@0: asm ("rsbs %0, %2, #0\n\t" \ Chris@0: "rsc %1, %3, #0" \ Chris@0: : "=r" (lo), "=r" (hi) \ Chris@0: : "0" (lo), "1" (hi) \ Chris@0: : "cc") Chris@0: Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed_t __result; \ Chris@0: asm ("movs %0, %1, lsr %3\n\t" \ Chris@0: "adc %0, %0, %2, lsl %4" \ Chris@0: : "=&r" (__result) \ Chris@0: : "r" (lo), "r" (hi), \ Chris@0: "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ Chris@0: : "cc"); \ Chris@0: __result; \ Chris@0: }) Chris@0: Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: Chris@0: /* --- MIPS ---------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_MIPS) Chris@0: Chris@0: /* Chris@0: * This MIPS version is fast and accurate; the disposition of the least Chris@0: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@0: */ Chris@0: # define MAD_F_MLX(hi, lo, x, y) \ Chris@0: asm ("mult %2,%3" \ Chris@0: : "=l" (lo), "=h" (hi) \ Chris@0: : "%r" (x), "r" (y)) Chris@0: Chris@0: # if defined(HAVE_MADD_ASM) Chris@0: # define MAD_F_MLA(hi, lo, x, y) \ Chris@0: asm ("madd %2,%3" \ Chris@0: : "+l" (lo), "+h" (hi) \ Chris@0: : "%r" (x), "r" (y)) Chris@0: # elif defined(HAVE_MADD16_ASM) Chris@0: /* Chris@0: * This loses significant accuracy due to the 16-bit integer limit in the Chris@0: * multiply/accumulate instruction. Chris@0: */ Chris@0: # define MAD_F_ML0(hi, lo, x, y) \ Chris@0: asm ("mult %2,%3" \ Chris@0: : "=l" (lo), "=h" (hi) \ Chris@0: : "%r" ((x) >> 12), "r" ((y) >> 16)) Chris@0: # define MAD_F_MLA(hi, lo, x, y) \ Chris@0: asm ("madd16 %2,%3" \ Chris@0: : "+l" (lo), "+h" (hi) \ Chris@0: : "%r" ((x) >> 12), "r" ((y) >> 16)) Chris@0: # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo)) Chris@0: # endif Chris@0: Chris@0: # if defined(OPT_SPEED) Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS))) Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: # endif Chris@0: Chris@0: /* --- SPARC --------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_SPARC) Chris@0: Chris@0: /* Chris@0: * This SPARC V8 version is fast and accurate; the disposition of the least Chris@0: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@0: */ Chris@0: # define MAD_F_MLX(hi, lo, x, y) \ Chris@0: asm ("smul %2, %3, %0\n\t" \ Chris@0: "rd %%y, %1" \ Chris@0: : "=r" (lo), "=r" (hi) \ Chris@0: : "%r" (x), "rI" (y)) Chris@0: Chris@0: /* --- PowerPC ------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_PPC) Chris@0: Chris@0: /* Chris@0: * This PowerPC version is fast and accurate; the disposition of the least Chris@0: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@0: */ Chris@0: # define MAD_F_MLX(hi, lo, x, y) \ Chris@0: do { \ Chris@0: asm ("mullw %0,%1,%2" \ Chris@0: : "=r" (lo) \ Chris@0: : "%r" (x), "r" (y)); \ Chris@0: asm ("mulhw %0,%1,%2" \ Chris@0: : "=r" (hi) \ Chris@0: : "%r" (x), "r" (y)); \ Chris@0: } \ Chris@0: while (0) Chris@0: Chris@0: # if defined(OPT_ACCURACY) Chris@0: /* Chris@0: * This gives best accuracy but is not very fast. Chris@0: */ Chris@0: # define MAD_F_MLA(hi, lo, x, y) \ Chris@0: ({ mad_fixed64hi_t __hi; \ Chris@0: mad_fixed64lo_t __lo; \ Chris@0: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@0: asm ("addc %0,%2,%3\n\t" \ Chris@0: "adde %1,%4,%5" \ Chris@0: : "=r" (lo), "=r" (hi) \ Chris@0: : "%r" (lo), "r" (__lo), \ Chris@0: "%r" (hi), "r" (__hi) \ Chris@0: : "xer"); \ Chris@0: }) Chris@0: # endif Chris@0: Chris@0: # if defined(OPT_ACCURACY) Chris@0: /* Chris@0: * This is slower than the truncating version below it. Chris@0: */ Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed_t __result, __round; \ Chris@0: asm ("rotrwi %0,%1,%2" \ Chris@0: : "=r" (__result) \ Chris@0: : "r" (lo), "i" (MAD_F_SCALEBITS)); \ Chris@0: asm ("extrwi %0,%1,1,0" \ Chris@0: : "=r" (__round) \ Chris@0: : "r" (__result)); \ Chris@0: asm ("insrwi %0,%1,%2,0" \ Chris@0: : "+r" (__result) \ Chris@0: : "r" (hi), "i" (MAD_F_SCALEBITS)); \ Chris@0: asm ("add %0,%1,%2" \ Chris@0: : "=r" (__result) \ Chris@0: : "%r" (__result), "r" (__round)); \ Chris@0: __result; \ Chris@0: }) Chris@0: # else Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ({ mad_fixed_t __result; \ Chris@0: asm ("rotrwi %0,%1,%2" \ Chris@0: : "=r" (__result) \ Chris@0: : "r" (lo), "i" (MAD_F_SCALEBITS)); \ Chris@0: asm ("insrwi %0,%1,%2,0" \ Chris@0: : "+r" (__result) \ Chris@0: : "r" (hi), "i" (MAD_F_SCALEBITS)); \ Chris@0: __result; \ Chris@0: }) Chris@0: # endif Chris@0: Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: Chris@0: /* --- Default ------------------------------------------------------------- */ Chris@0: Chris@0: # elif defined(FPM_DEFAULT) Chris@0: Chris@0: /* Chris@0: * This version is the most portable but it loses significant accuracy. Chris@0: * Furthermore, accuracy is biased against the second argument, so care Chris@0: * should be taken when ordering operands. Chris@0: * Chris@0: * The scale factors are constant as this is not used with SSO. Chris@0: * Chris@0: * Pre-rounding is required to stay within the limits of compliance. Chris@0: */ Chris@0: # if defined(OPT_SPEED) Chris@0: # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16)) Chris@0: # else Chris@0: # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \ Chris@0: (((y) + (1L << 15)) >> 16)) Chris@0: # endif Chris@0: Chris@0: /* ------------------------------------------------------------------------- */ Chris@0: Chris@0: # else Chris@0: # error "no FPM selected" Chris@0: # endif Chris@0: Chris@0: /* default implementations */ Chris@0: Chris@0: # if !defined(mad_f_mul) Chris@0: # define mad_f_mul(x, y) \ Chris@0: ({ register mad_fixed64hi_t __hi; \ Chris@0: register mad_fixed64lo_t __lo; \ Chris@0: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@0: mad_f_scale64(__hi, __lo); \ Chris@0: }) Chris@0: # endif Chris@0: Chris@0: # if !defined(MAD_F_MLA) Chris@0: # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y))) Chris@0: # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y))) Chris@0: # define MAD_F_MLN(hi, lo) ((lo) = -(lo)) Chris@0: # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo)) Chris@0: # endif Chris@0: Chris@0: # if !defined(MAD_F_ML0) Chris@0: # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y)) Chris@0: # endif Chris@0: Chris@0: # if !defined(MAD_F_MLN) Chris@0: # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi)) Chris@0: # endif Chris@0: Chris@0: # if !defined(MAD_F_MLZ) Chris@0: # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo)) Chris@0: # endif Chris@0: Chris@0: # if !defined(mad_f_scale64) Chris@0: # if defined(OPT_ACCURACY) Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ((((mad_fixed_t) \ Chris@0: (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \ Chris@0: ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1) Chris@0: # else Chris@0: # define mad_f_scale64(hi, lo) \ Chris@0: ((mad_fixed_t) \ Chris@0: (((hi) << (32 - MAD_F_SCALEBITS)) | \ Chris@0: ((lo) >> MAD_F_SCALEBITS))) Chris@0: # endif Chris@0: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@0: # endif Chris@0: Chris@0: /* C routines */ Chris@0: Chris@0: mad_fixed_t mad_f_abs(mad_fixed_t); Chris@0: mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t); Chris@0: Chris@0: # endif