view MurmurHash2.c @ 4:1ee100564408

Changed malloc and free to VMS__malloc and VMS__free
author Me
date Sun, 31 Oct 2010 20:25:30 -0700
parents
children 5b89d57e5d10
line source
1 //-----------------------------------------------------------------------------
2 // MurmurHash2, by Austin Appleby
4 // Note - This code makes a few assumptions about how your machine behaves -
6 // 1. We can read a 4-byte value from any address without crashing
7 // 2. sizeof(int) == 4
9 // And it has a few limitations -
11 // 1. It will not work incrementally.
12 // 2. It will not produce the same results on little-endian and big-endian
13 // machines.
15 unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )
16 {
17 // 'm' and 'r' are mixing constants generated offline.
18 // They're not really 'magic', they just happen to work well.
20 const unsigned int m = 0x5bd1e995;
21 const int r = 24;
23 // Initialize the hash to a 'random' value
25 unsigned int h = seed ^ len;
27 // Mix 4 bytes at a time into the hash
29 const unsigned char * data = (const unsigned char *)key;
31 while(len >= 4)
32 {
33 unsigned int k = *(unsigned int *)data;
35 k *= m;
36 k ^= k >> r;
37 k *= m;
39 h *= m;
40 h ^= k;
42 data += 4;
43 len -= 4;
44 }
46 // Handle the last few bytes of the input array
48 switch(len)
49 {
50 case 3: h ^= data[2] << 16;
51 case 2: h ^= data[1] << 8;
52 case 1: h ^= data[0];
53 h *= m;
54 };
56 // Do a few final mixes of the hash to ensure the last few
57 // bytes are well-incorporated.
59 h ^= h >> 13;
60 h *= m;
61 h ^= h >> 15;
63 return h;
64 }