Pol  Revision:4b29d2b
uophash.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 // The hash function below is necessary for UOP decoding and
6 // was modified from C# to C++ from the code provided in LegacyMULConverter
7 // by Eos, which claims comes from http://burtleburtle.net/bob/c/lookup3.c. (Nando)
8 //
9 inline uint64_t HashLittle2( std::string s )
10 {
11  uint32_t length = (uint32_t)s.size();
12 
13  uint32_t a, b, c;
14  a = b = c = 0xDEADBEEF + length;
15 
16  int k = 0;
17 
18  while ( length > 12 )
19  {
20  a += s[k];
21  a += ((uint32_t)s[k + 1]) << 8;
22  a += ((uint32_t)s[k + 2]) << 16;
23  a += ((uint32_t)s[k + 3]) << 24;
24  b += s[k + 4];
25  b += ((uint32_t)s[k + 5]) << 8;
26  b += ((uint32_t)s[k + 6]) << 16;
27  b += ((uint32_t)s[k + 7]) << 24;
28  c += s[k + 8];
29  c += ((uint32_t)s[k + 9]) << 8;
30  c += ((uint32_t)s[k + 10]) << 16;
31  c += ((uint32_t)s[k + 11]) << 24;
32 
33  a -= c; a ^= ((c << 4) | (c >> 28)); c += b;
34  b -= a; b ^= ((a << 6) | (a >> 26)); a += c;
35  c -= b; c ^= ((b << 8) | (b >> 24)); b += a;
36  a -= c; a ^= ((c << 16) | (c >> 16)); c += b;
37  b -= a; b ^= ((a << 19) | (a >> 13)); a += c;
38  c -= b; c ^= ((b << 4) | (b >> 28)); b += a;
39 
40  length -= 12;
41  k += 12;
42  }
43 
44  if ( length != 0 )
45  {
46  switch ( length )
47  {
48  case 12: c += ((uint32_t)s[k + 11]) << 24; /* fall through */
49  case 11: c += ((uint32_t)s[k + 10]) << 16; /* fall through */
50  case 10: c += ((uint32_t)s[k + 9]) << 8; /* fall through */
51  case 9: c += s[k + 8]; /* fall through */
52  case 8: b += ((uint32_t)s[k + 7]) << 24; /* fall through */
53  case 7: b += ((uint32_t)s[k + 6]) << 16; /* fall through */
54  case 6: b += ((uint32_t)s[k + 5]) << 8; /* fall through */
55  case 5: b += s[k + 4]; /* fall through */
56  case 4: a += ((uint32_t)s[k + 3]) << 24; /* fall through */
57  case 3: a += ((uint32_t)s[k + 2]) << 16; /* fall through */
58  case 2: a += ((uint32_t)s[k + 1]) << 8; /* fall through */
59  case 1: a += s[k]; break;
60  }
61 
62  c ^= b; c -= ((b << 14) | (b >> 18));
63  a ^= c; a -= ((c << 11) | (c >> 21));
64  b ^= a; b -= ((a << 25) | (a >> 7));
65  c ^= b; c -= ((b << 16) | (b >> 16));
66  a ^= c; a -= ((c << 4) | (c >> 28));
67  b ^= a; b -= ((a << 14) | (a >> 18));
68  c ^= b; c -= ((b << 24) | (b >> 8));
69  }
70 
71  return ((uint64_t)b << 32) | c;
72 }
uint64_t HashLittle2(std::string s)
Definition: uophash.h:9