1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
diff --git a/apps/codecs/libfaad/common.c b/apps/codecs/libfaad/common.c
index e8340d3..025c8f8 100644
--- a/apps/codecs/libfaad/common.c
+++ b/apps/codecs/libfaad/common.c
@@ -248,33 +248,78 @@ uint32_t random_int(void)
 #define TABLE_BITS 6
 /* just take the maximum number of bits for interpolation */
 #define INTERP_BITS (REAL_BITS-TABLE_BITS)
-
-static const real_t pow2_tab[] = {
-    REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117),
-    REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557),
-    REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258),
-    REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242),
-    REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578),
-    REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703),
-    REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484),
-    REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778),
-    REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741),
-    REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591),
-    REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095),
-    REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647),
-    REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423),
-    REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837),
-    REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254),
-    REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736),
-    REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478),
-    REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521),
-    REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342),
-    REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966),
-    REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400),
-    REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000)
+/* precision of values in pow2_tab */
+#define POWTBL_BITS (31-(INTERP_BITS))
+#define POWTBL_PRECIS (1U<<(POWTBL_BITS))
+static const uint32_t pow2_tab[] ICONST_ATTR = {
+    UFIX_CONST(1.000000000000000,POWTBL_PRECIS),
+    UFIX_CONST(1.010889286051701,POWTBL_PRECIS),
+    UFIX_CONST(1.021897148654117,POWTBL_PRECIS),
+    UFIX_CONST(1.033024879021228,POWTBL_PRECIS),
+    UFIX_CONST(1.044273782427414,POWTBL_PRECIS),
+    UFIX_CONST(1.055645178360557,POWTBL_PRECIS),
+    UFIX_CONST(1.067140400676824,POWTBL_PRECIS),
+    UFIX_CONST(1.078760797757120,POWTBL_PRECIS),
+    UFIX_CONST(1.090507732665258,POWTBL_PRECIS),
+    UFIX_CONST(1.102382583307841,POWTBL_PRECIS),
+    UFIX_CONST(1.114386742595892,POWTBL_PRECIS),
+    UFIX_CONST(1.126521618608242,POWTBL_PRECIS),
+    UFIX_CONST(1.138788634756692,POWTBL_PRECIS),
+    UFIX_CONST(1.151189229952983,POWTBL_PRECIS),
+    UFIX_CONST(1.163724858777578,POWTBL_PRECIS),
+    UFIX_CONST(1.176396991650281,POWTBL_PRECIS),
+    UFIX_CONST(1.189207115002721,POWTBL_PRECIS),
+    UFIX_CONST(1.202156731452703,POWTBL_PRECIS),
+    UFIX_CONST(1.215247359980469,POWTBL_PRECIS),
+    UFIX_CONST(1.228480536106870,POWTBL_PRECIS),
+    UFIX_CONST(1.241857812073484,POWTBL_PRECIS),
+    UFIX_CONST(1.255380757024691,POWTBL_PRECIS),
+    UFIX_CONST(1.269050957191733,POWTBL_PRECIS),
+    UFIX_CONST(1.282870016078778,POWTBL_PRECIS),
+    UFIX_CONST(1.296839554651010,POWTBL_PRECIS),
+    UFIX_CONST(1.310961211524764,POWTBL_PRECIS),
+    UFIX_CONST(1.325236643159741,POWTBL_PRECIS),
+    UFIX_CONST(1.339667524053303,POWTBL_PRECIS),
+    UFIX_CONST(1.354255546936893,POWTBL_PRECIS),
+    UFIX_CONST(1.369002422974591,POWTBL_PRECIS),
+    UFIX_CONST(1.383909881963832,POWTBL_PRECIS),
+    UFIX_CONST(1.398979672538311,POWTBL_PRECIS),
+    UFIX_CONST(1.414213562373095,POWTBL_PRECIS),
+    UFIX_CONST(1.429613338391970,POWTBL_PRECIS),
+    UFIX_CONST(1.445180806977047,POWTBL_PRECIS),
+    UFIX_CONST(1.460917794180647,POWTBL_PRECIS),
+    UFIX_CONST(1.476826145939499,POWTBL_PRECIS),
+    UFIX_CONST(1.492907728291265,POWTBL_PRECIS),
+    UFIX_CONST(1.509164427593423,POWTBL_PRECIS),
+    UFIX_CONST(1.525598150744538,POWTBL_PRECIS),
+    UFIX_CONST(1.542210825407941,POWTBL_PRECIS),
+    UFIX_CONST(1.559004400237837,POWTBL_PRECIS),
+    UFIX_CONST(1.575980845107887,POWTBL_PRECIS),
+    UFIX_CONST(1.593142151342267,POWTBL_PRECIS),
+    UFIX_CONST(1.610490331949254,POWTBL_PRECIS),
+    UFIX_CONST(1.628027421857348,POWTBL_PRECIS),
+    UFIX_CONST(1.645755478153965,POWTBL_PRECIS),
+    UFIX_CONST(1.663676580326736,POWTBL_PRECIS),
+    UFIX_CONST(1.681792830507429,POWTBL_PRECIS),
+    UFIX_CONST(1.700106353718524,POWTBL_PRECIS),
+    UFIX_CONST(1.718619298122478,POWTBL_PRECIS),
+    UFIX_CONST(1.737333835273706,POWTBL_PRECIS),
+    UFIX_CONST(1.756252160373300,POWTBL_PRECIS),
+    UFIX_CONST(1.775376492526521,POWTBL_PRECIS),
+    UFIX_CONST(1.794709075003107,POWTBL_PRECIS),
+    UFIX_CONST(1.814252175500399,POWTBL_PRECIS),
+    UFIX_CONST(1.834008086409342,POWTBL_PRECIS),
+    UFIX_CONST(1.853979125083386,POWTBL_PRECIS),
+    UFIX_CONST(1.874167634110300,POWTBL_PRECIS),
+    UFIX_CONST(1.894575981586966,POWTBL_PRECIS),
+    UFIX_CONST(1.915206561397147,POWTBL_PRECIS),
+    UFIX_CONST(1.936061793492294,POWTBL_PRECIS),
+    UFIX_CONST(1.957144124175400,POWTBL_PRECIS),
+    UFIX_CONST(1.978456026387951,POWTBL_PRECIS),
+    UFIX_CONST(2.000000000000000,POWTBL_PRECIS)
 };

