a104e64e1be045e34146f68db9ac12e65cf396ee
[vpp.git] / vppinfra / vppinfra / phash.c
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
94 static void init_keys_direct_u32 (phash_main_t * pm)
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
150 static void init_keys_direct_u64 (phash_main_t * pm)
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
206 static void init_keys_indirect_u32 (phash_main_t * pm)
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
268 static void init_keys_indirect_u64 (phash_main_t * pm)
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)
343         init_keys_indirect_u64 (pm);
344       else
345         init_keys_indirect_u32 (pm);
346     }
347   else
348     {
349       if (pm->flags & PHASH_FLAG_MIX64)
350         init_keys_direct_u64 (pm);
351       else
352         init_keys_direct_u32 (pm);
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
566 static phash_tabb_t * sort_tabb;
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
595   vec_sort_with_function (pm->tabb_sort, phash_tabb_compare);
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  */
643 static void guess_initial_parameters (phash_main_t * pm)
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           /* 
666            * Was: a_max = is_minimal ? s_max / 2 : s_max;
667            * However, we know that is_minimal must be true, so the
668            * if-arm of the ternary expression is always executed.
669            */
670           a_max = s_max/2;
671           b_max = s_max/2;
672           break;
673         case 9: case 10: case 11: case 12: case 13:
674         case 14: case 15: case 16: case 17:
675           if (is_fast_mode)
676             {
677               a_max = s_max/2;
678               b_max = s_max/4;
679             }
680           else if (s_max/4 < b_max_use_scramble_threshold)
681             {
682               if (n_keys <= s_max*0.52)
683                 a_max = b_max = s_max/8;
684               else
685                 a_max = b_max = s_max/4;
686             }
687           else
688             {
689               a_max = ((n_keys <= s_max*(5.0/8.0)) ? s_max/8 : 
690                        (n_keys <= s_max*(3.0/4.0)) ? s_max/4 : s_max/2);
691               b_max = s_max/4;                /* always give the small size a shot */
692             }
693           break;
694         case 18:
695           if (is_fast_mode)
696             a_max = b_max = s_max/2;
697           else
698             {
699               a_max = s_max/8;                 /* never require the multiword hash */
700               b_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/4 : s_max/2;
701             }
702           break;
703         case 19:
704         case 20:
705           a_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/8 : s_max/2;
706           b_max = (n_keys <= s_max*(5.0/8.0)) ? s_max/4 : s_max/2;
707           break;
708         default:
709           /* Just find a hash as quick as possible.
710              We'll be thrashing virtual memory at this size. */
711           a_max = b_max = s_max/2;
712           break;
713         }
714     }
715   else
716     {
717       /* Non-minimal perfect hash. */
718       if (is_fast_mode && n_keys > s_max*0.8)
719         {
720           s_max *= 2;
721           s_bits += 1;
722         }
723
724       if (s_max/4 <= (1 << 14))
725         b_max = ((n_keys <= s_max*0.56) ? s_max/32 :
726                  (n_keys <= s_max*0.74) ? s_max/16 : s_max/8);
727       else
728         b_max = ((n_keys <= s_max*0.6) ? s_max/16 : 
729                  (n_keys <= s_max*0.8) ? s_max/8 : s_max/4);
730
731       if (is_fast_mode && b_max < s_max/8)
732         b_max = s_max/8;
733
734       if (a_max < 1) a_max = 1;
735       if (b_max < 1) b_max = 1;
736     }
737
738   ASSERT (s_max == (1 << s_bits));
739   ASSERT (is_pow2 (a_max));
740   ASSERT (is_pow2 (b_max));
741   pm->s_bits = s_bits;
742   pm->a_bits = min_log2 (a_max);
743   pm->b_bits = min_log2 (b_max);
744   if (b_max >= b_max_use_scramble_threshold)
745     pm->flags |= PHASH_FLAG_USE_SCRAMBLE;
746 }
747
748 /* compute p(x), where p is a permutation of 0..(1<<nbits)-1 */
749 /* permute(0)=0.  This is intended and useful. */
750 always_inline u32 scramble_permute (u32 x, u32 nbits)
751 {
752   int i;
753   int mask   = (1 << nbits) - 1;
754   int const2 = 1+nbits/2;
755   int const3 = 1+nbits/3;
756   int const4 = 1+nbits/4;
757   int const5 = 1+nbits/5;
758   for (i = 0; i < 20; i++)
759     {
760       x = (x + (x << const2)) & mask; 
761       x = (x ^ (x >> const3));
762       x = (x + (x << const4)) & mask;
763       x = (x ^ (x >> const5));
764     }
765   return x;
766 }
767
768 /* initialize scramble[] with distinct random values in 0..smax-1 */
769 static void scramble_init (phash_main_t * pm)
770 {
771   u32 i;
772
773   /* fill scramble[] with distinct random integers in 0..smax-1 */
774   vec_validate (pm->scramble, (1 << (pm->s_bits < 8 ? 8 : pm->s_bits)) - 1);
775   for (i = 0; i < vec_len (pm->scramble); i++)
776     pm->scramble[i] = scramble_permute (i, pm->s_bits);
777 }
778
779 /* Try to find a perfect hash function. */
780 clib_error_t *
781 phash_find_perfect_hash (phash_main_t * pm)
782 {
783   clib_error_t * error = 0;
784   u32 max_a_bits, n_tries_this_a_b, want_minimal;
785
786   /* guess initial values for s_max, a_max and b_max */
787   guess_initial_parameters (pm);
788
789   want_minimal = pm->flags & PHASH_FLAG_MINIMAL;
790
791  new_s:
792   if (pm->b_bits == 0)
793     pm->a_bits = pm->s_bits;
794
795   max_a_bits = pm->s_bits - want_minimal;
796   if (max_a_bits < 1)
797     max_a_bits = 1;
798
799   pm->hash_max = want_minimal ? vec_len (pm->keys) : (1 << pm->s_bits);
800
801   scramble_init (pm);
802
803   /* Allocate working memory. */
804   vec_free (pm->tabh);
805   vec_validate_init_empty (pm->tabh, pm->hash_max - 1, ~0);
806   vec_free (pm->tabq);
807   vec_validate (pm->tabq, 1 << pm->b_bits);
808   
809   /* Actually find the perfect hash */
810   n_tries_this_a_b = 0;
811   while (1)
812     {
813       /* Choose random hash seeds until keys become unique. */
814       pm->hash_seed = random_u64 (&pm->random_seed);
815       pm->n_seed_trials++;
816       if (init_tabb (pm))
817         {
818           /* Found unique (A, B). */
819
820           /* Hash may already be perfect. */
821           if (pm->b_bits == 0)
822             goto done;
823
824           pm->n_perfect_calls++;
825           if (perfect (pm))
826             goto done;
827
828           goto increase_b;
829         }
830
831       /* Keep trying with different seed value. */
832       n_tries_this_a_b++;
833       if (n_tries_this_a_b < 2048)
834         continue;
835
836       /* Try to put more bits in (A,B) to make distinct (A,B) more likely */
837       if (pm->a_bits < max_a_bits)
838         pm->a_bits++;
839       else if (pm->b_bits < pm->s_bits)
840         {
841         increase_b:
842           vec_resize (pm->tabb, vec_len (pm->tabb));
843           vec_resize (pm->tabq, vec_len (pm->tabq));
844           pm->b_bits++;
845         }
846       else
847         {
848           /* Can't increase (A, B) any more, so try increasing S. */
849           goto new_s;
850         }
851     }
852
853  done:
854   /* Construct mapping table for hash lookups. */
855   if (! error)
856     {
857       u32 b, v;
858
859       pm->a_shift = ((pm->flags & PHASH_FLAG_MIX64) ? 64 : 32) - pm->a_bits;
860       pm->b_mask = (1 << pm->b_bits) - 1;
861
862       vec_resize (pm->tab, vec_len (pm->tabb));
863       for (b = 0; b < vec_len (pm->tabb); b++)
864         {
865           v = pm->tabb[b].val_b;
866
867           /* Apply scramble now for small enough value of b_bits. */
868           if (! (pm->flags & PHASH_FLAG_USE_SCRAMBLE))
869             v = pm->scramble[v];
870
871           pm->tab[b] = v;
872         }
873     }
874
875   /* Free working memory. */
876   phash_main_free_working_memory (pm);
877
878   return error;
879 }
880
881 /* Slow hash computation for general keys. */
882 uword phash_hash_slow (phash_main_t * pm, uword key)
883 {
884   u32 a, b, v;
885
886   if (pm->flags & PHASH_FLAG_MIX64)
887     {
888       u64 x0, y0, z0;
889
890       x0 = y0 = z0 = pm->hash_seed;
891
892       if (pm->key_seed1)
893         {
894           u64 xyz[3];
895           pm->key_seed1 (pm->private, key, &xyz);
896           x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
897         }
898       else
899         x0 += key;
900
901       hash_mix64 (x0, y0, z0);
902
903       a = z0 >> pm->a_shift;
904       b = z0 & pm->b_mask;
905     }
906   else
907     {
908       u32 x0, y0, z0;
909
910       x0 = y0 = z0 = pm->hash_seed;
911
912       if (pm->key_seed1)
913         {
914           u32 xyz[3];
915           pm->key_seed1 (pm->private, key, &xyz);
916           x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2];
917         }
918       else
919         x0 += key;
920
921       hash_mix32 (x0, y0, z0);
922
923       a = z0 >> pm->a_shift;
924       b = z0 & pm->b_mask;
925     }
926
927   v = pm->tab[b];
928   if (pm->flags & PHASH_FLAG_USE_SCRAMBLE)
929     v = pm->scramble[v];
930   return a ^ v;
931 }
932
933 /* Verify that perfect hash is perfect. */
934 clib_error_t *
935 phash_validate (phash_main_t * pm)
936 {
937   phash_key_t * k;
938   uword * unique_bitmap = 0;
939   clib_error_t * error = 0;
940
941   vec_foreach (k, pm->keys)
942     {
943       uword h = phash_hash_slow (pm, k->key);
944
945       if (h >= pm->hash_max)
946         {
947           error = clib_error_return (0, "hash out of range %wd", h);
948           goto done;
949         }
950
951       if (clib_bitmap_get (unique_bitmap, h))
952         {
953           error = clib_error_return (0, "hash non-unique");
954           goto done;
955         }
956
957       unique_bitmap = clib_bitmap_ori (unique_bitmap, h);
958     }
959
960  done:
961   clib_bitmap_free (unique_bitmap);
962   return error;
963 }