annotate zlib/examples/enough.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* enough.c -- determine the maximum size of inflate's Huffman code tables over
kono
parents:
diff changeset
2 * all possible valid and complete Huffman codes, subject to a length limit.
kono
parents:
diff changeset
3 * Copyright (C) 2007, 2008, 2012 Mark Adler
kono
parents:
diff changeset
4 * Version 1.4 18 August 2012 Mark Adler
kono
parents:
diff changeset
5 */
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 /* Version history:
kono
parents:
diff changeset
8 1.0 3 Jan 2007 First version (derived from codecount.c version 1.4)
kono
parents:
diff changeset
9 1.1 4 Jan 2007 Use faster incremental table usage computation
kono
parents:
diff changeset
10 Prune examine() search on previously visited states
kono
parents:
diff changeset
11 1.2 5 Jan 2007 Comments clean up
kono
parents:
diff changeset
12 As inflate does, decrease root for short codes
kono
parents:
diff changeset
13 Refuse cases where inflate would increase root
kono
parents:
diff changeset
14 1.3 17 Feb 2008 Add argument for initial root table size
kono
parents:
diff changeset
15 Fix bug for initial root table size == max - 1
kono
parents:
diff changeset
16 Use a macro to compute the history index
kono
parents:
diff changeset
17 1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
kono
parents:
diff changeset
18 Clean up comparisons of different types
kono
parents:
diff changeset
19 Clean up code indentation
kono
parents:
diff changeset
20 */
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 /*
kono
parents:
diff changeset
23 Examine all possible Huffman codes for a given number of symbols and a
kono
parents:
diff changeset
24 maximum code length in bits to determine the maximum table size for zilb's
kono
parents:
diff changeset
25 inflate. Only complete Huffman codes are counted.
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 Two codes are considered distinct if the vectors of the number of codes per
kono
parents:
diff changeset
28 length are not identical. So permutations of the symbol assignments result
kono
parents:
diff changeset
29 in the same code for the counting, as do permutations of the assignments of
kono
parents:
diff changeset
30 the bit values to the codes (i.e. only canonical codes are counted).
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 We build a code from shorter to longer lengths, determining how many symbols
kono
parents:
diff changeset
33 are coded at each length. At each step, we have how many symbols remain to
kono
parents:
diff changeset
34 be coded, what the last code length used was, and how many bit patterns of
kono
parents:
diff changeset
35 that length remain unused. Then we add one to the code length and double the
kono
parents:
diff changeset
36 number of unused patterns to graduate to the next code length. We then
kono
parents:
diff changeset
37 assign all portions of the remaining symbols to that code length that
kono
parents:
diff changeset
38 preserve the properties of a correct and eventually complete code. Those
kono
parents:
diff changeset
39 properties are: we cannot use more bit patterns than are available; and when
kono
parents:
diff changeset
40 all the symbols are used, there are exactly zero possible bit patterns
kono
parents:
diff changeset
41 remaining.
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 The inflate Huffman decoding algorithm uses two-level lookup tables for
kono
parents:
diff changeset
44 speed. There is a single first-level table to decode codes up to root bits
kono
parents:
diff changeset
45 in length (root == 9 in the current inflate implementation). The table
kono
parents:
diff changeset
46 has 1 << root entries and is indexed by the next root bits of input. Codes
kono
parents:
diff changeset
47 shorter than root bits have replicated table entries, so that the correct
kono
parents:
diff changeset
48 entry is pointed to regardless of the bits that follow the short code. If
kono
parents:
diff changeset
49 the code is longer than root bits, then the table entry points to a second-
kono
parents:
diff changeset
50 level table. The size of that table is determined by the longest code with
kono
parents:
diff changeset
51 that root-bit prefix. If that longest code has length len, then the table
kono
parents:
diff changeset
52 has size 1 << (len - root), to index the remaining bits in that set of
kono
parents:
diff changeset
53 codes. Each subsequent root-bit prefix then has its own sub-table. The
kono
parents:
diff changeset
54 total number of table entries required by the code is calculated
kono
parents:
diff changeset
55 incrementally as the number of codes at each bit length is populated. When
kono
parents:
diff changeset
56 all of the codes are shorter than root bits, then root is reduced to the
kono
parents:
diff changeset
57 longest code length, resulting in a single, smaller, one-level table.
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 The inflate algorithm also provides for small values of root (relative to
kono
parents:
diff changeset
60 the log2 of the number of symbols), where the shortest code has more bits
kono
parents:
diff changeset
61 than root. In that case, root is increased to the length of the shortest
kono
parents:
diff changeset
62 code. This program, by design, does not handle that case, so it is verified
kono
parents:
diff changeset
63 that the number of symbols is less than 2^(root + 1).
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 In order to speed up the examination (by about ten orders of magnitude for
kono
parents:
diff changeset
66 the default arguments), the intermediate states in the build-up of a code
kono
parents:
diff changeset
67 are remembered and previously visited branches are pruned. The memory
kono
parents:
diff changeset
68 required for this will increase rapidly with the total number of symbols and
kono
parents:
diff changeset
69 the maximum code length in bits. However this is a very small price to pay
kono
parents:
diff changeset
70 for the vast speedup.
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 First, all of the possible Huffman codes are counted, and reachable
kono
parents:
diff changeset
73 intermediate states are noted by a non-zero count in a saved-results array.
kono
parents:
diff changeset
74 Second, the intermediate states that lead to (root + 1) bit or longer codes
kono
parents:
diff changeset
75 are used to look at all sub-codes from those junctures for their inflate
kono
parents:
diff changeset
76 memory usage. (The amount of memory used is not affected by the number of
kono
parents:
diff changeset
77 codes of root bits or less in length.) Third, the visited states in the
kono
parents:
diff changeset
78 construction of those sub-codes and the associated calculation of the table
kono
parents:
diff changeset
79 size is recalled in order to avoid recalculating from the same juncture.
kono
parents:
diff changeset
80 Beginning the code examination at (root + 1) bit codes, which is enabled by
kono
parents:
diff changeset
81 identifying the reachable nodes, accounts for about six of the orders of
kono
parents:
diff changeset
82 magnitude of improvement for the default arguments. About another four
kono
parents:
diff changeset
83 orders of magnitude come from not revisiting previous states. Out of
kono
parents:
diff changeset
84 approximately 2x10^16 possible Huffman codes, only about 2x10^6 sub-codes
kono
parents:
diff changeset
85 need to be examined to cover all of the possible table memory usage cases
kono
parents:
diff changeset
86 for the default arguments of 286 symbols limited to 15-bit codes.
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 Note that an unsigned long long type is used for counting. It is quite easy
kono
parents:
diff changeset
89 to exceed the capacity of an eight-byte integer with a large number of
kono
parents:
diff changeset
90 symbols and a large maximum code length, so multiple-precision arithmetic
kono
parents:
diff changeset
91 would need to replace the unsigned long long arithmetic in that case. This
kono
parents:
diff changeset
92 program will abort if an overflow occurs. The big_t type identifies where
kono
parents:
diff changeset
93 the counting takes place.
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 An unsigned long long type is also used for calculating the number of
kono
parents:
diff changeset
96 possible codes remaining at the maximum length. This limits the maximum
kono
parents:
diff changeset
97 code length to the number of bits in a long long minus the number of bits
kono
parents:
diff changeset
98 needed to represent the symbols in a flat code. The code_t type identifies
kono
parents:
diff changeset
99 where the bit pattern counting takes place.
kono
parents:
diff changeset
100 */
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 #include <stdio.h>
kono
parents:
diff changeset
103 #include <stdlib.h>
kono
parents:
diff changeset
104 #include <string.h>
kono
parents:
diff changeset
105 #include <assert.h>
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 #define local static
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 /* special data types */
kono
parents:
diff changeset
110 typedef unsigned long long big_t; /* type for code counting */
kono
parents:
diff changeset
111 typedef unsigned long long code_t; /* type for bit pattern counting */
kono
parents:
diff changeset
112 struct tab { /* type for been here check */
kono
parents:
diff changeset
113 size_t len; /* length of bit vector in char's */
kono
parents:
diff changeset
114 char *vec; /* allocated bit vector */
kono
parents:
diff changeset
115 };
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 /* The array for saving results, num[], is indexed with this triplet:
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 syms: number of symbols remaining to code
kono
parents:
diff changeset
120 left: number of available bit patterns at length len
kono
parents:
diff changeset
121 len: number of bits in the codes currently being assigned
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 Those indices are constrained thusly when saving results:
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 syms: 3..totsym (totsym == total symbols to code)
kono
parents:
diff changeset
126 left: 2..syms - 1, but only the evens (so syms == 8 -> 2, 4, 6)
kono
parents:
diff changeset
127 len: 1..max - 1 (max == maximum code length in bits)
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 syms == 2 is not saved since that immediately leads to a single code. left
kono
parents:
diff changeset
130 must be even, since it represents the number of available bit patterns at
kono
parents:
diff changeset
131 the current length, which is double the number at the previous length.
kono
parents:
diff changeset
132 left ends at syms-1 since left == syms immediately results in a single code.
kono
parents:
diff changeset
133 (left > sym is not allowed since that would result in an incomplete code.)
kono
parents:
diff changeset
134 len is less than max, since the code completes immediately when len == max.
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 The offset into the array is calculated for the three indices with the
kono
parents:
diff changeset
137 first one (syms) being outermost, and the last one (len) being innermost.
kono
parents:
diff changeset
138 We build the array with length max-1 lists for the len index, with syms-3
kono
parents:
diff changeset
139 of those for each symbol. There are totsym-2 of those, with each one
kono
parents:
diff changeset
140 varying in length as a function of sym. See the calculation of index in
kono
parents:
diff changeset
141 count() for the index, and the calculation of size in main() for the size
kono
parents:
diff changeset
142 of the array.
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 For the deflate example of 286 symbols limited to 15-bit codes, the array
kono
parents:
diff changeset
145 has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than
kono
parents:
diff changeset
146 half of the space allocated for saved results is actually used -- not all
kono
parents:
diff changeset
147 possible triplets are reached in the generation of valid Huffman codes.
kono
parents:
diff changeset
148 */
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 /* The array for tracking visited states, done[], is itself indexed identically
kono
parents:
diff changeset
151 to the num[] array as described above for the (syms, left, len) triplet.
kono
parents:
diff changeset
152 Each element in the array is further indexed by the (mem, rem) doublet,
kono
parents:
diff changeset
153 where mem is the amount of inflate table space used so far, and rem is the
kono
parents:
diff changeset
154 remaining unused entries in the current inflate sub-table. Each indexed
kono
parents:
diff changeset
155 element is simply one bit indicating whether the state has been visited or
kono
parents:
diff changeset
156 not. Since the ranges for mem and rem are not known a priori, each bit
kono
parents:
diff changeset
157 vector is of a variable size, and grows as needed to accommodate the visited
kono
parents:
diff changeset
158 states. mem and rem are used to calculate a single index in a triangular
kono
parents:
diff changeset
159 array. Since the range of mem is expected in the default case to be about
kono
parents:
diff changeset
160 ten times larger than the range of rem, the array is skewed to reduce the
kono
parents:
diff changeset
161 memory usage, with eight times the range for mem than for rem. See the
kono
parents:
diff changeset
162 calculations for offset and bit in beenhere() for the details.
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 For the deflate example of 286 symbols limited to 15-bit codes, the bit
kono
parents:
diff changeset
165 vectors grow to total approximately 21 MB, in addition to the 4.3 MB done[]
kono
parents:
diff changeset
166 array itself.
kono
parents:
diff changeset
167 */
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 /* Globals to avoid propagating constants or constant pointers recursively */
kono
parents:
diff changeset
170 local int max; /* maximum allowed bit length for the codes */
kono
parents:
diff changeset
171 local int root; /* size of base code table in bits */
kono
parents:
diff changeset
172 local int large; /* largest code table so far */
kono
parents:
diff changeset
173 local size_t size; /* number of elements in num and done */
kono
parents:
diff changeset
174 local int *code; /* number of symbols assigned to each bit length */
kono
parents:
diff changeset
175 local big_t *num; /* saved results array for code counting */
kono
parents:
diff changeset
176 local struct tab *done; /* states already evaluated array */
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 /* Index function for num[] and done[] */
kono
parents:
diff changeset
179 #define INDEX(i,j,k) (((size_t)((i-1)>>1)*((i-2)>>1)+(j>>1)-1)*(max-1)+k-1)
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 /* Free allocated space. Uses globals code, num, and done. */
kono
parents:
diff changeset
182 local void cleanup(void)
kono
parents:
diff changeset
183 {
kono
parents:
diff changeset
184 size_t n;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 if (done != NULL) {
kono
parents:
diff changeset
187 for (n = 0; n < size; n++)
kono
parents:
diff changeset
188 if (done[n].len)
kono
parents:
diff changeset
189 free(done[n].vec);
kono
parents:
diff changeset
190 free(done);
kono
parents:
diff changeset
191 }
kono
parents:
diff changeset
192 if (num != NULL)
kono
parents:
diff changeset
193 free(num);
kono
parents:
diff changeset
194 if (code != NULL)
kono
parents:
diff changeset
195 free(code);
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 /* Return the number of possible Huffman codes using bit patterns of lengths
kono
parents:
diff changeset
199 len through max inclusive, coding syms symbols, with left bit patterns of
kono
parents:
diff changeset
200 length len unused -- return -1 if there is an overflow in the counting.
kono
parents:
diff changeset
201 Keep a record of previous results in num to prevent repeating the same
kono
parents:
diff changeset
202 calculation. Uses the globals max and num. */
kono
parents:
diff changeset
203 local big_t count(int syms, int len, int left)
kono
parents:
diff changeset
204 {
kono
parents:
diff changeset
205 big_t sum; /* number of possible codes from this juncture */
kono
parents:
diff changeset
206 big_t got; /* value returned from count() */
kono
parents:
diff changeset
207 int least; /* least number of syms to use at this juncture */
kono
parents:
diff changeset
208 int most; /* most number of syms to use at this juncture */
kono
parents:
diff changeset
209 int use; /* number of bit patterns to use in next call */
kono
parents:
diff changeset
210 size_t index; /* index of this case in *num */
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 /* see if only one possible code */
kono
parents:
diff changeset
213 if (syms == left)
kono
parents:
diff changeset
214 return 1;
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 /* note and verify the expected state */
kono
parents:
diff changeset
217 assert(syms > left && left > 0 && len < max);
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 /* see if we've done this one already */
kono
parents:
diff changeset
220 index = INDEX(syms, left, len);
kono
parents:
diff changeset
221 got = num[index];
kono
parents:
diff changeset
222 if (got)
kono
parents:
diff changeset
223 return got; /* we have -- return the saved result */
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 /* we need to use at least this many bit patterns so that the code won't be
kono
parents:
diff changeset
226 incomplete at the next length (more bit patterns than symbols) */
kono
parents:
diff changeset
227 least = (left << 1) - syms;
kono
parents:
diff changeset
228 if (least < 0)
kono
parents:
diff changeset
229 least = 0;
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 /* we can use at most this many bit patterns, lest there not be enough
kono
parents:
diff changeset
232 available for the remaining symbols at the maximum length (if there were
kono
parents:
diff changeset
233 no limit to the code length, this would become: most = left - 1) */
kono
parents:
diff changeset
234 most = (((code_t)left << (max - len)) - syms) /
kono
parents:
diff changeset
235 (((code_t)1 << (max - len)) - 1);
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 /* count all possible codes from this juncture and add them up */
kono
parents:
diff changeset
238 sum = 0;
kono
parents:
diff changeset
239 for (use = least; use <= most; use++) {
kono
parents:
diff changeset
240 got = count(syms - use, len + 1, (left - use) << 1);
kono
parents:
diff changeset
241 sum += got;
kono
parents:
diff changeset
242 if (got == (big_t)0 - 1 || sum < got) /* overflow */
kono
parents:
diff changeset
243 return (big_t)0 - 1;
kono
parents:
diff changeset
244 }
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* verify that all recursive calls are productive */
kono
parents:
diff changeset
247 assert(sum != 0);
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 /* save the result and return it */
kono
parents:
diff changeset
250 num[index] = sum;
kono
parents:
diff changeset
251 return sum;
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 /* Return true if we've been here before, set to true if not. Set a bit in a
kono
parents:
diff changeset
255 bit vector to indicate visiting this state. Each (syms,len,left) state
kono
parents:
diff changeset
256 has a variable size bit vector indexed by (mem,rem). The bit vector is
kono
parents:
diff changeset
257 lengthened if needed to allow setting the (mem,rem) bit. */
kono
parents:
diff changeset
258 local int beenhere(int syms, int len, int left, int mem, int rem)
kono
parents:
diff changeset
259 {
kono
parents:
diff changeset
260 size_t index; /* index for this state's bit vector */
kono
parents:
diff changeset
261 size_t offset; /* offset in this state's bit vector */
kono
parents:
diff changeset
262 int bit; /* mask for this state's bit */
kono
parents:
diff changeset
263 size_t length; /* length of the bit vector in bytes */
kono
parents:
diff changeset
264 char *vector; /* new or enlarged bit vector */
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 /* point to vector for (syms,left,len), bit in vector for (mem,rem) */
kono
parents:
diff changeset
267 index = INDEX(syms, left, len);
kono
parents:
diff changeset
268 mem -= 1 << root;
kono
parents:
diff changeset
269 offset = (mem >> 3) + rem;
kono
parents:
diff changeset
270 offset = ((offset * (offset + 1)) >> 1) + rem;
kono
parents:
diff changeset
271 bit = 1 << (mem & 7);
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 /* see if we've been here */
kono
parents:
diff changeset
274 length = done[index].len;
kono
parents:
diff changeset
275 if (offset < length && (done[index].vec[offset] & bit) != 0)
kono
parents:
diff changeset
276 return 1; /* done this! */
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 /* we haven't been here before -- set the bit to show we have now */
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 /* see if we need to lengthen the vector in order to set the bit */
kono
parents:
diff changeset
281 if (length <= offset) {
kono
parents:
diff changeset
282 /* if we have one already, enlarge it, zero out the appended space */
kono
parents:
diff changeset
283 if (length) {
kono
parents:
diff changeset
284 do {
kono
parents:
diff changeset
285 length <<= 1;
kono
parents:
diff changeset
286 } while (length <= offset);
kono
parents:
diff changeset
287 vector = realloc(done[index].vec, length);
kono
parents:
diff changeset
288 if (vector != NULL)
kono
parents:
diff changeset
289 memset(vector + done[index].len, 0, length - done[index].len);
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 /* otherwise we need to make a new vector and zero it out */
kono
parents:
diff changeset
293 else {
kono
parents:
diff changeset
294 length = 1 << (len - root);
kono
parents:
diff changeset
295 while (length <= offset)
kono
parents:
diff changeset
296 length <<= 1;
kono
parents:
diff changeset
297 vector = calloc(length, sizeof(char));
kono
parents:
diff changeset
298 }
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 /* in either case, bail if we can't get the memory */
kono
parents:
diff changeset
301 if (vector == NULL) {
kono
parents:
diff changeset
302 fputs("abort: unable to allocate enough memory\n", stderr);
kono
parents:
diff changeset
303 cleanup();
kono
parents:
diff changeset
304 exit(1);
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 /* install the new vector */
kono
parents:
diff changeset
308 done[index].len = length;
kono
parents:
diff changeset
309 done[index].vec = vector;
kono
parents:
diff changeset
310 }
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 /* set the bit */
kono
parents:
diff changeset
313 done[index].vec[offset] |= bit;
kono
parents:
diff changeset
314 return 0;
kono
parents:
diff changeset
315 }
kono
parents:
diff changeset
316
kono
parents:
diff changeset
317 /* Examine all possible codes from the given node (syms, len, left). Compute
kono
parents:
diff changeset
318 the amount of memory required to build inflate's decoding tables, where the
kono
parents:
diff changeset
319 number of code structures used so far is mem, and the number remaining in
kono
parents:
diff changeset
320 the current sub-table is rem. Uses the globals max, code, root, large, and
kono
parents:
diff changeset
321 done. */
kono
parents:
diff changeset
322 local void examine(int syms, int len, int left, int mem, int rem)
kono
parents:
diff changeset
323 {
kono
parents:
diff changeset
324 int least; /* least number of syms to use at this juncture */
kono
parents:
diff changeset
325 int most; /* most number of syms to use at this juncture */
kono
parents:
diff changeset
326 int use; /* number of bit patterns to use in next call */
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* see if we have a complete code */
kono
parents:
diff changeset
329 if (syms == left) {
kono
parents:
diff changeset
330 /* set the last code entry */
kono
parents:
diff changeset
331 code[len] = left;
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 /* complete computation of memory used by this code */
kono
parents:
diff changeset
334 while (rem < left) {
kono
parents:
diff changeset
335 left -= rem;
kono
parents:
diff changeset
336 rem = 1 << (len - root);
kono
parents:
diff changeset
337 mem += rem;
kono
parents:
diff changeset
338 }
kono
parents:
diff changeset
339 assert(rem == left);
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 /* if this is a new maximum, show the entries used and the sub-code */
kono
parents:
diff changeset
342 if (mem > large) {
kono
parents:
diff changeset
343 large = mem;
kono
parents:
diff changeset
344 printf("max %d: ", mem);
kono
parents:
diff changeset
345 for (use = root + 1; use <= max; use++)
kono
parents:
diff changeset
346 if (code[use])
kono
parents:
diff changeset
347 printf("%d[%d] ", code[use], use);
kono
parents:
diff changeset
348 putchar('\n');
kono
parents:
diff changeset
349 fflush(stdout);
kono
parents:
diff changeset
350 }
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 /* remove entries as we drop back down in the recursion */
kono
parents:
diff changeset
353 code[len] = 0;
kono
parents:
diff changeset
354 return;
kono
parents:
diff changeset
355 }
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 /* prune the tree if we can */
kono
parents:
diff changeset
358 if (beenhere(syms, len, left, mem, rem))
kono
parents:
diff changeset
359 return;
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 /* we need to use at least this many bit patterns so that the code won't be
kono
parents:
diff changeset
362 incomplete at the next length (more bit patterns than symbols) */
kono
parents:
diff changeset
363 least = (left << 1) - syms;
kono
parents:
diff changeset
364 if (least < 0)
kono
parents:
diff changeset
365 least = 0;
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 /* we can use at most this many bit patterns, lest there not be enough
kono
parents:
diff changeset
368 available for the remaining symbols at the maximum length (if there were
kono
parents:
diff changeset
369 no limit to the code length, this would become: most = left - 1) */
kono
parents:
diff changeset
370 most = (((code_t)left << (max - len)) - syms) /
kono
parents:
diff changeset
371 (((code_t)1 << (max - len)) - 1);
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 /* occupy least table spaces, creating new sub-tables as needed */
kono
parents:
diff changeset
374 use = least;
kono
parents:
diff changeset
375 while (rem < use) {
kono
parents:
diff changeset
376 use -= rem;
kono
parents:
diff changeset
377 rem = 1 << (len - root);
kono
parents:
diff changeset
378 mem += rem;
kono
parents:
diff changeset
379 }
kono
parents:
diff changeset
380 rem -= use;
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 /* examine codes from here, updating table space as we go */
kono
parents:
diff changeset
383 for (use = least; use <= most; use++) {
kono
parents:
diff changeset
384 code[len] = use;
kono
parents:
diff changeset
385 examine(syms - use, len + 1, (left - use) << 1,
kono
parents:
diff changeset
386 mem + (rem ? 1 << (len - root) : 0), rem << 1);
kono
parents:
diff changeset
387 if (rem == 0) {
kono
parents:
diff changeset
388 rem = 1 << (len - root);
kono
parents:
diff changeset
389 mem += rem;
kono
parents:
diff changeset
390 }
kono
parents:
diff changeset
391 rem--;
kono
parents:
diff changeset
392 }
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 /* remove entries as we drop back down in the recursion */
kono
parents:
diff changeset
395 code[len] = 0;
kono
parents:
diff changeset
396 }
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 /* Look at all sub-codes starting with root + 1 bits. Look at only the valid
kono
parents:
diff changeset
399 intermediate code states (syms, left, len). For each completed code,
kono
parents:
diff changeset
400 calculate the amount of memory required by inflate to build the decoding
kono
parents:
diff changeset
401 tables. Find the maximum amount of memory required and show the code that
kono
parents:
diff changeset
402 requires that maximum. Uses the globals max, root, and num. */
kono
parents:
diff changeset
403 local void enough(int syms)
kono
parents:
diff changeset
404 {
kono
parents:
diff changeset
405 int n; /* number of remaing symbols for this node */
kono
parents:
diff changeset
406 int left; /* number of unused bit patterns at this length */
kono
parents:
diff changeset
407 size_t index; /* index of this case in *num */
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 /* clear code */
kono
parents:
diff changeset
410 for (n = 0; n <= max; n++)
kono
parents:
diff changeset
411 code[n] = 0;
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 /* look at all (root + 1) bit and longer codes */
kono
parents:
diff changeset
414 large = 1 << root; /* base table */
kono
parents:
diff changeset
415 if (root < max) /* otherwise, there's only a base table */
kono
parents:
diff changeset
416 for (n = 3; n <= syms; n++)
kono
parents:
diff changeset
417 for (left = 2; left < n; left += 2)
kono
parents:
diff changeset
418 {
kono
parents:
diff changeset
419 /* look at all reachable (root + 1) bit nodes, and the
kono
parents:
diff changeset
420 resulting codes (complete at root + 2 or more) */
kono
parents:
diff changeset
421 index = INDEX(n, left, root + 1);
kono
parents:
diff changeset
422 if (root + 1 < max && num[index]) /* reachable node */
kono
parents:
diff changeset
423 examine(n, root + 1, left, 1 << root, 0);
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 /* also look at root bit codes with completions at root + 1
kono
parents:
diff changeset
426 bits (not saved in num, since complete), just in case */
kono
parents:
diff changeset
427 if (num[index - 1] && n <= left << 1)
kono
parents:
diff changeset
428 examine((n - left) << 1, root + 1, (n - left) << 1,
kono
parents:
diff changeset
429 1 << root, 0);
kono
parents:
diff changeset
430 }
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 /* done */
kono
parents:
diff changeset
433 printf("done: maximum of %d table entries\n", large);
kono
parents:
diff changeset
434 }
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 /*
kono
parents:
diff changeset
437 Examine and show the total number of possible Huffman codes for a given
kono
parents:
diff changeset
438 maximum number of symbols, initial root table size, and maximum code length
kono
parents:
diff changeset
439 in bits -- those are the command arguments in that order. The default
kono
parents:
diff changeset
440 values are 286, 9, and 15 respectively, for the deflate literal/length code.
kono
parents:
diff changeset
441 The possible codes are counted for each number of coded symbols from two to
kono
parents:
diff changeset
442 the maximum. The counts for each of those and the total number of codes are
kono
parents:
diff changeset
443 shown. The maximum number of inflate table entires is then calculated
kono
parents:
diff changeset
444 across all possible codes. Each new maximum number of table entries and the
kono
parents:
diff changeset
445 associated sub-code (starting at root + 1 == 10 bits) is shown.
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 To count and examine Huffman codes that are not length-limited, provide a
kono
parents:
diff changeset
448 maximum length equal to the number of symbols minus one.
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 For the deflate literal/length code, use "enough". For the deflate distance
kono
parents:
diff changeset
451 code, use "enough 30 6".
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 This uses the %llu printf format to print big_t numbers, which assumes that
kono
parents:
diff changeset
454 big_t is an unsigned long long. If the big_t type is changed (for example
kono
parents:
diff changeset
455 to a multiple precision type), the method of printing will also need to be
kono
parents:
diff changeset
456 updated.
kono
parents:
diff changeset
457 */
kono
parents:
diff changeset
458 int main(int argc, char **argv)
kono
parents:
diff changeset
459 {
kono
parents:
diff changeset
460 int syms; /* total number of symbols to code */
kono
parents:
diff changeset
461 int n; /* number of symbols to code for this run */
kono
parents:
diff changeset
462 big_t got; /* return value of count() */
kono
parents:
diff changeset
463 big_t sum; /* accumulated number of codes over n */
kono
parents:
diff changeset
464 code_t word; /* for counting bits in code_t */
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 /* set up globals for cleanup() */
kono
parents:
diff changeset
467 code = NULL;
kono
parents:
diff changeset
468 num = NULL;
kono
parents:
diff changeset
469 done = NULL;
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 /* get arguments -- default to the deflate literal/length code */
kono
parents:
diff changeset
472 syms = 286;
kono
parents:
diff changeset
473 root = 9;
kono
parents:
diff changeset
474 max = 15;
kono
parents:
diff changeset
475 if (argc > 1) {
kono
parents:
diff changeset
476 syms = atoi(argv[1]);
kono
parents:
diff changeset
477 if (argc > 2) {
kono
parents:
diff changeset
478 root = atoi(argv[2]);
kono
parents:
diff changeset
479 if (argc > 3)
kono
parents:
diff changeset
480 max = atoi(argv[3]);
kono
parents:
diff changeset
481 }
kono
parents:
diff changeset
482 }
kono
parents:
diff changeset
483 if (argc > 4 || syms < 2 || root < 1 || max < 1) {
kono
parents:
diff changeset
484 fputs("invalid arguments, need: [sym >= 2 [root >= 1 [max >= 1]]]\n",
kono
parents:
diff changeset
485 stderr);
kono
parents:
diff changeset
486 return 1;
kono
parents:
diff changeset
487 }
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 /* if not restricting the code length, the longest is syms - 1 */
kono
parents:
diff changeset
490 if (max > syms - 1)
kono
parents:
diff changeset
491 max = syms - 1;
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 /* determine the number of bits in a code_t */
kono
parents:
diff changeset
494 for (n = 0, word = 1; word; n++, word <<= 1)
kono
parents:
diff changeset
495 ;
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 /* make sure that the calculation of most will not overflow */
kono
parents:
diff changeset
498 if (max > n || (code_t)(syms - 2) >= (((code_t)0 - 1) >> (max - 1))) {
kono
parents:
diff changeset
499 fputs("abort: code length too long for internal types\n", stderr);
kono
parents:
diff changeset
500 return 1;
kono
parents:
diff changeset
501 }
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 /* reject impossible code requests */
kono
parents:
diff changeset
504 if ((code_t)(syms - 1) > ((code_t)1 << max) - 1) {
kono
parents:
diff changeset
505 fprintf(stderr, "%d symbols cannot be coded in %d bits\n",
kono
parents:
diff changeset
506 syms, max);
kono
parents:
diff changeset
507 return 1;
kono
parents:
diff changeset
508 }
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 /* allocate code vector */
kono
parents:
diff changeset
511 code = calloc(max + 1, sizeof(int));
kono
parents:
diff changeset
512 if (code == NULL) {
kono
parents:
diff changeset
513 fputs("abort: unable to allocate enough memory\n", stderr);
kono
parents:
diff changeset
514 return 1;
kono
parents:
diff changeset
515 }
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 /* determine size of saved results array, checking for overflows,
kono
parents:
diff changeset
518 allocate and clear the array (set all to zero with calloc()) */
kono
parents:
diff changeset
519 if (syms == 2) /* iff max == 1 */
kono
parents:
diff changeset
520 num = NULL; /* won't be saving any results */
kono
parents:
diff changeset
521 else {
kono
parents:
diff changeset
522 size = syms >> 1;
kono
parents:
diff changeset
523 if (size > ((size_t)0 - 1) / (n = (syms - 1) >> 1) ||
kono
parents:
diff changeset
524 (size *= n, size > ((size_t)0 - 1) / (n = max - 1)) ||
kono
parents:
diff changeset
525 (size *= n, size > ((size_t)0 - 1) / sizeof(big_t)) ||
kono
parents:
diff changeset
526 (num = calloc(size, sizeof(big_t))) == NULL) {
kono
parents:
diff changeset
527 fputs("abort: unable to allocate enough memory\n", stderr);
kono
parents:
diff changeset
528 cleanup();
kono
parents:
diff changeset
529 return 1;
kono
parents:
diff changeset
530 }
kono
parents:
diff changeset
531 }
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 /* count possible codes for all numbers of symbols, add up counts */
kono
parents:
diff changeset
534 sum = 0;
kono
parents:
diff changeset
535 for (n = 2; n <= syms; n++) {
kono
parents:
diff changeset
536 got = count(n, 1, 2);
kono
parents:
diff changeset
537 sum += got;
kono
parents:
diff changeset
538 if (got == (big_t)0 - 1 || sum < got) { /* overflow */
kono
parents:
diff changeset
539 fputs("abort: can't count that high!\n", stderr);
kono
parents:
diff changeset
540 cleanup();
kono
parents:
diff changeset
541 return 1;
kono
parents:
diff changeset
542 }
kono
parents:
diff changeset
543 printf("%llu %d-codes\n", got, n);
kono
parents:
diff changeset
544 }
kono
parents:
diff changeset
545 printf("%llu total codes for 2 to %d symbols", sum, syms);
kono
parents:
diff changeset
546 if (max < syms - 1)
kono
parents:
diff changeset
547 printf(" (%d-bit length limit)\n", max);
kono
parents:
diff changeset
548 else
kono
parents:
diff changeset
549 puts(" (no length limit)");
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 /* allocate and clear done array for beenhere() */
kono
parents:
diff changeset
552 if (syms == 2)
kono
parents:
diff changeset
553 done = NULL;
kono
parents:
diff changeset
554 else if (size > ((size_t)0 - 1) / sizeof(struct tab) ||
kono
parents:
diff changeset
555 (done = calloc(size, sizeof(struct tab))) == NULL) {
kono
parents:
diff changeset
556 fputs("abort: unable to allocate enough memory\n", stderr);
kono
parents:
diff changeset
557 cleanup();
kono
parents:
diff changeset
558 return 1;
kono
parents:
diff changeset
559 }
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 /* find and show maximum inflate table usage */
kono
parents:
diff changeset
562 if (root > max) /* reduce root to max length */
kono
parents:
diff changeset
563 root = max;
kono
parents:
diff changeset
564 if ((code_t)syms < ((code_t)1 << (root + 1)))
kono
parents:
diff changeset
565 enough(syms);
kono
parents:
diff changeset
566 else
kono
parents:
diff changeset
567 puts("cannot handle minimum code lengths > root");
kono
parents:
diff changeset
568
kono
parents:
diff changeset
569 /* done */
kono
parents:
diff changeset
570 cleanup();
kono
parents:
diff changeset
571 return 0;
kono
parents:
diff changeset
572 }