summaryrefslogtreecommitdiff
path: root/apps/codecs/lib
diff options
context:
space:
mode:
authorAndrew Mahone <andrew.mahone@gmail.com>2009-12-09 02:24:45 +0000
committerAndrew Mahone <andrew.mahone@gmail.com>2009-12-09 02:24:45 +0000
commit85aad9b3972208b0e34ba0241ebb5314118ae05e (patch)
tree27724c068f90b517d4bf9be6ed78d34a01eeba9b /apps/codecs/lib
parent3683bb67db4d5d59a55aabed6eaed72233323ee7 (diff)
downloadrockbox-85aad9b3972208b0e34ba0241ebb5314118ae05e.tar.gz
rockbox-85aad9b3972208b0e34ba0241ebb5314118ae05e.zip
Extend av_log2 in codeclib into a generic for scanning for set bits, which can provide either log2 or leading-zero-count output, and can force mapping 0 input to 0 output if needed (otherwise 0 input produces undefined result). Replace av_log2 in lib/codeclib.h, floor_log2 and wl_min_lzc in libfaad/common.c and common.h, and count_leading_zeros in libalac/alac.c with macros using bs_generic.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23903 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/lib')
-rw-r--r--apps/codecs/lib/codeclib.c22
-rw-r--r--apps/codecs/lib/codeclib.h101
2 files changed, 89 insertions, 34 deletions
diff --git a/apps/codecs/lib/codeclib.c b/apps/codecs/lib/codeclib.c
index 1c624e0f8c..3a137068ba 100644
--- a/apps/codecs/lib/codeclib.c
+++ b/apps/codecs/lib/codeclib.c
@@ -33,6 +33,15 @@ unsigned char* mp3buf; // The actual MP3 buffer from Rockbox
33unsigned char* mallocbuf; // 512K from the start of MP3 buffer 33unsigned char* mallocbuf; // 512K from the start of MP3 buffer
34unsigned char* filebuf; // The rest of the MP3 buffer 34unsigned char* filebuf; // The rest of the MP3 buffer
35 35
36unsigned bs_log2(unsigned x)
37{ return bs_generic(x, BS_LOG2); }
38unsigned bs_log2_0(unsigned x)
39{ return bs_generic(x, BS_LOG2|BS_0_0); }
40unsigned bs_clz(unsigned x)
41{ return bs_generic(x, BS_CLZ); }
42unsigned bs_clz_0(unsigned x)
43{ return bs_generic(x, BS_CLZ|BS_0_0); }
44
36int codec_init(void) 45int codec_init(void)
37{ 46{
38 mem_ptr = 0; 47 mem_ptr = 0;
@@ -139,7 +148,7 @@ void qsort(void *base, size_t nmemb, size_t size,
139} 148}
140 149
141/* From ffmpeg - libavutil/common.h */ 150/* From ffmpeg - libavutil/common.h */
142const uint8_t ff_log2_tab[256] ICONST_ATTR = { 151const uint8_t bs_log2_tab[256] ICONST_ATTR = {
143 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, 152 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,
144 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, 153 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,
145 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, 154 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 +159,17 @@ const uint8_t ff_log2_tab[256] ICONST_ATTR = {
150 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 159 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
151}; 160};
152 161
162const uint8_t bs_clz_tab[256] ICONST_ATTR = {
163 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,
164 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,
165 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,
166 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,
167 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,
168 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,
169 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,
170 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
171};
172
153#ifdef RB_PROFILE 173#ifdef RB_PROFILE
154void __cyg_profile_func_enter(void *this_fn, void *call_site) { 174void __cyg_profile_func_enter(void *this_fn, void *call_site) {
155#ifdef CPU_COLDFIRE 175#ifdef CPU_COLDFIRE
diff --git a/apps/codecs/lib/codeclib.h b/apps/codecs/lib/codeclib.h
index 9c3624b422..aeae5d6369 100644
--- a/apps/codecs/lib/codeclib.h
+++ b/apps/codecs/lib/codeclib.h
@@ -74,45 +74,80 @@ unsigned udiv32_arm(unsigned a, unsigned b);
74#define UDIV32(a, b) (a / b) 74#define UDIV32(a, b) (a / b)
75#endif 75#endif
76 76
77/* TODO figure out if we really need to care about calculating 77#if !defined(CPU_ARM) || ARM_ARCH < 5
78 av_log2(0) */
79#if defined(CPU_ARM) && ARM_ARCH >= 6
80static inline unsigned int av_log2(uint32_t v)
81{
82 unsigned int r;
83 asm volatile("clz %[r], %[v]\n\t" /* count leading zeroes */
84 "rsb %[r], %[r], #31\n\t" /* r = 31 - leading zeroes */
85 "usat %[r], #5, %[r]\n\t" /* unsigned saturate r so -1 -> 0 */
86 :[r] "=r" (r) : [v] "r" (v));
87 return(r);
88}
89#elif defined(CPU_ARM) && ARM_ARCH >= 5
90static inline unsigned int av_log2(uint32_t v)
91{
92 return v ? 31 - __builtin_clz(v) : 0;
93}
94#else /* CPU_ARM */
95/* From libavutil/common.h */ 78/* From libavutil/common.h */
96extern const uint8_t ff_log2_tab[256] ICONST_ATTR; 79extern const uint8_t bs_log2_tab[256] ICONST_ATTR;
80extern const uint8_t bs_clz_tab[256] ICONST_ATTR;
81#endif
97 82
98static inline unsigned int av_log2(unsigned int v) 83#define BS_LOG2 0 /* default personality, equivalent floor(log2(x)) */
99{ 84#define BS_CLZ 1 /* alternate personality, Count Leading Zeros */
100 int n; 85#define BS_SHORT 2 /* input guaranteed not to exceed 16 bits */
86#define BS_0_0 4 /* guarantee mapping of 0 input to 0 output */
101 87
102 n = 0; 88/* Generic bit-scanning function, used to wrap platform CLZ instruction or
103 if (v & 0xffff0000) { 89 scan-and-lookup code, and to provide control over output for 0 inputs. */
104 v >>= 16; 90static inline unsigned int bs_generic(unsigned int v, int mode)
105 n += 16; 91{
92#if defined(CPU_ARM) && ARM_ARCH >= 5
93 unsigned int r = __builtin_clz(v);
94 if (mode & BS_CLZ)
95 {
96 if (mode & BS_0_0)
97 r &= 31;
98 } else {
99 r = 31 - r;
100 /* If mode is constant, this is a single conditional instruction */
101 if (mode & BS_0_0 && (signed)r < 0)
102 r += 1;
103 }
104#else
105 const uint8_t *bs_tab;
106 unsigned int r;
107 unsigned int n = v;
108 int inc;
109 /* Set up table, increment, and initial result value based on
110 personality. */
111 if (mode & BS_CLZ)
112 {
113 bs_tab = bs_clz_tab;
114 r = 24;
115 inc = -16;
116 } else {
117 bs_tab = bs_log2_tab;
118 r = 0;
119 inc = 16;
106 } 120 }
107 if (v & 0xff00) { 121 if (!(mode & BS_SHORT) && n >= 0x10000) {
108 v >>= 8; 122 n >>= 16;
109 n += 8; 123 r += inc;
110 } 124 }
111 n += ff_log2_tab[v]; 125 if (n > 0xff) {
112 126 n >>= 8;
113 return n; 127 r += inc / 2;
114} 128 }
129#ifdef CPU_COLDFIRE
130 /* The high 24 bits of n are guaranteed empty after the above, so a
131 superfluous ext.b instruction can be saved by loading the LUT value over
132 n with asm */
133 asm volatile (
134 "move.b (%1,%0.l),%0"
135 : "+d" (n)
136 : "a" (bs_tab)
137 );
138#else
139 n = bs_tab[n];
115#endif 140#endif
141 r += n;
142 if (mode & BS_CLZ && mode & BS_0_0 && v == 0)
143 r = 0;
144#endif
145 return r;
146}
147
148/* TODO figure out if we really need to care about calculating
149 av_log2(0) */
150#define av_log2(v) bs_generic(v, BS_0_0)
116 151
117/* Various codec helper functions */ 152/* Various codec helper functions */
118 153