5 * https://sites.google.com/site/murmurhash/
7 * Public Domain hash functions by Austin Appleby.
9 * Trivially adapted for use with Ruby TDB by Eric Wong.
13 * 'm' and 'r' are mixing constants generated offline.
14 * They're not really 'magic', they just happen to work well.
16 static const unsigned int m
= 0xc6a4a793;
17 static const int r
= 16;
18 static const unsigned int seed
;
20 unsigned int rbtdb_murmur1(TDB_DATA
* key
)
22 const unsigned char *data
= key
->dptr
;
23 int len
= (int)key
->dsize
;
24 /* Initialize the hash to a 'random' value */
25 unsigned int h
= seed
^ (len
* m
);
28 h
+= *(const unsigned int *)data
;
36 /* Handle the last few bytes of the input array */
49 * Do a few final mixes of the hash to ensure the last few
50 * bytes are well-incorporated.
60 /* adapted from MurmurHashAligned */
61 unsigned int rbtdb_murmur1_aligned(TDB_DATA
* key
)
63 const unsigned char *data
= key
->dptr
;
64 int len
= (int)key
->dsize
;
65 unsigned int h
= seed
^ (len
* m
);
66 union { const unsigned char *byte
; int integer
; } cast
= { data
};
67 int align
= cast
.integer
& 3;
69 if (align
& (len
>= 4)) {
70 /* Pre-load the temp registers */
71 unsigned int t
= 0, d
= 0;
75 case 1: t
|= data
[2] << 16;
76 case 2: t
|= data
[1] << 8;
90 assert((cast
.integer
& 3) == 0);
92 d
= *(const unsigned int *)data
;
93 t
= (t
>> sr
) | (d
<< sl
);
103 /* Handle leftover data in temp registers */
104 pack
= len
< align
? len
: align
;
115 h
+= (t
>> sr
) | (d
<< sl
);
124 h
+= *(const unsigned int *)data
;
133 /* Handle tail bytes */