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