2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
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.
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.
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
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: fixed.h,v 1.32 2007/11/01 12:33:30 menno Exp $
38 #if defined(_WIN32_WCE) && defined(_ARM_)
39 #include <cmnintrin.h>
44 #define COEF_PRECISION (1 << COEF_BITS)
45 #define REAL_BITS 14 // MAXIMUM OF 14 FOR FIXED POINT SBR
46 #define REAL_PRECISION (1 << REAL_BITS)
48 /* FRAC is the fractional only part of the fixed point number [0.0..1.0) */
49 #define FRAC_SIZE 32 /* frac is a 32 bit integer */
51 #define FRAC_PRECISION ((uint32_t)(1 << FRAC_BITS))
52 #define FRAC_MAX 0x7FFFFFFF
54 typedef int32_t real_t;
57 #define REAL_CONST(A) (((A) >= 0) ? ((real_t)((A)*(REAL_PRECISION)+0.5)) : ((real_t)((A)*(REAL_PRECISION)-0.5)))
58 #define COEF_CONST(A) (((A) >= 0) ? ((real_t)((A)*(COEF_PRECISION)+0.5)) : ((real_t)((A)*(COEF_PRECISION)-0.5)))
59 #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))))
60 //#define FRAC_CONST(A) (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5)))
63 #define Q2_PRECISION (1 << Q2_BITS)
64 #define Q2_CONST(A) (((A) >= 0) ? ((real_t)((A)*(Q2_PRECISION)+0.5)) : ((real_t)((A)*(Q2_PRECISION)-0.5)))
66 #if defined(_WIN32) && !defined(_WIN32_WCE)
68 /* multiply with real shift */
69 static INLINE real_t MUL_R(real_t A, real_t B)
74 shrd eax,edx,REAL_BITS
78 /* multiply with coef shift */
79 static INLINE real_t MUL_C(real_t A, real_t B)
84 shrd eax,edx,COEF_BITS
88 static INLINE real_t MUL_Q2(real_t A, real_t B)
97 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
106 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
116 static INLINE real_t _MulHigh(real_t A, real_t B)
125 /* multiply with fractional shift */
126 static INLINE real_t MUL_F(real_t A, real_t B)
128 return _MulHigh(A,B) << (FRAC_SIZE-FRAC_BITS);
131 /* Complex multiplication */
132 static INLINE void ComplexMult(real_t *y1, real_t *y2,
133 real_t x1, real_t x2, real_t c1, real_t c2)
135 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2))<<(FRAC_SIZE-FRAC_BITS);
136 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2))<<(FRAC_SIZE-FRAC_BITS);
139 static INLINE real_t MUL_F(real_t A, real_t B)
144 shrd eax,edx,FRAC_BITS
148 /* Complex multiplication */
149 static INLINE void ComplexMult(real_t *y1, real_t *y2,
150 real_t x1, real_t x2, real_t c1, real_t c2)
152 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
153 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
157 #elif defined(__GNUC__) && defined (__arm__)
160 #define arm_mul(x, y, SCALEBITS) \
165 asm("smull %0, %1, %3, %4\n\t" \
166 "movs %0, %0, lsr %5\n\t" \
167 "adc %2, %0, %1, lsl %6" \
168 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
169 : "%r" (x), "r" (y), \
170 "M" (SCALEBITS), "M" (32 - (SCALEBITS)) \
175 static INLINE real_t MUL_R(real_t A, real_t B)
177 return arm_mul(A, B, REAL_BITS);
180 static INLINE real_t MUL_C(real_t A, real_t B)
182 return arm_mul(A, B, COEF_BITS);
185 static INLINE real_t MUL_Q2(real_t A, real_t B)
187 return arm_mul(A, B, Q2_BITS);
190 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
192 return arm_mul(A, B, 6);
195 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
197 return arm_mul(A, B, 23);
200 static INLINE real_t _MulHigh(real_t x, real_t y)
204 asm("smull\t%0, %1, %2, %3"
205 : "=&r"(__lo),"=&r"(__hi)
211 static INLINE real_t MUL_F(real_t A, real_t B)
213 return _MulHigh(A, B) << (FRAC_SIZE-FRAC_BITS);
216 /* Complex multiplication */
217 static INLINE void ComplexMult(real_t *y1, real_t *y2,
218 real_t x1, real_t x2, real_t c1, real_t c2)
220 int32_t tmp, yt1, yt2;
221 asm("smull %0, %1, %4, %6\n\t"
222 "smlal %0, %1, %5, %7\n\t"
224 "smull %0, %2, %5, %6\n\t"
225 "smlal %0, %2, %3, %7"
226 : "=&r" (tmp), "=&r" (yt1), "=&r" (yt2), "=r" (x1)
227 : "3" (x1), "r" (x2), "r" (c1), "r" (c2)
229 *y1 = yt1 << (FRAC_SIZE-FRAC_BITS);
230 *y2 = yt2 << (FRAC_SIZE-FRAC_BITS);
235 /* multiply with real shift */
236 #define MUL_R(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (REAL_BITS-1))) >> REAL_BITS)
237 /* multiply with coef shift */
238 #define MUL_C(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (COEF_BITS-1))) >> COEF_BITS)
239 /* multiply with fractional shift */
240 #if defined(_WIN32_WCE) && defined(_ARM_)
241 /* eVC for PocketPC has an intrinsic function that returns only the high 32 bits of a 32x32 bit multiply */
242 static INLINE real_t MUL_F(real_t A, real_t B)
244 return _MulHigh(A,B) << (32-FRAC_BITS);
248 #define _MulHigh(X,Y) ({ int __xxo; \
250 "a1 = %2.H * %1.L (IS,M);\n\t" \
251 "a0 = %1.H * %2.H, a1+= %1.H * %2.L (IS,M);\n\t"\
252 "a1 = a1 >>> 16;\n\t" \
253 "%0 = (a0 += a1);\n\t" \
254 : "=d" (__xxo) : "d" (X), "d" (Y) : "A0","A1"); __xxo; })
256 #define MUL_F(X,Y) ({ int __xxo; \
258 "a1 = %2.H * %1.L (M);\n\t" \
259 "a0 = %1.H * %2.H, a1+= %1.H * %2.L (M);\n\t" \
260 "a1 = a1 >>> 16;\n\t" \
261 "%0 = (a0 += a1);\n\t" \
262 : "=d" (__xxo) : "d" (X), "d" (Y) : "A0","A1"); __xxo; })
264 #define _MulHigh(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_SIZE-1))) >> FRAC_SIZE)
265 #define MUL_F(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (FRAC_BITS-1))) >> FRAC_BITS)
268 #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
269 #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
270 #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
272 /* Complex multiplication */
273 static INLINE void ComplexMult(real_t *y1, real_t *y2,
274 real_t x1, real_t x2, real_t c1, real_t c2)
276 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2))<<(FRAC_SIZE-FRAC_BITS);
277 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2))<<(FRAC_SIZE-FRAC_BITS);