summaryrefslogtreecommitdiff
path: root/include/murmur_hash2a.h
blob: 51da7db624c918ef123322668faa6cb2483da07b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
   Copyright (C) 2009 Red Hat, Inc.

   This software is licensed under the GNU General Public License,
   version 2 (GPLv2) (see COPYING for details), subject to the
   following clarification.

   With respect to binaries built using the Microsoft(R) Windows
   Driver Kit (WDK), GPLv2 does not extend to any code contained in or
   derived from the WDK ("WDK Code").  As to WDK Code, by using or
   distributing such binaries you agree to be bound by the Microsoft
   Software License Terms for the WDK.  All WDK Code is considered by
   the GPLv2 licensors to qualify for the special exception stated in
   section 3 of GPLv2 (commonly known as the system library
   exception).

   There is NO WARRANTY for this software, express or implied,
   including the implied warranties of NON-INFRINGEMENT, TITLE,
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

//Some modifications by Red Hat any bug is probably our fault

//-----------------------------------------------------------------------------
// MurmurHash2A, by Austin Appleby

// This is a variant of MurmurHash2 modified to use the Merkle-Damgard
// construction. Bulk speed should be identical to Murmur2, small-key speed
// will be 10%-20% slower due to the added overhead at the end of the hash.

// This variant fixes a minor issue where null keys were more likely to
// collide with each other than expected, and also makes the algorithm
// more amenable to incremental implementations. All other caveats from
// MurmurHash2 still apply.

#ifndef __MURMUR_HASH2A_H
#define __MURMUR_HASH2A_H

#include <windef.h>
#include "os_dep.h"

typedef UINT32 uint32_t;
typedef UINT16 uint16_t;
typedef UINT8 uint8_t;

#define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }

_inline uint32_t MurmurHash2A(const void * key, uint32_t len, uint32_t seed )
{
    const uint32_t m = 0x5bd1e995;
    const uint32_t r = 24;
    uint32_t l = len;
    uint32_t t = 0;

    const uint8_t * data = (const uint8_t *)key;

    uint32_t h = seed;

    while (len >= 4) {
        uint32_t k = *(uint32_t*)data;

        mmix(h,k);

        data += 4;
        len -= 4;
    }

    switch (len) {
    case 3: t ^= data[2] << 16;
    case 2: t ^= data[1] << 8;
    case 1: t ^= data[0];
    };

    mmix(h,t);
    mmix(h,l);

    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;

    return h;
}

_inline uint32_t MurmurHash2AJump3(const uint32_t * key, uint32_t len, uint32_t seed )
{
    uint32_t m = 0x5bd1e995;
    uint32_t r = 24;
    uint32_t l = len << 2;

    const uint8_t * data = (const uint8_t *)key;

    uint32_t h = seed;

    while (len >= 4) {
        uint32_t k = *(uint32_t*)data;
        uint32_t tmp;

        data += 4;
        tmp = *(uint32_t *)data;
        k = k << 8;
        k |= (uint8_t)tmp;
        mmix(h,k);

        k = tmp << 8;
        k = k & 0xffff0000;
        data += 4;
        tmp = *(uint32_t *)data;
        k |= (uint16_t)(tmp >> 8);
        mmix(h,k);

        data += 4;
        k = *(uint32_t *)data;
        k = k << 8;
        k |=  (uint8_t)tmp;
        mmix(h,k);

        data += 4;
        len -= 4;
    }

    while (len >= 1) {
        uint32_t k = *(uint32_t*)data;

        k = k << 8;
        mmix(h,k);

        data += 4;
        len--;
    }

    h *= m;
    mmix(h,l);

    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;

    return h;
}


_inline uint32_t murmurhash2a(const void *key, size_t length, uint32_t initval)
{
    return MurmurHash2A(key, length, initval);
}

_inline uint32_t murmurhash2ajump3(const uint32_t *key, size_t length, uint32_t initval)
{
    return MurmurHash2AJump3(key, length, initval);
}
#endif