This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Hash Function for "switch statement"
- From: "Unruh, Erwin" <erwin dot unruh at ts dot fujitsu dot com>
- To: "gcc at gnu dot org" <gcc at gnu dot org>
- Date: Mon, 22 Mar 2010 13:19:54 +0100
- Subject: Re: Hash Function for "switch statement"
Hi,
the discussion so far did omit one specific aspect. When comparing two implementations for a switch, you have to compare the full code. For the hash you have to include the code to calculate the hash function. This might be more code than a simple tree lookup.
The example function:
>public int hash32shift(int key)
>{
> key = ~key + (key << 15); // key = (key << 15) - key - 1;
> key = key ^ (key >>> 12);
> key = key + (key << 2);
> key = key ^ (key >>> 4);
> key = key * 2057; // key = (key + (key << 3)) + (key << 11);
> key = key ^ (key >>> 16);
> return key;
>}
has 12 operations. Add a table and verification you get to about 18. That is worse than a tree search with 9 levels. So for all switches with less than 512 elements, the hash is not faster.
Erwin