Coverage Report

Created: 2025-07-11 06:40

/proc/self/cwd/libfaad/fixed.h
Line
Count
Source
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
35
extern "C" {
36
#endif
37
38
#if defined(_WIN32_WCE) && defined(_ARM_)
39
#include <cmnintrin.h>
40
#endif
41
42
43
4.79G
#define COEF_BITS 28
44
10.0M
#define COEF_PRECISION (1 << COEF_BITS)
45
2.56G
#define REAL_BITS 14 // MAXIMUM OF 14 FOR FIXED POINT SBR
46
1.97G
#define REAL_PRECISION (1 << REAL_BITS)
47
48
/* FRAC is the fractional only part of the fixed point number [0.0..1.0) */
49
4.91G
#define FRAC_SIZE 32 /* frac is a 32 bit integer */
50
14.3G
#define FRAC_BITS 31
51
/* Multiplication by power of 2 will be compiled to left-shift */
52
983M
#define FRAC_MUL (1u << (FRAC_SIZE - FRAC_BITS))
53
611k
#define FRAC_PRECISION ((uint32_t)(1u << FRAC_BITS))
54
137k
#define FRAC_MAX 0x7FFFFFFF
55
56
typedef int32_t real_t;
57
58
59
1.97G
#define REAL_CONST(A) (((A) >= 0) ? ((real_t)((A)*(REAL_PRECISION)+0.5)) : ((real_t)((A)*(REAL_PRECISION)-0.5)))
60
10.0M
#define COEF_CONST(A) (((A) >= 0) ? ((real_t)((A)*(COEF_PRECISION)+0.5)) : ((real_t)((A)*(COEF_PRECISION)-0.5)))
61
581k
#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))))
62
//#define FRAC_CONST(A) (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5)))
63
64
#define Q2_BITS 22
65
#define Q2_PRECISION (1 << Q2_BITS)
66
#define Q2_CONST(A) (((A) >= 0) ? ((real_t)((A)*(Q2_PRECISION)+0.5)) : ((real_t)((A)*(Q2_PRECISION)-0.5)))
67
68
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__GNUC__) && !defined(_WIN64)
69
70
/* multiply with real shift */
71
static INLINE real_t MUL_R(real_t A, real_t B)
72
{
73
    _asm {
74
        mov eax,A
75
        imul B
76
        shrd eax,edx,REAL_BITS
77
    }
78
}
79
80
/* multiply with coef shift */
81
static INLINE real_t MUL_C(real_t A, real_t B)
82
{
83
    _asm {
84
        mov eax,A
85
        imul B
86
        shrd eax,edx,COEF_BITS
87
    }
88
}
89
90
static INLINE real_t MUL_Q2(real_t A, real_t B)
91
{
92
    _asm {
93
        mov eax,A
94
        imul B
95
        shrd eax,edx,Q2_BITS
96
    }
97
}
98
99
static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
100
{
101
    _asm {
102
        mov eax,A
103
        imul B
104
        shrd eax,edx,6
105
    }
106
}
107
108
static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
109
{
110
    _asm {
111
        mov eax,A
112
        imul B
113
        shrd eax,edx,23
114
    }
115
}
116
117
#if 1
118
static INLINE real_t _MulHigh(real_t A, real_t B)
119
{
120
    _asm {
121
        mov eax,A
122
        imul B
123
        mov eax,edx
124
    }
125
}
126
127
/* multiply with fractional shift */
128
static INLINE real_t MUL_F(real_t A, real_t B)
129
{
130
    return _MulHigh(A,B) << (FRAC_SIZE-FRAC_BITS);
131
}
132
133
/* Complex multiplication */
134
static INLINE void ComplexMult(real_t *y1, real_t *y2,
135
    real_t x1, real_t x2, real_t c1, real_t c2)
136
{
137
    *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2))<<(FRAC_SIZE-FRAC_BITS);
138
    *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2))<<(FRAC_SIZE-FRAC_BITS);
139
}
140
#else
141
static INLINE real_t MUL_F(real_t A, real_t B)
142
{
143
    _asm {
144
        mov eax,A
145
        imul B
146
        shrd eax,edx,FRAC_BITS
147
    }
148
}
149
150
/* Complex multiplication */
151
static INLINE void ComplexMult(real_t *y1, real_t *y2,
152
    real_t x1, real_t x2, real_t c1, real_t c2)
