fix bug: WAV file playback does not resume (FS#11077)
[kugel-rb.git] / apps / codecs / libfaad / fixed.h
blob6cda967399ab6883b6ca1db9cf2fb49567c66c5b
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 #ifndef __FIXED_H__
29 #define __FIXED_H__
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
35 #if defined(_WIN32_WCE) && defined(_ARM_)
36 #include <cmnintrin.h>
37 #endif
39 #define COEF_BITS 28
40 #define COEF_PRECISION (1 << COEF_BITS)
41 #define REAL_BITS 14 // MAXIMUM OF 14 FOR FIXED POINT SBR
42 #define REAL_PRECISION (1 << REAL_BITS)
44 /* FRAC is the fractional only part of the fixed point number [0.0..1.0) */
45 #define FRAC_SIZE 32 /* frac is a 32 bit integer */
46 #define FRAC_BITS 31
47 #define FRAC_PRECISION ((uint32_t)(1 << FRAC_BITS))
48 #define FRAC_MAX 0x7FFFFFFF
50 typedef int32_t real_t;
52 #define UFIX_CONST(A,PRECISION) ((uint32_t)((A)*(PRECISION)+0.5))
53 #define FIX_CONST(A,PRECISION) (((A) >= 0) ? ((real_t)((A)*(PRECISION)+0.5)) : ((real_t)((A)*(PRECISION)-0.5)))
54 #define REAL_CONST(A) FIX_CONST((A),(REAL_PRECISION))
55 #define COEF_CONST(A) FIX_CONST((A),(COEF_PRECISION))
56 #define FRAC_CONST(A) (((A) == 1.00) ? ((real_t)FRAC_MAX) : FIX_CONST((A),(FRAC_PRECISION)))
57 //#define FRAC_CONST(A) (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5)))
59 #define Q2_BITS 22
60 #define Q2_PRECISION (1 << Q2_BITS)
61 #define Q2_CONST(A) (((A) >= 0) ? ((real_t)((A)*(Q2_PRECISION)+0.5)) : ((real_t)((A)*(Q2_PRECISION)-0.5)))
63 #if defined(CPU_COLDFIRE)
65 static INLINE real_t MUL_F(real_t A, real_t B)
67 asm volatile (
68 "mac.l %[A], %[B], %%acc0\n\t"
69 "movclr.l %%acc0, %[A]"
70 : [A] "+&r" (A) : [B] "r" (B)
72 return A;
75 static INLINE real_t MUL_C(real_t A, real_t B)
77 asm volatile (
78 "mac.l %[A], %[B], %%acc0\n\t"
79 "movclr.l %%acc0, %[A]\n\t"
80 : [A] "+&d" (A) : [B] "r" (B)
82 return A << 3;
85 /* MUL_R needs too many shifts for us to just operate on the top 32 bits the
86 emac unit gives as usual, so we do a full 64 bit mul here. */
87 static INLINE real_t MUL_R(real_t x, real_t y)
89 real_t t1, t2;
90 asm volatile (
91 "mac.l %[x],%[y],%%acc0\n" /* multiply */
92 "mulu.l %[y],%[x] \n" /* get lower half, avoid emac stall */
93 "movclr.l %%acc0,%[t1] \n" /* get higher half */
94 "moveq.l #17,%[t2] \n"
95 "asl.l %[t2],%[t1] \n" /* hi <<= 17, plus one free */
96 "moveq.l #14,%[t2] \n"
97 "lsr.l %[t2],%[x] \n" /* (unsigned)lo >>= 14 */
98 "or.l %[x],%[t1] \n" /* combine result */
99 : /* outputs */
100 [t1]"=&d"(t1),
101 [t2]"=&d"(t2),
102 [x] "+d" (x)
103 : /* inputs */
104 [y] "d" (y)
106 return t1;
109 static INLINE void ComplexMult(real_t *y1, real_t *y2,
110 real_t x1, real_t x2, real_t c1, real_t c2)
112 asm volatile(
113 "mac.l %[x1], %[c1], %%acc0\n\t"
114 "mac.l %[x2], %[c2], %%acc0\n\t"
115 "mac.l %[x2], %[c1], %%acc1\n\t"
116 "msac.l %[x1], %[c2], %%acc1\n\t"
117 "movclr.l %%acc0, %[x1]\n\t"
118 "move.l %[x1], (%[y1])\n\t"
119 "movclr.l %%acc1, %[x1]\n\t"
120 "move.l %[x1], (%[y2])"
121 : [x1] "+&r" (x1)
122 : [x2] "r" (x2), [y1] "a" (y1), [y2] "a" (y2),
123 [c1] "r" (c1), [c2] "r" (c2)
124 : "memory"
128 /* the following see little or no use, so just ignore them for now */
129 #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
130 #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
131 #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
132 #define DESCALE(A,S) ((S)>0?(((A)>>((S)-1))+1)>>1:(A)<<-(S))
133 #define DESCALE_SHIFT(A,SH,SC) DESCALE((A),(SC)-(SH))
135 #elif defined(__GNUC__) && defined (__arm__)
137 /* taken from MAD */
138 #define arm_mul(x, y, SCALEBITS) \
139 ({ \
140 uint32_t __hi; \
141 uint32_t __lo; \
142 uint32_t __result; \
143 asm("smull %0, %1, %3, %4\n\t" \
144 "movs %0, %0, lsr %5\n\t" \
145 "adc %2, %0, %1, lsl %6" \
146 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
147 : "%r" (x), "r" (y), \
148 "M" (SCALEBITS), "M" (32 - (SCALEBITS)) \
149 : "cc"); \
150 __result; \
153 static INLINE real_t MUL_R(real_t A, real_t B)
155 return arm_mul(A, B, REAL_BITS);
158 static INLINE real_t MUL_C(real_t A, real_t B)
160 return arm_mul(A, B, COEF_BITS);
163 static INLINE real_t MUL_Q2(real_t A, real_t B)
165 return arm_mul(A, B, Q2_BITS);
168 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
170 return arm_mul(A, B, 6);
173 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
175 return arm_mul(A, B, 23);
178 static INLINE real_t _MulHigh(real_t x, real_t y)
180 uint32_t __lo;
181 uint32_t __hi;
182 asm("smull\t%0, %1, %2, %3"
183 : "=&r"(__lo),"=&r"(__hi)
184 : "%r"(x),"r"(y)
185 : "cc");
186 return __hi;
189 static INLINE real_t MUL_F(real_t A, real_t B)
191 return _MulHigh(A, B) << (FRAC_SIZE-FRAC_BITS);
194 /* Complex multiplication */
195 static INLINE void ComplexMult(real_t *y1, real_t *y2,
196 real_t x1, real_t x2, real_t c1, real_t c2)
198 int32_t tmp, yt1, yt2;
199 asm("smull %0, %1, %4, %6\n\t"
200 "smlal %0, %1, %5, %7\n\t"
201 "rsb %3, %4, #0\n\t"
202 "smull %0, %2, %5, %6\n\t"
203 "smlal %0, %2, %3, %7"
204 : "=&r" (tmp), "=&r" (yt1), "=&r" (yt2), "=r" (x1)
205 : "3" (x1), "r" (x2), "r" (c1), "r" (c2)
206 : "cc" );
207 *y1 = yt1 << (FRAC_SIZE-FRAC_BITS);
208 *y2 = yt2 << (FRAC_SIZE-FRAC_BITS);
211 static inline real_t DESCALE_SHIFT(unsigned val, int shift, int scale)
213 unsigned out;
214 if ((out = val >> (scale - shift - 1)))
216 out++;
217 out >>= 1;
218 } else
219 out = val << (shift - scale);
220 return out;
223 #else
225 /* multiply with real shift */
226 #define MUL_R(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (REAL_BITS-1))) >> REAL_BITS)
227 /* multiply with coef shift */
228 #define MUL_C(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (COEF_BITS-1))) >> COEF_BITS)
229 /* multiply with fractional shift */
230 #if defined(_WIN32_WCE) && defined(_ARM_)
231 /* eVC for PocketPC has an intrinsic function that returns only the high 32 bits of a 32x32 bit multiply */
232 static INLINE real_t MUL_F(real_t A, real_t B)
234 return _MulHigh(A,B) << (32-FRAC_BITS);
236 #else
237 #define _MulHigh(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_SIZE-1))) >> FRAC_SIZE)
238 #define MUL_F(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_BITS-1))) >> FRAC_BITS)
239 #endif
240 #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
241 #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
242 #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
243 #define DESCALE(A,S) ((S)>0?(((A)>>((S)-1))+1)>>1:(A)<<-(S))
244 #define DESCALE_SHIFT(A,SH,SC) DESCALE((A),(SC)-(SH))
246 /* Complex multiplication */
247 static INLINE void ComplexMult(real_t *y1, real_t *y2,
248 real_t x1, real_t x2, real_t c1, real_t c2)
250 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2))<<(FRAC_SIZE-FRAC_BITS);
251 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2))<<(FRAC_SIZE-FRAC_BITS);
254 #endif
258 #ifdef __cplusplus
260 #endif
261 #endif