1 /* $NetBSD: nbperf-chm.c,v 1.3 2011/10/21 23:47:11 joerg Exp $ */
3 * Copyright (c) 2009 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Joerg Sonnenberger.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: nbperf-chm.c,v 1.3 2011/10/21 23:47:11 joerg Exp $");
55 * A full description of the algorithm can be found in:
56 * "An optimal algorithm for generating minimal perfect hash functions"
57 * by Czech, Havas and Majewski in Information Processing Letters,
58 * 43(5):256-264, October 1992.
62 * The algorithm is based on random, acyclic graphs.
64 * Each edge in the represents a key. The vertices are the reminder of
65 * the hash function mod n. n = cm with c > 2, otherwise the propability
66 * of finding an acyclic graph is very low (for 2-graphs). The constant
67 * for 3-graphs is 1.24.
69 * After the hashing phase, the graph is checked for cycles.
70 * A cycle-free graph is either empty or has a vertex of degree 1.
71 * Removing the edge for this vertex doesn't change this property,
72 * so applying this recursively reduces the size of the graph.
73 * If the graph is empty at the end of the process, it was acyclic.
75 * The assignment step now sets g[i] := 0 and processes the edges
76 * in reverse order of removal. That ensures that at least one vertex
77 * is always unvisited and can be assigned.
91 assign_nodes(struct state
*state
)
101 for (i
= 0; i
< state
->graph
.e
; ++i
) {
102 e_idx
= state
->graph
.output_order
[i
];
103 e
= &state
->graph
.edges
[e_idx
];
106 if (!state
->visited
[e
->left
]) {
107 state
->g
[e
->left
] = (2 * state
->graph
.e
+ e_idx
108 - state
->g
[e
->middle
] - state
->g
[e
->right
])
110 } else if (!state
->visited
[e
->middle
]) {
111 state
->g
[e
->middle
] = (2 * state
->graph
.e
+ e_idx
112 - state
->g
[e
->left
] - state
->g
[e
->right
])
115 state
->g
[e
->right
] = (2 * state
->graph
.e
+ e_idx
116 - state
->g
[e
->left
] - state
->g
[e
->middle
])
119 state
->visited
[e
->left
] = 1;
120 state
->visited
[e
->middle
] = 1;
121 state
->visited
[e
->right
] = 1;
123 if (!state
->visited
[e
->left
]) {
124 state
->g
[e
->left
] = (state
->graph
.e
+ e_idx
125 - state
->g
[e
->right
]) % state
->graph
.e
;
127 state
->g
[e
->right
] = (state
->graph
.e
+ e_idx
128 - state
->g
[e
->left
]) % state
->graph
.e
;
130 state
->visited
[e
->left
] = 1;
131 state
->visited
[e
->right
] = 1;
137 print_hash(struct nbperf
*nbperf
, struct state
*state
)
139 uint32_t i
, per_line
;
143 fprintf(nbperf
->output
, "#include <stdlib.h>\n\n");
145 fprintf(nbperf
->output
, "%suint32_t\n",
146 nbperf
->static_hash
? "static " : "");
147 fprintf(nbperf
->output
,
148 "%s(const void * __restrict key, size_t keylen)\n",
150 fprintf(nbperf
->output
, "{\n");
151 if (state
->graph
.v
>= 65536) {
155 } else if (state
->graph
.v
>= 256) {
164 fprintf(nbperf
->output
, "\tstatic const %s g[%" PRId32
"] = {\n",
165 g_type
, state
->graph
.v
);
166 for (i
= 0; i
< state
->graph
.v
; ++i
) {
167 fprintf(nbperf
->output
, "%s0x%0*" PRIx32
",%s",
168 (i
% per_line
== 0 ? "\t " : " "),
169 g_width
, state
->g
[i
],
170 (i
% per_line
== per_line
- 1 ? "\n" : ""));
172 if (i
% per_line
!= 0)
173 fprintf(nbperf
->output
, "\n\t};\n");
175 fprintf(nbperf
->output
, "\t};\n");
176 fprintf(nbperf
->output
, "\tuint32_t h[%zu];\n\n", nbperf
->hash_size
);
177 (*nbperf
->print_hash
)(nbperf
, "\t", "key", "keylen", "h");
179 fprintf(nbperf
->output
, "\treturn (g[h[0] %% %" PRIu32
"] + "
180 "g[h[1] %% %" PRIu32
"] + "
181 "g[h[2] %% %" PRIu32
"]) %% %" PRIu32
";\n",
182 state
->graph
.v
, state
->graph
.v
, state
->graph
.v
, state
->graph
.e
);
184 fprintf(nbperf
->output
, "\treturn (g[h[0] %% %" PRIu32
"] + "
185 "g[h[1] %% %" PRIu32
"]) %% %" PRIu32
";\n",
186 state
->graph
.v
, state
->graph
.v
, state
->graph
.e
);
188 fprintf(nbperf
->output
, "}\n");
190 if (nbperf
->map_output
!= NULL
) {
191 for (i
= 0; i
< state
->graph
.e
; ++i
)
192 fprintf(nbperf
->map_output
, "%" PRIu32
"\n", i
);
198 chm3_compute(struct nbperf
*nbperf
)
200 chm_compute(struct nbperf
*nbperf
)
211 if (nbperf
->c
< 1.24)
212 errx(1, "The argument for option -c must be at least 1.24");
214 if (nbperf
->hash_size
< 3)
215 errx(1, "The hash function must generate at least 3 values");
221 errx(1, "The argument for option -c must be at least 2");
223 if (nbperf
->hash_size
< 2)
224 errx(1, "The hash function must generate at least 2 values");
227 (*nbperf
->seed_hash
)(nbperf
);
229 v
= nbperf
->c
* nbperf
->n
;
231 if (v
== 1.24 * nbperf
->n
)
236 if (v
== 2 * nbperf
->n
)
240 state
.g
= calloc(sizeof(uint32_t), v
);
241 state
.visited
= calloc(sizeof(uint8_t), v
);
242 if (state
.g
== NULL
|| state
.visited
== NULL
)
243 err(1, "malloc failed");
246 graph3_setup(&state
.graph
, v
, e
);
247 if (graph3_hash(nbperf
, &state
.graph
))
249 if (graph3_output_order(&state
.graph
))
252 graph2_setup(&state
.graph
, v
, e
);
253 if (graph2_hash(nbperf
, &state
.graph
))
255 if (graph2_output_order(&state
.graph
))
258 assign_nodes(&state
);
259 print_hash(nbperf
, &state
);
265 graph3_free(&state
.graph
);
267 graph2_free(&state
.graph
);