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; a -= c; a ^= (c>>43); \
25  b -= c; b -= a; b ^= (a<<9); \
26  c -= a; c -= b; c ^= (b>>8); \
27  a -= b; a -= c; a ^= (c>>38); \
28  b -= c; b -= a; b ^= (a<<23); \
29  c -= a; c -= b; c ^= (b>>5); \
30  a -= b; a -= c; a ^= (c>>35); \
31  b -= c; b -= a; b ^= (a<<49); \
32  c -= a; c -= b; c ^= (b>>11); \
33  a -= b; a -= c; a ^= (c>>12); \
34  b -= c; b -= a; b ^= (a<<18); \
35  c -= a; c -= b; c ^= (b>>22); \
36 }
37 
38 typedef unsigned long long ub8; /* unsigned 8-byte quantities */
39 typedef unsigned long int ub4; /* unsigned 4-byte quantities */
40 typedef unsigned char ub1;
41 
42 
43 namespace cond {
44 
45  ub8 hash64( ub1 * k, ub8 length, ub8 level)
46  // register ub1 *k; /* the key */
47  // register ub8 length; /* the length of the key */
48  // register ub8 level; /* the previous hash, or an arbitrary value */
49  {
50  ub8 a,b,c,len;
51 
52  /* Set up the internal state */
53  len = length;
54  a = b = level; /* the previous hash value */
55  c = 0x9e3779b97f4a7c13LL; /* the golden ratio; an arbitrary value */
56 
57  /*---------------------------------------- handle most of the key */
58  if (((unsigned long)k)&7)
59  {
60  while (len >= 24)
61  {
62  a += (k[0] +((ub8)k[ 1]<< 8)+((ub8)k[ 2]<<16)+((ub8)k[ 3]<<24)
63  +((ub8)k[4 ]<<32)+((ub8)k[ 5]<<40)+((ub8)k[ 6]<<48)+((ub8)k[ 7]<<56));
64  b += (k[8] +((ub8)k[ 9]<< 8)+((ub8)k[10]<<16)+((ub8)k[11]<<24)
65  +((ub8)k[12]<<32)+((ub8)k[13]<<40)+((ub8)k[14]<<48)+((ub8)k[15]<<56));
66  c += (k[16] +((ub8)k[17]<< 8)+((ub8)k[18]<<16)+((ub8)k[19]<<24)
67  +((ub8)k[20]<<32)+((ub8)k[21]<<40)+((ub8)k[22]<<48)+((ub8)k[23]<<56));
68  mix64(a,b,c);
69  k += 24; len -= 24;
70  }
71  }
72  else
73  {
74  while (len >= 24) /* aligned */
75  {
76  a += *(ub8 *)(k+0);
77  b += *(ub8 *)(k+8);
78  c += *(ub8 *)(k+16);
79  mix64(a,b,c);
80  k += 24; len -= 24;
81  }
82  }
83 
84  /*------------------------------------- handle the last 23 bytes */
85  c += length;
86  switch(len) /* all the case statements fall through */
87  {
88  case 23: c+=((ub8)k[22]<<56);
89  case 22: c+=((ub8)k[21]<<48);
90  case 21: c+=((ub8)k[20]<<40);
91  case 20: c+=((ub8)k[19]<<32);
92  case 19: c+=((ub8)k[18]<<24);
93  case 18: c+=((ub8)k[17]<<16);
94  case 17: c+=((ub8)k[16]<<8);
95  /* the first byte of c is reserved for the length */
96  case 16: b+=((ub8)k[15]<<56);
97  case 15: b+=((ub8)k[14]<<48);
98  case 14: b+=((ub8)k[13]<<40);
99  case 13: b+=((ub8)k[12]<<32);
100  case 12: b+=((ub8)k[11]<<24);
101  case 11: b+=((ub8)k[10]<<16);
102  case 10: b+=((ub8)k[ 9]<<8);
103  case 9: b+=((ub8)k[ 8]);
104  case 8: a+=((ub8)k[ 7]<<56);
105  case 7: a+=((ub8)k[ 6]<<48);
106  case 6: a+=((ub8)k[ 5]<<40);
107  case 5: a+=((ub8)k[ 4]<<32);
108  case 4: a+=((ub8)k[ 3]<<24);
109  case 3: a+=((ub8)k[ 2]<<16);
110  case 2: a+=((ub8)k[ 1]<<8);
111  case 1: a+=((ub8)k[ 0]);
112  /* case 0: nothing left to add */
113  }
114  mix64(a,b,c);
115  /*-------------------------------------------- report the result */
116  return c;
117  }
118 
119 
120 }
#define mix64(a, b, c)
Definition: hash64.cc:22
unsigned long long ub8
Definition: hash64.cc:38
unsigned long long hash64(unsigned char *k, unsigned long long length, unsigned long long level)
Definition: hash64.cc:45
int k[5][pyjets_maxn]
double b
Definition: hdecay.h:120
Definition: plugin.cc:24
double a
Definition: hdecay.h:121
unsigned long int ub4
Definition: hash64.cc:39
unsigned char ub1
Definition: hash64.cc:40