Skip to content

Commit

Permalink
Improve pointer hash function to include all bits
Browse files Browse the repository at this point in the history
The hashtab pointer hash function is not very good. It throws most of the
bits in the pointer away.

This changes pointer_hash to use the mix code from jhash function that mixes
all the bits on the pointer and makes them dependent on each other, before doing
the modulo.

libiberty/:

2013-04-22  Andi Kleen <ak@linux.intel.com>

	* hashtab.c (hash_pointer): Move to end of file and reimplement.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@198171 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
ak committed Apr 23, 2013
1 parent ab16985 commit 88a9613
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions libiberty/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2013-04-22 Andi Kleen <ak@linux.intel.com>

* hashtab.c (hash_pointer): Move to end of file and reimplement.

2013-04-03 Jason Merrill <jason@redhat.com>

* cp-demangle.c (cplus_demangle_type): Fix function quals.
Expand Down
33 changes: 25 additions & 8 deletions libiberty/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,6 @@ higher_prime_index (unsigned long n)
return low;
}

/* Returns a hash code for P. */

static hashval_t
hash_pointer (const PTR p)
{
return (hashval_t) ((intptr_t)p >> 3);
}

/* Returns non-zero if P1 and P2 are equal. */

static int
Expand Down Expand Up @@ -988,3 +980,28 @@ iterative_hash (const PTR k_in /* the key */,
/*-------------------------------------------- report the result */
return c;
}

/* Returns a hash code for pointer P. Simplified version of evahash */

static hashval_t
hash_pointer (const PTR p)
{
intptr_t v = (intptr_t) p;
unsigned a, b, c;

a = b = 0x9e3779b9;
if (sizeof (intptr_t) == 4)
{
/* Mix as 16bit for now */
a += v >> 16;
b += v & 0xffff;
}
else
{
a += v >> 32;
b += v & 0xffffffff;
}
c = 0x42135234;
mix (a, b, c);
return c;
}

0 comments on commit 88a9613

Please sign in to comment.