Show all the codes for the maximum tables size in enough.c.

pull/392/head
Mark Adler 6 years ago
parent d00c147f53
commit cd16ff0b3a
  1. 351
      examples/enough.c

@ -1,7 +1,7 @@
/* enough.c -- determine the maximum size of inflate's Huffman code tables over
* all possible valid and complete prefix codes, subject to a length limit.
* Copyright (C) 2007, 2008, 2012, 2018 Mark Adler
* Version 1.5 1 August 2018 Mark Adler
* Version 1.5 5 August 2018 Mark Adler
*/
/* Version history:
@ -17,8 +17,8 @@
1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
Clean up comparisons of different types
Clean up code indentation
1.5 1 Aug 2018 Clean up code style, formatting, and comments
Use inline function instead of macro for index
1.5 5 Aug 2018 Clean up code style, formatting, and comments
Show all the codes for the maximum, and only the maximum
*/
/*
@ -39,16 +39,17 @@
assign all portions of the remaining symbols to that code length that
preserve the properties of a correct and eventually complete code. Those
properties are: we cannot use more bit patterns than are available; and when
all the symbols are used, there are exactly zero possible bit patterns
remaining.
all the symbols are used, there are exactly zero possible bit patterns left
unused.
The inflate Huffman decoding algorithm uses two-level lookup tables for
speed. There is a single first-level table to decode codes up to root bits
in length (root == 9 in the current inflate implementation). The table has 1
<< root entries and is indexed by the next root bits of input. Codes shorter
than root bits have replicated table entries, so that the correct entry is
in length (root == 9 for literal/length codes and root == 6 for distance
codes, in the current inflate implementation). The base table has 1 << root
entries and is indexed by the next root bits of input. Codes shorter than
root bits have replicated table entries, so that the correct entry is
pointed to regardless of the bits that follow the short code. If the code is
longer than root bits, then the table entry points to a second- level table.
longer than root bits, then the table entry points to a second-level table.
The size of that table is determined by the longest code with that root-bit
prefix. If that longest code has length len, then the table has size 1 <<
(len - root), to index the remaining bits in that set of codes. Each
@ -62,7 +63,7 @@
the log2 of the number of symbols), where the shortest code has more bits
than root. In that case, root is increased to the length of the shortest
code. This program, by design, does not handle that case, so it is verified
that the number of symbols is less than 2^(root + 1).
that the number of symbols is less than 1 << (root + 1).
In order to speed up the examination (by about ten orders of magnitude for
the default arguments), the intermediate states in the build-up of a code
@ -87,34 +88,36 @@
need to be examined to cover all of the possible table memory usage cases
for the default arguments of 286 symbols limited to 15-bit codes.
Note that an unsigned long long type is used for counting. It is quite easy
to exceed the capacity of an eight-byte integer with a large number of
symbols and a large maximum code length, so multiple-precision arithmetic
would need to replace the unsigned long long arithmetic in that case. This
program will abort if an overflow occurs. The big_t type identifies where
the counting takes place.
An unsigned long long type is also used for calculating the number of
possible codes remaining at the maximum length. This limits the maximum code
length to the number of bits in a long long minus the number of bits needed
to represent the symbols in a flat code. The code_t type identifies where
the bit pattern counting takes place.
Note that the uintmax_t type is used for counting. It is quite easy to
exceed the capacity of an eight-byte integer with a large number of symbols
and a large maximum code length, so multiple-precision arithmetic would need
to replace the integer arithmetic in that case. This program will abort if
an overflow occurs. The big_t type identifies where the counting takes
place.
The uintmax_t type is also used for calculating the number of possible codes
remaining at the maximum length. This limits the maximum code length to the
number of bits in a long long minus the number of bits needed to represent
the symbols in a flat code. The code_t type identifies where the bit-pattern
counting takes place.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <assert.h>
#define local static
// Special data types.
typedef unsigned long long big_t; // type for code counting
#define PRIbig "llu" // printf format for big_t
typedef unsigned long long code_t; // type for bit pattern counting
struct tab { // type for been here check
size_t len; // length of bit vector in octets
char *vec; // allocated bit vector
typedef uintmax_t big_t; // type for code counting
#define PRIbig "ju" // printf format for big_t
typedef uintmax_t code_t; // type for bit pattern counting
struct tab { // type for been-here check
size_t len; // allocated length of bit vector in octets
char *vec; // allocated bit vector
};
/* The array for saving results, num[], is indexed with this triplet:
@ -161,59 +164,101 @@ struct tab { // type for been here check
array. Since the range of mem is expected in the default case to be about
ten times larger than the range of rem, the array is skewed to reduce the
memory usage, with eight times the range for mem than for rem. See the
calculations for offset and bit in beenhere() for the details.
calculations for offset and bit in been_here() for the details.
For the deflate example of 286 symbols limited to 15-bit codes, the bit
vectors grow to total approximately 21 MB, in addition to the 4.3 MB done[]
array itself.
vectors grow to total 5.5 MB, in addition to the 4.3 MB done array itself.
*/
// Type for a variable-length, allocated string.
typedef struct {
char *str; // pointer to allocated string
size_t size; // size of allocation
size_t len; // length of string, not including terminating zero
} string_t;
// Clear a string_t.
local void string_clear(string_t *s) {
s->str[0] = 0;
s->len = 0;
}
// Initialize a string_t.
local void string_init(string_t *s) {
s->size = 16;
s->str = malloc(s->size);
assert(s->str != NULL && "out of memory");
string_clear(s);
}
// Release the allocation of a string_t.
local void string_free(string_t *s) {
free(s->str);
s->str = NULL;
s->size = 0;
s->len = 0;
}
// Save the results of printf with fmt and the subsequent argument list to s.
// Each call appends to s. The allocated space for s is increased as needed.
local void string_printf(string_t *s, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
size_t len = s->len;
int ret = vsnprintf(s->str + len, s->size - len, fmt, ap);
assert(ret >= 0 && "out of memory");
s->len += ret;
if (s->size < s->len + 1) {
do {
s->size <<= 1;
assert(s->size != 0 && "overflow");
} while (s->size < s->len + 1);
s->str = realloc(s->str, s->size);
assert(s->str != NULL && "out of memory");
vsnprintf(s->str + len, s->size - len, fmt, ap);
}
va_end(ap);
}
// Globals to avoid propagating constants or constant pointers recursively.
struct {
int max; // maximum allowed bit length for the codes
int root; // size of base code table in bits
int large; // largest code table so far
size_t size; // number of elements in num and done
big_t tot; // total number of codes with maximum tables size
string_t out; // display of subcodes for maximum tables size
int *code; // number of symbols assigned to each bit length
big_t *num; // saved results array for code counting
struct tab *done; // states already evaluated array
} g;
// Index function for num[] and done[].
local inline size_t map(int i, int j, int k) {
return k - 1 + ((size_t)((i - 1) >> 1) * ((i - 2) >> 1) + (j >> 1) - 1) *
(g.max - 1);
local inline size_t map(int syms, int left, int len) {
return ((size_t)((syms - 1) >> 1) * ((syms - 2) >> 1) +
(left >> 1) - 1) * (g.max - 1) +
len - 1;
}
// Free allocated space. Uses globals code, num, and done.
// Free allocated space in globals.
local void cleanup(void) {
size_t n;
if (g.done != NULL) {
for (n = 0; n < g.size; n++)
for (size_t n = 0; n < g.size; n++)
if (g.done[n].len)
free(g.done[n].vec);
free(g.done);
g.size = 0;
free(g.done); g.done = NULL;
}
if (g.num != NULL)
free(g.num);
if (g.code != NULL)
free(g.code);
free(g.num); g.num = NULL;
free(g.code); g.code = NULL;
string_free(&g.out);
}
// Return the number of possible prefix codes using bit patterns of lengths len
// through max inclusive, coding syms symbols, with left bit patterns of length
// len unused -- return -1 if there is an overflow in the counting. Keep a
// record of previous results in num to prevent repeating the same calculation.
// Uses the globals max and num.
local big_t count(int syms, int len, int left) {
big_t sum; // number of possible codes from this juncture
big_t got; // value returned from count()
int least; // least number of syms to use at this juncture
int most; // most number of syms to use at this juncture
int use; // number of bit patterns to use in next call
size_t index; // index of this case in *num
local big_t count(int syms, int left, int len) {
// see if only one possible code
if (syms == left)
return 1;
@ -222,30 +267,30 @@ local big_t count(int syms, int len, int left) {
assert(syms > left && left > 0 && len < g.max);
// see if we've done this one already
index = map(syms, left, len);
got = g.num[index];
size_t index = map(syms, left, len);
big_t got = g.num[index];
if (got)
return got; // we have -- return the saved result
// we need to use at least this many bit patterns so that the code won't be
// incomplete at the next length (more bit patterns than symbols)
least = (left << 1) - syms;
int least = (left << 1) - syms;
if (least < 0)
least = 0;
// we can use at most this many bit patterns, lest there not be enough
// available for the remaining symbols at the maximum length (if there were
// no limit to the code length, this would become: most = left - 1)
most = (((code_t)left << (g.max - len)) - syms) /
(((code_t)1 << (g.max - len)) - 1);
int most = (((code_t)left << (g.max - len)) - syms) /
(((code_t)1 << (g.max - len)) - 1);
// count all possible codes from this juncture and add them up
sum = 0;
for (use = least; use <= most; use++) {
got = count(syms - use, len + 1, (left - use) << 1);
big_t sum = 0;
for (int use = least; use <= most; use++) {
got = count(syms - use, (left - use) << 1, len + 1);
sum += got;
if (got == (big_t)0 - 1 || sum < got) // overflow
return (big_t)0 - 1;
if (got == (big_t)-1 || sum < got) // overflow
return (big_t)-1;
}
// verify that all recursive calls are productive
@ -259,23 +304,19 @@ local big_t count(int syms, int len, int left) {
// Return true if we've been here before, set to true if not. Set a bit in a
// bit vector to indicate visiting this state. Each (syms,len,left) state has a
// variable size bit vector indexed by (mem,rem). The bit vector is lengthened
// if needed to allow setting the (mem,rem) bit.
local int beenhere(int syms, int len, int left, int mem, int rem) {
size_t index; // index for this state's bit vector
size_t offset; // offset in this state's bit vector
int bit; // mask for this state's bit
size_t length; // length of the bit vector in bytes
char *vector; // new or enlarged bit vector
// as needed to allow setting the (mem,rem) bit.
local int been_here(int syms, int left, int len, int mem, int rem) {
// point to vector for (syms,left,len), bit in vector for (mem,rem)
index = map(syms, left, len);
mem -= 1 << g.root;
offset = (mem >> 3) + rem;
size_t index = map(syms, left, len);
mem -= 1 << g.root; // mem always includes the root table
mem >>= 1; // mem and rem are always even
rem >>= 1;
size_t offset = (mem >> 3) + rem;
offset = ((offset * (offset + 1)) >> 1) + rem;
bit = 1 << (mem & 7);
int bit = 1 << (mem & 7);
// see if we've been here
length = g.done[index].len;
size_t length = g.done[index].len;
if (offset < length && (g.done[index].vec[offset] & bit) != 0)
return 1; // done this!
@ -284,29 +325,23 @@ local int beenhere(int syms, int len, int left, int mem, int rem) {
// see if we need to lengthen the vector in order to set the bit
if (length <= offset) {
// if we have one already, enlarge it, zero out the appended space
char *vector;
if (length) {
do {
length <<= 1;
} while (length <= offset);
vector = realloc(g.done[index].vec, length);
if (vector != NULL)
memset(vector + g.done[index].len, 0,
length - g.done[index].len);
assert(vector != NULL && "out of memory");
memset(vector + g.done[index].len, 0, length - g.done[index].len);
}
// otherwise we need to make a new vector and zero it out
else {
length = 1 << (len - g.root);
length = 16;
while (length <= offset)
length <<= 1;
vector = calloc(length, sizeof(char));
}
// in either case, bail if we can't get the memory
if (vector == NULL) {
fputs("abort: unable to allocate enough memory\n", stderr);
cleanup();
exit(1);
vector = calloc(length, 1);
assert(vector != NULL && "out of memory");
}
// install the new vector
@ -322,13 +357,8 @@ local int beenhere(int syms, int len, int left, int mem, int rem) {
// Examine all possible codes from the given node (syms, len, left). Compute
// the amount of memory required to build inflate's decoding tables, where the
// number of code structures used so far is mem, and the number remaining in
// the current sub-table is rem. Uses the globals max, code, root, large, and
// done.
local void examine(int syms, int len, int left, int mem, int rem) {
int least; // least number of syms to use at this juncture
int most; // most number of syms to use at this juncture
int use; // number of bit patterns to use in next call
// the current sub-table is rem.
local void examine(int syms, int left, int len, int mem, int rem) {
// see if we have a complete code
if (syms == left) {
// set the last code entry
@ -342,15 +372,32 @@ local void examine(int syms, int len, int left, int mem, int rem) {
}
assert(rem == left);
// if this is a new maximum, show the entries used and the sub-code
if (mem > g.large) {
g.large = mem;
printf("max %d: ", mem);
for (use = g.root + 1; use <= g.max; use++)
if (g.code[use])
printf("%d[%d] ", g.code[use], use);
putchar('\n');
fflush(stdout);
// if this is at the maximum, show the sub-code
if (mem >= g.large) {
// if this is a new maximum, update the maximum and clear out the
// printed sub-codes from the previous maximum
if (mem > g.large) {
g.large = mem;
string_clear(&g.out);
}
// compute the starting state for this sub-code
syms = 0;
left = 1 << g.max;
for (int bits = g.max; bits > g.root; bits--) {
syms += g.code[bits];
left -= g.code[bits];
assert((left & 1) == 0);
left >>= 1;
}
// print the starting state and the resulting sub-code to g.out
string_printf(&g.out, "<%u, %u, %u>:",
syms, g.root + 1, ((1 << g.root) - left) << 1);
for (int bits = g.root + 1; bits <= g.max; bits++)
if (g.code[bits])
string_printf(&g.out, " %d[%d]", g.code[bits], bits);
string_printf(&g.out, "\n");
}
// remove entries as we drop back down in the recursion
@ -359,23 +406,23 @@ local void examine(int syms, int len, int left, int mem, int rem) {
}
// prune the tree if we can
if (beenhere(syms, len, left, mem, rem))
if (been_here(syms, left, len, mem, rem))
return;
// we need to use at least this many bit patterns so that the code won't be
// incomplete at the next length (more bit patterns than symbols)
least = (left << 1) - syms;
int least = (left << 1) - syms;
if (least < 0)
least = 0;
// we can use at most this many bit patterns, lest there not be enough
// available for the remaining symbols at the maximum length (if there were
// no limit to the code length, this would become: most = left - 1)
most = (((code_t)left << (g.max - len)) - syms) /
(((code_t)1 << (g.max - len)) - 1);
int most = (((code_t)left << (g.max - len)) - syms) /
(((code_t)1 << (g.max - len)) - 1);
// occupy least table spaces, creating new sub-tables as needed
use = least;
int use = least;
while (rem < use) {
use -= rem;
rem = 1 << (len - g.root);
@ -386,7 +433,7 @@ local void examine(int syms, int len, int left, int mem, int rem) {
// examine codes from here, updating table space as we go
for (use = least; use <= most; use++) {
g.code[len] = use;
examine(syms - use, len + 1, (left - use) << 1,
examine(syms - use, (left - use) << 1, len + 1,
mem + (rem ? 1 << (len - g.root) : 0), rem << 1);
if (rem == 0) {
rem = 1 << (len - g.root);
@ -402,37 +449,35 @@ local void examine(int syms, int len, int left, int mem, int rem) {
// Look at all sub-codes starting with root + 1 bits. Look at only the valid
// intermediate code states (syms, left, len). For each completed code,
// calculate the amount of memory required by inflate to build the decoding
// tables. Find the maximum amount of memory required and show the code that
// requires that maximum. Uses the globals max, root, and num.
// tables. Find the maximum amount of memory required and show the codes that
// require that maximum.
local void enough(int syms) {
int n; // number of remaing symbols for this node
int left; // number of unused bit patterns at this length
size_t index; // index of this case in *num
// clear code
for (n = 0; n <= g.max; n++)
for (int n = 0; n <= g.max; n++)
g.code[n] = 0;
// look at all (root + 1) bit and longer codes
string_clear(&g.out); // empty saved results
g.large = 1 << g.root; // base table
if (g.root < g.max) // otherwise, there's only a base table
for (n = 3; n <= syms; n++)
for (left = 2; left < n; left += 2) {
for (int n = 3; n <= syms; n++)
for (int left = 2; left < n; left += 2) {
// look at all reachable (root + 1) bit nodes, and the
// resulting codes (complete at root + 2 or more)
index = map(n, left, g.root + 1);
size_t index = map(n, left, g.root + 1);
if (g.root + 1 < g.max && g.num[index]) // reachable node
examine(n, g.root + 1, left, 1 << g.root, 0);
examine(n, left, g.root + 1, 1 << g.root, 0);
// also look at root bit codes with completions at root + 1
// bits (not saved in num, since complete), just in case
if (g.num[index - 1] && n <= left << 1)
examine((n - left) << 1, g.root + 1, (n - left) << 1,
examine((n - left) << 1, (n - left) << 1, g.root + 1,
1 << g.root, 0);
}
// done
printf("done: maximum of %d table entries\n", g.large);
printf("maximum of %d table entries for root = %d\n", g.large, g.root);
fputs(g.out.str, stdout);
}
// Examine and show the total number of possible prefix codes for a given
@ -451,19 +496,14 @@ local void enough(int syms) {
// For the deflate literal/length code, use "enough". For the deflate distance
// code, use "enough 30 6".
int main(int argc, char **argv) {
int syms; // total number of symbols to code
int n; // number of symbols to code for this run
big_t got; // return value of count()
big_t sum; // accumulated number of codes over n
code_t word; // for counting bits in code_t
// set up globals for cleanup()
g.code = NULL;
g.num = NULL;
g.done = NULL;
string_init(&g.out);
// get arguments -- default to the deflate literal/length code
syms = 286;
int syms = 286;
g.root = 9;
g.max = 15;
if (argc > 1) {
@ -485,11 +525,12 @@ int main(int argc, char **argv) {
g.max = syms - 1;
// determine the number of bits in a code_t
for (n = 0, word = 1; word; n++, word <<= 1)
;
int bits = 0;
for (code_t word = 1; word; word <<= 1)
bits++;
// make sure that the calculation of most will not overflow
if (g.max > n || (code_t)(syms - 2) >= (((code_t)0 - 1) >> (g.max - 1))) {
if (g.max > bits || (code_t)(syms - 2) >= ((code_t)-1 >> (g.max - 1))) {
fputs("abort: code length too long for internal types\n", stderr);
return 1;
}
@ -503,10 +544,7 @@ int main(int argc, char **argv) {
// allocate code vector
g.code = calloc(g.max + 1, sizeof(int));
if (g.code == NULL) {
fputs("abort: unable to allocate enough memory\n", stderr);
return 1;
}
assert(g.code != NULL && "out of memory");
// determine size of saved results array, checking for overflows,
// allocate and clear the array (set all to zero with calloc())
@ -514,27 +552,22 @@ int main(int argc, char **argv) {
g.num = NULL; // won't be saving any results
else {
g.size = syms >> 1;
if (g.size > ((size_t)0 - 1) / (n = (syms - 1) >> 1) ||
(g.size *= n, g.size > ((size_t)0 - 1) / (n = g.max - 1)) ||
(g.size *= n, g.size > ((size_t)0 - 1) / sizeof(big_t)) ||
(g.num = calloc(g.size, sizeof(big_t))) == NULL) {
fputs("abort: unable to allocate enough memory\n", stderr);
cleanup();
return 1;
}
int n = (syms - 1) >> 1;
assert(g.size <= (size_t)-1 / n && "overflow");
g.size *= n;
n = g.max - 1;
assert(g.size <= (size_t)-1 / n && "overflow");
g.size *= n;
g.num = calloc(g.size, sizeof(big_t));
assert(g.num != NULL && "out of memory");
}
// count possible codes for all numbers of symbols, add up counts
sum = 0;
for (n = 2; n <= syms; n++) {
got = count(n, 1, 2);
big_t sum = 0;
for (int n = 2; n <= syms; n++) {
big_t got = count(n, 2, 1);
sum += got;
if (got == (big_t)0 - 1 || sum < got) { // overflow
fputs("abort: can't count that high!\n", stderr);
cleanup();
return 1;
}
printf("%"PRIbig" %d-codes\n", got, n);
assert(got != (big_t)-1 && sum >= got && "overflow");
}
printf("%"PRIbig" total codes for 2 to %d symbols", sum, syms);
if (g.max < syms - 1)
@ -542,14 +575,12 @@ int main(int argc, char **argv) {
else
puts(" (no length limit)");
// allocate and clear done array for beenhere()
// allocate and clear done array for been_here()
if (syms == 2)
g.done = NULL;
else if (g.size > ((size_t)0 - 1) / sizeof(struct tab) ||
(g.done = calloc(g.size, sizeof(struct tab))) == NULL) {
fputs("abort: unable to allocate enough memory\n", stderr);
cleanup();
return 1;
else {
g.done = calloc(g.size, sizeof(struct tab));
assert(g.done != NULL && "out of memory");
}
// find and show maximum inflate table usage
@ -558,7 +589,7 @@ int main(int argc, char **argv) {
if ((code_t)syms < ((code_t)1 << (g.root + 1)))
enough(syms);
else
puts("cannot handle minimum code lengths > root");
fputs("cannot handle minimum code lengths > root", stderr);
// done
cleanup();

Loading…
Cancel
Save