-static const real_t log2_tab[] = {
+static const real_t log2_tab[] ICONST_ATTR = {
     REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453),
     REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169),
     REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312),
@@ -299,12 +344,12 @@ static const real_t log2_tab[] = {
     REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000)
 };

-real_t pow2_fix(real_t val)
+uint32_t pow2_fix(real_t val)
 {
     uint32_t x1, x2;
     uint32_t errcorr;
     uint32_t index_frac;
-    real_t retval;
+    uint32_t retval;
     int32_t whole = (val >> REAL_BITS);

     /* rest = [0..1] */
@@ -313,41 +358,27 @@ real_t pow2_fix(real_t val)
     /* index into pow2_tab */
     int32_t index = rest >> (REAL_BITS-TABLE_BITS);

-
-    if (val == 0)
-        return (1<<REAL_BITS);
-
     /* leave INTERP_BITS bits */
     index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
     index_frac = index_frac & ((1<<INTERP_BITS)-1);

-    if (whole > 0)
-    {
-        retval = 1 << whole;
-    } else {
-        retval = REAL_CONST(1) >> -whole;
-    }
-
     x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
     x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
-    errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
+    errcorr = ( (index_frac*(x2-x1)));

-    if (whole > 0)
-    {
-        retval = retval * (errcorr + x1);
-    } else {
-        retval = MUL_R(retval, (errcorr + x1));
-    }
+    retval = errcorr + (x1<<INTERP_BITS);
+
+    retval = DESCALE_SHIFT(retval, whole, POWTBL_BITS + INTERP_BITS - REAL_BITS);

     return retval;
 }

-int32_t pow2_int(real_t val)
+uint32_t pow2_int(real_t val)
 {
     uint32_t x1, x2;
     uint32_t errcorr;
     uint32_t index_frac;
-    real_t retval;
+    uint32_t retval;
     int32_t whole = (val >> REAL_BITS);

     /* rest = [0..1] */
@@ -356,24 +387,17 @@ int32_t pow2_int(real_t val)
     /* index into pow2_tab */
     int32_t index = rest >> (REAL_BITS-TABLE_BITS);

-
-    if (val == 0)
-        return 1;
-
     /* leave INTERP_BITS bits */
     index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
     index_frac = index_frac & ((1<<INTERP_BITS)-1);

-    if (whole > 0)
-        retval = 1 << whole;
-    else
-        retval = 0;
-
     x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
     x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
-    errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
+    errcorr = ( (index_frac*(x2-x1)));
+
+    retval = errcorr + (x1<<INTERP_BITS);

-    retval = MUL_R(retval, (errcorr + x1));
+    retval = DESCALE_SHIFT(retval, whole, POWTBL_BITS + INTERP_BITS);

     return retval;
 }
