1 /* PackTab - Pack a static table
2 * Copyright (C) 2001 Behdad Esfahbod.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library, in a file named COPYING; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA
19 * For licensing issues, contact <fwpg@sharif.edu>.
30 #endif /* HAVE_CONFIG_H */
42 # if !STDC_HEADERS && HAVE_MEMORY_H
53 typedef signed int uni_table[1024 * 1024 * 2];
54 static int n, a, max_depth, digits, tab_width, per_row;
57 static uni_table temp, x, perm, *tab;
58 static long pow[22], cluster, cmpcluster;
59 static const char *const *name, *key_type_name, *table_name, *macro_name;
68 /* min should be less than max */
74 for (i = 21; max < pow[i]; i--)
77 while (i && !((min ^ max) & pow[i]))
82 /* min is less than half of max */
83 for (i = 21 - 1; min < pow[i]; i--)
89 return max & (pow[i] - 1);
94 const signed int *table
99 /* initialize powers of two */
101 for (i = 1; i <= 21; i++)
102 pow[i] = pow[i - 1] << 1;
104 /* reduce number of elements to get a more binary number */
108 /* find number of essential items */
110 while (essen && table[essen] == def_key)
114 N = most_binary (essen, N);
117 for (n = 21; N % pow[n]; n--)
119 digits = (n + 3) / 4;
121 if (pow[i] * (tab_width + 1) < 80)
133 for (i = 0; i < cmpcluster; i++)
134 if (((int *) r)[i] != ((int *) s)[i])
135 return ((int *) r)[i] - ((int *) s)[i];
139 static int lev, best_lev, p[22], best_p[22], nn;
140 static long c[22], best_c[22], s, best_s;
141 static long t[22], best_t[22], clusters[22], best_cluster[22];
154 for (i = 0; i <= lev; i++)
159 best_cluster[i] = clusters[i];
169 long i, j, k, y, sbak;
177 if (lev == max_depth)
180 for (i = 1 - t[lev] % 2; i <= nn + (t[lev] >> nn) % 2; i++)
183 clusters[lev] = cluster = (i && nn >= 0) ? pow[i] : t[lev];
184 cmpcluster = cluster + 1;
186 t[lev + 1] = (t[lev] - 1) / cluster + 1;
187 for (j = 0; j < t[lev + 1]; j++)
189 memmove (temp + j * cmpcluster, tab[lev] + j * cluster,
190 cluster * sizeof (tab[lev][0]));
191 temp[j * cmpcluster + cluster] = j;
193 qsort (temp, t[lev + 1], cmpcluster * sizeof (temp[0]), compare);
194 for (j = 0; j < t[lev + 1]; j++)
196 perm[j] = temp[j * cmpcluster + cluster];
197 temp[j * cmpcluster + cluster] = 0;
201 tab[lev + 1][perm[0]] = perm[0];
202 for (j = 1; j < t[lev + 1]; j++)
204 if (compare (temp + y, temp + y + cmpcluster))
207 tab[lev + 1][perm[j]] = perm[j];
210 tab[lev + 1][perm[j]] = tab[lev + 1][perm[j - 1]];
214 s += k * node_size * cluster;
224 key_bytes = k * cluster;
225 key_bytes = key_bytes < 0xff ? 1 : key_bytes < 0xffff ? 2 : 4;
240 best_lev = max_depth + 2;
254 int i, j, k, y, ii, ofs;
255 const char *key_type;
257 if (best_t[lev] == 1)
260 nn -= (i = best_p[lev]);
261 cluster = best_cluster[lev];
262 cmpcluster = cluster + 1;
264 t[lev + 1] = best_t[lev + 1];
265 for (j = 0; j < t[lev + 1]; j++)
267 memmove (temp + j * cmpcluster, tab[lev] + j * cluster,
268 cluster * sizeof (tab[lev][0]));
269 temp[j * cmpcluster + cluster] = j;
271 qsort (temp, t[lev + 1], cmpcluster * sizeof (temp[0]), compare);
272 for (j = 0; j < t[lev + 1]; j++)
274 perm[j] = temp[j * cmpcluster + cluster];
275 temp[j * cmpcluster + cluster] = 0;
279 tab[lev + 1][perm[0]] = x[0] = perm[0];
280 for (j = 1; j < t[lev + 1]; j++)
282 if (compare (temp + y, temp + y + cmpcluster))
285 tab[lev + 1][perm[j]] = x[k];
289 tab[lev + 1][perm[j]] = tab[lev + 1][perm[j - 1]];
294 for (ii = 1; ii < k; ii++)
298 key_type = !lev ? key_type_name :
299 max_key <= 0xff ? "PACKTAB_UINT8" :
300 max_key <= 0xffff ? "PACKTAB_UINT16" : "PACKTAB_UINT32";
301 fprintf (f, "static const %s %sLev%d[%ld*%d] = {", key_type, table_name,
302 best_lev - lev - 1, cluster, k);
304 for (ii = 0; ii < k; ii++)
307 fprintf (f, "\n#define %sLev%d_%0*lX 0x%0X", table_name,
308 best_lev - lev - 1, digits, x[i] * pow[n - nn], ofs);
312 for (j = 0; j < cluster; j++)
314 if (!(j % per_row) && j != cluster - 1)
316 fprintf (f, "%*s,", tab_width, name[tab[lev][kk++]]);
319 for (j = 0; j < cluster; j++)
321 if (!(j % per_row) && j != cluster - 1)
323 fprintf (f, "%*d,", tab_width, tab[lev][kk++]);
326 for (j = 0; j < cluster; j++, kk++)
327 fprintf (f, "\n %sLev%d_%0*lX, /* %0*lX..%0*lX */", table_name,
328 best_lev - lev, digits,
329 tab[lev][kk] * pow[n - nn - best_p[lev]], digits,
330 x[i] * pow[n - nn] + j * pow[n - nn - best_p[lev]], digits,
331 x[i] * pow[n - nn] + (j + 1) * pow[n - nn - best_p[lev]] -
335 for (j = 0; j < k; j++)
336 if (x[j] > x[i] && (x[j] < x[jj] || jj == i))
340 fprintf (f, "\n};\n\n");
342 write_array (cluster * k);
357 fprintf (f, "\n" "/* *IND" "ENT-OFF* */\n\n");
359 fprintf (f, "/* *IND" "ENT-ON* */\n\n");
361 fprintf (f, "#define %s(x) \\\n", macro_name);
362 fprintf (f, "\t((x) >= 0x%lx ? ", N);
364 fprintf (f, "%s", name[def_key]);
366 fprintf (f, "%d", def_key);
369 for (i = best_lev - 1; i >= 0; i--)
371 fprintf (f, " \\\n\t%sLev%d[((x)", table_name, i);
373 fprintf (f, " >> %d", j);
375 fprintf (f, " & 0x%02lx) +", pow[best_p[best_lev - 1 - i]] - 1);
376 j += best_p[best_lev - 1 - i];
379 for (i = 0; i < best_lev; i++)
381 fprintf (f, ")\n\n");
391 " generated by packtab.c version %d\n\n"
392 " use %s(key) to access your table\n\n"
393 " assumed sizeof(%s): %d\n"
394 " required memory: %ld\n"
396 " partition shape: %s",
397 packtab_version, macro_name, key_type_name, a, best_s, best_lev,
399 for (i = best_lev - 1; i >= 0; i--)
400 fprintf (f, "[%ld]", best_cluster[i]);
401 fprintf (f, "\n" " different table entries:");
402 for (i = best_lev - 1; i >= 0; i--)
403 fprintf (f, " %ld", best_c[i]);
404 fprintf (f, "\n*/\n");
410 const signed int *base,
413 signed int default_key,
416 const char *const *p_name,
417 const char *p_key_type_name,
418 const char *p_table_name,
419 const char *p_macro_name,
425 def_key = default_key;
426 max_depth = p_max_depth;
427 tab_width = p_tab_width;
429 key_type_name = p_key_type_name;
430 table_name = p_table_name;
431 macro_name = p_macro_name;
434 if (!(tab = malloc ((n + 1) * sizeof (tab[0]))))
436 memmove (tab[0], base, N * sizeof (base[0]));
443 /* End of packtab.c */