]> granicus.if.org Git - yasm/commitdiff
* phash.c (phash_lookup), perfect.c (initnorm): Mask upper bits in hash
authorPeter Johnson <peter@tortall.net>
Sun, 5 Mar 2006 01:07:38 +0000 (01:07 -0000)
committerPeter Johnson <peter@tortall.net>
Sun, 5 Mar 2006 01:07:38 +0000 (01:07 -0000)
calculations so that these functions behave identically on 32-bit and >32-bit
architectures.

* gap.c: Fix warnings.
* perfect.c: Change K&R to prototypes to avoid warnings.

svn path=/trunk/yasm/; revision=1398

libyasm/phash.c
tools/gap/gap.c
tools/gap/perfect.c

index 6e23fb4fc5acc357caeafa9254b3c734ffdd0e29..ed14001ba4cf68f9e9de0f094754e795ce6453fe 100644 (file)
@@ -46,14 +46,23 @@ mix() was built out of 36 single-cycle latency instructions in a
 #define mix(a,b,c) \
 { \
     a -= b; a -= c; a ^= (c>>13); \
+    a &= 0xffffffff; \
     b -= c; b -= a; b ^= (a<<8); \
+    b &= 0xffffffff; \
     c -= a; c -= b; c ^= (b>>13); \
+    c &= 0xffffffff; \
     a -= b; a -= c; a ^= (c>>12);  \
+    a &= 0xffffffff; \
     b -= c; b -= a; b ^= (a<<16); \
+    b &= 0xffffffff; \
     c -= a; c -= b; c ^= (b>>5); \
+    c &= 0xffffffff; \
     a -= b; a -= c; a ^= (c>>3);  \
+    a &= 0xffffffff; \
     b -= c; b -= a; b ^= (a<<10); \
+    b &= 0xffffffff; \
     c -= a; c -= b; c ^= (b>>15); \
+    c &= 0xffffffff; \
 }
 
 /*
@@ -102,8 +111,11 @@ phash_lookup(
     while (len >= 12)
     {
        a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
+       a &= 0xffffffff;
        b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
+       b &= 0xffffffff;
        c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
+       c &= 0xffffffff;
        mix(a,b,c);
        k += 12; len -= 12;
     }
@@ -115,15 +127,18 @@ phash_lookup(
        case 11: c+=((ub4)k[10]<<24);
        case 10: c+=((ub4)k[9]<<16);
        case 9 : c+=((ub4)k[8]<<8);
+                c &= 0xffffffff;
            /* the first byte of c is reserved for the length */
        case 8 : b+=((ub4)k[7]<<24);
        case 7 : b+=((ub4)k[6]<<16);
        case 6 : b+=((ub4)k[5]<<8);
        case 5 : b+=k[4];
+                b &= 0xffffffff;
        case 4 : a+=((ub4)k[3]<<24);
        case 3 : a+=((ub4)k[2]<<16);
        case 2 : a+=((ub4)k[1]<<8);
        case 1 : a+=k[0];
+                a &= 0xffffffff;
        /* case 0: nothing left to add */
     }
     mix(a,b,c);
index b78b2da4dff09962bac62c305a8f065a5862a992..7347d4e59ce71fb2a7461911d9ce55b3f5e8fdab 100644 (file)
@@ -32,6 +32,7 @@
 #include "tools/gap/perfect.h"
 #include "libyasm/compat-queue.h"
 #include "libyasm/coretype.h"
+#include "libyasm/errwarn.h"
 #include "libyasm/hamt.h"
 
 typedef STAILQ_HEAD(slist, sval) slist;
@@ -116,12 +117,12 @@ yasm__fatal(const char *message, ...)
 }
 
 static void
-hamt_error(const char *file, unsigned int line, const char *message)
+hamt_error(const char *file, unsigned int ln, const char *message)
 {
     abort();
 }
 
