1 /* $NetBSD: nbperf-bdz.c,v 1.5 2012/09/25 20:53:46 joerg Exp $ */
3 * Copyright (c) 2009, 2012 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 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: nbperf-bdz.c,v 1.5 2012/09/25 20:53:46 joerg Exp $");
50 * A full description of the algorithm can be found in:
51 * "Simple and Space-Efficient Minimal Perfect Hash Functions"
52 * by Botelho, Pagh and Ziviani, proceeedings of WADS 2007.
56 * The algorithm is based on random, acyclic 3-graphs.
58 * Each edge in the represents a key. The vertices are the reminder of
59 * the hash function mod n. n = cm with c > 1.23. This ensures that
60 * an acyclic graph can be found with a very high probality.
62 * An acyclic graph has an edge order, where at least one vertex of
63 * each edge hasn't been seen before. It is declares the first unvisited
64 * vertex as authoritive for the edge and assigns a 2bit value to unvisited
65 * vertices, so that the sum of all vertices of the edge modulo 4 is
66 * the index of the authoritive vertex.
81 assign_nodes(struct state
*state
)
87 for (i
= 0; i
< state
->graph
.v
; ++i
)
90 for (i
= 0; i
< state
->graph
.e
; ++i
) {
91 j
= state
->graph
.output_order
[i
];
92 e
= &state
->graph
.edges
[j
];
93 if (!state
->visited
[e
->left
]) {
96 } else if (!state
->visited
[e
->middle
]) {
100 if (state
->visited
[e
->right
])
106 state
->visited
[t
] = 2 + j
;
107 if (state
->visited
[e
->left
] == 0)
108 state
->visited
[e
->left
] = 1;
109 if (state
->visited
[e
->middle
] == 0)
110 state
->visited
[e
->middle
] = 1;
111 if (state
->visited
[e
->right
] == 0)
112 state
->visited
[e
->right
] = 1;
114 state
->g
[t
] = (9 + r
- state
->g
[e
->left
] - state
->g
[e
->middle
]
115 - state
->g
[e
->right
]) % 3;
119 for (i
= 0; i
< state
->graph
.v
; ++i
) {
121 state
->holes64k
[i
>> 16] = holes
;
124 state
->holes64
[i
>> 6] = holes
- state
->holes64k
[i
>> 16];
126 if (state
->visited
[i
] > 1) {
127 j
= state
->visited
[i
] - 2;
128 state
->result_map
[j
] = i
- holes
;
131 if (state
->g
[i
] == 3)
137 print_hash(struct nbperf
*nbperf
, struct state
*state
)
142 fprintf(nbperf
->output
, "#include <stdlib.h>\n");
143 fprintf(nbperf
->output
, "#include <strings.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");
152 fprintf(nbperf
->output
,
153 "\tstatic const uint64_t g1[%" PRId32
"] = {\n",
154 (state
->graph
.v
+ 63) / 64);
156 for (i
= 0; i
< state
->graph
.v
; ++i
) {
157 sum
|= ((uint64_t)state
->g
[i
] & 1) << (i
& 63);
159 fprintf(nbperf
->output
, "%s0x%016" PRIx64
"ULL,%s",
160 (i
/ 64 % 2 == 0 ? "\t " : " "),
162 (i
/ 64 % 2 == 1 ? "\n" : ""));
167 fprintf(nbperf
->output
, "%s0x%016" PRIx64
"ULL,%s",
168 (i
/ 64 % 2 == 0 ? "\t " : " "),
170 (i
/ 64 % 2 == 1 ? "\n" : ""));
172 fprintf(nbperf
->output
, "%s\t};\n", (i
% 2 ? "\n" : ""));
174 fprintf(nbperf
->output
,
175 "\tstatic const uint64_t g2[%" PRId32
"] = {\n",
176 (state
->graph
.v
+ 63) / 64);
178 for (i
= 0; i
< state
->graph
.v
; ++i
) {
179 sum
|= (((uint64_t)state
->g
[i
] & 2) >> 1) << (i
& 63);
181 fprintf(nbperf
->output
, "%s0x%016" PRIx64
"ULL,%s",
182 (i
/ 64 % 2 == 0 ? "\t " : " "),
184 (i
/ 64 % 2 == 1 ? "\n" : ""));
189 fprintf(nbperf
->output
, "%s0x%016" PRIx64
"ULL,%s",
190 (i
/ 64 % 2 == 0 ? "\t " : " "),
192 (i
/ 64 % 2 == 1 ? "\n" : ""));
194 fprintf(nbperf
->output
, "%s\t};\n", (i
% 2 ? "\n" : ""));
196 fprintf(nbperf
->output
,
197 "\tstatic const uint32_t holes64k[%" PRId32
"] = {\n",
198 (state
->graph
.v
+ 65535) / 65536);
199 for (i
= 0; i
< state
->graph
.v
; i
+= 65536)
200 fprintf(nbperf
->output
, "%s0x%08" PRIx32
",%s",
201 (i
/ 65536 % 4 == 0 ? "\t " : " "),
202 state
->holes64k
[i
>> 16],
203 (i
/ 65536 % 4 == 3 ? "\n" : ""));
204 fprintf(nbperf
->output
, "%s\t};\n", (i
/ 65536 % 4 ? "\n" : ""));
206 fprintf(nbperf
->output
,
207 "\tstatic const uint16_t holes64[%" PRId32
"] = {\n",
208 (state
->graph
.v
+ 63) / 64);
209 for (i
= 0; i
< state
->graph
.v
; i
+= 64)
210 fprintf(nbperf
->output
, "%s0x%04" PRIx32
",%s",
211 (i
/ 64 % 4 == 0 ? "\t " : " "),
212 state
->holes64
[i
>> 6],
213 (i
/ 64 % 4 == 3 ? "\n" : ""));
214 fprintf(nbperf
->output
, "%s\t};\n", (i
/ 64 % 4 ? "\n" : ""));
216 fprintf(nbperf
->output
, "\tuint64_t m;\n");
217 fprintf(nbperf
->output
, "\tuint32_t idx, i, idx2;\n");
218 fprintf(nbperf
->output
, "\tuint32_t h[%zu];\n\n", nbperf
->hash_size
);
220 (*nbperf
->print_hash
)(nbperf
, "\t", "key", "keylen", "h");
222 fprintf(nbperf
->output
, "\n\th[0] = h[0] %% %" PRIu32
";\n",
224 fprintf(nbperf
->output
, "\th[1] = h[1] %% %" PRIu32
";\n",
226 fprintf(nbperf
->output
, "\th[2] = h[2] %% %" PRIu32
";\n",
229 fprintf(nbperf
->output
,
230 "\tidx = 9 + ((g1[h[0] >> 6] >> (h[0] & 63)) &1)"
231 "\t + ((g1[h[1] >> 6] >> (h[1] & 63)) & 1)"
232 "\t + ((g1[h[2] >> 6] >> (h[2] & 63)) & 1)"
233 "\t - ((g2[h[0] >> 6] >> (h[0] & 63)) & 1)"
234 "\t - ((g2[h[1] >> 6] >> (h[1] & 63)) & 1)"
235 "\t - ((g2[h[2] >> 6] >> (h[2] & 63)) & 1);"
238 fprintf(nbperf
->output
,
239 "\tidx = h[idx %% 3];\n");
240 fprintf(nbperf
->output
,
241 "\tidx2 = idx - holes64[idx >> 6] - holes64k[idx >> 16];\n"
242 "\tidx2 -= popcount64(g1[idx >> 6] & g2[idx >> 6]\n"
243 "\t & (((uint64_t)1 << idx) - 1));\n"
246 fprintf(nbperf
->output
, "}\n");
248 if (nbperf
->map_output
!= NULL
) {
249 for (i
= 0; i
< state
->graph
.e
; ++i
)
250 fprintf(nbperf
->map_output
, "%" PRIu32
"\n",
251 state
->result_map
[i
]);
256 bdz_compute(struct nbperf
*nbperf
)
264 if (nbperf
->c
< 1.24)
265 errx(1, "The argument for option -c must be at least 1.24");
266 if (nbperf
->hash_size
< 3)
267 errx(1, "The hash function must generate at least 3 values");
269 (*nbperf
->seed_hash
)(nbperf
);
271 v
= nbperf
->c
* nbperf
->n
;
272 if (1.24 * nbperf
->n
> v
)
277 graph3_setup(&state
.graph
, v
, e
);
279 state
.holes64k
= calloc(sizeof(uint32_t), (v
+ 65535) / 65536);
280 state
.holes64
= calloc(sizeof(uint16_t), (v
+ 63) / 64 );
281 state
.g
= calloc(sizeof(uint32_t), v
| 63);
282 state
.visited
= calloc(sizeof(uint32_t), v
);
283 state
.result_map
= calloc(sizeof(uint32_t), e
);
285 if (state
.holes64k
== NULL
|| state
.holes64
== NULL
||
286 state
.g
== NULL
|| state
.visited
== NULL
||
287 state
.result_map
== NULL
)
288 err(1, "malloc failed");
290 if (graph3_hash(nbperf
, &state
.graph
))
292 if (graph3_output_order(&state
.graph
))
294 assign_nodes(&state
);
295 print_hash(nbperf
, &state
);
300 graph3_free(&state
.graph
);
303 free(state
.holes64k
);
305 free(state
.result_map
);