1 /* $NetBSD: nbperf-chm.c,v 1.1 2009/08/15 16:21:05 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
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: nbperf-chm.c,v 1.1 2009/08/15 16:21:05 joerg Exp $");
52 * A full description of the algorithm can be found in:
53 * "An optimal algorithm for generating minimal perfect hash functions"
54 * by Czech, Havas and Majewski in Information Processing Letters,
55 * 43(5):256-264, October 1992.
59 * The algorithm is based on random, acyclic graphs.
61 * Each edge in the represents a key. The vertices are the reminder of
62 * the hash function mod n. n = cm with c > 2, otherwise the propability
63 * of finding an acyclic graph is very low (for 2-graphs). The constant
64 * for 3-graphs is 1.24.
66 * After the hashing phase, the graph is checked for cycles.
67 * A cycle-free graph is either empty or has a vertex of degree 1.
68 * Removing the edge for this vertex doesn't change this property,
69 * so applying this recursively reduces the size of the graph.
70 * If the graph is empty at the end of the process, it was acyclic.
72 * The assignment step now sets g[i] := 0 and processes the edges
73 * in reverse order of removal. That ensures that at least one vertex
74 * is always unvisited and can be assigned.
88 assign_nodes(struct state
*state
)
98 for (i
= 0; i
< state
->graph
.e
; ++i
) {
99 e_idx
= state
->graph
.output_order
[i
];
100 e
= &state
->graph
.edges
[e_idx
];
103 if (!state
->visited
[e
->left
]) {
104 state
->g
[e
->left
] = (2 * state
->graph
.e
+ e_idx
105 - state
->g
[e
->middle
] - state
->g
[e
->right
])
107 } else if (!state
->visited
[e
->middle
]) {
108 state
->g
[e
->middle
] = (2 * state
->graph
.e
+ e_idx
109 - state
->g
[e
->left
] - state
->g
[e
->right
])
112 state
->g
[e
->right
] = (2 * state
->graph
.e
+ e_idx
113 - state
->g
[e
->left
] - state
->g
[e
->middle
])
116 state
->visited
[e
->left
] = 1;
117 state
->visited
[e
->middle
] = 1;
118 state
->visited
[e
->right
] = 1;
120 if (!state
->visited
[e
->left
]) {
121 state
->g
[e
->left
] = (state
->graph
.e
+ e_idx
122 - state
->g
[e
->right
]) % state
->graph
.e
;
124 state
->g
[e
->right
] = (state
->graph
.e
+ e_idx
125 - state
->g
[e
->left
]) % state
->graph
.e
;
127 state
->visited
[e
->left
] = 1;
128 state
->visited
[e
->right
] = 1;
134 print_hash(struct nbperf
*nbperf
, struct state
*state
)
136 uint32_t i
, per_line
;
140 fprintf(nbperf
->output
, "#include <stdlib.h>\n\n");
142 fprintf(nbperf
->output
, "%suint32_t\n",
143 nbperf
->static_hash
? "static " : "");
144 fprintf(nbperf
->output
,
145 "%s(const void * __restrict key, size_t keylen)\n",
147 fprintf(nbperf
->output
, "{\n");
148 if (state
->graph
.v
>= 65536) {
152 } else if (state
->graph
.v
>= 256) {
161 fprintf(nbperf
->output
, "\tstatic const %s g[%" PRId32
"] = {\n",
162 g_type
, state
->graph
.v
);
163 for (i
= 0; i
< state
->graph
.v
; ++i
) {
164 fprintf(nbperf
->output
, "%s0x%0*" PRIx32
",%s",
165 (i
% per_line
== 0 ? "\t " : " "),
166 g_width
, state
->g
[i
],
167 (i
% per_line
== per_line
- 1 ? "\n" : ""));
169 if (i
% per_line
!= 0)
170 fprintf(nbperf
->output
, "\n\t};\n");
172 fprintf(nbperf
->output
, "\t};\n");
173 fprintf(nbperf
->output
, "\tuint32_t h[%zu];\n\n", nbperf
->hash_size
);
174 (*nbperf
->print_hash
)(nbperf
, "\t", "key", "keylen", "h");
176 fprintf(nbperf
->output
, "\treturn (g[h[0] %% %" PRIu32
"] + "
177 "g[h[1] %% %" PRIu32
"] + "
178 "g[h[2] %% %" PRIu32
"]) %% %" PRIu32
";\n",
179 state
->graph
.v
, state
->graph
.v
, state
->graph
.v
, state
->graph
.e
);
181 fprintf(nbperf
->output
, "\treturn (g[h[0] %% %" PRIu32
"] + "
182 "g[h[1] %% %" PRIu32
"]) %% %" PRIu32
";\n",
183 state
->graph
.v
, state
->graph
.v
, state
->graph
.e
);
185 fprintf(nbperf
->output
, "}\n");
187 if (nbperf
->map_output
!= NULL
) {
188 for (i
= 0; i
< state
->graph
.e
; ++i
)
189 fprintf(nbperf
->map_output
, "%" PRIu32
"\n", i
);
195 chm3_compute(struct nbperf
*nbperf
)
197 chm_compute(struct nbperf
*nbperf
)
208 if (nbperf
->c
< 1.24)
209 errx(1, "The argument for option -c must be at least 1.24");
211 if (nbperf
->hash_size
< 3)
212 errx(1, "The hash function must generate at least 3 values");
218 errx(1, "The argument for option -c must be at least 2");
220 if (nbperf
->hash_size
< 2)
221 errx(1, "The hash function must generate at least 2 values");
224 (*nbperf
->seed_hash
)(nbperf
);
226 v
= nbperf
->c
* nbperf
->n
;
228 if (v
== 1.24 * nbperf
->n
)
233 if (v
== 2 * nbperf
->n
)
237 state
.g
= calloc(sizeof(uint32_t), v
);
238 state
.visited
= calloc(sizeof(uint8_t), v
);
239 if (state
.g
== NULL
|| state
.visited
== NULL
)
240 err(1, "malloc failed");
243 graph3_setup(&state
.graph
, v
, e
);
244 if (graph3_hash(nbperf
, &state
.graph
))
246 if (graph3_output_order(&state
.graph
))
249 graph2_setup(&state
.graph
, v
, e
);
250 if (graph2_hash(nbperf
, &state
.graph
))
252 if (graph2_output_order(&state
.graph
))
255 assign_nodes(&state
);
256 print_hash(nbperf
, &state
);
262 graph3_free(&state
.graph
);
264 graph2_free(&state
.graph
);