VedaLibSoundMP3/fixed.h

00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2004 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.38 2004/02/17 02:02:03 rob 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 #   elif defined(OPT_INTEL)
00212 /*
00213  * Alternate Intel scaling that may or may not perform better.
00214  */
00215 #    define mad_f_scale64(hi, lo)  \
00216     ({ mad_fixed_t __result;  \
00217        asm ("shrl %3,%1\n\t"  \
00218         "shll %4,%2\n\t"  \
00219         "orl %2,%1"  \
00220         : "=rm" (__result)  \
00221         : "0" (lo), "r" (hi),  \
00222           "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS)  \
00223         : "cc");  \
00224        __result;  \
00225     })
00226 #   else
00227 #    define mad_f_scale64(hi, lo)  \
00228     ({ mad_fixed_t __result;  \
00229        asm ("shrdl %3,%2,%1"  \
00230         : "=rm" (__result)  \
00231         : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)  \
00232         : "cc");  \
00233        __result;  \
00234     })
00235 #   endif  /* OPT_ACCURACY */
00236 
00237 #   define MAD_F_SCALEBITS  MAD_F_FRACBITS
00238 #  endif
00239 
00240 /* --- ARM ----------------------------------------------------------------- */
00241 
00242 # elif defined(FPM_ARM)
00243 
00244 /* 
00245  * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
00246  * least significant bit is properly rounded at no CPU cycle cost!
00247  */
00248 # if 1
00249 /*
00250  * This is faster than the default implementation via MAD_F_MLX() and
00251  * mad_f_scale64().
00252  */
00253 #  define mad_f_mul(x, y)  \
00254     ({ mad_fixed64hi_t __hi;  \
00255        mad_fixed64lo_t __lo;  \
00256        mad_fixed_t __result;  \
00257        asm ("smull  %0, %1, %3, %4\n\t"  \
00258         "movs   %0, %0, lsr %5\n\t"  \
00259         "adc    %2, %0, %1, lsl %6"  \
00260         : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
00261         : "%r" (x), "r" (y),  \
00262           "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
00263         : "cc");  \
00264        __result;  \
00265     })
00266 # endif
00267 
00268 #  define MAD_F_MLX(hi, lo, x, y)  \
00269     asm ("smull %0, %1, %2, %3"  \
00270      : "=&r" (lo), "=&r" (hi)  \
00271      : "%r" (x), "r" (y))
00272 
00273 #  define MAD_F_MLA(hi, lo, x, y)  \
00274     asm ("smlal %0, %1, %2, %3"  \
00275      : "+r" (lo), "+r" (hi)  \
00276      : "%r" (x), "r" (y))
00277 
00278 #  define MAD_F_MLN(hi, lo)  \
00279     asm ("rsbs  %0, %2, #0\n\t"  \
00280      "rsc   %1, %3, #0"  \
00281      : "=r" (lo), "=r" (hi)  \
00282      : "0" (lo), "1" (hi)  \
00283      : "cc")
00284 
00285 #  define mad_f_scale64(hi, lo)  \
00286     ({ mad_fixed_t __result;  \
00287        asm ("movs   %0, %1, lsr %3\n\t"  \
00288         "adc    %0, %0, %2, lsl %4"  \
00289         : "=&r" (__result)  \
00290         : "r" (lo), "r" (hi),  \
00291           "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
00292         : "cc");  \
00293        __result;  \
00294     })
00295 
00296 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00297 
00298 /* --- MIPS ---------------------------------------------------------------- */
00299 
00300 # elif defined(FPM_MIPS)
00301 
00302 /*
00303  * This MIPS version is fast and accurate; the disposition of the least
00304  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
00305  */
00306 #  define MAD_F_MLX(hi, lo, x, y)  \
00307     asm ("mult  %2,%3"  \
00308      : "=l" (lo), "=h" (hi)  \
00309      : "%r" (x), "r" (y))
00310 
00311 # if defined(HAVE_MADD_ASM)
00312 #  define MAD_F_MLA(hi, lo, x, y)  \
00313     asm ("madd  %2,%3"  \
00314      : "+l" (lo), "+h" (hi)  \
00315      : "%r" (x), "r" (y))
00316 # elif defined(HAVE_MADD16_ASM)
00317 /*
00318  * This loses significant accuracy due to the 16-bit integer limit in the
00319  * multiply/accumulate instruction.
00320  */
00321 #  define MAD_F_ML0(hi, lo, x, y)  \
00322     asm ("mult  %2,%3"  \
00323      : "=l" (lo), "=h" (hi)  \
00324      : "%r" ((x) >> 12), "r" ((y) >> 16))
00325 #  define MAD_F_MLA(hi, lo, x, y)  \
00326     asm ("madd16    %2,%3"  \
00327      : "+l" (lo), "+h" (hi)  \
00328      : "%r" ((x) >> 12), "r" ((y) >> 16))
00329 #  define MAD_F_MLZ(hi, lo)  ((mad_fixed_t) (lo))
00330 # endif
00331 
00332 # if defined(OPT_SPEED)
00333 #  define mad_f_scale64(hi, lo)  \
00334     ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
00335 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00336 # endif
00337 
00338 /* --- SPARC --------------------------------------------------------------- */
00339 
00340 # elif defined(FPM_SPARC)
00341 
00342 /*
00343  * This SPARC V8 version is fast and accurate; the disposition of the least
00344  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
00345  */
00346 #  define MAD_F_MLX(hi, lo, x, y)  \
00347     asm ("smul %2, %3, %0\n\t"  \
00348      "rd %%y, %1"  \
00349      : "=r" (lo), "=r" (hi)  \
00350      : "%r" (x), "rI" (y))
00351 
00352 /* --- PowerPC ------------------------------------------------------------- */
00353 
00354 # elif defined(FPM_PPC)
00355 
00356 /*
00357  * This PowerPC version is fast and accurate; the disposition of the least
00358  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
00359  */
00360 #  define MAD_F_MLX(hi, lo, x, y)  \
00361     do {  \
00362       asm ("mullw %0,%1,%2"  \
00363        : "=r" (lo)  \
00364        : "%r" (x), "r" (y));  \
00365       asm ("mulhw %0,%1,%2"  \
00366        : "=r" (hi)  \
00367        : "%r" (x), "r" (y));  \
00368     }  \
00369     while (0)
00370 
00371 #  if defined(OPT_ACCURACY)
00372 /*
00373  * This gives best accuracy but is not very fast.
00374  */
00375 #   define MAD_F_MLA(hi, lo, x, y)  \
00376     ({ mad_fixed64hi_t __hi;  \
00377        mad_fixed64lo_t __lo;  \
00378        MAD_F_MLX(__hi, __lo, (x), (y));  \
00379        asm ("addc %0,%2,%3\n\t"  \
00380         "adde %1,%4,%5"  \
00381         : "=r" (lo), "=r" (hi)  \
00382         : "%r" (lo), "r" (__lo),  \
00383           "%r" (hi), "r" (__hi)  \
00384         : "xer");  \
00385     })
00386 #  endif
00387 
00388 #  if defined(OPT_ACCURACY)
00389 /*
00390  * This is slower than the truncating version below it.
00391  */
00392 #   define mad_f_scale64(hi, lo)  \
00393     ({ mad_fixed_t __result, __round;  \
00394        asm ("rotrwi %0,%1,%2"  \
00395         : "=r" (__result)  \
00396         : "r" (lo), "i" (MAD_F_SCALEBITS));  \
00397        asm ("extrwi %0,%1,1,0"  \
00398         : "=r" (__round)  \
00399         : "r" (__result));  \
00400        asm ("insrwi %0,%1,%2,0"  \
00401         : "+r" (__result)  \
00402         : "r" (hi), "i" (MAD_F_SCALEBITS));  \
00403        asm ("add %0,%1,%2"  \
00404         : "=r" (__result)  \
00405         : "%r" (__result), "r" (__round));  \
00406        __result;  \
00407     })
00408 #  else
00409 #   define mad_f_scale64(hi, lo)  \
00410     ({ mad_fixed_t __result;  \
00411        asm ("rotrwi %0,%1,%2"  \
00412         : "=r" (__result)  \
00413         : "r" (lo), "i" (MAD_F_SCALEBITS));  \
00414        asm ("insrwi %0,%1,%2,0"  \
00415         : "+r" (__result)  \
00416         : "r" (hi), "i" (MAD_F_SCALEBITS));  \
00417        __result;  \
00418     })
00419 #  endif
00420 
00421 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00422 
00423 /* --- Default ------------------------------------------------------------- */
00424 
00425 # elif defined(FPM_DEFAULT)
00426 
00427 /*
00428  * This version is the most portable but it loses significant accuracy.
00429  * Furthermore, accuracy is biased against the second argument, so care
00430  * should be taken when ordering operands.
00431  *
00432  * The scale factors are constant as this is not used with SSO.
00433  *
00434  * Pre-rounding is required to stay within the limits of compliance.
00435  */
00436 #  if defined(OPT_SPEED)
00437 #   define mad_f_mul(x, y)  (((x) >> 12) * ((y) >> 16))
00438 #  else
00439 #   define mad_f_mul(x, y)  ((((x) + (1L << 11)) >> 12) *  \
00440                  (((y) + (1L << 15)) >> 16))
00441 #  endif
00442 
00443 /* ------------------------------------------------------------------------- */
00444 
00445 # else
00446 #  error "no FPM selected"
00447 # endif
00448 
00449 /* default implementations */
00450 
00451 # if !defined(mad_f_mul)
00452 #  define mad_f_mul(x, y)  \
00453     ({ register mad_fixed64hi_t __hi;  \
00454        register mad_fixed64lo_t __lo;  \
00455        MAD_F_MLX(__hi, __lo, (x), (y));  \
00456        mad_f_scale64(__hi, __lo);  \
00457     })
00458 # endif
00459 
00460 # if !defined(MAD_F_MLA)
00461 #  define MAD_F_ML0(hi, lo, x, y)   ((lo)  = mad_f_mul((x), (y)))
00462 #  define MAD_F_MLA(hi, lo, x, y)   ((lo) += mad_f_mul((x), (y)))
00463 #  define MAD_F_MLN(hi, lo)     ((lo)  = -(lo))
00464 #  define MAD_F_MLZ(hi, lo)     ((void) (hi), (mad_fixed_t) (lo))
00465 # endif
00466 
00467 # if !defined(MAD_F_ML0)
00468 #  define MAD_F_ML0(hi, lo, x, y)   MAD_F_MLX((hi), (lo), (x), (y))
00469 # endif
00470 
00471 # if !defined(MAD_F_MLN)
00472 #  define MAD_F_MLN(hi, lo)     ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
00473 # endif
00474 
00475 # if !defined(MAD_F_MLZ)
00476 #  define MAD_F_MLZ(hi, lo)     mad_f_scale64((hi), (lo))
00477 # endif
00478 
00479 # if !defined(mad_f_scale64)
00480 #  if defined(OPT_ACCURACY)
00481 #   define mad_f_scale64(hi, lo)  \
00482     ((((mad_fixed_t)  \
00483        (((hi) << (32 - (MAD_F_SCALEBITS - 1))) |  \
00484     ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
00485 #  else
00486 #   define mad_f_scale64(hi, lo)  \
00487     ((mad_fixed_t)  \
00488      (((hi) << (32 - MAD_F_SCALEBITS)) |  \
00489       ((lo) >> MAD_F_SCALEBITS)))
00490 #  endif
00491 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00492 # endif
00493 
00494 /* C routines */
00495 
00496 mad_fixed_t mad_f_abs(mad_fixed_t);
00497 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
00498 
00499 # endif

      /\/\        4         N         k         !         N         D
                      _______  _ __ ___  _____            ___ _ _  ____
     ___________  __//___   /________  |/    / ___________\_______/    \
    /   _   _   \/   _     /    _   /      _/_/____/    _       __     /
   /    /   /       /     /    /    \      \/     /    /    \   \     /
  \\___/___/___/    ¯    _____/_____/       ______\___/_____/\________\\
               \________/_ ___ __ l____\      /elD!  
                 http://www.m4nkind.com \____/