153
{
154
    *y1 = MUL_F(x1, c1) + MUL_F(x2, c2);
155
    *y2 = MUL_F(x2, c1) - MUL_F(x1, c2);
156
}
157
#endif
158
159
#elif defined(__GNUC__) && defined (__arm__)
160
161
/* taken from MAD */
162
#define arm_mul(x, y, SCALEBITS) \
163
({ \
164
    uint32_t __hi; \
165
    uint32_t __lo; \
166
    uint32_t __result; \
167
    asm("smull  %0, %1, %3, %4\n\t" \
168
        "movs   %0, %0, lsr %5\n\t" \
169
        "adc    %2, %0, %1, lsl %6" \
170
        : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
171
        : "%r" (x), "r" (y), \
172
        "M" (SCALEBITS), "M" (32 - (SCALEBITS)) \
173
        : "cc"); \
174
        __result; \
175
})
176
177
static INLINE real_t MUL_R(real_t A, real_t B)
178
{
179
    return arm_mul(A, B, REAL_BITS);
180
}
181
182
static INLINE real_t MUL_C(real_t A, real_t B)
183
{
184
    return arm_mul(A, B, COEF_BITS);
185
}
186
187
static INLINE real_t MUL_Q2(real_t A, real_t B)
188
{
189
    return arm_mul(A, B, Q2_BITS);
190
}
191
192
static INLINE real_t MUL_SHIFT6(real_t A, real_t B)
193
{
194
    return arm_mul(A, B, 6);
195
}
196
197
static INLINE real_t MUL_SHIFT23(real_t A, real_t B)
198
{
199
    return arm_mul(A, B, 23);
200
}
201
202
static INLINE real_t _MulHigh(real_t x, real_t y)
203
{
204
    uint32_t __lo;
205
    uint32_t __hi;
206
    asm("smull\t%0, %1, %2, %3"
207
        : "=&r"(__lo),"=&r"(__hi)
208
        : "%r"(x),"r"(y)
209
        : "cc");
210
    return __hi;
211
}
212
213
static INLINE real_t MUL_F(real_t A, real_t B)
214
{
215
    return _MulHigh(A, B) << (FRAC_SIZE-FRAC_BITS);
216
}
217
218
/* Complex multiplication */
219
static INLINE void ComplexMult(real_t *y1, real_t *y2,
220
    real_t x1, real_t x2, real_t c1, real_t c2)
221
{
222
    int32_t tmp, yt1, yt2;
223
    asm("smull %0, %1, %4, %6\n\t"
224
        "smlal %0, %1, %5, %7\n\t"
225
        "rsb   %3, %4, #0\n\t"
226
        "smull %0, %2, %5, %6\n\t"
227
        "smlal %0, %2, %3, %7"
228
        : "=&r" (tmp), "=&r" (yt1), "=&r" (yt2), "=r" (x1)
229
        : "3" (x1), "r" (x2), "r" (c1), "r" (c2)
230
        : "cc" );
231
    *y1 = yt1 << (FRAC_SIZE-FRAC_BITS);
232
    *y2 = yt2 << (FRAC_SIZE-FRAC_BITS);
233
}
234
235
#else
236
237
  /* multiply with real shift */
238
58.6M
  #define MUL_R(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (REAL_BITS-1))) >> REAL_BITS)
239
  /* multiply with coef shift */
240
2.39G
  #define MUL_C(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (COEF_BITS-1))) >> COEF_BITS)
241
  /* multiply with fractional shift */
242
#if defined(_WIN32_WCE) && defined(_ARM_)
243
  /* eVC for PocketPC has an intrinsic function that returns only the high 32 bits of a 32x32 bit multiply */
244
  static INLINE real_t MUL_F(real_t A, real_t B)
245
  {
246
      return _MulHigh(A,B) << (32-FRAC_BITS);
247
  }
248
#else
249
#ifdef __BFIN__
250
#define _MulHigh(X,Y) ({ int __xxo;                      \
251
     asm (                                               \
252
         "a1 = %2.H * %1.L (IS,M);\n\t"                  \
253
         "a0 = %1.H * %2.H, a1+= %1.H * %2.L (IS,M);\n\t"\
254
         "a1 = a1 >>> 16;\n\t"                           \
255
         "%0 = (a0 += a1);\n\t"                          \
256
         : "=d" (__xxo) : "d" (X), "d" (Y) : "A0","A1"); __xxo; })
