- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# fixed.h

    00001 /*
    00002  * libmad - MPEG audio decoder library
    00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
    00004  *
    00005  * This program is free software; you can redistribute it and/or modify
    00006  * it under the terms of the GNU General Public License as published by
    00007  * the Free Software Foundation; either version 2 of the License, or
    00008  * (at your option) any later version.
    00009  *
    00010  * This program is distributed in the hope that it will be useful,
    00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013  * GNU General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU General Public License
    00016  * along with this program; if not, write to the Free Software
    00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018  *
    00019  * $Id: fixed.h,v 1.1 2003/08/31 18:59:46 gabest Exp $
    00020  */
    00021
    00022 # ifndef LIBMAD_FIXED_H
    00023 # define LIBMAD_FIXED_H
    00024
    00025 # if SIZEOF_INT >= 4
    00026 typedef   signed int mad_fixed_t;
    00027
    00028 typedef   signed int mad_fixed64hi_t;
    00029 typedef unsigned int mad_fixed64lo_t;
    00030 # else
    00031 typedef   signed long mad_fixed_t;
    00032
    00033 typedef   signed long mad_fixed64hi_t;
    00034 typedef unsigned long mad_fixed64lo_t;
    00035 # endif
    00036
    00037 # if defined(_MSC_VER)
    00038 #  define mad_fixed64_t  signed __int64
    00039 # elif 1 || defined(__GNUC__)
    00040 #  define mad_fixed64_t  signed long long
    00041 # endif
    00042
    00043 # if defined(FPM_FLOAT)
    00044 typedef double mad_sample_t;
    00045 # else
    00046 typedef mad_fixed_t mad_sample_t;
    00047 # endif
    00048
    00049 /*
    00050  * Fixed-point format: 0xABBBBBBB
    00051  * A == whole part      (sign + 3 bits)
    00052  * B == fractional part (28 bits)
    00053  *
    00054  * Values are signed two's complement, so the effective range is:
    00055  * 0x80000000 to 0x7fffffff
    00056  *       -8.0 to +7.9999999962747097015380859375
    00057  *
    00058  * The smallest representable value is:
    00059  * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
    00060  *
    00061  * 28 bits of fractional accuracy represent about
    00062  * 8.6 digits of decimal accuracy.
    00063  *
    00064  * Fixed-point numbers can be added or subtracted as normal
    00065  * integers, but multiplication requires shifting the 64-bit result
    00066  * from 56 fractional bits back to 28 (and rounding.)
    00067  *
    00068  * Changing the definition of MAD_F_FRACBITS is only partially
    00069  * supported, and must be done with care.
    00070  */
    00071
    00072 # define MAD_F_FRACBITS         28
    00073
    00074 # if MAD_F_FRACBITS == 28
    00075 #  define MAD_F(x)              ((mad_fixed_t) (x##L))
    00076 # else
    00077 #  if MAD_F_FRACBITS < 28
    00078 #   warning "MAD_F_FRACBITS < 28"
    00079 #   define MAD_F(x)             ((mad_fixed_t)  \
    00080                                  (((x##L) +  \
    00081                                    (1L << (28 - MAD_F_FRACBITS - 1))) >>  \
    00082                                   (28 - MAD_F_FRACBITS)))
    00083 #  elif MAD_F_FRACBITS > 28
    00084 #   error "MAD_F_FRACBITS > 28 not currently supported"
    00085 #   define MAD_F(x)             ((mad_fixed_t)  \
    00086                                  ((x##L) << (MAD_F_FRACBITS - 28)))
    00087 #  endif
    00088 # endif
    00089
    00090 # define MAD_F_MIN              ((mad_fixed_t) -0x80000000L)
    00091 # define MAD_F_MAX              ((mad_fixed_t) +0x7fffffffL)
    00092
    00093 # define MAD_F_ONE              MAD_F(0x10000000)
    00094
    00095 # define mad_f_tofixed(x)       ((mad_fixed_t)  \
    00096                                  ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
    00097 # define mad_f_todouble(x)      ((double)  \
    00098                                  ((x) / (double) (1L << MAD_F_FRACBITS)))
    00099
    00100 # define mad_f_intpart(x)       ((x) >> MAD_F_FRACBITS)
    00101 # define mad_f_fracpart(x)      ((x) & ((1L << MAD_F_FRACBITS) - 1))
    00102                                 /* (x should be positive) */
    00103
    00104 # define mad_f_fromint(x)       ((x) << MAD_F_FRACBITS)
    00105
    00106 # define mad_f_add(x, y)        ((x) + (y))
    00107 # define mad_f_sub(x, y)        ((x) - (y))
    00108
    00109 # if defined(FPM_FLOAT)
    00110 #  error "FPM_FLOAT not yet supported"
    00111
    00112 #  undef MAD_F
    00113 #  define MAD_F(x)              mad_f_todouble(x)
    00114
    00115 #  define mad_f_mul(x, y)       ((x) * (y))
    00116 #  define mad_f_scale64
    00117
    00118 #  undef ASO_ZEROCHECK
    00119
    00120 # elif defined(FPM_64BIT)
    00121
    00122 /*
    00123  * This version should be the most accurate if 64-bit types are supported by
    00124  * the compiler, although it may not be the most efficient.
    00125  */
    00126 #  if defined(OPT_ACCURACY)
    00127 #   define mad_f_mul(x, y)  \
    00128     ((mad_fixed_t)  \
    00129      ((((mad_fixed64_t) (x) * (y)) +  \
    00130        (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
    00131 #  else
    00132 #   define mad_f_mul(x, y)  \
    00133     ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
    00134 #  endif
    00135
    00136 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00137
    00138 /* --- Intel --------------------------------------------------------------- */
    00139
    00140 # elif defined(FPM_INTEL)
    00141
    00142 #  if defined(_MSC_VER)
    00143 #   pragma warning(push)
    00144 #   pragma warning(disable: 4035)  /* no return value */
    00145 static __forceinline
    00146 mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
    00147 {
    00148   enum {
    00149     fracbits = MAD_F_FRACBITS
    00150   };
    00151
    00152   __asm {
    00153     mov eax, x
    00154     imul y
    00155     shrd eax, edx, fracbits
    00156   }
    00157
    00158   /* implicit return of eax */
    00159 }
    00160 #   pragma warning(pop)
    00161
    00162 #   define mad_f_mul            mad_f_mul_inline
    00163 #   define mad_f_scale64
    00164 #  else
    00165 /*
    00166  * This Intel version is fast and accurate; the disposition of the least
    00167  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00168  */
    00169 #   define MAD_F_MLX(hi, lo, x, y)  \
    00170     asm ("imull %3"  \
    00171          : "=a" (lo), "=d" (hi)  \
    00172          : "%a" (x), "rm" (y)  \
    00173          : "cc")
    00174
    00175 #   if defined(OPT_ACCURACY)
    00176 /*
    00177  * This gives best accuracy but is not very fast.
    00178  */
    00179 #    define MAD_F_MLA(hi, lo, x, y)  \
    00180     ({ mad_fixed64hi_t __hi;  \
    00181        mad_fixed64lo_t __lo;  \
    00182        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00183        asm ("addl %2,%0\n\t"  \
    00184             "adcl %3,%1"  \
    00185             : "=rm" (lo), "=rm" (hi)  \
    00186             : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi)  \
    00187             : "cc");  \
    00188     })
    00189 #   endif  /* OPT_ACCURACY */
    00190
    00191 #   if defined(OPT_ACCURACY)
    00192 /*
    00193  * Surprisingly, this is faster than SHRD followed by ADC.
    00194  */
    00195 #    define mad_f_scale64(hi, lo)  \
    00196     ({ mad_fixed64hi_t __hi_;  \
    00197        mad_fixed64lo_t __lo_;  \
    00198        mad_fixed_t __result;  \
    00199        asm ("addl %4,%2\n\t"  \
    00200             "adcl %5,%3"  \
    00201             : "=rm" (__lo_), "=rm" (__hi_)  \
    00202             : "0" (lo), "1" (hi),  \
    00203               "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0)  \
    00204             : "cc");  \
    00205        asm ("shrdl %3,%2,%1"  \
    00206             : "=rm" (__result)  \
    00207             : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)  \
    00208             : "cc");  \
    00209        __result;  \
    00210     })
    00211 #    else
    00212 #    define mad_f_scale64(hi, lo)  \
    00213     ({ mad_fixed_t __result;  \
    00214        asm ("shrdl %3,%2,%1"  \
    00215             : "=rm" (__result)  \
    00216             : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)  \
    00217             : "cc");  \
    00218        __result;  \
    00219     })
    00220 #   endif  /* OPT_ACCURACY */
    00221
    00222 #   define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00223 #  endif
    00224
    00225 /* --- ARM ----------------------------------------------------------------- */
    00226
    00227 # elif defined(FPM_ARM)
    00228
    00229 /*
    00230  * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
    00231  * least significant bit is properly rounded at no CPU cycle cost!
    00232  */
    00233 # if 1
    00234 /*
    00235  * This is faster than the default implementation via MAD_F_MLX() and
    00236  * mad_f_scale64().
    00237  */
    00238 #  define mad_f_mul(x, y)  \
    00239     ({ mad_fixed64hi_t __hi;  \
    00240        mad_fixed64lo_t __lo;  \
    00241        mad_fixed_t __result;  \
    00242        asm ("smull      %0, %1, %3, %4\n\t"  \
    00243             "movs       %0, %0, lsr %5\n\t"  \
    00244             "adc        %2, %0, %1, lsl %6"  \
    00245             : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
    00246             : "%r" (x), "r" (y),  \
    00247               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
    00248             : "cc");  \
    00249        __result;  \
    00250     })
    00251 # endif
    00252
    00253 #  define MAD_F_MLX(hi, lo, x, y)  \
    00254     asm ("smull %0, %1, %2, %3"  \
    00255          : "=&r" (lo), "=&r" (hi)  \
    00256          : "%r" (x), "r" (y))
    00257
    00258 #  define MAD_F_MLA(hi, lo, x, y)  \
    00259     asm ("smlal %0, %1, %2, %3"  \
    00260          : "+r" (lo), "+r" (hi)  \
    00261          : "%r" (x), "r" (y))
    00262
    00263 #  define MAD_F_MLN(hi, lo)  \
    00264     asm ("rsbs  %0, %2, #0\n\t"  \
    00265          "rsc   %1, %3, #0"  \
    00266          : "=r" (lo), "=r" (hi)  \
    00267          : "0" (lo), "1" (hi)  \
    00268          : "cc")
    00269
    00270 #  define mad_f_scale64(hi, lo)  \
    00271     ({ mad_fixed_t __result;  \
    00272        asm ("movs       %0, %1, lsr %3\n\t"  \
    00273             "adc        %0, %0, %2, lsl %4"  \
    00274             : "=&r" (__result)  \
    00275             : "r" (lo), "r" (hi),  \
    00276               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
    00277             : "cc");  \
    00278        __result;  \
    00279     })
    00280
    00281 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00282
    00283 /* --- MIPS ---------------------------------------------------------------- */
    00284
    00285 # elif defined(FPM_MIPS)
    00286
    00287 /*
    00288  * This MIPS version is fast and accurate; the disposition of the least
    00289  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00290  */
    00291 #  define MAD_F_MLX(hi, lo, x, y)  \
    00292     asm ("mult  %2,%3"  \
    00293          : "=l" (lo), "=h" (hi)  \
    00294          : "%r" (x), "r" (y))
    00295
    00296 # if defined(HAVE_MADD_ASM)
    00297 #  define MAD_F_MLA(hi, lo, x, y)  \
    00298     asm ("madd  %2,%3"  \
    00299          : "+l" (lo), "+h" (hi)  \
    00300          : "%r" (x), "r" (y))
    00301 # elif defined(HAVE_MADD16_ASM)
    00302 /*
    00303  * This loses significant accuracy due to the 16-bit integer limit in the
    00304  * multiply/accumulate instruction.
    00305  */
    00306 #  define MAD_F_ML0(hi, lo, x, y)  \
    00307     asm ("mult  %2,%3"  \
    00308          : "=l" (lo), "=h" (hi)  \
    00309          : "%r" ((x) >> 12), "r" ((y) >> 16))
    00310 #  define MAD_F_MLA(hi, lo, x, y)  \
    00311     asm ("madd16        %2,%3"  \
    00312          : "+l" (lo), "+h" (hi)  \
    00313          : "%r" ((x) >> 12), "r" ((y) >> 16))
    00314 #  define MAD_F_MLZ(hi, lo)  ((mad_fixed_t) (lo))
    00315 # endif
    00316
    00317 # if defined(OPT_SPEED)
    00318 #  define mad_f_scale64(hi, lo)  \
    00319     ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
    00320 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00321 # endif
    00322
    00323 /* --- SPARC --------------------------------------------------------------- */
    00324
    00325 # elif defined(FPM_SPARC)
    00326
    00327 /*
    00328  * This SPARC V8 version is fast and accurate; the disposition of the least
    00329  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00330  */
    00331 #  define MAD_F_MLX(hi, lo, x, y)  \
    00332     asm ("smul %2, %3, %0\n\t"  \
    00333          "rd %%y, %1"  \
    00334          : "=r" (lo), "=r" (hi)  \
    00335          : "%r" (x), "rI" (y))
    00336
    00337 /* --- PowerPC ------------------------------------------------------------- */
    00338
    00339 # elif defined(FPM_PPC)
    00340
    00341 /*
    00342  * This PowerPC version is fast and accurate; the disposition of the least
    00343  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00344  */
    00345 #  define MAD_F_MLX(hi, lo, x, y)  \
    00346     do {  \
    00347       asm ("mullw %0,%1,%2"  \
    00348            : "=r" (lo)  \
    00349            : "%r" (x), "r" (y));  \
    00350       asm ("mulhw %0,%1,%2"  \
    00351            : "=r" (hi)  \
    00352            : "%r" (x), "r" (y));  \
    00353     }  \
    00354     while (0)
    00355
    00356 #  if defined(OPT_ACCURACY)
    00357 /*
    00358  * This gives best accuracy but is not very fast.
    00359  */
    00360 #   define MAD_F_MLA(hi, lo, x, y)  \
    00361     ({ mad_fixed64hi_t __hi;  \
    00362        mad_fixed64lo_t __lo;  \
    00363        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00364        asm ("addc %0,%2,%3\n\t"  \
    00365             "adde %1,%4,%5"  \
    00366             : "=r" (lo), "=r" (hi)  \
    00367             : "%r" (lo), "r" (__lo),  \
    00368               "%r" (hi), "r" (__hi)  \
    00369             : "xer");  \
    00370     })
    00371 #  endif
    00372
    00373 #  if defined(OPT_ACCURACY)
    00374 /*
    00375  * This is slower than the truncating version below it.
    00376  */
    00377 #   define mad_f_scale64(hi, lo)  \
    00378     ({ mad_fixed_t __result, __round;  \
    00379        asm ("rotrwi %0,%1,%2"  \
    00380             : "=r" (__result)  \
    00381             : "r" (lo), "i" (MAD_F_SCALEBITS));  \
    00382        asm ("extrwi %0,%1,1,0"  \
    00383             : "=r" (__round)  \
    00384             : "r" (__result));  \
    00385        asm ("insrwi %0,%1,%2,0"  \
    00386             : "+r" (__result)  \
    00387             : "r" (hi), "i" (MAD_F_SCALEBITS));  \
    00388        asm ("add %0,%1,%2"  \
    00389             : "=r" (__result)  \
    00390             : "%r" (__result), "r" (__round));  \
    00391        __result;  \
    00392     })
    00393 #  else
    00394 #   define mad_f_scale64(hi, lo)  \
    00395     ({ mad_fixed_t __result;  \
    00396        asm ("rotrwi %0,%1,%2"  \
    00397             : "=r" (__result)  \
    00398             : "r" (lo), "i" (MAD_F_SCALEBITS));  \
    00399        asm ("insrwi %0,%1,%2,0"  \
    00400             : "+r" (__result)  \
    00401             : "r" (hi), "i" (MAD_F_SCALEBITS));  \
    00402        __result;  \
    00403     })
    00404 #  endif
    00405
    00406 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00407
    00408 /* --- Default ------------------------------------------------------------- */
    00409
    00410 # elif defined(FPM_DEFAULT)
    00411
    00412 /*
    00413  * This version is the most portable but it loses significant accuracy.
    00414  * Furthermore, accuracy is biased against the second argument, so care
    00415  * should be taken when ordering operands.
    00416  *
    00417  * The scale factors are constant as this is not used with SSO.
    00418  *
    00419  * Pre-rounding is required to stay within the limits of compliance.
    00420  */
    00421 #  if defined(OPT_SPEED)
    00422 #   define mad_f_mul(x, y)      (((x) >> 12) * ((y) >> 16))
    00423 #  else
    00424 #   define mad_f_mul(x, y)      ((((x) + (1L << 11)) >> 12) *  \
    00425                                  (((y) + (1L << 15)) >> 16))
    00426 #  endif
    00427
    00428 /* ------------------------------------------------------------------------- */
    00429
    00430 # else
    00431 #  error "no FPM selected"
    00432 # endif
    00433
    00434 /* default implementations */
    00435
    00436 # if !defined(mad_f_mul)
    00437 #  define mad_f_mul(x, y)  \
    00438     ({ register mad_fixed64hi_t __hi;  \
    00439        register mad_fixed64lo_t __lo;  \
    00440        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00441        mad_f_scale64(__hi, __lo);  \
    00442     })
    00443 # endif
    00444
    00445 # if !defined(MAD_F_MLA)
    00446 #  define MAD_F_ML0(hi, lo, x, y)       ((lo)  = mad_f_mul((x), (y)))
    00447 #  define MAD_F_MLA(hi, lo, x, y)       ((lo) += mad_f_mul((x), (y)))
    00448 #  define MAD_F_MLN(hi, lo)             ((lo)  = -(lo))
    00449 #  define MAD_F_MLZ(hi, lo)             ((void) (hi), (mad_fixed_t) (lo))
    00450 # endif
    00451
    00452 # if !defined(MAD_F_ML0)
    00453 #  define MAD_F_ML0(hi, lo, x, y)       MAD_F_MLX((hi), (lo), (x), (y))
    00454 # endif
    00455
    00456 # if !defined(MAD_F_MLN)
    00457 #  define MAD_F_MLN(hi, lo)             ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
    00458 # endif
    00459
    00460 # if !defined(MAD_F_MLZ)
    00461 #  define MAD_F_MLZ(hi, lo)             mad_f_scale64((hi), (lo))
    00462 # endif
    00463
    00464 # if !defined(mad_f_scale64)
    00465 #  if defined(OPT_ACCURACY)
    00466 #   define mad_f_scale64(hi, lo)  \
    00467     ((((mad_fixed_t)  \
    00468        (((hi) << (32 - (MAD_F_SCALEBITS - 1))) |  \
    00469         ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
    00470 #  else
    00471 #   define mad_f_scale64(hi, lo)  \
    00472     ((mad_fixed_t)  \
    00473      (((hi) << (32 - MAD_F_SCALEBITS)) |  \
    00474       ((lo) >> MAD_F_SCALEBITS)))
    00475 #  endif
    00476 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00477 # endif
    00478
    00479 /* C routines */
    00480
    00481 mad_fixed_t mad_f_abs(mad_fixed_t);
    00482 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
    00483
    00484 # endif