The shift-add-XOR hash was designed as a string hashing function, but because it is so effective, it works for any data as well with similar efficiency.
Similar to the rotating hash except a different choice of constants for the rotation is used and addition is a preferred operation for mixing.. All in all, this is a surprisingly powerful and flexible hash. Like many effective hashes, it will fail tests for avalanche, but that does not seem to affect its performance in practice.
- 1 unsigned sax_hash ( void *key, int len )
- 2 {
- 3 unsigned char *p = key;
- 4 unsigned h = 0;
- 5 int i;
- 6
- 7 for ( i = 0; i < len; i++ )
- 8 h ^= ( h << 5 ) + ( h >> 2 ) + p[i];
- 9
- 10 return h;
- 11 }