CMS 3D CMS Logo

hash64.cc
Go to the documentation of this file.
2 
3 /*
4 --------------------------------------------------------------------
5 mix -- mix 3 64-bit values reversibly.
6 mix() takes 48 machine instructions, but only 24 cycles on a superscalar
7  machine (like Intel's new MMX architecture). It requires 4 64-bit
8  registers for 4::2 parallelism.
9 All 1-bit deltas, all 2-bit deltas, all deltas composed of top bits of
10  (a,b,c), and all deltas of bottom bits were tested. All deltas were
11  tested both on random keys and on keys that were nearly all zero.
12  These deltas all cause every bit of c to change between 1/3 and 2/3
13  of the time (well, only 113/400 to 287/400 of the time for some
14  2-bit delta). These deltas all cause at least 80 bits to change
15  among (a,b,c) when the mix is run either forward or backward (yes it
16  is reversible).
17 This implies that a hash using mix64 has no funnels. There may be
18  characteristics with 3-bit deltas or bigger, I didn't test for
19  those.
20 --------------------------------------------------------------------
21 */
22 #define mix64(a, b, c) \
23  { \
24  a -= b; \
25  a -= c; \
26  a ^= (c >> 43); \
27  b -= c; \
28  b -= a; \
29  b ^= (a << 9); \
30  c -= a; \
31  c -= b; \
32  c ^= (b >> 8); \
33  a -= b; \
34  a -= c; \
35  a ^= (c >> 38); \
36  b -= c; \
37  b -= a; \
38  b ^= (a << 23); \
39  c -= a; \
40  c -= b; \
41  c ^= (b >> 5); \
42  a -= b; \
43  a -= c; \
44  a ^= (c >> 35); \
45  b -= c; \
46  b -= a; \
47  b ^= (a << 49); \
48  c -= a; \
49  c -= b; \
50  c ^= (b >> 11); \
51  a -= b; \
52  a -= c; \
53  a ^= (c >> 12); \
54  b -= c; \
55  b -= a; \
56  b ^= (a << 18); \
57  c -= a; \
58  c -= b; \
59  c ^= (b >> 22); \
60  }
61 
62 typedef unsigned long long ub8; /* unsigned 8-byte quantities */
63 typedef unsigned long int ub4; /* unsigned 4-byte quantities */
64 typedef unsigned char ub1;
65 
66 namespace cond {
67 
68  ub8 hash64(ub1 *k, ub8 length, ub8 level)
69  // register ub1 *k; /* the key */
70  // register ub8 length; /* the length of the key */
71  // register ub8 level; /* the previous hash, or an arbitrary value */
72  {
73  ub8 a, b, c, len;
74 
75  /* Set up the internal state */
76  len = length;
77  a = b = level; /* the previous hash value */
78  c = 0x9e3779b97f4a7c13LL; /* the golden ratio; an arbitrary value */
79 
80  /*---------------------------------------- handle most of the key */
81  if (((unsigned long)k) & 7) {
82  while (len >= 24) {
83  a += (k[0] + ((ub8)k[1] << 8) + ((ub8)k[2] << 16) + ((ub8)k[3] << 24) + ((ub8)k[4] << 32) + ((ub8)k[5] << 40) +
84  ((ub8)k[6] << 48) + ((ub8)k[7] << 56));
85  b += (k[8] + ((ub8)k[9] << 8) + ((ub8)k[10] << 16) + ((ub8)k[11] << 24) + ((ub8)k[12] << 32) +
86  ((ub8)k[13] << 40) + ((ub8)k[14] << 48) + ((ub8)k[15] << 56));
87  c += (k[16] + ((ub8)k[17] << 8) + ((ub8)k[18] << 16) + ((ub8)k[19] << 24) + ((ub8)k[20] << 32) +
88  ((ub8)k[21] << 40) + ((ub8)k[22] << 48) + ((ub8)k[23] << 56));
89  mix64(a, b, c);
90  k += 24;
91  len -= 24;
92  }
93  } else {
94  while (len >= 24) /* aligned */
95  {
96  a += *(ub8 *)(k + 0);
97  b += *(ub8 *)(k + 8);
98  c += *(ub8 *)(k + 16);
99  mix64(a, b, c);
100  k += 24;
101  len -= 24;
102  }
103  }
104 
105  /*------------------------------------- handle the last 23 bytes */
106  c += length;
107  switch (len) /* all the case statements fall through */
108  {
109  case 23:
110  c += ((ub8)k[22] << 56);
111  [[fallthrough]];
112  case 22:
113  c += ((ub8)k[21] << 48);
114  [[fallthrough]];
115  case 21:
116  c += ((ub8)k[20] << 40);
117  [[fallthrough]];
118  case 20:
119  c += ((ub8)k[19] << 32);
120  [[fallthrough]];
121  case 19:
122  c += ((ub8)k[18] << 24);
123  [[fallthrough]];
124  case 18:
125  c += ((ub8)k[17] << 16);
126  [[fallthrough]];
127  case 17:
128  c += ((ub8)k[16] << 8);
129  [[fallthrough]];
130  /* the first byte of c is reserved for the length */
131  case 16:
132  b += ((ub8)k[15] << 56);
133  [[fallthrough]];
134  case 15:
135  b += ((ub8)k[14] << 48);
136  [[fallthrough]];
137  case 14:
138  b += ((ub8)k[13] << 40);
139  [[fallthrough]];
140  case 13:
141  b += ((ub8)k[12] << 32);
142  [[fallthrough]];
143  case 12:
144  b += ((ub8)k[11] << 24);
145  [[fallthrough]];
146  case 11:
147  b += ((ub8)k[10] << 16);
148  [[fallthrough]];
149  case 10:
150  b += ((ub8)k[9] << 8);
151  [[fallthrough]];
152  case 9:
153  b += ((ub8)k[8]);
154  [[fallthrough]];
155  case 8:
156  a += ((ub8)k[7] << 56);
157  [[fallthrough]];
158  case 7:
159  a += ((ub8)k[6] << 48);
160  [[fallthrough]];
161  case 6:
162  a += ((ub8)k[5] << 40);
163  [[fallthrough]];
164  case 5:
165  a += ((ub8)k[4] << 32);
166  [[fallthrough]];
167  case 4:
168  a += ((ub8)k[3] << 24);
169  [[fallthrough]];
170  case 3:
171  a += ((ub8)k[2] << 16);
172  [[fallthrough]];
173  case 2:
174  a += ((ub8)k[1] << 8);
175  [[fallthrough]];
176  case 1:
177  a += ((ub8)k[0]);
178  /* case 0: nothing left to add */
179  }
180  mix64(a, b, c);
181  /*-------------------------------------------- report the result */
182  return c;
183  }
184 
185 } // namespace cond
#define mix64(a, b, c)
Definition: hash64.cc:22
unsigned long long ub8
Definition: hash64.cc:62
unsigned long long hash64(unsigned char *k, unsigned long long length, unsigned long long level)
Definition: hash64.cc:68
double b
Definition: hdecay.h:120
Definition: plugin.cc:23
double a
Definition: hdecay.h:121
unsigned long int ub4
Definition: hash64.cc:63
unsigned char ub1
Definition: hash64.cc:64