mpcmath.h

00001 /*
00002  * Musepack audio compression
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include <mpc/mpc_types.h>
00020 
00021 typedef union mpc_floatint
00022 {
00023         float   f;
00024         mpc_int32_t n;
00025 } mpc_floatint;
00026 
00027 typedef union mpc_doubleint
00028 {
00029         double   d;
00030         mpc_int32_t n[2];
00031 } mpc_doubleint;
00032 
00033 static mpc_inline mpc_int32_t mpc_lrintf(float fVal)
00034 {
00035         mpc_floatint tmp;
00036         tmp.f = fVal  + 0x00FF8000;
00037         return tmp.n - 0x4B7F8000;
00038 }
00039 
00040 #define mpc_round32             mpc_lrintf
00041 #define mpc_nearbyintf  mpc_lrintf
00042 
00043 
00044 #ifndef M_PI
00045 # define M_PI            3.1415926535897932384626433832795029     // 4*atan(1)
00046 # define M_PIl           3.1415926535897932384626433832795029L
00047 # define M_LN2           0.6931471805599453094172321214581766     // ln(2)
00048 # define M_LN2l          0.6931471805599453094172321214581766L
00049 # define M_LN10          2.3025850929940456840179914546843642     // ln 10 */
00050 # define M_LN10l         2.3025850929940456840179914546843642L
00051 #endif
00052 
00053 // fast but maybe more inaccurate, use if you need speed
00054 #if defined(__GNUC__) && !defined(__APPLE__)
00055 #  define SIN(x)      sinf ((float)(x))
00056 #  define COS(x)      cosf ((float)(x))
00057 #  define ATAN2(x,y)  atan2f ((float)(x), (float)(y))
00058 #  define SQRT(x)     sqrtf ((float)(x))
00059 #  define LOG(x)      logf ((float)(x))
00060 #  define LOG10(x)    log10f ((float)(x))
00061 #  define POW(x,y)    expf (logf(x) * (y))
00062 #  define POW10(x)    expf (M_LN10 * (x))
00063 #  define FLOOR(x)    floorf ((float)(x))
00064 #  define IFLOOR(x)   (int) floorf ((float)(x))
00065 #  define FABS(x)     fabsf ((float)(x))
00066 #else
00067 # define SIN(x)      (float) sin (x)
00068 # define COS(x)      (float) cos (x)
00069 # define ATAN2(x,y)  (float) atan2 (x, y)
00070 # define SQRT(x)     (float) sqrt (x)
00071 # define LOG(x)      (float) log (x)
00072 # define LOG10(x)    (float) log10 (x)
00073 # define POW(x,y)    (float) pow (x,y)
00074 # define POW10(x)    (float) pow (10., (x))
00075 # define FLOOR(x)    (float) floor (x)
00076 # define IFLOOR(x)   (int)   floor (x)
00077 # define FABS(x)     (float) fabs (x)
00078 #endif
00079 
00080 #define SQRTF(x)      SQRT (x)
00081 #ifdef FAST_MATH
00082 # define TABSTEP      64
00083 # define COSF(x)      my_cos ((float)(x))
00084 # define ATAN2F(x,y)  my_atan2 ((float)(x), (float)(y))
00085 # define IFLOORF(x)   my_ifloor ((float)(x))
00086 
00087 void   Init_FastMath ( void );
00088 extern const float  tabatan2   [] [2];
00089 extern const float  tabcos     [] [2];
00090 extern const float  tabsqrt_ex [];
00091 extern const float  tabsqrt_m  [] [2];
00092 
00093 static mpc_inline float my_atan2 ( float x, float y )
00094 {
00095         float t, ret; int i; mpc_floatint mx, my;
00096 
00097         mx.f = x;
00098         my.f = y;
00099         if ( (mx.n & 0x7FFFFFFF) < (my.n & 0x7FFFFFFF) ) {
00100                 i   = mpc_round32 (t = TABSTEP * (mx.f / my.f));
00101                 ret = tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (t-i);
00102                 if ( my.n < 0 )
00103                         ret = (float)(ret - M_PI);
00104         }
00105         else if ( mx.n < 0 ) {
00106                 i   = mpc_round32 (t = TABSTEP * (my.f / mx.f));
00107                 ret = - M_PI/2 - tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (i-t);
00108         }
00109         else if ( mx.n > 0 ) {
00110                 i   = mpc_round32 (t = TABSTEP * (my.f / mx.f));
00111                 ret = + M_PI/2 - tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (i-t);
00112         }
00113         else {
00114                 ret = 0.;
00115         }
00116         return ret;
00117 }
00118 
00119 
00120 static mpc_inline float my_cos ( float x )
00121 {
00122         float t, ret; int i;
00123         i   = mpc_round32 (t = TABSTEP * x);
00124         ret = tabcos [13*TABSTEP+i][0] + tabcos [13*TABSTEP+i][1] * (t-i);
00125         return ret;
00126 }
00127 
00128 
00129 static mpc_inline int my_ifloor ( float x )
00130 {
00131         mpc_floatint mx;
00132         mx.f = (float) (x + (0x0C00000L + 0.500000001));
00133         return mx.n - 1262485505;
00134 }
00135 
00136 
00137 static mpc_inline float my_sqrt ( float x )
00138 {
00139         float  ret; int i, ex; mpc_floatint mx;
00140         mx.f = x;
00141         ex   = mx.n >> 23;                     // get the exponent
00142         mx.n = (mx.n & 0x7FFFFF) | 0x42800000; // delete the exponent
00143         i    = mpc_round32 (mx.f);             // Integer-part of the mantissa  (round ????????????)
00144         ret  = tabsqrt_m [i-TABSTEP][0] + tabsqrt_m [i-TABSTEP][1] * (mx.f-i); // calculate value
00145         ret *= tabsqrt_ex [ex];
00146         return ret;
00147 }
00148 #else
00149 # define COSF(x)      COS (x)
00150 # define ATAN2F(x,y)  ATAN2 (x,y)
00151 # define IFLOORF(x)   IFLOOR (x)
00152 #endif
00153 

Generated on 20 Jan 2011 for libmpcdec by  doxygen 1.6.1