-void
+static void
 dup_slist(slist *out, slist *in)
 {
     sval *sv;
@@ -134,7 +135,7 @@ dup_slist(slist *out, slist *in)
     }
 }
 
-dir *
+static dir *
 dup_dir(dir *in)
 {
     dir *out = yasm_xmalloc(sizeof(dir));
@@ -260,7 +261,7 @@ parse_generic(dir_type type, const char *func, dir_byp_list *byp)
 static void
 parse_arch(void)
 {
-    int i;
+    size_t i;
     int found = 0;
     char *tok = strtok(NULL, " \t\n");
 
@@ -795,7 +796,7 @@ int
 main(int argc, char *argv[])
 {
     FILE *in, *out;
-    int i;
+    size_t i;
     char *tok;
     int count[NUM_DIRS];
     dir_byp *db;
index 3313a5a352a906ea57d47f8e1293b89858903185..c0b07d2898825b5e0b12feb3f44b609795917315 100644 (file)
@@ -75,9 +75,9 @@ ub4  val;
 
 /* compute p(x), where p is a permutation of 0..(1<<nbits)-1 */
 /* permute(0)=0.  This is intended and useful. */
-static ub4  permute(x, nbits)
-ub4 x;                                       /* input, a value in some range */
-ub4 nbits;                                 /* input, number of bits in range */
+static ub4  permute(
+    ub4 x,                                   /* input, a value in some range */
+    ub4 nbits)                             /* input, number of bits in range */
 {
   int i;
   int mask   = ((ub4)1<<nbits)-1;                                /* all ones */
@@ -96,9 +96,9 @@ ub4 nbits;                                 /* input, number of bits in range */
 }
 
 /* initialize scramble[] with distinct random values in 0..smax-1 */
-static void scrambleinit(scramble, smax)
-ub4      *scramble;                            /* hash is a^scramble[tab[b]] */
-ub4       smax;                    /* scramble values should be in 0..smax-1 */
+static void scrambleinit(
+    ub4      *scramble,                        /* hash is a^scramble[tab[b]] */
+    ub4       smax)                /* scramble values should be in 0..smax-1 */
 {
   ub4 i;
 
@@ -113,10 +113,10 @@ ub4       smax;                    /* scramble values should be in 0..smax-1 */
  * Check if key1 and key2 are the same. 
  * We already checked (a,b) are the same.
  */
-static void checkdup(key1, key2, form)
-key      *key1;
-key      *key2;
-hashform *form;
+static void checkdup(
+    key      *key1,
+    key      *key2,
+    hashform *form)
 {
   switch(form->hashtype)
   {
@@ -153,12 +153,12 @@ hashform *form;
  * put keys in tabb according to key->b_k
  * check if the initial hash might work 
  */
-static int inittab(tabb, blen, keys, form, complete)
-bstuff   *tabb;                     /* output, list of keys with b for (a,b) */
-ub4       blen;                                            /* length of tabb */
-key      *keys;                               /* list of keys already hashed */
-hashform *form;                                           /* user directives */
-int       complete;        /* TRUE means to complete init despite collisions */
+static int inittab(
+    bstuff   *tabb,                 /* output, list of keys with b for (a,b) */
+    ub4       blen,                                        /* length of tabb */
+    key      *keys,                           /* list of keys already hashed */
+    hashform *form,                                       /* user directives */
+    int       complete)    /* TRUE means to complete init despite collisions */
 {
   int  nocollision = TRUE;
   key *mykey;
@@ -193,18 +193,18 @@ int       complete;        /* TRUE means to complete init despite collisions */
 
 
 /* Do the initial hash for normal mode (use lookup and checksum) */
-static void initnorm(keys, alen, blen, smax, salt, final)
-key      *keys;                                          /* list of all keys */
-ub4       alen;                    /* (a,b) has a in 0..alen-1, a power of 2 */
-ub4       blen;                    /* (a,b) has b in 0..blen-1, a power of 2 */
-ub4       smax;                   /* maximum range of computable hash values */
-ub4       salt;                     /* used to initialize the hash function */
-gencode  *final;                          /* output, code for the final hash */
+static void initnorm(
+    key      *keys,                                      /* list of all keys */
+    ub4       alen,                /* (a,b) has a in 0..alen-1, a power of 2 */
+    ub4       blen,                /* (a,b) has b in 0..blen-1, a power of 2 */
+    ub4       smax,               /* maximum range of computable hash values */
+    ub4       salt,                 /* used to initialize the hash function */
+    gencode  *final)                      /* output, code for the final hash */
 {
   key *mykey;
   if (phash_log2(alen)+phash_log2(blen) > UB4BITS)
   {
-    ub4 initlev = salt*0x9e3779b9;  /* the golden ratio; an arbitrary value */
+    ub4 initlev = (salt*0x9e3779b9)&0xffffffff;  /* the golden ratio; an arbitrary value */
 
     for (mykey=keys; mykey; mykey=mykey->next_k)
     {
@@ -228,7 +228,7 @@ gencode  *final;                          /* output, code for the final hash */
   else
   {
     ub4 loga = phash_log2(alen);                            /* log based 2 of blen */
-    ub4 initlev = salt*0x9e3779b9;  /* the golden ratio; an arbitrary value */
+    ub4 initlev = (salt*0x9e3779b9)&0xffffffff;  /* the golden ratio; an arbitrary value */
 
     for (mykey=keys; mykey; mykey=mykey->next_k)
     {
@@ -259,13 +259,13 @@ gencode  *final;                          /* output, code for the final hash */
 
 
 /* Do initial hash for inline mode */
-static void initinl(keys, alen, blen, smax, salt, final)
-key      *keys;                                          /* list of all keys */
-ub4       alen;                    /* (a,b) has a in 0..alen-1, a power of 2 */
-ub4       blen;                    /* (a,b) has b in 0..blen-1, a power of 2 */
-ub4       smax;                           /* range of computable hash values */
-ub4       salt;                     /* used to initialize the hash function */
-gencode  *final;                            /* generated code for final hash */
+static void initinl(
+    key      *keys,                                      /* list of all keys */
+    ub4       alen,                /* (a,b) has a in 0..alen-1, a power of 2 */
+    ub4       blen,                /* (a,b) has b in 0..blen-1, a power of 2 */
+    ub4       smax,                       /* range of computable hash values */
+    ub4       salt,                  /* used to initialize the hash function */
+    gencode  *final)                        /* generated code for final hash */
 {
   key *mykey;
   ub4  amask = alen-1;
@@ -310,16 +310,16 @@ gencode  *final;                            /* generated code for final hash */
  *   1: found distinct (a,b) for all keys, put keys in tabb[]
  *   2: found a perfect hash, no need to do any more work
  */
-static ub4 initkey(keys, nkeys, tabb, alen, blen, smax, salt, form, final)
-key      *keys;                                          /* list of all keys */
-ub4       nkeys;                                     /* total number of keys */
-bstuff   *tabb;                                        /* stuff indexed by b */
-ub4       alen;                    /* (a,b) has a in 0..alen-1, a power of 2 */
-ub4       blen;                    /* (a,b) has b in 0..blen-1, a power of 2 */
-ub4       smax;                           /* range of computable hash values */
-ub4       salt;                     /* used to initialize the hash function */
-hashform *form;                                           /* user directives */
-gencode  *final;                                      /* code for final hash */
+static ub4 initkey(
+    key      *keys,                                      /* list of all keys */
+    ub4       nkeys,                                 /* total number of keys */
+    bstuff   *tabb,                                    /* stuff indexed by b */
+    ub4       alen,                /* (a,b) has a in 0..alen-1, a power of 2 */
+    ub4       blen,                /* (a,b) has b in 0..blen-1, a power of 2 */
+    ub4       smax,                       /* range of computable hash values */
+    ub4       salt,                 /* used to initialize the hash function */
+    hashform *form,                                       /* user directives */
+    gencode  *final)                                  /* code for final hash */
 {
   /* Do the initial hash of the keys */
   switch(form->mode)
@@ -353,11 +353,11 @@ gencode  *final;                                      /* code for final hash */
 }
 
 /* Print an error message and exit if there are duplicates */
-static void duplicates(tabb, blen, keys, form)
-bstuff   *tabb;                    /* array of lists of keys with the same b */
-ub4       blen;                              /* length of tabb, a power of 2 */
-key      *keys;
-hashform *form;                                           /* user directives */
+static void duplicates(
+    bstuff   *tabb,                /* array of lists of keys with the same b */
+    ub4       blen,                          /* length of tabb, a power of 2 */
+    key      *keys,
+    hashform *form)                                       /* user directives */
 {
   ub4  i;
   key *key1;
@@ -374,14 +374,14 @@ hashform *form;                                           /* user directives */
 
 
 /* Try to apply an augmenting list */
-static int apply(tabb, tabh, tabq, blen, scramble, tail, rollback)
-bstuff *tabb;
-hstuff *tabh;
-qstuff *tabq;
-ub4     blen;
-ub4    *scramble;
-ub4     tail;
-int     rollback;          /* FALSE applies augmenting path, TRUE rolls back */
+static int apply(
+    bstuff *tabb,
+    hstuff *tabh,
+    qstuff *tabq,
+    ub4     blen,
+    ub4    *scramble,
+    ub4     tail,
+    int     rollback)      /* FALSE applies augmenting path, TRUE rolls back */
 {
   ub4     hash;
   key    *mykey;
@@ -422,7 +422,7 @@ int     rollback;          /* FALSE applies augmenting path, TRUE rolls back */
       else if (tabh[hash].key_h)
       {
        /* very rare: roll back any changes */
-       (void *)apply(tabb, tabh, tabq, blen, scramble, tail, TRUE);
+       apply(tabb, tabh, tabq, blen, scramble, tail, TRUE);
        return FALSE;                                  /* failure, collision */
       }
       tabh[hash].key_h = mykey;
@@ -451,18 +451,17 @@ an unused node is found.  Sum(i=1..n)(n/i) is about nlogn, so expect
 this approach to take about nlogn time to map all single-key b's.
 -------------------------------------------------------------------------------
 */
-static int augment(tabb, tabh, tabq, blen, scramble, smax, item, nkeys, 
-                  highwater, form)
-bstuff   *tabb;                                        /* stuff indexed by b */
-hstuff   *tabh;  /* which key is associated with which hash, indexed by hash */
-qstuff   *tabq;            /* queue of *b* values, this is the spanning tree */
-ub4       blen;                                            /* length of tabb */
-ub4      *scramble;                      /* final hash is a^scramble[tab[b]] */
-ub4       smax;                                 /* highest value in scramble */
-bstuff   *item;                           /* &tabb[b] for the b to be mapped */
-ub4       nkeys;                         /* final hash must be in 0..nkeys-1 */
-ub4       highwater;        /* a value higher than any now in tabb[].water_b */
-hashform *form;               /* TRUE if we should do a minimal perfect hash */
+static int augment(
+    bstuff   *tabb,                                    /* stuff indexed by b */
+    hstuff   *tabh,  /* which key is associated with which hash, indexed by hash */
+    qstuff   *tabq,        /* queue of *b* values, this is the spanning tree */
+    ub4       blen,                                        /* length of tabb */
+    ub4      *scramble,                  /* final hash is a^scramble[tab[b]] */
+    ub4       smax,                             /* highest value in scramble */
+    bstuff   *item,                       /* &tabb[b] for the b to be mapped */
+    ub4       nkeys,                     /* final hash must be in 0..nkeys-1 */
+    ub4       highwater,    /* a value higher than any now in tabb[].water_b */
+    hashform *form)           /* TRUE if we should do a minimal perfect hash */
 {
   ub4  q;                      /* current position walking through the queue */
   ub4  tail;              /* tail of the queue.  0 is the head of the queue. */
@@ -536,15 +535,15 @@ hashform *form;               /* TRUE if we should do a minimal perfect hash */
 
 
 /* find a mapping that makes this a perfect hash */
-static int perfect(tabb, tabh, tabq, blen, smax, scramble, nkeys, form)
-bstuff   *tabb;
-hstuff   *tabh;
-qstuff   *tabq;
-ub4       blen;
-ub4       smax;
-ub4      *scramble;
-ub4       nkeys;
-hashform *form;
+static int perfect(
+    bstuff   *tabb,
+    hstuff   *tabh,
+    qstuff   *tabq,
+    ub4       blen,
+    ub4       smax,
+    ub4      *scramble,
+    ub4       nkeys,
+    hashform *form)
 {
   ub4 maxkeys;                           /* maximum number of keys for any b */
   ub4 i, j;
@@ -579,18 +578,17 @@ hashform *form;
  * Simple case: user gave (a,b).  No more mixing, no guessing alen or blen. 
  * This assumes a,b reside in (key->a_k, key->b_k), and final->form == AB_HK.
  */
-static void hash_ab(tabb, alen, blen, salt, final, 
-            scramble, smax, keys, nkeys, form)
-bstuff  **tabb;           /* output, tab[] of the perfect hash, length *blen */
-ub4      *alen;                 /* output, 0..alen-1 is range for a of (a,b) */
-ub4      *blen;                 /* output, 0..blen-1 is range for b of (a,b) */
-ub4      *salt;                         /* output, initializes initial hash */
-gencode  *final;                                      /* code for final hash */
-ub4      *scramble;                      /* input, hash = a^scramble[tab[b]] */
-ub4      *smax;                           /* input, scramble[i] in 0..smax-1 */
-key      *keys;                                       /* input, keys to hash */
-ub4       nkeys;                       /* input, number of keys being hashed */
-hashform *form;                                           /* user directives */
+static void hash_ab(
+    bstuff  **tabb,       /* output, tab[] of the perfect hash, length *blen */
+    ub4      *alen,             /* output, 0..alen-1 is range for a of (a,b) */
+    ub4      *blen,             /* output, 0..blen-1 is range for b of (a,b) */
+    ub4      *salt,                     /* output, initializes initial hash */
+    gencode  *final,                                  /* code for final hash */
+    ub4      *scramble,                  /* input, hash = a^scramble[tab[b]] */
+    ub4      *smax,                       /* input, scramble[i] in 0..smax-1 */
+    key      *keys,                                   /* input, keys to hash */
+    ub4       nkeys,                   /* input, number of keys being hashed */
+    hashform *form)                                       /* user directives */
 {
   hstuff *tabh;
   qstuff *tabq;
@@ -683,12 +681,12 @@ hashform *form;                                           /* user directives */
 
 
 /* guess initial values for alen and blen */
-static void initalen(alen, blen, smax, nkeys, form)
-ub4      *alen;                                      /* output, initial alen */
-ub4      *blen;                                      /* output, initial blen */
-ub4      *smax;    /* input, power of two greater or equal to max hash value */
-ub4       nkeys;                              /* number of keys being hashed */
-hashform *form;                                           /* user directives */
+static void initalen(
+    ub4      *alen,                                  /* output, initial alen */
+    ub4      *blen,                                  /* output, initial blen */
+    ub4      *smax,/* input, power of two greater or equal to max hash value */
+    ub4       nkeys,                          /* number of keys being hashed */
+    hashform *form)                                       /* user directives */
 {
   /*
    * Find initial *alen, *blen