xref: /minix3/usr.bin/nbperf/nbperf-chm.c (revision c75851fccb32314d0f8a8e6a641cc7928622bf76)
1*c75851fcSLionel Sambuc /*	$NetBSD: nbperf-chm.c,v 1.3 2011/10/21 23:47:11 joerg Exp $	*/
2*c75851fcSLionel Sambuc /*-
3*c75851fcSLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4*c75851fcSLionel Sambuc  * All rights reserved.
5*c75851fcSLionel Sambuc  *
6*c75851fcSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
7*c75851fcSLionel Sambuc  * by Joerg Sonnenberger.
8*c75851fcSLionel Sambuc  *
9*c75851fcSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10*c75851fcSLionel Sambuc  * modification, are permitted provided that the following conditions
11*c75851fcSLionel Sambuc  * are met:
12*c75851fcSLionel Sambuc  *
13*c75851fcSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*c75851fcSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*c75851fcSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*c75851fcSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
17*c75851fcSLionel Sambuc  *    the documentation and/or other materials provided with the
18*c75851fcSLionel Sambuc  *    distribution.
19*c75851fcSLionel Sambuc  *
20*c75851fcSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*c75851fcSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*c75851fcSLionel Sambuc  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*c75851fcSLionel Sambuc  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24*c75851fcSLionel Sambuc  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*c75851fcSLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*c75851fcSLionel Sambuc  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*c75851fcSLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*c75851fcSLionel Sambuc  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*c75851fcSLionel Sambuc  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*c75851fcSLionel Sambuc  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*c75851fcSLionel Sambuc  * SUCH DAMAGE.
32*c75851fcSLionel Sambuc  */
33*c75851fcSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
34*c75851fcSLionel Sambuc #include "nbtool_config.h"
35*c75851fcSLionel Sambuc #endif
36*c75851fcSLionel Sambuc 
37*c75851fcSLionel Sambuc #include <sys/cdefs.h>
38*c75851fcSLionel Sambuc __RCSID("$NetBSD: nbperf-chm.c,v 1.3 2011/10/21 23:47:11 joerg Exp $");
39*c75851fcSLionel Sambuc 
40*c75851fcSLionel Sambuc #include <err.h>
41*c75851fcSLionel Sambuc #include <inttypes.h>
42*c75851fcSLionel Sambuc #include <stdlib.h>
43*c75851fcSLionel Sambuc #include <stdio.h>
44*c75851fcSLionel Sambuc #include <string.h>
45*c75851fcSLionel Sambuc 
46*c75851fcSLionel Sambuc #include "nbperf.h"
47*c75851fcSLionel Sambuc 
48*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
49*c75851fcSLionel Sambuc #include "graph3.h"
50*c75851fcSLionel Sambuc #else
51*c75851fcSLionel Sambuc #include "graph2.h"
52*c75851fcSLionel Sambuc #endif
53*c75851fcSLionel Sambuc 
54*c75851fcSLionel Sambuc /*
55*c75851fcSLionel Sambuc  * A full description of the algorithm can be found in:
56*c75851fcSLionel Sambuc  * "An optimal algorithm for generating minimal perfect hash functions"
57*c75851fcSLionel Sambuc  * by Czech, Havas and Majewski in Information Processing Letters,
58*c75851fcSLionel Sambuc  * 43(5):256-264, October 1992.
59*c75851fcSLionel Sambuc  */
60*c75851fcSLionel Sambuc 
61*c75851fcSLionel Sambuc /*
62*c75851fcSLionel Sambuc  * The algorithm is based on random, acyclic graphs.
63*c75851fcSLionel Sambuc  *
64*c75851fcSLionel Sambuc  * Each edge in the represents a key.  The vertices are the reminder of
65*c75851fcSLionel Sambuc  * the hash function mod n.  n = cm with c > 2, otherwise the propability
66*c75851fcSLionel Sambuc  * of finding an acyclic graph is very low (for 2-graphs).  The constant
67*c75851fcSLionel Sambuc  * for 3-graphs is 1.24.
68*c75851fcSLionel Sambuc  *
69*c75851fcSLionel Sambuc  * After the hashing phase, the graph is checked for cycles.
70*c75851fcSLionel Sambuc  * A cycle-free graph is either empty or has a vertex of degree 1.
71*c75851fcSLionel Sambuc  * Removing the edge for this vertex doesn't change this property,
72*c75851fcSLionel Sambuc  * so applying this recursively reduces the size of the graph.
73*c75851fcSLionel Sambuc  * If the graph is empty at the end of the process, it was acyclic.
74*c75851fcSLionel Sambuc  *
75*c75851fcSLionel Sambuc  * The assignment step now sets g[i] := 0 and processes the edges
76*c75851fcSLionel Sambuc  * in reverse order of removal.  That ensures that at least one vertex
77*c75851fcSLionel Sambuc  * is always unvisited and can be assigned.
78*c75851fcSLionel Sambuc  */
79*c75851fcSLionel Sambuc 
80*c75851fcSLionel Sambuc struct state {
81*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
82*c75851fcSLionel Sambuc 	struct graph3 graph;
83*c75851fcSLionel Sambuc #else
84*c75851fcSLionel Sambuc 	struct graph2 graph;
85*c75851fcSLionel Sambuc #endif
86*c75851fcSLionel Sambuc 	uint32_t *g;
87*c75851fcSLionel Sambuc 	uint8_t *visited;
88*c75851fcSLionel Sambuc };
89*c75851fcSLionel Sambuc 
90*c75851fcSLionel Sambuc static void
assign_nodes(struct state * state)91*c75851fcSLionel Sambuc assign_nodes(struct state *state)
92*c75851fcSLionel Sambuc {
93*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
94*c75851fcSLionel Sambuc 	struct edge3 *e;
95*c75851fcSLionel Sambuc #else
96*c75851fcSLionel Sambuc 	struct edge2 *e;
97*c75851fcSLionel Sambuc #endif
98*c75851fcSLionel Sambuc 	size_t i;
99*c75851fcSLionel Sambuc 	uint32_t e_idx;
100*c75851fcSLionel Sambuc 
101*c75851fcSLionel Sambuc 	for (i = 0; i < state->graph.e; ++i) {
102*c75851fcSLionel Sambuc 		e_idx = state->graph.output_order[i];
103*c75851fcSLionel Sambuc 		e = &state->graph.edges[e_idx];
104*c75851fcSLionel Sambuc 
105*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
106*c75851fcSLionel Sambuc 		if (!state->visited[e->left]) {
107*c75851fcSLionel Sambuc 			state->g[e->left] = (2 * state->graph.e + e_idx
108*c75851fcSLionel Sambuc 			    - state->g[e->middle] - state->g[e->right])
109*c75851fcSLionel Sambuc 			    % state->graph.e;
110*c75851fcSLionel Sambuc 		} else if (!state->visited[e->middle]) {
111*c75851fcSLionel Sambuc 			state->g[e->middle] = (2 * state->graph.e + e_idx
112*c75851fcSLionel Sambuc 			    - state->g[e->left] - state->g[e->right])
113*c75851fcSLionel Sambuc 			    % state->graph.e;
114*c75851fcSLionel Sambuc 		} else {
115*c75851fcSLionel Sambuc 			state->g[e->right] = (2 * state->graph.e + e_idx
116*c75851fcSLionel Sambuc 			    - state->g[e->left] - state->g[e->middle])
117*c75851fcSLionel Sambuc 			    % state->graph.e;
118*c75851fcSLionel Sambuc 		}
119*c75851fcSLionel Sambuc 		state->visited[e->left] = 1;
120*c75851fcSLionel Sambuc 		state->visited[e->middle] = 1;
121*c75851fcSLionel Sambuc 		state->visited[e->right] = 1;
122*c75851fcSLionel Sambuc #else
123*c75851fcSLionel Sambuc 		if (!state->visited[e->left]) {
124*c75851fcSLionel Sambuc 			state->g[e->left] = (state->graph.e + e_idx
125*c75851fcSLionel Sambuc 			    - state->g[e->right]) % state->graph.e;
126*c75851fcSLionel Sambuc 		} else {
127*c75851fcSLionel Sambuc 			state->g[e->right] = (state->graph.e + e_idx
128*c75851fcSLionel Sambuc 			    - state->g[e->left]) % state->graph.e;
129*c75851fcSLionel Sambuc 		}
130*c75851fcSLionel Sambuc 		state->visited[e->left] = 1;
131*c75851fcSLionel Sambuc 		state->visited[e->right] = 1;
132*c75851fcSLionel Sambuc #endif
133*c75851fcSLionel Sambuc 	}
134*c75851fcSLionel Sambuc }
135*c75851fcSLionel Sambuc 
136*c75851fcSLionel Sambuc static void
print_hash(struct nbperf * nbperf,struct state * state)137*c75851fcSLionel Sambuc print_hash(struct nbperf *nbperf, struct state *state)
138*c75851fcSLionel Sambuc {
139*c75851fcSLionel Sambuc 	uint32_t i, per_line;
140*c75851fcSLionel Sambuc 	const char *g_type;
141*c75851fcSLionel Sambuc 	int g_width;
142*c75851fcSLionel Sambuc 
143*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "#include <stdlib.h>\n\n");
144*c75851fcSLionel Sambuc 
145*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "%suint32_t\n",
146*c75851fcSLionel Sambuc 	    nbperf->static_hash ? "static " : "");
147*c75851fcSLionel Sambuc 	fprintf(nbperf->output,
148*c75851fcSLionel Sambuc 	    "%s(const void * __restrict key, size_t keylen)\n",
149*c75851fcSLionel Sambuc 	    nbperf->hash_name);
150*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "{\n");
151*c75851fcSLionel Sambuc 	if (state->graph.v >= 65536) {
152*c75851fcSLionel Sambuc 		g_type = "uint32_t";
153*c75851fcSLionel Sambuc 		g_width = 8;
154*c75851fcSLionel Sambuc 		per_line = 4;
155*c75851fcSLionel Sambuc 	} else if (state->graph.v >= 256) {
156*c75851fcSLionel Sambuc 		g_type = "uint16_t";
157*c75851fcSLionel Sambuc 		g_width = 4;
158*c75851fcSLionel Sambuc 		per_line = 8;
159*c75851fcSLionel Sambuc 	} else {
160*c75851fcSLionel Sambuc 		g_type = "uint8_t";
161*c75851fcSLionel Sambuc 		g_width = 2;
162*c75851fcSLionel Sambuc 		per_line = 10;
163*c75851fcSLionel Sambuc 	}
164*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "\tstatic const %s g[%" PRId32 "] = {\n",
165*c75851fcSLionel Sambuc 	    g_type, state->graph.v);
166*c75851fcSLionel Sambuc 	for (i = 0; i < state->graph.v; ++i) {
167*c75851fcSLionel Sambuc 		fprintf(nbperf->output, "%s0x%0*" PRIx32 ",%s",
168*c75851fcSLionel Sambuc 		    (i % per_line == 0 ? "\t    " : " "),
169*c75851fcSLionel Sambuc 		    g_width, state->g[i],
170*c75851fcSLionel Sambuc 		    (i % per_line == per_line - 1 ? "\n" : ""));
171*c75851fcSLionel Sambuc 	}
172*c75851fcSLionel Sambuc 	if (i % per_line != 0)
173*c75851fcSLionel Sambuc 		fprintf(nbperf->output, "\n\t};\n");
174*c75851fcSLionel Sambuc 	else
175*c75851fcSLionel Sambuc 		fprintf(nbperf->output, "\t};\n");
176*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "\tuint32_t h[%zu];\n\n", nbperf->hash_size);
177*c75851fcSLionel Sambuc 	(*nbperf->print_hash)(nbperf, "\t", "key", "keylen", "h");
178*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
179*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "\treturn (g[h[0] %% %" PRIu32 "] + "
180*c75851fcSLionel Sambuc 	    "g[h[1] %% %" PRIu32 "] + "
181*c75851fcSLionel Sambuc 	    "g[h[2] %% %" PRIu32"]) %% %" PRIu32 ";\n",
182*c75851fcSLionel Sambuc 	    state->graph.v, state->graph.v, state->graph.v, state->graph.e);
183*c75851fcSLionel Sambuc #else
184*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "\treturn (g[h[0] %% %" PRIu32 "] + "
185*c75851fcSLionel Sambuc 	    "g[h[1] %% %" PRIu32"]) %% %" PRIu32 ";\n",
186*c75851fcSLionel Sambuc 	    state->graph.v, state->graph.v, state->graph.e);
187*c75851fcSLionel Sambuc #endif
188*c75851fcSLionel Sambuc 	fprintf(nbperf->output, "}\n");
189*c75851fcSLionel Sambuc 
190*c75851fcSLionel Sambuc 	if (nbperf->map_output != NULL) {
191*c75851fcSLionel Sambuc 		for (i = 0; i < state->graph.e; ++i)
192*c75851fcSLionel Sambuc 			fprintf(nbperf->map_output, "%" PRIu32 "\n", i);
193*c75851fcSLionel Sambuc 	}
194*c75851fcSLionel Sambuc }
195*c75851fcSLionel Sambuc 
196*c75851fcSLionel Sambuc int
197*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
chm3_compute(struct nbperf * nbperf)198*c75851fcSLionel Sambuc chm3_compute(struct nbperf *nbperf)
199*c75851fcSLionel Sambuc #else
200*c75851fcSLionel Sambuc chm_compute(struct nbperf *nbperf)
201*c75851fcSLionel Sambuc #endif
202*c75851fcSLionel Sambuc {
203*c75851fcSLionel Sambuc 	struct state state;
204*c75851fcSLionel Sambuc 	int retval = -1;
205*c75851fcSLionel Sambuc 	uint32_t v, e;
206*c75851fcSLionel Sambuc 
207*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
208*c75851fcSLionel Sambuc 	if (nbperf->c == 0)
209*c75851fcSLionel Sambuc 		nbperf-> c = 1.24;
210*c75851fcSLionel Sambuc 
211*c75851fcSLionel Sambuc 	if (nbperf->c < 1.24)
212*c75851fcSLionel Sambuc 		errx(1, "The argument for option -c must be at least 1.24");
213*c75851fcSLionel Sambuc 
214*c75851fcSLionel Sambuc 	if (nbperf->hash_size < 3)
215*c75851fcSLionel Sambuc 		errx(1, "The hash function must generate at least 3 values");
216*c75851fcSLionel Sambuc #else
217*c75851fcSLionel Sambuc 	if (nbperf->c == 0)
218*c75851fcSLionel Sambuc 		nbperf-> c = 2;
219*c75851fcSLionel Sambuc 
220*c75851fcSLionel Sambuc 	if (nbperf->c < 2)
221*c75851fcSLionel Sambuc 		errx(1, "The argument for option -c must be at least 2");
222*c75851fcSLionel Sambuc 
223*c75851fcSLionel Sambuc 	if (nbperf->hash_size < 2)
224*c75851fcSLionel Sambuc 		errx(1, "The hash function must generate at least 2 values");
225*c75851fcSLionel Sambuc #endif
226*c75851fcSLionel Sambuc 
227*c75851fcSLionel Sambuc 	(*nbperf->seed_hash)(nbperf);
228*c75851fcSLionel Sambuc 	e = nbperf->n;
229*c75851fcSLionel Sambuc 	v = nbperf->c * nbperf->n;
230*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
231*c75851fcSLionel Sambuc 	if (v == 1.24 * nbperf->n)
232*c75851fcSLionel Sambuc 		++v;
233*c75851fcSLionel Sambuc 	if (v < 10)
234*c75851fcSLionel Sambuc 		v = 10;
235*c75851fcSLionel Sambuc #else
236*c75851fcSLionel Sambuc 	if (v == 2 * nbperf->n)
237*c75851fcSLionel Sambuc 		++v;
238*c75851fcSLionel Sambuc #endif
239*c75851fcSLionel Sambuc 
240*c75851fcSLionel Sambuc 	state.g = calloc(sizeof(uint32_t), v);
241*c75851fcSLionel Sambuc 	state.visited = calloc(sizeof(uint8_t), v);
242*c75851fcSLionel Sambuc 	if (state.g == NULL || state.visited == NULL)
243*c75851fcSLionel Sambuc 		err(1, "malloc failed");
244*c75851fcSLionel Sambuc 
245*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
246*c75851fcSLionel Sambuc 	graph3_setup(&state.graph, v, e);
247*c75851fcSLionel Sambuc 	if (graph3_hash(nbperf, &state.graph))
248*c75851fcSLionel Sambuc 		goto failed;
249*c75851fcSLionel Sambuc 	if (graph3_output_order(&state.graph))
250*c75851fcSLionel Sambuc 		goto failed;
251*c75851fcSLionel Sambuc #else
252*c75851fcSLionel Sambuc 	graph2_setup(&state.graph, v, e);
253*c75851fcSLionel Sambuc 	if (graph2_hash(nbperf, &state.graph))
254*c75851fcSLionel Sambuc 		goto failed;
255*c75851fcSLionel Sambuc 	if (graph2_output_order(&state.graph))
256*c75851fcSLionel Sambuc 		goto failed;
257*c75851fcSLionel Sambuc #endif
258*c75851fcSLionel Sambuc 	assign_nodes(&state);
259*c75851fcSLionel Sambuc 	print_hash(nbperf, &state);
260*c75851fcSLionel Sambuc 
261*c75851fcSLionel Sambuc 	retval = 0;
262*c75851fcSLionel Sambuc 
263*c75851fcSLionel Sambuc failed:
264*c75851fcSLionel Sambuc #ifdef BUILD_CHM3
265*c75851fcSLionel Sambuc 	graph3_free(&state.graph);
266*c75851fcSLionel Sambuc #else
267*c75851fcSLionel Sambuc 	graph2_free(&state.graph);
268*c75851fcSLionel Sambuc #endif
269*c75851fcSLionel Sambuc 	free(state.g);
270*c75851fcSLionel Sambuc 	free(state.visited);
271*c75851fcSLionel Sambuc 	return retval;
272*c75851fcSLionel Sambuc }
273