FD.io VPP  v16.06
Vector Packet Processing
phash.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2005 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 /* This is all stolen from Bob Jenkins and reworked for clib. Thanks
39  once again Bob for the great work. */
40 
41 /*
42 ------------------------------------------------------------------------------
43 perfect.c: code to generate code for a hash for perfect hashing.
44 (c) Bob Jenkins, September 1996, December 1999
45 You may use this code in any way you wish, and it is free. No warranty.
46 I hereby place this in the public domain.
47 Source is http://burtleburtle.net/bob/c/perfect.c
48 
49 This generates a minimal perfect hash function. That means, given a
50 set of n keys, this determines a hash function that maps each of
51 those keys into a value in 0..n-1 with no collisions.
52 
53 The perfect hash function first uses a normal hash function on the key
54 to determine (a,b) such that the pair (a,b) is distinct for all
55 keys, then it computes a^scramble[tab[b]] to get the final perfect hash.
56 tab[] is an array of 1-byte values and scramble[] is a 256-term array of
57 2-byte or 4-byte values. If there are n keys, the length of tab[] is a
58 power of two between n/3 and n.
59 
60 I found the idea of computing distinct (a,b) values in "Practical minimal
61 perfect hash functions for large databases", Fox, Heath, Chen, and Daoud,
62 Communications of the ACM, January 1992. They found the idea in Chichelli
63 (CACM Jan 1980). Beyond that, our methods differ.
64 
65 The key is hashed to a pair (a,b) where a in 0..*alen*-1 and b in
66 0..*blen*-1. A fast hash function determines both a and b
67 simultaneously. Any decent hash function is likely to produce
68 hashes so that (a,b) is distinct for all pairs. I try the hash
69 using different values of *salt* until all pairs are distinct.
70 
71 The final hash is (a XOR scramble[tab[b]]). *scramble* is a
72 predetermined mapping of 0..255 into 0..smax-1. *tab* is an
73 array that we fill in in such a way as to make the hash perfect.
74 
75 First we fill in all values of *tab* that are used by more than one
76 key. We try all possible values for each position until one works.
77 
78 This leaves m unmapped keys and m values that something could hash to.
79 If you treat unmapped keys as lefthand nodes and unused hash values
80 as righthand nodes, and draw a line connecting each key to each hash
81 value it could map to, you get a bipartite graph. We attempt to
82 find a perfect matching in this graph. If we succeed, we have
83 determined a perfect hash for the whole set of keys.
84 
85 *scramble* is used because (a^tab[i]) clusters keys around *a*.
86 ------------------------------------------------------------------------------
87 */
88 
89 #include <vppinfra/bitmap.h>
90 #include <vppinfra/format.h>
91 #include <vppinfra/phash.h>
92 #include <vppinfra/random.h>
93 
95 {
96  int n_keys_left, b_mask, a_shift;
97  u32 seed;
98  phash_key_t * k;
99 
100  seed = pm->hash_seed;
101  b_mask = (1 << pm->b_bits) - 1;
102  a_shift = BITS (seed) - pm->a_bits;
103 
104  k = pm->keys;
105  n_keys_left = vec_len (pm->keys);
106 
107  while (n_keys_left >= 2)
108  {
109  u32 x0, y0, z0;
110  u32 x1, y1, z1;
111 
112  x0 = y0 = z0 = seed;
113  x1 = y1 = z1 = seed;
114  x0 += (u32) k[0].key;
115  x1 += (u32) k[1].key;
116 
117  hash_mix32 (x0, y0, z0);
118  hash_mix32 (x1, y1, z1);
119 
120  k[0].b = z0 & b_mask;
121  k[1].b = z1 & b_mask;
122  k[0].a = z0 >> a_shift;
123  k[1].a = z1 >> a_shift;
124  if (PREDICT_FALSE (a_shift >= BITS (z0)))
125  k[0].a = k[1].a = 0;
126 
127  k += 2;
128  n_keys_left -= 2;
129  }
130 
131  if (n_keys_left >= 1)
132  {
133  u32 x0, y0, z0;
134 
135  x0 = y0 = z0 = seed;
136  x0 += k[0].key;
137 
138  hash_mix32 (x0, y0, z0);
139 
140  k[0].b = z0 & b_mask;
141  k[0].a = z0 >> a_shift;
142  if (PREDICT_FALSE (a_shift >= BITS (z0)))
143  k[0].a = 0;
144 
145  k += 1;
146  n_keys_left -= 1;
147  }
148 }
149 
151 {
152  int n_keys_left, b_mask, a_shift;
153  u64 seed;
154  phash_key_t * k;
155 
156  seed = pm->hash_seed;
157  b_mask = (1 << pm->b_bits) - 1;
158  a_shift = BITS (seed) - pm->a_bits;
159 
160  k = pm->keys;
161  n_keys_left = vec_len (pm->keys);
162 
163  while (n_keys_left >= 2)
164  {
165  u64 x0, y0, z0;
166  u64 x1, y1, z1;
167 
168  x0 = y0 = z0 = seed;
169  x1 = y1 = z1 = seed;
170  x0 += (u64) k[0].key;
171  x1 += (u64) k[1].key;
172 
173  hash_mix64 (x0, y0, z0);
174  hash_mix64 (x1, y1, z1);
175 
176  k[0].b = z0 & b_mask;
177  k[1].b = z1 & b_mask;
178  k[0].a = z0 >> a_shift;
179  k[1].a = z1 >> a_shift;
180  if (PREDICT_FALSE (a_shift >= BITS (z0)))
181  k[0].a = k[1].a = 0;
182 
183  k += 2;
184  n_keys_left -= 2;
185  }
186 
187  if (n_keys_left >= 1)
188  {
189  u64 x0, y0, z0;
190 
191  x0 = y0 = z0 = seed;
192  x0 += k[0].key;
193 
194  hash_mix64 (x0, y0, z0);
195 
196  k[0].b = z0 & b_mask;
197  k[0].a = z0 >> a_shift;
198  if (PREDICT_FALSE (a_shift >= BITS (z0)))
199  k[0].a = 0;
200 
201  k += 1;
202  n_keys_left -= 1;
203  }
204 }
205 
207 {
208  int n_keys_left, b_mask, a_shift;
209  u32 seed;
210  phash_key_t * k;
211 
212  seed = pm->hash_seed;
213  b_mask = (1 << pm->b_bits) - 1;
214  a_shift = BITS (seed) - pm->a_bits;
215 
216  k = pm->keys;
217  n_keys_left = vec_len (pm->keys);
218 
219  while (n_keys_left >= 2)
220  {
221  u32 xyz[6];
222  u32 x0, y0, z0;
223  u32 x1, y1, z1;
224 
225  pm->key_seed2 (pm->private, k[0].key, k[1].key, &xyz);
226 
227  x0 = y0 = z0 = seed;
228  x1 = y1 = z1 = seed;
229  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
230  x1 += xyz[3]; y1 += xyz[4]; z1 += xyz[5];
231 
232  hash_mix32 (x0, y0, z0);
233  hash_mix32 (x1, y1, z1);
234 
235  k[0].b = z0 & b_mask;
236  k[1].b = z1 & b_mask;
237  k[0].a = z0 >> a_shift;
238  k[1].a = z1 >> a_shift;
239  if (PREDICT_FALSE (a_shift >= BITS (z0)))
240  k[0].a = k[1].a = 0;
241 
242  k += 2;
243  n_keys_left -= 2;
244  }
245 
246  if (n_keys_left >= 1)
247  {
248  u32 xyz[3];
249  u32 x0, y0, z0;
250 
251  pm->key_seed1 (pm->private, k[0].key, &xyz);
252 
253  x0 = y0 = z0 = seed;
254  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
255 
256  hash_mix32 (x0, y0, z0);
257 
258  k[0].b = z0 & b_mask;
259  k[0].a = z0 >> a_shift;
260  if (PREDICT_FALSE (a_shift >= BITS (z0)))
261  k[0].a = 0;
262 
263  k += 1;
264  n_keys_left -= 1;
265  }
266 }
267 
269 {
270  int n_keys_left, b_mask, a_shift;
271  u64 seed;
272  phash_key_t * k;
273 
274  seed = pm->hash_seed;
275  b_mask = (1 << pm->b_bits) - 1;
276  a_shift = BITS (seed) - pm->a_bits;
277 
278  k = pm->keys;
279  n_keys_left = vec_len (pm->keys);
280 
281  while (n_keys_left >= 2)
282  {
283  u64 xyz[6];
284  u64 x0, y0, z0;
285  u64 x1, y1, z1;
286 
287  pm->key_seed2 (pm->private, k[0].key, k[1].key, &xyz);
288 
289  x0 = y0 = z0 = seed;
290  x1 = y1 = z1 = seed;
291  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
292  x1 += xyz[3]; y1 += xyz[4]; z1 += xyz[5];
293 
294  hash_mix64 (x0, y0, z0);
295  hash_mix64 (x1, y1, z1);
296 
297  k[0].b = z0 & b_mask;
298  k[1].b = z1 & b_mask;
299  k[0].a = z0 >> a_shift;
300  k[1].a = z1 >> a_shift;
301  if (PREDICT_FALSE (a_shift >= BITS (z0)))
302  k[0].a = k[1].a = 0;
303 
304  k += 2;
305  n_keys_left -= 2;
306  }
307 
308  if (n_keys_left >= 1)
309  {
310  u64 xyz[3];
311  u64 x0, y0, z0;
312 
313  pm->key_seed1 (pm->private, k[0].key, &xyz);
314 
315  x0 = y0 = z0 = seed;
316  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
317 
318  hash_mix64 (x0, y0, z0);
319 
320  k[0].b = z0 & b_mask;
321  k[0].a = z0 >> a_shift;
322  if (PREDICT_FALSE (a_shift >= BITS (z0)))
323  k[0].a = 0;
324 
325  k += 1;
326  n_keys_left -= 1;
327  }
328 }
329 
330 /*
331  * insert keys into table according to key->b
332  * check if the initial hash might work
333  */
334 static int init_tabb (phash_main_t * pm)
335 {
336  int no_collisions;
337  phash_tabb_t * tb;
338  phash_key_t * k, * l;
339 
340  if (pm->key_seed1)
341  {
342  if (pm->flags & PHASH_FLAG_MIX64)
344  else
346  }
347  else
348  {
349  if (pm->flags & PHASH_FLAG_MIX64)
351  else
353  }
354 
355  if (! pm->tabb)
356  vec_resize (pm->tabb, 1 << pm->b_bits);
357  else
358  vec_foreach (tb, pm->tabb)
359  phash_tabb_free (tb);
360 
361  /* Two keys with the same (a,b) guarantees a collision */
362  no_collisions = 1;
363  vec_foreach (k, pm->keys)
364  {
365  u32 i, * ki;
366 
367  tb = pm->tabb + k->b;
368  ki = tb->keys;
369  for (i = 0; i < vec_len (ki); i++)
370  {
371  l = pm->keys + ki[i];
372  if (k->a == l->a)
373  {
374  /* Given keys are supposed to be unique. */
375  if (pm->key_is_equal
376  && pm->key_is_equal (pm->private, l->key, k->key))
377  clib_error ("duplicate keys");
378  no_collisions = 0;
379  goto done;
380  }
381  }
382 
383  vec_add1 (tb->keys, k - pm->keys);
384  }
385 
386  done:
387  return no_collisions;
388 }
389 
390 /* Try to apply an augmenting list */
391 static int apply (phash_main_t * pm, u32 tail, u32 rollback)
392 {
393  phash_key_t * k;
394  phash_tabb_t * pb;
395  phash_tabq_t * q_child, * q_parent;
396  u32 ki, i, hash, child, parent;
397  u32 stabb; /* scramble[tab[b]] */
398  int no_collision;
399 
400  no_collision = 1;
401 
402  /* Walk from child to parent until root is reached. */
403  for (child = tail - 1; child; child = parent)
404  {
405  q_child = &pm->tabq[child];
406  parent = q_child->parent_q;
407  q_parent = &pm->tabq[parent];
408 
409  /* find parent's list of siblings */
410  ASSERT (q_parent->b_q < vec_len (pm->tabb));
411  pb = pm->tabb + q_parent->b_q;
412 
413  /* erase old hash values */
414  stabb = pm->scramble[pb->val_b];
415  for (i = 0; i < vec_len (pb->keys); i++)
416  {
417  ki = pb->keys[i];
418  k = pm->keys + ki;
419  hash = k->a ^ stabb;
420 
421  /* Erase hash for all of child's siblings. */
422  if (ki == pm->tabh[hash])
423  pm->tabh[hash] = ~0;
424  }
425 
426  /* change pb->val_b, which will change the hashes of all parent siblings */
427  pb->val_b = rollback ? q_child->oldval_q : q_child->newval_q;
428 
429  /* set new hash values */
430  stabb = pm->scramble[pb->val_b];
431  for (i = 0; i < vec_len (pb->keys); i++)
432  {
433  ki = pb->keys[i];
434  k = pm->keys + ki;
435 
436  hash = k->a ^ stabb;
437  if (rollback)
438  {
439  if (parent == 0) continue; /* root never had a hash */
440  }
441  else if (pm->tabh[hash] != ~0)
442  {
443  /* Very rare case: roll back any changes. */
444  apply (pm, tail, /* rollback changes */ 1);
445  no_collision = 0;
446  goto done;
447  }
448  pm->tabh[hash] = ki;
449  }
450  }
451 
452  done:
453  return no_collision;
454 }
455 
456 
457 /*
458 -------------------------------------------------------------------------------
459 augment(): Add item to the mapping.
460 
461 Construct a spanning tree of *b*s with *item* as root, where each
462 parent can have all its hashes changed (by some new val_b) with
463 at most one collision, and each child is the b of that collision.
464 
465 I got this from Tarjan's "Data Structures and Network Algorithms". The
466 path from *item* to a *b* that can be remapped with no collision is
467 an "augmenting path". Change values of tab[b] along the path so that
468 the unmapped key gets mapped and the unused hash value gets used.
469 
470 Assuming 1 key per b, if m out of n hash values are still unused,
471 you should expect the transitive closure to cover n/m nodes before
472 an unused node is found. Sum(i=1..n)(n/i) is about nlogn, so expect
473 this approach to take about nlogn time to map all single-key b's.
474 -------------------------------------------------------------------------------
475 
476 high_water: a value higher than any now in tabb[].water_b.
477 */
478 static int augment (phash_main_t * pm, u32 b_root, u32 high_water)
479 {
480  u32 q; /* current position walking through the queue */
481  u32 tail; /* tail of the queue. 0 is the head of the queue. */
482  phash_tabb_t * tb_parent, * tb_child, * tb_hit;
483  phash_key_t * k_parent, * k_child;
484  u32 v, v_limit; /* possible value for myb->val_b */
485  u32 i, ki, hash;
486 
487  v_limit = 1 << ((pm->flags & PHASH_FLAG_USE_SCRAMBLE) ? pm->s_bits : BITS (u8));
488 
489  /* Initialize the root of the spanning tree. */
490  pm->tabq[0].b_q = b_root;
491  tail = 1;
492 
493  /* construct the spanning tree by walking the queue, add children to tail */
494  for (q = 0; q < tail; q++)
495  {
496  if ((pm->flags & PHASH_FLAG_FAST_MODE)
497  && ! (pm->flags & PHASH_FLAG_MINIMAL)
498  && q == 1)
499  break; /* don't do transitive closure */
500 
501  tb_parent = pm->tabb + pm->tabq[q].b_q; /* the b for this node */
502 
503  for (v = 0; v < v_limit; v++)
504  {
505  tb_child = 0;
506 
507  for (i = 0; i < vec_len (tb_parent->keys); i++)
508  {
509  ki = tb_parent->keys[i];
510  k_parent = pm->keys + ki;
511 
512  hash = k_parent->a ^ pm->scramble[v];
513  if (hash >= pm->hash_max)
514  goto try_next_v; /* hash code out of bounds => we can't use this v */
515 
516  ki = pm->tabh[hash];
517  if (ki == ~0)
518  continue;
519 
520  k_child = pm->keys + ki;
521  tb_hit = pm->tabb + k_child->b;
522 
523  if (tb_child)
524  {
525  /* Hit at most one child b. */
526  if (tb_child == tb_hit)
527  goto try_next_v;
528  }
529  else
530  {
531  /* Remember this as child b. */
532  tb_child = tb_hit;
533  if (tb_hit->water_b == high_water)
534  goto try_next_v; /* already explored */
535  }
536  }
537 
538  /* tb_parent with v has either one or zero collisions. */
539 
540  /* add childb to the queue of reachable things */
541  if (tb_child)
542  tb_child->water_b = high_water;
543  pm->tabq[tail].b_q = tb_child ? tb_child - pm->tabb : ~0;
544  pm->tabq[tail].newval_q = v; /* how to make parent (myb) use this hash */
545  pm->tabq[tail].oldval_q = tb_parent->val_b; /* need this for rollback */
546  pm->tabq[tail].parent_q = q;
547  ++tail;
548 
549  /* Found a v with no collisions? */
550  if (! tb_child)
551  {
552  /* Try to apply the augmenting path. */
553  if (apply (pm, tail, /* rollback */ 0))
554  return 1; /* success, item was added to the perfect hash */
555  --tail; /* don't know how to handle such a child! */
556  }
557 
558  try_next_v:
559  ;
560  }
561  }
562  return 0;
563 }
564 
565 
567 
568 static int phash_tabb_compare (void *a1, void *a2)
569 {
570  u32 *b1 = a1;
571  u32 *b2 = a2;
572  phash_tabb_t * tb1, * tb2;
573 
574  tb1 = sort_tabb + b1[0];
575  tb2 = sort_tabb + b2[0];
576 
577  return ((int) vec_len (tb2->keys) - (int) vec_len(tb1->keys));
578 }
579 
580 /* find a mapping that makes this a perfect hash */
581 static int perfect (phash_main_t * pm)
582 {
583  u32 i;
584 
585  /* clear any state from previous attempts */
586  if (vec_bytes(pm->tabh))
587  memset (pm->tabh, ~0, vec_bytes (pm->tabh));
588 
589  vec_validate (pm->tabb_sort, vec_len (pm->tabb) - 1);
590  for (i = 0; i < vec_len (pm->tabb_sort); i++)
591  pm->tabb_sort[i] = i;
592 
593  sort_tabb = pm->tabb;
594 
596 
597  /* In descending order by number of keys, map all *b*s */
598  for (i = 0; i < vec_len (pm->tabb_sort); i++)
599  {
600  if (! augment(pm, pm->tabb_sort[i], i + 1))
601  return 0;
602  }
603 
604  /* Success! We found a perfect hash of all keys into 0..nkeys-1. */
605  return 1;
606 }
607 
608 
609 /*
610  * Find initial a_bits = log2 (a_max), b_bits = log2 (b_max).
611  * Initial a_max and b_max values were found empirically. Some factors:
612  *
613  * If s_max<256 there is no scramble, so tab[b] needs to cover 0..s_max-1.
614  *
615  * a_max and b_max must be powers of 2 because the values in 0..a_max-1 and
616  * 0..b_max-1 are produced by applying a bitmask to the initial hash function.
617  *
618  * a_max must be less than s_max, in fact less than n_keys, because otherwise
619  * there would often be no i such that a^scramble[i] is in 0..n_keys-1 for
620  * all the *a*s associated with a given *b*, so there would be no legal
621  * value to assign to tab[b]. This only matters when we're doing a minimal
622  * perfect hash.
623  *
624  * It takes around 800 trials to find distinct (a,b) with nkey=s_max*(5/8)
625  * and a_max*b_max = s_max*s_max/32.
626  *
627  * Values of b_max less than s_max/4 never work, and s_max/2 always works.
628  *
629  * We want b_max as small as possible because it is the number of bytes in
630  * the huge array we must create for the perfect hash.
631  *
632  * When nkey <= s_max*(5/8), b_max=s_max/4 works much more often with
633  * a_max=s_max/8 than with a_max=s_max/4. Above s_max*(5/8), b_max=s_max/4
634  * doesn't seem to care whether a_max=s_max/8 or a_max=s_max/4. I think it
635  * has something to do with 5/8 = 1/8 * 5. For example examine 80000,
636  * 85000, and 90000 keys with different values of a_max. This only matters
637  * if we're doing a minimal perfect hash.
638  *
639  * When a_max*b_max <= 1<<U32BITS, the initial hash must produce one integer.
640  * Bigger than that it must produce two integers, which increases the
641  * cost of the hash per character hashed.
642  */
644 {
645  u32 s_bits, s_max, a_max, b_max, n_keys;
646  int is_minimal, is_fast_mode;
647  const u32 b_max_use_scramble_threshold = 4096;
648 
649  is_minimal = (pm->flags & PHASH_FLAG_MINIMAL) != 0;
650  is_fast_mode = (pm->flags & PHASH_FLAG_FAST_MODE) != 0;
651 
652  n_keys = vec_len (pm->keys);
653  s_bits = max_log2 (n_keys);
654  s_max = 1 << s_bits;
655  a_max = 0;
656 
657  if (is_minimal)
658  {
659  switch (s_bits)
660  {
661  case 0:
662  a_max = 1;
663  b_max = 1;
664  case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
665  a_max = is_minimal ? s_max / 2 : s_max;
666  b_max = s_max/2;
667  break;
668  case 9: case 10: case 11: case 12: case 13:
669  case 14: case 15: case 16: case 17:
670  if (is_fast_mode)
671  {
672  a_max = s_max/2;
673  b_max = s_max/4;
674  }
675  else if (s_max/4 < b_max_use_scramble_threshold)
676  {
677  if (n_keys <= s_max*0.52)
678  a_max = b_max = s_max/8;
679  else
680  a_max = b_max = s_max/4;
681  }
682  else
683  {
684  a_max = ((n_keys <= s_max*(5.0/8.0)) ? s_max/8 :
685  (n_keys <= s_max*(3.0/4.0)) ? s_max/4 : s_max/2);
686  b_max = s_max/4; /* always give the small size a shot */
687  }
688  break;
689  case 18:
690  if (is_fast_mode)
691  a_max = b_max = s_max/2;
692  else
693  {
694  a_max = s_max/8; /* never require the multiword hash */
695  b_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/4 : s_max/2;
696  }
697  break;
698  case 19:
699  case 20:
700  a_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/8 : s_max/2;
701  b_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/4 : s_max/2;
702  break;
703  default:
704  /* Just find a hash as quick as possible.
705  We'll be thrashing virtual memory at this size. */
706  a_max = b_max = s_max/2;
707  break;
708  }
709  }
710  else
711  {
712  /* Non-minimal perfect hash. */
713  if (is_fast_mode && n_keys > s_max*0.8)
714  {
715  s_max *= 2;
716  s_bits += 1;
717  }
718 
719  if (s_max/4 <= (1 << 14))
720  b_max = ((n_keys <= s_max*0.56) ? s_max/32 :
721  (n_keys <= s_max*0.74) ? s_max/16 : s_max/8);
722  else
723  b_max = ((n_keys <= s_max*0.6) ? s_max/16 :
724  (n_keys <= s_max*0.8) ? s_max/8 : s_max/4);
725 
726  if (is_fast_mode && b_max < s_max/8)
727  b_max = s_max/8;
728 
729  if (a_max < 1) a_max = 1;
730  if (b_max < 1) b_max = 1;
731  }
732 
733  ASSERT (s_max == (1 << s_bits));
734  ASSERT (is_pow2 (a_max));
735  ASSERT (is_pow2 (b_max));
736  pm->s_bits = s_bits;
737  pm->a_bits = min_log2 (a_max);
738  pm->b_bits = min_log2 (b_max);
739  if (b_max >= b_max_use_scramble_threshold)
741 }
742 
743 /* compute p(x), where p is a permutation of 0..(1<<nbits)-1 */
744 /* permute(0)=0. This is intended and useful. */
746 {
747  int i;
748  int mask = (1 << nbits) - 1;
749  int const2 = 1+nbits/2;
750  int const3 = 1+nbits/3;
751  int const4 = 1+nbits/4;
752  int const5 = 1+nbits/5;
753  for (i = 0; i < 20; i++)
754  {
755  x = (x + (x << const2)) & mask;
756  x = (x ^ (x >> const3));
757  x = (x + (x << const4)) & mask;
758  x = (x ^ (x >> const5));
759  }
760  return x;
761 }
762 
763 /* initialize scramble[] with distinct random values in 0..smax-1 */
764 static void scramble_init (phash_main_t * pm)
765 {
766  u32 i;
767 
768  /* fill scramble[] with distinct random integers in 0..smax-1 */
769  vec_validate (pm->scramble, (1 << (pm->s_bits < 8 ? 8 : pm->s_bits)) - 1);
770  for (i = 0; i < vec_len (pm->scramble); i++)
771  pm->scramble[i] = scramble_permute (i, pm->s_bits);
772 }
773 
774 /* Try to find a perfect hash function. */
775 clib_error_t *
777 {
778  clib_error_t * error = 0;
779  u32 max_a_bits, n_tries_this_a_b, want_minimal;
780 
781  /* guess initial values for s_max, a_max and b_max */
783 
784  want_minimal = pm->flags & PHASH_FLAG_MINIMAL;
785 
786  new_s:
787  if (pm->b_bits == 0)
788  pm->a_bits = pm->s_bits;
789 
790  max_a_bits = pm->s_bits - want_minimal;
791  if (max_a_bits < 1)
792  max_a_bits = 1;
793 
794  pm->hash_max = want_minimal ? vec_len (pm->keys) : (1 << pm->s_bits);
795 
796  scramble_init (pm);
797 
798  /* Allocate working memory. */
799  vec_free (pm->tabh);
800  vec_validate_init_empty (pm->tabh, pm->hash_max - 1, ~0);
801  vec_free (pm->tabq);
802  vec_validate (pm->tabq, 1 << pm->b_bits);
803 
804  /* Actually find the perfect hash */
805  n_tries_this_a_b = 0;
806  while (1)
807  {
808  /* Choose random hash seeds until keys become unique. */
809  pm->hash_seed = random_u64 (&pm->random_seed);
810  pm->n_seed_trials++;
811  if (init_tabb (pm))
812  {
813  /* Found unique (A, B). */
814 
815  /* Hash may already be perfect. */
816  if (pm->b_bits == 0)
817  goto done;
818 
819  pm->n_perfect_calls++;
820  if (perfect (pm))
821  goto done;
822 
823  goto increase_b;
824  }
825 
826  /* Keep trying with different seed value. */
827  n_tries_this_a_b++;
828  if (n_tries_this_a_b < 2048)
829  continue;
830 
831  /* Try to put more bits in (A,B) to make distinct (A,B) more likely */
832  if (pm->a_bits < max_a_bits)
833  pm->a_bits++;
834  else if (pm->b_bits < pm->s_bits)
835  {
836  increase_b:
837  vec_resize (pm->tabb, vec_len (pm->tabb));
838  vec_resize (pm->tabq, vec_len (pm->tabq));
839  pm->b_bits++;
840  }
841  else
842  {
843  /* Can't increase (A, B) any more, so try increasing S. */
844  goto new_s;
845  }
846  }
847 
848  done:
849  /* Construct mapping table for hash lookups. */
850  if (! error)
851  {
852  u32 b, v;
853 
854  pm->a_shift = ((pm->flags & PHASH_FLAG_MIX64) ? 64 : 32) - pm->a_bits;
855  pm->b_mask = (1 << pm->b_bits) - 1;
856 
857  vec_resize (pm->tab, vec_len (pm->tabb));
858  for (b = 0; b < vec_len (pm->tabb); b++)
859  {
860  v = pm->tabb[b].val_b;
861 
862  /* Apply scramble now for small enough value of b_bits. */
863  if (! (pm->flags & PHASH_FLAG_USE_SCRAMBLE))
864  v = pm->scramble[v];
865 
866  pm->tab[b] = v;
867  }
868  }
869 
870  /* Free working memory. */
872 
873  return error;
874 }
875 
876 /* Slow hash computation for general keys. */
878 {
879  u32 a, b, v;
880 
881  if (pm->flags & PHASH_FLAG_MIX64)
882  {
883  u64 x0, y0, z0;
884 
885  x0 = y0 = z0 = pm->hash_seed;
886 
887  if (pm->key_seed1)
888  {
889  u64 xyz[3];
890  pm->key_seed1 (pm->private, key, &xyz);
891  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
892  }
893  else
894  x0 += key;
895 
896  hash_mix64 (x0, y0, z0);
897 
898  a = z0 >> pm->a_shift;
899  b = z0 & pm->b_mask;
900  }
901  else
902  {
903  u32 x0, y0, z0;
904 
905  x0 = y0 = z0 = pm->hash_seed;
906 
907  if (pm->key_seed1)
908  {
909  u32 xyz[3];
910  pm->key_seed1 (pm->private, key, &xyz);
911  x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
912  }
913  else
914  x0 += key;
915 
916  hash_mix32 (x0, y0, z0);
917 
918  a = z0 >> pm->a_shift;
919  b = z0 & pm->b_mask;
920  }
921 
922  v = pm->tab[b];
923  if (pm->flags & PHASH_FLAG_USE_SCRAMBLE)
924  v = pm->scramble[v];
925  return a ^ v;
926 }
927 
928 /* Verify that perfect hash is perfect. */
929 clib_error_t *
931 {
932  phash_key_t * k;
933  uword * unique_bitmap = 0;
934  clib_error_t * error = 0;
935 
936  vec_foreach (k, pm->keys)
937  {
938  uword h = phash_hash_slow (pm, k->key);
939 
940  if (h >= pm->hash_max)
941  {
942  error = clib_error_return (0, "hash out of range %wd", h);
943  goto done;
944  }
945 
946  if (clib_bitmap_get (unique_bitmap, h))
947  {
948  error = clib_error_return (0, "hash non-unique");
949  goto done;
950  }
951 
952  unique_bitmap = clib_bitmap_ori (unique_bitmap, h);
953  }
954 
955  done:
956  clib_bitmap_free (unique_bitmap);
957  return error;
958 }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
static void init_keys_indirect_u32(phash_main_t *pm)
Definition: phash.c:206
void * private
Definition: phash.h:120
u32 water_b
Definition: phash.h:60
void(* key_seed1)(void *private, uword key, void *seed)
Definition: phash.h:126
clib_error_t * phash_validate(phash_main_t *pm)
Definition: phash.c:930
u8 a_shift
Definition: phash.h:86
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
a
Definition: bitmap.h:393
u32 random_seed
Definition: phash.h:132
#define PHASH_FLAG_FAST_MODE
Definition: phash.h:106
uword key
Definition: phash.h:45
#define clib_error(format, args...)
Definition: error.h:62
u32 * tab
Definition: phash.h:88
u32 b
Definition: phash.h:48
u32 parent_q
Definition: phash.h:76
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
static void guess_initial_parameters(phash_main_t *pm)
Definition: phash.c:643
always_inline uword max_log2(uword x)
Definition: clib.h:216
u32 n_perfect_calls
Definition: phash.h:148
u32 * tabh
Definition: phash.h:142
always_inline u32 scramble_permute(u32 x, u32 nbits)
Definition: phash.c:745
#define vec_bytes(v)
Number of data bytes in vector.
static int phash_tabb_compare(void *a1, void *a2)
Definition: phash.c:568
u32 oldval_q
Definition: phash.h:82
static void init_keys_direct_u32(phash_main_t *pm)
Definition: phash.c:94
#define PHASH_FLAG_MINIMAL
Definition: phash.h:110
#define always_inline
Definition: clib.h:84
clib_error_t * phash_find_perfect_hash(phash_main_t *pm)
Definition: phash.c:776
int(* key_is_equal)(void *private, uword key1, uword key2)
Definition: phash.h:123
static int augment(phash_main_t *pm, u32 b_root, u32 high_water)
Definition: phash.c:478
unsigned long u64
Definition: types.h:89
u32 n_seed_trials
Definition: phash.h:148
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:199
u32 a
Definition: phash.h:48
void(* key_seed2)(void *private, uword key1, uword key2, void *seed)
Definition: phash.h:129
u8 s_bits
Definition: phash.h:86
#define PREDICT_FALSE(x)
Definition: clib.h:97
static int init_tabb(phash_main_t *pm)
Definition: phash.c:334
always_inline void phash_main_free_working_memory(phash_main_t *pm)
Definition: phash.h:152
phash_key_t * keys
Definition: phash.h:117
always_inline uword clib_bitmap_get(uword *ai, uword i)
Definition: bitmap.h:158
static int perfect(phash_main_t *pm)
Definition: phash.c:581
u32 hash_max
Definition: phash.h:114
u32 flags
Definition: phash.h:94
#define hash_mix64(a0, b0, c0)
Definition: hash.h:466
u32 val_b
Definition: phash.h:57
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u64 hash_seed
Definition: phash.h:92
#define hash_mix32(a0, b0, c0)
Definition: hash.h:474
u32 * keys
Definition: phash.h:54
always_inline uword is_pow2(uword x)
Definition: clib.h:252
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
always_inline u64 random_u64(u32 *seed)
64-bit random number generator
Definition: random.h:112
static int apply(phash_main_t *pm, u32 tail, u32 rollback)
Definition: phash.c:391
#define clib_bitmap_free(v)
Definition: bitmap.h:76
u8 a_bits
Definition: phash.h:86
phash_tabq_t * tabq
Definition: phash.h:145
u32 b_mask
Definition: phash.h:87
u64 uword
Definition: types.h:112
u32 b_q
Definition: phash.h:73
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define PHASH_FLAG_MIX64
Definition: phash.h:98
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:898
static phash_tabb_t * sort_tabb
Definition: phash.c:566
uword phash_hash_slow(phash_main_t *pm, uword key)
Definition: phash.c:877
Linear Congruential Random Number Generator.
#define vec_foreach(var, vec)
Vector iterator.
static void init_keys_indirect_u64(phash_main_t *pm)
Definition: phash.c:268
u32 * tabb_sort
Definition: phash.h:138
always_inline uword min_log2(uword x)
Definition: clib.h:181
u32 newval_q
Definition: phash.h:79
u8 b_bits
Definition: phash.h:86
static void init_keys_direct_u64(phash_main_t *pm)
Definition: phash.c:150
#define clib_error_return(e, args...)
Definition: error.h:112
always_inline void phash_tabb_free(phash_tabb_t *b)
Definition: phash.h:64
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:443
#define BITS(x)
Definition: clib.h:58
static void scramble_init(phash_main_t *pm)
Definition: phash.c:764
#define PHASH_FLAG_USE_SCRAMBLE
Definition: phash.h:102
phash_tabb_t * tabb
Definition: phash.h:135
u32 * scramble
Definition: phash.h:89