257
258
#define MUL_F(X,Y) ({ int __xxo;                         \
259
     asm (                                               \
260
         "a1 = %2.H * %1.L (M);\n\t"                     \
261
         "a0 = %1.H * %2.H, a1+= %1.H * %2.L (M);\n\t"   \
262
         "a1 = a1 >>> 16;\n\t"                           \
263
         "%0 = (a0 += a1);\n\t"                          \
264
         : "=d" (__xxo) : "d" (X), "d" (Y) : "A0","A1"); __xxo; })
265
#else
266
1.96G
  #define _MulHigh(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1u << (FRAC_SIZE-1))) >> FRAC_SIZE)
267
6.70G
  #define MUL_F(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1u << (FRAC_BITS-1))) >> FRAC_BITS)
268
#endif
269
#endif
270
  #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
271
  #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
272
  #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
273
274
/* Complex multiplication */
275
static INLINE void ComplexMult(real_t *y1, real_t *y2,
276
    real_t x1, real_t x2, real_t c1, real_t c2)
277
491M
{
278
491M
    *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) * FRAC_MUL;
279
491M
    *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) * FRAC_MUL;
280
491M
}
Unexecuted instantiation: decoder.c:ComplexMult
Unexecuted instantiation: common.c:ComplexMult
Unexecuted instantiation: bits.c:ComplexMult
Unexecuted instantiation: drc.c:ComplexMult
Unexecuted instantiation: error.c:ComplexMult
Unexecuted instantiation: filtbank.c:ComplexMult
mdct.c:ComplexMult
Line
Count
Source
277
201M
{
278
201M
    *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) * FRAC_MUL;
279
201M
    *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) * FRAC_MUL;
280
201M
}
cfft.c:ComplexMult
Line
Count
Source
277
273M
{
278
273M
    *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) * FRAC_MUL;
279
273M
    *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) * FRAC_MUL;
280
273M
}
Unexecuted instantiation: mp4.c:ComplexMult
Unexecuted instantiation: output.c:ComplexMult
Unexecuted instantiation: sbr_dec.c:ComplexMult
ps_dec.c:ComplexMult
Line
Count
Source
277
16.8M
{
278
16.8M
    *y1 = (_MulHigh(x1, c1) + _MulHigh(x2, c2)) * FRAC_MUL;
279
16.8M
    *y2 = (_MulHigh(x2, c1) - _MulHigh(x1, c2)) * FRAC_MUL;
280
16.8M
}
Unexecuted instantiation: sbr_hfadj.c:ComplexMult
Unexecuted instantiation: sbr_hfgen.c:ComplexMult
Unexecuted instantiation: sbr_fbt.c:ComplexMult
Unexecuted instantiation: sbr_qmf.c:ComplexMult
Unexecuted instantiation: sbr_dct.c:ComplexMult
Unexecuted instantiation: syntax.c:ComplexMult
Unexecuted instantiation: specrec.c:ComplexMult
Unexecuted instantiation: lt_predict.c:ComplexMult
Unexecuted instantiation: pns.c:ComplexMult
Unexecuted instantiation: ms.c:ComplexMult
Unexecuted instantiation: is.c:ComplexMult
Unexecuted instantiation: sbr_syntax.c:ComplexMult
Unexecuted instantiation: sbr_huff.c:ComplexMult
Unexecuted instantiation: sbr_e_nf.c:ComplexMult
Unexecuted instantiation: sbr_tf_grid.c:ComplexMult
Unexecuted instantiation: ps_syntax.c:ComplexMult
Unexecuted instantiation: rvlc.c:ComplexMult
Unexecuted instantiation: hcr.c:ComplexMult
Unexecuted instantiation: huffman.c:ComplexMult
Unexecuted instantiation: pulse.c:ComplexMult
Unexecuted instantiation: tns.c:ComplexMult
281
282
#endif
283
284
/* Saturated left shift */
285
19.9k
#define SAT_SHIFT_MASK(E) (~0u << (31u - (E)))
286
3.53M
#define SAT_SHIFT(V,E,M) (((((V) >> ((E) + 1)) ^ (V)) & (M)) \
287
3.53M
    ? (((V) < 0) ? (int32_t)0x80000000 : 0x7FFFFFFF) \
288
3.53M
    : ((int32_t)((uint32_t)(V) << (E))))
289
290
#ifdef __cplusplus
291
}
292
#endif
293
#endif