summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/fixed.h (plain)
blob: 20f45cfe9326a928bf2036e5a4793ff6a39d7ba1
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero 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.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
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"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: fixed.h,v 1.32 2007/11/01 12:33:30 menno Exp $
29**/
30
31#ifndef __FIXED_H__
32#define __FIXED_H__
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#if defined(_WIN32_WCE) && defined(_ARM_)
39#include <cmnintrin.h>
40#endif
41
42
43#define COEF_BITS 28
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)
47
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 */
50#define FRAC_BITS 31
51#define FRAC_PRECISION ((uint32_t)(1 << FRAC_BITS))
52#define FRAC_MAX 0x7FFFFFFF
53
54 typedef int32_t real_t;
55
56
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)))
61
62#define Q2_BITS 22
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)))
65
66#if defined(_WIN32) && !defined(_WIN32_WCE)
67
68 /* multiply with real shift */
69 static INLINE real_t MUL_R(real_t A, real_t B)
70 {
71 _asm {
72 mov eax, A
73 imul B
74 shrd eax, edx, REAL_BITS
75 }
76 }
77
78 /* multiply with coef shift */
79 static INLINE real_t MUL_C(real_t A, real_t B)
80 {
81 _asm {
82 mov eax, A
83 imul B
84 shrd eax, edx, COEF_BITS
85 }
86 }
87
88 static INLINE real_t MUL_Q2(real_t A, real_t B)
89 {
90 _asm {
91 mov eax, A
92 imul B
93 shrd eax, edx, Q2_BITS
94 }
95 }
96
97 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
98 {
99 _asm {
100 mov eax, A
101 imul B
102 shrd eax, edx, 6
103 }
104 }
105
106 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
107 {
108 _asm {
109 mov eax, A
110 imul B
111 shrd eax, edx, 23
112 }
113 }
114
115#if 1
116 static INLINE real_t _MulHigh(real_t A, real_t B)
117 {
118 _asm {
119 mov eax, A
120 imul B
121 mov eax, edx
122 }
123 }
124
125 /* multiply with fractional shift */
126 static INLINE real_t MUL_F(real_t A, real_t B)
127 {
128 return _MulHigh(A, B) << (FRAC_SIZE - FRAC_BITS);
129 }
130
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)
134 {
135 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) << (FRAC_SIZE - FRAC_BITS);
136 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) << (FRAC_SIZE - FRAC_BITS);
137 }
138#else
139 static INLINE real_t MUL_F(real_t A, real_t B)
140 {
141 _asm {
142 mov eax, A
143 imul B
144 shrd eax, edx, FRAC_BITS
145 }
146 }
147
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)
151 {
152 *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
153 *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
154 }
155#endif
156
157#elif defined(__GNUC__) && defined (__arm__)
158
159 /* taken from MAD */
160#define arm_mul(x, y, SCALEBITS) \
161({ \
162 uint32_t __hi; \
163 uint32_t __lo; \
164 uint32_t __result; \
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)) \
171 : "cc"); \
172 __result; \
173})
174
175 static INLINE real_t MUL_R(real_t A, real_t B)
176 {
177 return arm_mul(A, B, REAL_BITS);
178 }
179
180 static INLINE real_t MUL_C(real_t A, real_t B)
181 {
182 return arm_mul(A, B, COEF_BITS);
183 }
184
185 static INLINE real_t MUL_Q2(real_t A, real_t B)
186 {
187 return arm_mul(A, B, Q2_BITS);
188 }
189
190 static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
191 {
192 return arm_mul(A, B, 6);
193 }
194
195 static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
196 {
197 return arm_mul(A, B, 23);
198 }
199
200 static INLINE real_t _MulHigh(real_t x, real_t y)
201 {
202 uint32_t __lo;
203 uint32_t __hi;
204 asm("smull\t%0, %1, %2, %3"
205 : "=&r"(__lo), "=&r"(__hi)
206 : "%r"(x), "r"(y)
207 : "cc");
208 return __hi;
209 }
210
211 static INLINE real_t MUL_F(real_t A, real_t B)
212 {
213 return _MulHigh(A, B) << (FRAC_SIZE - FRAC_BITS);
214 }
215
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)
219 {
220 int32_t tmp, yt1, yt2;
221 asm("smull %0, %1, %4, %6\n\t"
222 "smlal %0, %1, %5, %7\n\t"
223 "rsb %3, %4, #0\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)
228 : "cc");
229 *y1 = yt1 << (FRAC_SIZE - FRAC_BITS);
230 *y2 = yt2 << (FRAC_SIZE - FRAC_BITS);
231 }
232
233#else
234
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)
243 {
244 return _MulHigh(A, B) << (32 - FRAC_BITS);
245 }
246#else
247#ifdef __BFIN__
248#define _MulHigh(X,Y) ({ int __xxo; \
249 asm ( \
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; })
255
256#define MUL_F(X,Y) ({ int __xxo; \
257 asm ( \
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; })
263#else
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)
266#endif
267#endif
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)
271
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)
275 {
276 *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) << (FRAC_SIZE - FRAC_BITS);
277 *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) << (FRAC_SIZE - FRAC_BITS);
278 }
279
280#endif
281
282
283
284#ifdef __cplusplus
285}
286#endif
287#endif
288