isl_hash_table_init: take minimal size instead of number of bits needed
authorSven Verdoolaege <skimo@kotnet.org>
Tue, 3 Mar 2009 14:54:03 +0000 (15:54 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 20 Mar 2009 14:21:03 +0000 (15:21 +0100)
include/isl_hash.h
isl_hash.c

index b850751..a4d85ba 100644 (file)
@@ -42,7 +42,7 @@ struct isl_hash_table {
 struct isl_ctx;
 
 int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
-                       int init_bits);
+                       int min_size);
 void isl_hash_table_clear(struct isl_hash_table *table);
 struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
                                struct isl_hash_table *table,
index 3a39bbc..6c4ed42 100644 (file)
@@ -9,17 +9,28 @@ uint32_t isl_hash_string(uint32_t hash, const char *s)
        return hash;
 }
 
+static unsigned int round_up(unsigned int v)
+{
+       int old_v = v;
+
+       while (v) {
+               old_v = v;
+               v ^= v & -v;
+       }
+       return old_v << 1;
+}
+
 int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
-                       int init_bits)
+                       int min_size)
 {
        size_t size;
 
        if (!table)
                return -1;
 
-       if (init_bits < 2)
-               init_bits = 2;
-       table->bits = init_bits;
+       if (min_size < 2)
+               min_size = 2;
+       table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
        table->n = 0;
 
        size = 2 << table->bits;