* Onda VX747: add browse screen, pitchscreen, context menu, quickscreen, rewind...
[kugel-rb.git] / apps / codecs / libfaad / fixed.h
blobda895e81bc7fade9a58f319b443bc5a17e1af134
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;
53 #define REAL_CONST(A) (((A) >= 0) ? ((real_t)((A)*(REAL_PRECISION)+0.5)) : ((real_t)((A)*(REAL_PRECISION)-0.5)))
54 #define COEF_CONST(A) (((A) >= 0) ? ((real_t)((A)*(COEF_PRECISION)+0.5)) : ((real_t)((A)*(COEF_PRECISION)-0.5)))
55 #define FRAC_CONST(A) (((A) == 1.00) ? ((real_t)FRAC_MAX) : (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5))))
56 //#define FRAC_CONST(A) (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5)))
58 #define Q2_BITS 22
59 #define Q2_PRECISION (1 << Q2_BITS)
60 #define Q2_CONST(A) (((A) >= 0) ? ((real_t)((A)*(Q2_PRECISION)+0.5)) : ((real_t)((A)*(Q2_PRECISION)-0.5)))
62 #if defined(CPU_COLDFIRE)
64 static INLINE real_t MUL_F(real_t A, real_t B)
66 asm volatile (
67 "mac.l %[A], %[B], %%acc0\n\t"
68 "movclr.l %%acc0, %[A]"
69 : [A] "+&r" (A) : [B] "r" (B)
71 return A;
74 static INLINE real_t MUL_C(real_t A, real_t B)
76 asm volatile (
77 "mac.l %[A], %[B], %%acc0\n\t"
78 "movclr.l %%acc0, %[A]\n\t"
79 : [A] "+&d" (A) : [B] "r" (B)
81 return A << 3;
84 /* MUL_R needs too many shifts for us to just operate on the top 32 bits the
85 emac unit gives as usual, so we do a full 64 bit mul here. */
86 static INLINE real_t MUL_R(real_t x, real_t y)
88 real_t t1, t2;
89 asm volatile (
90 "mac.l %[x],%[y],%%acc0\n" /* multiply */
91 "mulu.l %[y],%[x] \n" /* get lower half, avoid emac stall */
92 "movclr.l %%acc0,%[t1] \n" /* get higher half */
93 "moveq.l #17,%[t2] \n"
94 "asl.l %[t2],%[t1] \n" /* hi <<= 17, plus one free */
95 "moveq.l #14,%[t2] \n"
96 "lsr.l %[t2],%[x] \n" /* (unsigned)lo >>= 14 */
97 "or.l %[x],%[t1] \n" /* combine result */
98 : /* outputs */
99 [t1]"=&d"(t1),
100 [t2]"=&d"(t2),
101 [x] "+d" (x)
102 : /* inputs */
103 [y] "d" (y)
105 return t1;
108 static INLINE void ComplexMult(real_t *y1, real_t *y2,
109 real_t x1, real_t x2, real_t c1, real_t c2)
111 asm volatile(
112 "mac.l %[x1], %[c1], %%acc0\n\t"
113 "mac.l %[x2], %[c2], %%acc0\n\t"
114 "mac.l %[x2], %[c1], %%acc1\n\t"
115 "msac.l %[x1], %[c2], %%acc1\n\t"
116 "movclr.l %%acc0, %[x1]\n\t"
117 "move.l %[x1], (%[y1])\n\t"
118 "movclr.l %%acc1, %[x1]\n\t"
119 "move.l %[x1], (%[y2])"
120 : [x1] "+&r" (x1)
121 : [x2] "r" (x2), [y1] "a" (y1), [y2] "a" (y2),
122 [c1] "r" (c1), [c2] "r" (c2)
123 : "memory"
127 /* the following see little or no use, so just ignore them for now */
128 #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
129 #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
130 #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
132 #elif defined(__GNUC__) && defined (__arm__)
134 /* taken from MAD */
135 #define arm_mul(x, y, SCALEBITS) \
136 ({ \
137 uint32_t __hi; \
138 uint32_t __lo; \
139 uint32_t __result; \
140 asm("smull %0, %1, %3, %4\n\t" \
141 "movs %0, %0, lsr %5\n\t" \
142 "adc %2, %0, %1, lsl %6" \
143 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
144 : "%r" (x), "r" (y), \
145 "M" (SCALEBITS), "M" (32 - (SCALEBITS)) \
146 : "cc"); \
147 __result; \
150 static INLINE real_t MUL_R(real_t A, real_t B)
152 return arm_mul(A, B, REAL_BITS);
155 static INLINE real_t MUL_C(real_t A, real_t B)
157 return arm_mul(A, B, COEF_BITS);
160 static INLINE real_t MUL_Q2(real_t A, real_t B)
162 return arm_mul(A, B, Q2_BITS);
165 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
167 return arm_mul(A, B, 6);
170 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
172 return arm_mul(A, B, 23);
175 static INLINE real_t _MulHigh(real_t x, real_t y)
177 uint32_t __lo;
178 uint32_t __hi;
179 asm("smull\t%0, %1, %2, %3"
180 : "=&r"(__lo),"=&r"(__hi)
181 : "%r"(x),"r"(y)
182 : "cc");
183 return __hi;
186 static INLINE real_t MUL_F(real_t A, real_t B)
188 return _MulHigh(A, B) << (FRAC_SIZE-FRAC_BITS);
191 /* Complex multiplication */
192 static INLINE void ComplexMult(real_t *y1, real_t *y2,
193 real_t x1, real_t x2, real_t c1, real_t c2)
195 int32_t tmp, yt1, yt2;
196 asm("smull %0, %1, %4, %6\n\t"
197 "smlal %0, %1, %5, %7\n\t"
198 "rsb %3, %4, #0\n\t"
199 "smull %0, %2, %5, %6\n\t"
200 "smlal %0, %2, %3, %7"
201 : "=&r" (tmp), "=&r" (yt1), "=&r" (yt2), "=r" (x1)
202 : "3" (x1), "r" (x2), "r" (c1), "r" (c2)
203 : "cc" );
204 *y1 = yt1 << (FRAC_SIZE-FRAC_BITS);
205 *y2 = yt2 << (FRAC_SIZE-FRAC_BITS);
208 #else
210 /* multiply with real shift */
211 #define MUL_R(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (REAL_BITS-1))) >> REAL_BITS)
212 /* multiply with coef shift */
213 #define MUL_C(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (COEF_BITS-1))) >> COEF_BITS)
214 /* multiply with fractional shift */
215 #if defined(_WIN32_WCE) && defined(_ARM_)
216 /* eVC for PocketPC has an intrinsic function that returns only the high 32 bits of a 32x32 bit multiply */
217 static INLINE real_t MUL_F(real_t A, real_t B)
219 return _MulHigh(A,B) << (32-FRAC_BITS);
221 #else
222 #define _MulHigh(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_SIZE-1))) >> FRAC_SIZE)
223 #define MUL_F(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_BITS-1))) >> FRAC_BITS)
224 #endif
225 #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
226 #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
227 #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
229 /* Complex multiplication */
230 static INLINE void ComplexMult(real_t *y1, real_t *y2,
231 real_t x1, real_t x2, real_t c1, real_t c2)
233 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2))<<(FRAC_SIZE-FRAC_BITS);
234 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2))<<(FRAC_SIZE-FRAC_BITS);
237 #endif
241 #ifdef __cplusplus
243 #endif
244 #endif