libmpcdec  1.2.2
mpcmath.h
1 /*
2  * Musepack audio compression
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <mpc/mpc_types.h>
20 
21 typedef union mpc_floatint
22 {
23  float f;
24  mpc_int32_t n;
25 } mpc_floatint;
26 
27 typedef union mpc_doubleint
28 {
29  double d;
30  mpc_int32_t n[2];
32 
33 static mpc_inline mpc_int32_t mpc_lrintf(float fVal)
34 {
35  mpc_floatint tmp;
36  tmp.f = fVal + 0x00FF8000;
37  return tmp.n - 0x4B7F8000;
38 }
39 
40 #define mpc_round32 mpc_lrintf
41 #define mpc_nearbyintf mpc_lrintf
42 
43 
44 #ifndef M_PI
45 # define M_PI 3.1415926535897932384626433832795029 // 4*atan(1)
46 # define M_PIl 3.1415926535897932384626433832795029L
47 # define M_LN2 0.6931471805599453094172321214581766 // ln(2)
48 # define M_LN2l 0.6931471805599453094172321214581766L
49 # define M_LN10 2.3025850929940456840179914546843642 // ln 10 */
50 # define M_LN10l 2.3025850929940456840179914546843642L
51 #endif
52 
53 // fast but maybe more inaccurate, use if you need speed
54 #if defined(__GNUC__) && !defined(__APPLE__)
55 # define SIN(x) sinf ((float)(x))
56 # define COS(x) cosf ((float)(x))
57 # define ATAN2(x,y) atan2f ((float)(x), (float)(y))
58 # define SQRT(x) sqrtf ((float)(x))
59 # define LOG(x) logf ((float)(x))
60 # define LOG10(x) log10f ((float)(x))
61 # define POW(x,y) expf (logf(x) * (y))
62 # define POW10(x) expf (M_LN10 * (x))
63 # define FLOOR(x) floorf ((float)(x))
64 # define IFLOOR(x) (int) floorf ((float)(x))
65 # define FABS(x) fabsf ((float)(x))
66 #else
67 # define SIN(x) (float) sin (x)
68 # define COS(x) (float) cos (x)
69 # define ATAN2(x,y) (float) atan2 (x, y)
70 # define SQRT(x) (float) sqrt (x)
71 # define LOG(x) (float) log (x)
72 # define LOG10(x) (float) log10 (x)
73 # define POW(x,y) (float) pow (x,y)
74 # define POW10(x) (float) pow (10., (x))
75 # define FLOOR(x) (float) floor (x)
76 # define IFLOOR(x) (int) floor (x)
77 # define FABS(x) (float) fabs (x)
78 #endif
79 
80 #define SQRTF(x) SQRT (x)
81 #ifdef FAST_MATH
82 # define TABSTEP 64
83 # define COSF(x) my_cos ((float)(x))
84 # define ATAN2F(x,y) my_atan2 ((float)(x), (float)(y))
85 # define IFLOORF(x) my_ifloor ((float)(x))
86 
87 void Init_FastMath ( void );
88 extern const float tabatan2 [] [2];
89 extern const float tabcos [] [2];
90 extern const float tabsqrt_ex [];
91 extern const float tabsqrt_m [] [2];
92 
93 static mpc_inline float my_atan2 ( float x, float y )
94 {
95  float t, ret; int i; mpc_floatint mx, my;
96 
97  mx.f = x;
98  my.f = y;
99  if ( (mx.n & 0x7FFFFFFF) < (my.n & 0x7FFFFFFF) ) {
100  i = mpc_round32 (t = TABSTEP * (mx.f / my.f));
101  ret = tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (t-i);
102  if ( my.n < 0 )
103  ret = (float)(ret - M_PI);
104  }
105  else if ( mx.n < 0 ) {
106  i = mpc_round32 (t = TABSTEP * (my.f / mx.f));
107  ret = - M_PI/2 - tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (i-t);
108  }
109  else if ( mx.n > 0 ) {
110  i = mpc_round32 (t = TABSTEP * (my.f / mx.f));
111  ret = + M_PI/2 - tabatan2 [1*TABSTEP+i][0] + tabatan2 [1*TABSTEP+i][1] * (i-t);
112  }
113  else {
114  ret = 0.;
115  }
116  return ret;
117 }
118 
119 
120 static mpc_inline float my_cos ( float x )
121 {
122  float t, ret; int i;
123  i = mpc_round32 (t = TABSTEP * x);
124  ret = tabcos [13*TABSTEP+i][0] + tabcos [13*TABSTEP+i][1] * (t-i);
125  return ret;
126 }
127 
128 
129 static mpc_inline int my_ifloor ( float x )
130 {
131  mpc_floatint mx;
132  mx.f = (float) (x + (0x0C00000L + 0.500000001));
133  return mx.n - 1262485505;
134 }
135 
136 
137 static mpc_inline float my_sqrt ( float x )
138 {
139  float ret; int i, ex; mpc_floatint mx;
140  mx.f = x;
141  ex = mx.n >> 23; // get the exponent
142  mx.n = (mx.n & 0x7FFFFF) | 0x42800000; // delete the exponent
143  i = mpc_round32 (mx.f); // Integer-part of the mantissa (round ????????????)
144  ret = tabsqrt_m [i-TABSTEP][0] + tabsqrt_m [i-TABSTEP][1] * (mx.f-i); // calculate value
145  ret *= tabsqrt_ex [ex];
146  return ret;
147 }
148 #else
149 # define COSF(x) COS (x)
150 # define ATAN2F(x,y) ATAN2 (x,y)
151 # define IFLOORF(x) IFLOOR (x)
152 #endif
153