diff --git a/apps/codecs/libfaad/common.h b/apps/codecs/libfaad/common.h
index fe0d02b..658e92f 100644
--- a/apps/codecs/libfaad/common.h
+++ b/apps/codecs/libfaad/common.h
@@ -404,8 +404,8 @@ uint32_t random_int(void);
 #define LOG2_MIN_INF REAL_CONST(-10000)
 int32_t log2_int(uint32_t val);
 int32_t log2_fix(uint32_t val);
-int32_t pow2_int(real_t val);
-real_t pow2_fix(real_t val);
+uint32_t pow2_int(real_t val);
+uint32_t pow2_fix(real_t val);
 #endif
 uint8_t get_sr_index(const uint32_t samplerate);
 uint8_t max_pred_sfb(const uint8_t sr_index);
diff --git a/apps/codecs/libfaad/fixed.h b/apps/codecs/libfaad/fixed.h
index da895e8..9781a84 100644
--- a/apps/codecs/libfaad/fixed.h
+++ b/apps/codecs/libfaad/fixed.h
@@ -49,10 +49,11 @@ extern "C" {

 typedef int32_t real_t;

-
-#define REAL_CONST(A) (((A) >= 0) ? ((real_t)((A)*(REAL_PRECISION)+0.5)) : ((real_t)((A)*(REAL_PRECISION)-0.5)))
-#define COEF_CONST(A) (((A) >= 0) ? ((real_t)((A)*(COEF_PRECISION)+0.5)) : ((real_t)((A)*(COEF_PRECISION)-0.5)))
-#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))))
+#define UFIX_CONST(A,PRECISION) ((uint32_t)((A)*(PRECISION)+0.5))
+#define FIX_CONST(A,PRECISION) (((A) >= 0) ? ((real_t)((A)*(PRECISION)+0.5)) : ((real_t)((A)*(PRECISION)-0.5)))
+#define REAL_CONST(A) FIX_CONST((A),(REAL_PRECISION))
+#define COEF_CONST(A) FIX_CONST((A),(COEF_PRECISION))
+#define FRAC_CONST(A) (((A) == 1.00) ? ((real_t)FRAC_MAX) : FIX_CONST((A),(FRAC_PRECISION)))
 //#define FRAC_CONST(A) (((A) >= 0) ? ((real_t)((A)*(FRAC_PRECISION)+0.5)) : ((real_t)((A)*(FRAC_PRECISION)-0.5)))

 #define Q2_BITS 22
@@ -128,6 +129,8 @@ static INLINE void ComplexMult(real_t *y1, real_t *y2,
   #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
   #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
   #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
+  #define DESCALE(A,S) ((S)>0?(((A)>>((S)-1))+1)>>1:(A)<<-(S))
+  #define DESCALE_SHIFT(A,SH,SC) DESCALE((A),(SC)-(SH)

 #elif defined(__GNUC__) && defined (__arm__)

@@ -205,6 +208,18 @@ static INLINE void ComplexMult(real_t *y1, real_t *y2,
     *y2 = yt2 << (FRAC_SIZE-FRAC_BITS);
 }

+static inline real_t DESCALE_SHIFT(unsigned val, int shift, int scale)
+{
+    unsigned out;
+    if ((out = val >> (scale - shift - 1)))
+    {
+        out++;
+        out >>= 1;
+    } else
+        out = val << (shift - scale);
+    return out;
+}
+
 #else

   /* multiply with real shift */
@@ -225,6 +240,8 @@ static INLINE void ComplexMult(real_t *y1, real_t *y2,
   #define MUL_Q2(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (Q2_BITS-1))) >> Q2_BITS)
   #define MUL_SHIFT6(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (6-1))) >> 6)
   #define MUL_SHIFT23(A,B) (real_t)(((int64_t)(A)*(int64_t)(B)+(1 << (23-1))) >> 23)
+  #define DESCALE(A,S) ((S)>0?(((A)>>((S)-1))+1)>>1:(A)<<-(S))
+  #define DESCALE_SHIFT(A,SH,SC) DESCALE((A),(SC)-(SH)

 /* Complex multiplication */
 static INLINE void ComplexMult(real_t *y1, real_t *y2,