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
273
274
275
276
277
278
279
280
281
282
diff --git a/apps/codecs/lib/codeclib.c b/apps/codecs/lib/codeclib.c
index 1c624e0..329b0e2 100644
--- a/apps/codecs/lib/codeclib.c
+++ b/apps/codecs/lib/codeclib.c
@@ -139,7 +139,7 @@ void qsort(void *base, size_t nmemb, size_t size,
 }

 /* From ffmpeg - libavutil/common.h */
-const uint8_t ff_log2_tab[256] ICONST_ATTR = {
+const uint8_t bs_log2_tab[256] ICONST_ATTR = {
     0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
     5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
     6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
@@ -150,6 +150,17 @@ const uint8_t ff_log2_tab[256] ICONST_ATTR = {
     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
 };

+const uint8_t bs_clz_tab[256] ICONST_ATTR = {
+    8,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
+    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
+};
+
 #ifdef RB_PROFILE
 void __cyg_profile_func_enter(void *this_fn, void *call_site) {
 #ifdef CPU_COLDFIRE
diff --git a/apps/codecs/lib/codeclib.h b/apps/codecs/lib/codeclib.h
index 9c3624b..831cff7 100644
--- a/apps/codecs/lib/codeclib.h
+++ b/apps/codecs/lib/codeclib.h
@@ -76,44 +76,79 @@ unsigned udiv32_arm(unsigned a, unsigned b);

 /* TODO figure out if we really need to care about calculating
    av_log2(0) */
-#if defined(CPU_ARM) && ARM_ARCH >= 6
-static inline unsigned int av_log2(uint32_t v)
-{
-    unsigned int r;
-    asm volatile("clz %[r], %[v]\n\t" /* count leading zeroes */
-                 "rsb %[r], %[r], #31\n\t" /* r = 31 - leading zeroes */
-                 "usat %[r], #5, %[r]\n\t" /* unsigned saturate r so -1 -> 0 */
-                 :[r] "=r" (r) : [v] "r" (v));
-    return(r);
-}
-#elif defined(CPU_ARM) && ARM_ARCH >= 5
-static inline unsigned int av_log2(uint32_t v)
-{
-    return v ? 31 - __builtin_clz(v) : 0;
-}
-#else /* CPU_ARM */
+#if !defined(CPU_ARM) || ARM_ARCH < 5
 /* From libavutil/common.h */
-extern const uint8_t ff_log2_tab[256] ICONST_ATTR;
+extern const uint8_t bs_log2_tab[256] ICONST_ATTR;
+extern const uint8_t bs_clz_tab[256] ICONST_ATTR;
+#endif
+
+#define BS_CLZ   1 /* TODO */
+#define BS_SHORT 2
+#define BS_0_0   4

-static inline unsigned int av_log2(unsigned int v)
-{
-    int n;

-    n = 0;
-    if (v & 0xffff0000) {
-        v >>= 16;
-        n += 16;
+static inline unsigned int bs_generic(unsigned int v, int mode)
+{
+#if defined(CPU_ARM) && ARM_ARCH >= 5
+    unsigned int r = __builtin_clz(v);
+    if (mode & BS_CLZ)
+    {
+        if (mode & BS_0_0)
+            r &= 31;
+    } else {
+        r = 31 - r;
+#if ARM_ARCH >= 6
+        if (mode & BS_0_0)
+            asm volatile(
+                "usat %0, #5, %0"
+                : "+r" (r)
+            );
+#else
+        if (mode & BS_0_0 && v == 0)
+            r = 0;
     }
-    if (v & 0xff00) {
-        v >>= 8;
-        n += 8;
+#endif
+#else
+    const uint8_t *bs_tab;
+    unsigned int r;
+    unsigned int n = v;
+    int inc;
+    if (mode & BS_CLZ)
+    {
+        bs_tab = bs_clz_tab;
+        r = 24;
+        inc = -16;
+    } else {
+        bs_tab = bs_log2_tab;
+        r = 0;
+        inc = 16;
     }
-    n += ff_log2_tab[v];
-
-    return n;
+    if (!(mode & BS_SHORT) && n >= 0x10000) {
+        n >>= 16;
+        r += inc;
+    }
+    if (n > 0xff) {
+        n >>= 8;
+        r += inc / 2;
+    }
+#ifdef CPU_COLDFIRE
+    asm volatile (
+        "move.b (%1,%0.l),%0"
+        : "+d" (n)
+        : "a" (bs_tab)
+    );
+    r += n;
+#else
+    r += bs_tab[n];
+#endif
+    if (mode & BS_CLZ && mode & BS_0_0 && v == 0)
+        r = 0;
+    return r;
 }
 #endif

+#define av_log2(v) bs_generic(v, BS_0_0)
+
 /* Various codec helper functions */

 int codec_init(void);
diff --git a/apps/codecs/libalac/alac.c b/apps/codecs/libalac/alac.c
index f94ff0f..1f7867b 100644
--- a/apps/codecs/libalac/alac.c
+++ b/apps/codecs/libalac/alac.c
@@ -166,46 +166,7 @@ static inline void unreadbits(alac_file *alac, int bits)
         alac->input_buffer_bitaccumulator *= -1;
 }

-/* ARMv5+ has a clz instruction equivalent to our function.
- */
-#if (defined(CPU_ARM) && (ARM_ARCH > 4))
-static inline int count_leading_zeros(uint32_t v)
-{
-    return __builtin_clz(v);
-}
-#else
-
-static const unsigned char bittab[16] ICONST_ATTR = {
-    0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
-};
-
-static inline int count_leading_zeros(int input)
-{
-    int output = 32;
-
-#if 0
-    /* Experimentation has shown that the following test is always false,
-       so we don't bother to perform it. */
-    if (input & 0xffff0000)
-    {
-        input >>= 16;
-        output -= 16;
-    }
-#endif
-    if (input & 0xff00)
-    {
-        input >>= 8;
-        output -= 8;
-    }
-    if (input & 0xf0)
-    {
-        input >>= 4;
-        output -= 4;
-    }
-    output -= bittab[input];
-    return output;
-}
-#endif
+#define count_leading_zeros(x) bs_generic(x, BS_CLZ|BS_SHORT)

 void basterdised_rice_decompress(alac_file *alac,
                                  int32_t *output_buffer,
diff --git a/apps/codecs/libfaad/common.c b/apps/codecs/libfaad/common.c
index debc125..0e7a5cd 100644
--- a/apps/codecs/libfaad/common.c
+++ b/apps/codecs/libfaad/common.c
@@ -251,7 +251,7 @@ uint32_t ones32(uint32_t x)

     return (x & 0x0000003f);
 }
-
+/*
 uint32_t floor_log2(uint32_t x)
 {
 #if 1
@@ -271,9 +271,12 @@ uint32_t floor_log2(uint32_t x)
     return count;
 #endif
 }
+*/
+#define floor_log2(x) bs_generic(x, 0)

 /* returns position of first bit that is not 0 from msb,
  * starting count at lsb */
+/*
 uint32_t wl_min_lzc(uint32_t x)
 {
 #if 1
@@ -293,6 +296,7 @@ uint32_t wl_min_lzc(uint32_t x)
     return (count + 1);
 #endif
 }
+*/

 #ifdef FIXED_POINT

diff --git a/apps/codecs/libfaad/common.h b/apps/codecs/libfaad/common.h
index ea028b1..910906b 100644
--- a/apps/codecs/libfaad/common.h
+++ b/apps/codecs/libfaad/common.h
@@ -400,8 +400,8 @@ typedef real_t complex_t[2];
 uint8_t cpu_has_sse(void);
 uint32_t random_int(void);
 uint32_t ones32(uint32_t x);
-uint32_t floor_log2(uint32_t x);
-uint32_t wl_min_lzc(uint32_t x);
+//uint32_t wl_min_lzc(uint32_t x);
+#define wl_min_lzc(x) bs_generic(x, BS_0_0)
 #ifdef FIXED_POINT
 #define LOG2_MIN_INF REAL_CONST(-10000)
 int32_t log2_int(uint32_t val);
diff --git a/apps/codecs/libfaad/sbr_hfgen.c b/apps/codecs/libfaad/sbr_hfgen.c
index 4991839..f77bbd0 100644
--- a/apps/codecs/libfaad/sbr_hfgen.c
+++ b/apps/codecs/libfaad/sbr_hfgen.c
@@ -222,10 +222,6 @@ static void auto_correlation(sbr_info *sbr, acorr_coef *ac,

     exp = wl_min_lzc(mask);

-    /* improves accuracy */
-    if (exp > 0)
-        exp -= 1;
-
     for (j = offset; j < len + offset; j++)
     {
         real_t buf_j = ((QMF_RE(buffer[j][bd])+(1<<(exp-1)))>>exp);
@@ -292,10 +288,6 @@ static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTS
     }

     exp = wl_min_lzc(mask);
-
-    /* improves accuracy */
-    if (exp > 0)
-        exp -= 1;

     pow2_to_exp = 1<<(exp-1);

diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index 4b60562..0f346a7 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -1,4 +1,5 @@
 /* plugins common to all models */
+test_codec.c
 chessclock.c
 credits.c
 cube.c