MINI2440: Auto probe for SDRAM size
[u-boot-openmoko/mini2440.git] / lib_generic / bzlib_huffman.c
blobeffae98a733d1aba691c5aa66104afe214167fca
1 #include <config.h>
2 #ifdef CONFIG_BZIP2
4 /*-------------------------------------------------------------*/
5 /*--- Huffman coding low-level stuff ---*/
6 /*--- huffman.c ---*/
7 /*-------------------------------------------------------------*/
9 /*--
10 This file is a part of bzip2 and/or libbzip2, a program and
11 library for lossless, block-sorting data compression.
13 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 2. The origin of this software must not be misrepresented; you must
23 not claim that you wrote the original software. If you use this
24 software in a product, an acknowledgment in the product
25 documentation would be appreciated but is not required.
27 3. Altered source versions must be plainly marked as such, and must
28 not be misrepresented as being the original software.
30 4. The name of the author may not be used to endorse or promote
31 products derived from this software without specific prior written
32 permission.
34 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
35 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
38 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 Julian Seward, Cambridge, UK.
47 jseward@acm.org
48 bzip2/libbzip2 version 1.0 of 21 March 2000
50 This program is based on (at least) the work of:
51 Mike Burrows
52 David Wheeler
53 Peter Fenwick
54 Alistair Moffat
55 Radford Neal
56 Ian H. Witten
57 Robert Sedgewick
58 Jon L. Bentley
60 For more information on these sources, see the manual.
61 --*/
64 #include "bzlib_private.h"
66 /*---------------------------------------------------*/
67 #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
68 #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
69 #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
71 #define ADDWEIGHTS(zw1,zw2) \
72 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
73 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
75 #define UPHEAP(z) \
76 { \
77 Int32 zz, tmp; \
78 zz = z; tmp = heap[zz]; \
79 while (weight[tmp] < weight[heap[zz >> 1]]) { \
80 heap[zz] = heap[zz >> 1]; \
81 zz >>= 1; \
82 } \
83 heap[zz] = tmp; \
86 #define DOWNHEAP(z) \
87 { \
88 Int32 zz, yy, tmp; \
89 zz = z; tmp = heap[zz]; \
90 while (True) { \
91 yy = zz << 1; \
92 if (yy > nHeap) break; \
93 if (yy < nHeap && \
94 weight[heap[yy+1]] < weight[heap[yy]]) \
95 yy++; \
96 if (weight[tmp] < weight[heap[yy]]) break; \
97 heap[zz] = heap[yy]; \
98 zz = yy; \
99 } \
100 heap[zz] = tmp; \
104 /*---------------------------------------------------*/
105 void BZ2_hbMakeCodeLengths ( UChar *len,
106 Int32 *freq,
107 Int32 alphaSize,
108 Int32 maxLen )
110 /*--
111 Nodes and heap entries run from 1. Entry 0
112 for both the heap and nodes is a sentinel.
113 --*/
114 Int32 nNodes, nHeap, n1, n2, i, j, k;
115 Bool tooLong;
117 Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
118 Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
119 Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
121 for (i = 0; i < alphaSize; i++)
122 weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
124 while (True) {
126 nNodes = alphaSize;
127 nHeap = 0;
129 heap[0] = 0;
130 weight[0] = 0;
131 parent[0] = -2;
133 for (i = 1; i <= alphaSize; i++) {
134 parent[i] = -1;
135 nHeap++;
136 heap[nHeap] = i;
137 UPHEAP(nHeap);
140 AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
142 while (nHeap > 1) {
143 n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
144 n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
145 nNodes++;
146 parent[n1] = parent[n2] = nNodes;
147 weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
148 parent[nNodes] = -1;
149 nHeap++;
150 heap[nHeap] = nNodes;
151 UPHEAP(nHeap);
154 AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
156 tooLong = False;
157 for (i = 1; i <= alphaSize; i++) {
158 j = 0;
159 k = i;
160 while (parent[k] >= 0) { k = parent[k]; j++; }
161 len[i-1] = j;
162 if (j > maxLen) tooLong = True;
165 if (! tooLong) break;
167 for (i = 1; i < alphaSize; i++) {
168 j = weight[i] >> 8;
169 j = 1 + (j / 2);
170 weight[i] = j << 8;
176 /*---------------------------------------------------*/
177 void BZ2_hbAssignCodes ( Int32 *code,
178 UChar *length,
179 Int32 minLen,
180 Int32 maxLen,
181 Int32 alphaSize )
183 Int32 n, vec, i;
185 vec = 0;
186 for (n = minLen; n <= maxLen; n++) {
187 for (i = 0; i < alphaSize; i++)
188 if (length[i] == n) { code[i] = vec; vec++; };
189 vec <<= 1;
194 /*---------------------------------------------------*/
195 void BZ2_hbCreateDecodeTables ( Int32 *limit,
196 Int32 *base,
197 Int32 *perm,
198 UChar *length,
199 Int32 minLen,
200 Int32 maxLen,
201 Int32 alphaSize )
203 Int32 pp, i, j, vec;
205 pp = 0;
206 for (i = minLen; i <= maxLen; i++)
207 for (j = 0; j < alphaSize; j++)
208 if (length[j] == i) { perm[pp] = j; pp++; };
210 for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
211 for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
213 for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
215 for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
216 vec = 0;
218 for (i = minLen; i <= maxLen; i++) {
219 vec += (base[i+1] - base[i]);
220 limit[i] = vec-1;
221 vec <<= 1;
223 for (i = minLen + 1; i <= maxLen; i++)
224 base[i] = ((limit[i-1] + 1) << 1) - base[i];
228 /*-------------------------------------------------------------*/
229 /*--- end huffman.c ---*/
230 /*-------------------------------------------------------------*/
232 #endif /* CONFIG_BZIP2 */