hash_murmur64.hpp 3.6 KB
Newer Older
tolgabirdal's avatar
tolgabirdal committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
Copyright (c) <2014> SMHasher

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
22 23 24 25 26 27 28
#ifndef __OPENCV_HASH_MURMUR64_HPP_
#define __OPENCV_HASH_MURMUR64_HPP_

//-----------------------------------------------------------------------------
// Block read - if your platform needs to do endian-swapping or can only
// handle aligned reads, do the conversion here

29
FORCE_INLINE uint getblock ( const uint * p, int i )
30
{
Bence Magyar's avatar
Bence Magyar committed
31
  return p[i];
32 33 34 35 36 37 38
}

//----------
// Finalization mix - force all bits of a hash block to avalanche

// avalanches all bits to within 0.25% bias

39
FORCE_INLINE uint fmix32 ( uint h )
40
{
Bence Magyar's avatar
Bence Magyar committed
41 42 43 44 45 46 47
  h ^= h >> 16;
  h *= 0x85ebca6b;
  h ^= h >> 13;
  h *= 0xc2b2ae35;
  h ^= h >> 16;

  return h;
48 49 50 51
}

//-----------------------------------------------------------------------------

52
FORCE_INLINE void bmix32 ( uint & h1, uint & k1, uint & c1, uint & c2 )
53
{
Bence Magyar's avatar
Bence Magyar committed
54 55 56 57 58 59 60 61 62
  k1 *= c1;
  k1  = ROTL32(k1,11);
  k1 *= c2;
  h1 ^= k1;

  h1 = h1*3+0x52dce729;

  c1 = c1*5+0x7b7d159c;
  c2 = c2*5+0x6bce6396;
63 64 65 66
}

//-----------------------------------------------------------------------------

67
FORCE_INLINE void bmix32 ( uint & h1, uint & h2, uint & k1, uint & k2, uint & c1, uint & c2 )
68
{
Bence Magyar's avatar
Bence Magyar committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  k1 *= c1;
  k1  = ROTL32(k1,11);
  k1 *= c2;
  h1 ^= k1;
  h1 += h2;

  h2 = ROTL32(h2,17);

  k2 *= c2;
  k2  = ROTL32(k2,11);
  k2 *= c1;
  h2 ^= k2;
  h2 += h1;

  h1 = h1*3+0x52dce729;
  h2 = h2*3+0x38495ab5;

  c1 = c1*5+0x7b7d159c;
  c2 = c2*5+0x6bce6396;
88 89 90 91
}

//----------

92
FORCE_INLINE void hashMurmurx64 ( const void * key, const int len, const uint seed, void * out )
93
{
94
  const uchar * data = (const uchar*)key;
Bence Magyar's avatar
Bence Magyar committed
95 96
  const int nblocks = len / 8;

97 98
  uint h1 = 0x8de1c3ac ^ seed;
  uint h2 = 0xbab98226 ^ seed;
Bence Magyar's avatar
Bence Magyar committed
99

100 101
  uint c1 = 0x95543787;
  uint c2 = 0x2ad7eb25;
Bence Magyar's avatar
Bence Magyar committed
102 103 104 105

  //----------
  // body

106
  const uint * blocks = (const uint *)(data + nblocks*8);
Bence Magyar's avatar
Bence Magyar committed
107 108 109

  for (int i = -nblocks; i; i++)
  {
110 111
    uint k1 = getblock(blocks,i*2+0);
    uint k2 = getblock(blocks,i*2+1);
Bence Magyar's avatar
Bence Magyar committed
112 113 114 115 116 117 118

    bmix32(h1,h2,k1,k2,c1,c2);
  }

  //----------
  // tail

119
  const uchar * tail = (const uchar*)(data + nblocks*8);
Bence Magyar's avatar
Bence Magyar committed
120

121 122
  uint k1 = 0;
  uint k2 = 0;
Bence Magyar's avatar
Bence Magyar committed
123 124 125 126 127

  switch (len & 7)
  {
  case 7:
    k2 ^= tail[6] << 16;
128
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
129 130
  case 6:
    k2 ^= tail[5] << 8;
131
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
132 133
  case 5:
    k2 ^= tail[4] << 0;
134
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
135 136
  case 4:
    k1 ^= tail[3] << 24;
137
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
138 139
  case 3:
    k1 ^= tail[2] << 16;
140
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
141 142
  case 2:
    k1 ^= tail[1] << 8;
143
    /* fallthrough */
Bence Magyar's avatar
Bence Magyar committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  case 1:
    k1 ^= tail[0] << 0;
    bmix32(h1,h2,k1,k2,c1,c2);
  };

  //----------
  // finalization

  h2 ^= len;

  h1 += h2;
  h2 += h1;

  h1 = fmix32(h1);
  h2 = fmix32(h2);

  h1 += h2;
  h2 += h1;

163 164
  ((uint*)out)[0] = h1;
  ((uint*)out)[1] = h2;
165 166 167
}


Bence Magyar's avatar
Bence Magyar committed
168
#endif