Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

fixed.h

Go to the documentation of this file.
00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2001 Robert Leslie
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.2 2002/04/19 16:08:55 harlekin 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  * There's a bug somewhere, possibly in the compiler, that sometimes makes
00236  * this necessary instead of the default implementation via MAD_F_MLX and
00237  * mad_f_scale64. It may be related to the use (or lack) of
00238  * -finline-functions and/or -fstrength-reduce.
00239  *
00240  * This is also apparently faster than MAD_F_MLX/mad_f_scale64.
00241  */
00242 #  define mad_f_mul(x, y)  \
00243     ({ mad_fixed64hi_t __hi;  \
00244        mad_fixed64lo_t __lo;  \
00245        mad_fixed_t __result;  \
00246        asm ("smull      %0, %1, %3, %4\n\t"  \
00247             "movs       %0, %0, lsr %5\n\t"  \
00248             "adc        %2, %0, %1, lsl %6"  \
00249             : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
00250             : "%r" (x), "r" (y),  \
00251               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
00252             : "cc");  \
00253        __result;  \
00254     })
00255 # endif
00256 
00257 #  define MAD_F_MLX(hi, lo, x, y)  \
00258     asm ("smull %0, %1, %2, %3"  \
00259          : "=&r" (lo), "=&r" (hi)  \
00260          : "%r" (x), "r" (y))
00261 
00262 #  define MAD_F_MLA(hi, lo, x, y)  \
00263     asm ("smlal %0, %1, %2, %3"  \
00264          : "+r" (lo), "+r" (hi)  \
00265          : "%r" (x), "r" (y))
00266 
00267 #  define MAD_F_MLN(hi, lo)  \
00268     asm ("rsbs  %0, %2, #0\n\t"  \
00269          "rsc   %1, %3, #0"  \
00270          : "=r" (lo), "=r" (hi)  \
00271          : "0" (lo), "1" (hi)  \
00272          : "cc")
00273 
00274 #  define mad_f_scale64(hi, lo)  \
00275     ({ mad_fixed_t __result;  \
00276        asm ("movs       %0, %1, lsr %3\n\t"  \
00277             "adc        %0, %0, %2, lsl %4"  \
00278             : "=r" (__result)  \
00279             : "r" (lo), "r" (hi),  \
00280               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
00281             : "cc");  \
00282        __result;  \
00283     })
00284 
00285 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00286 
00287 /* --- MIPS ---------------------------------------------------------------- */
00288 
00289 # elif defined(FPM_MIPS)
00290 
00291 /*
00292  * This MIPS version is fast and accurate; the disposition of the least
00293  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
00294  */
00295 #  define MAD_F_MLX(hi, lo, x, y)  \
00296     asm ("mult  %2,%3"  \
00297          : "=l" (lo), "=h" (hi)  \
00298          : "%r" (x), "r" (y))
00299 
00300 # if defined(HAVE_MADD_ASM)
00301 #  define MAD_F_MLA(hi, lo, x, y)  \
00302     asm ("madd  %2,%3"  \
00303          : "+l" (lo), "+h" (hi)  \
00304          : "%r" (x), "r" (y))
00305 # elif defined(HAVE_MADD16_ASM)
00306 /*
00307  * This loses significant accuracy due to the 16-bit integer limit in the
00308  * multiply/accumulate instruction.
00309  */
00310 #  define MAD_F_ML0(hi, lo, x, y)  \
00311     asm ("mult  %2,%3"  \
00312          : "=l" (lo), "=h" (hi)  \
00313          : "%r" ((x) >> 12), "r" ((y) >> 16))
00314 #  define MAD_F_MLA(hi, lo, x, y)  \
00315     asm ("madd16        %2,%3"  \
00316          : "+l" (lo), "+h" (hi)  \
00317          : "%r" ((x) >> 12), "r" ((y) >> 16))
00318 #  define MAD_F_MLZ(hi, lo)  ((mad_fixed_t) (lo))
00319 # endif
00320 
00321 # if defined(OPT_SPEED)
00322 #  define mad_f_scale64(hi, lo)  \
00323     ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
00324 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00325 # endif
00326 
00327 /* --- SPARC --------------------------------------------------------------- */
00328 
00329 # elif defined(FPM_SPARC)
00330 
00331 /*
00332  * This SPARC V8 version is fast and accurate; the disposition of the least
00333  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
00334  */
00335 #  define MAD_F_MLX(hi, lo, x, y)  \
00336     asm ("smul %2, %3, %0\n\t"  \
00337          "rd %%y, %1"  \
00338          : "=r" (lo), "=r" (hi)  \
00339          : "%r" (x), "rI" (y))
00340 
00341 /* --- PowerPC ------------------------------------------------------------- */
00342 
00343 # elif defined(FPM_PPC)
00344 
00345 /*
00346  * This PowerPC version is tuned for the 4xx embedded processors. It is
00347  * effectively a tuned version of FPM_64BIT. It is a little faster and just
00348  * as accurate. The disposition of the least significant bit depends on
00349  * OPT_ACCURACY via mad_f_scale64().
00350  */
00351 #  define MAD_F_MLX(hi, lo, x, y)  \
00352     asm ("mulhw %1, %2, %3\n\t"  \
00353          "mullw %0, %2, %3"  \
00354          : "=&r" (lo), "=&r" (hi)  \
00355          : "%r" (x), "r" (y))
00356 
00357 #  define MAD_F_MLA(hi, lo, x, y)  \
00358     ({ mad_fixed64hi_t __hi;  \
00359        mad_fixed64lo_t __lo;  \
00360        MAD_F_MLX(__hi, __lo, (x), (y));  \
00361        asm ("addc %0, %2, %3\n\t"  \
00362             "adde %1, %4, %5"  \
00363             : "=r" (lo), "=r" (hi)  \
00364             : "%r" (__lo), "0" (lo), "%r" (__hi), "1" (hi));  \
00365     })
00366 
00367 #  if defined(OPT_ACCURACY)
00368 /*
00369  * This is accurate and ~2 - 2.5 times slower than the unrounded version.
00370  *
00371  * The __volatile__ improves the generated code by another 5% (fewer spills
00372  * to memory); eventually they should be removed.
00373  */
00374 #   define mad_f_scale64(hi, lo)  \
00375     ({ mad_fixed_t __result;  \
00376        mad_fixed64hi_t __hi_;  \
00377        mad_fixed64lo_t __lo_;  \
00378        asm __volatile__ ("addc %0, %2, %4\n\t"  \
00379                          "addze %1, %3"  \
00380             : "=r" (__lo_), "=r" (__hi_)  \
00381             : "r" (lo), "r" (hi), "r" (1 << (MAD_F_SCALEBITS - 1)));  \
00382        asm __volatile__ ("rlwinm %0, %2,32-%3,0,%3-1\n\t"  \
00383                          "rlwimi %0, %1,32-%3,%3,31"  \
00384             : "=&r" (__result)  \
00385             : "r" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS));  \
00386             __result;  \
00387     })
00388 #  else
00389 #   define mad_f_scale64(hi, lo)  \
00390     ({ mad_fixed_t __result;  \
00391        asm ("rlwinm %0, %2,32-%3,0,%3-1\n\t"  \
00392             "rlwimi %0, %1,32-%3,%3,31"  \
00393             : "=r" (__result)  \
00394             : "r" (lo), "r" (hi), "I" (MAD_F_SCALEBITS));  \
00395             __result;  \
00396     })
00397 #  endif  /* OPT_ACCURACY */
00398 
00399 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00400 
00401 /* --- Default ------------------------------------------------------------- */
00402 
00403 # elif defined(FPM_DEFAULT)
00404 
00405 /*
00406  * This version is the most portable but it loses significant accuracy.
00407  * Furthermore, accuracy is biased against the second argument, so care
00408  * should be taken when ordering operands.
00409  *
00410  * The scale factors are constant as this is not used with SSO.
00411  *
00412  * Pre-rounding is required to stay within the limits of compliance.
00413  */
00414 #  if defined(OPT_SPEED)
00415 #   define mad_f_mul(x, y)      (((x) >> 12) * ((y) >> 16))
00416 #  else
00417 #   define mad_f_mul(x, y)      ((((x) + (1L << 11)) >> 12) *  \
00418                                  (((y) + (1L << 15)) >> 16))
00419 #  endif
00420 
00421 /* ------------------------------------------------------------------------- */
00422 
00423 # else
00424 #  error "no FPM selected"
00425 # endif
00426 
00427 /* default implementations */
00428 
00429 # if !defined(mad_f_mul)
00430 #  define mad_f_mul(x, y)  \
00431     ({ mad_fixed64hi_t __hi;  \
00432        mad_fixed64lo_t __lo;  \
00433        MAD_F_MLX(__hi, __lo, (x), (y));  \
00434        mad_f_scale64(__hi, __lo);  \
00435     })
00436 # endif
00437 
00438 # if !defined(MAD_F_MLA)
00439 #  define MAD_F_ML0(hi, lo, x, y)       ((lo)  = mad_f_mul((x), (y)))
00440 #  define MAD_F_MLA(hi, lo, x, y)       ((lo) += mad_f_mul((x), (y)))
00441 #  define MAD_F_MLN(hi, lo)             ((lo)  = -(lo))
00442 #  define MAD_F_MLZ(hi, lo)             ((void) (hi), (mad_fixed_t) (lo))
00443 # endif
00444 
00445 # if !defined(MAD_F_ML0)
00446 #  define MAD_F_ML0(hi, lo, x, y)       MAD_F_MLX((hi), (lo), (x), (y))
00447 # endif
00448 
00449 # if !defined(MAD_F_MLN)
00450 #  define MAD_F_MLN(hi, lo)             ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
00451 # endif
00452 
00453 # if !defined(MAD_F_MLZ)
00454 #  define MAD_F_MLZ(hi, lo)             mad_f_scale64((hi), (lo))
00455 # endif
00456 
00457 # if !defined(mad_f_scale64)
00458 #  if defined(OPT_ACCURACY)
00459 #   define mad_f_scale64(hi, lo)  \
00460     ((((mad_fixed_t)  \
00461        (((hi) << (32 - (MAD_F_SCALEBITS - 1))) |  \
00462         ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
00463 #  else
00464 #   define mad_f_scale64(hi, lo)  \
00465     ((mad_fixed_t)  \
00466      (((hi) << (32 - MAD_F_SCALEBITS)) |  \
00467       ((lo) >> MAD_F_SCALEBITS)))
00468 #  endif
00469 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
00470 # endif
00471 
00472 /* miscellaneous C routines */
00473 
00474 mad_fixed_t mad_f_abs(mad_fixed_t);
00475 
00476 # endif

Generated on Sat Nov 5 16:15:36 2005 for OPIE by  doxygen 1.4.2