Debian Codegraph @

src/c/newhash.h

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=8 shiftwidth=4 expandtab: */
3
4 /********************
5 * HASHING ROUTINES *
6 ********************/
7
8 /*
9 * SBCL's newest algorithm. Leads to few collisions, and it is faster.
10 */
11
12 #if ECL_FIXNUM_BITS > 32
13 /*
14 * 64 bit version
15 */
16 #define GOLDEN_RATIO 0x9e3779b97f4a7c13L
17 #define mix(a,b,c) \
18 { \
19 a=a-b; a=a-c; a=a^(c>>43); \
20 b=b-c; b=b-a; b=b^(a<<9); \
21 c=c-a; c=c-b; c=c^(b>>8); \
22 a=a-b; a=a-c; a=a^(c>>38); \
23 b=b-c; b=b-a; b=b^(a<<23); \
24 c=c-a; c=c-b; c=c^(b>>5); \
25 a=a-b; a=a-c; a=a^(c>>35); \
26 b=b-c; b=b-a; b=b^(a<<49); \
27 c=c-a; c=c-b; c=c^(b>>11); \
28 a=a-b; a=a-c; a=a^(c>>12); \
29 b=b-c; b=b-a; b=b^(a<<18); \
30 c=c-a; c=c-b; c=c^(b>>22); \
31 }
32
33 #define extract_word(k) \
34 (k[0]+((cl_index)k[1]<<8)+((cl_index)k[2]<<16)+((cl_index)k[3]<<24)+ \
35 ((cl_index)k[4]<<32)+((cl_index)k[5]<<40)+((cl_index)k[6]<<48)+ \
36 ((cl_index)k[7]<<52))
37
38 static cl_index
39 hash_string(cl_index initval, const unsigned char *k, cl_index length)
40 {
41 cl_index a = GOLDEN_RATIO, b = GOLDEN_RATIO, c = initval;
42 cl_index len;
43 for (len = length; len >= 24; len -= 24) {
44 a += extract_word(k); k+=8;
45 b += extract_word(k); k+=8;
46 c += extract_word(k); k+=8;
47 mix(a,b,c);
48 }
49
50 /*------------------------------------- handle the last 11 bytes */
51 c += length;
52 switch(len) {
53 /* all the case statements fall through */
54 case 23: c+=((cl_index)k[22]<<52);
55 case 22: c+=((cl_index)k[21]<<48);
56 case 21: c+=((cl_index)k[20]<<40);
57 case 20: c+=((cl_index)k[19]<<32);
58 case 19: c+=((cl_index)k[18]<<24);
59 case 18: c+=((cl_index)k[17]<<16);
60 case 17: c+=((cl_index)k[16]<<8);
61 /* the first byte of c is reserved for the length */
62 case 16: b+=((cl_index)k[15]<<52);
63 case 15: b+=((cl_index)k[14]<<48);
64 case 14: b+=((cl_index)k[13]<<40);
65 case 13: b+=((cl_index)k[12]<<32);
66 case 12: b+=((cl_index)k[11]<<24);
67 case 11: b+=((cl_index)k[10]<<16);
68 case 10: b+=((cl_index)k[9]<<8);
69 case 9 : b+=k[8];
70 case 8 : a+=((cl_index)k[7]<<52);
71 case 7 : a+=((cl_index)k[6]<<48);
72 case 6 : a+=((cl_index)k[5]<<40);
73 case 5 : a+=((cl_index)k[4]<<32);
74 case 4 : a+=((cl_index)k[3]<<24);
75 case 3 : a+=((cl_index)k[2]<<16);
76 case 2 : a+=((cl_index)k[1]<<8);
77 case 1 : a+=k[0];
78 /* case 0: nothing left to add */
79 }
80 mix(a,b,c);
81 /*-------------------------------------------- report the result */
82 return c;
83 }
84
85 #else
86 /*
87 * 32 bit version
88 */
89
90 #define GOLDEN_RATIO 0x9e3779b9L
91 #define mix(a,b,c) \
92 { \
93 a -= b; a -= c; a ^= (c>>13); \
94 b -= c; b -= a; b ^= (a<<8); \
95 c -= a; c -= b; c ^= (b>>13); \
96 a -= b; a -= c; a ^= (c>>12); \
97 b -= c; b -= a; b ^= (a<<16); \
98 c -= a; c -= b; c ^= (b>>5); \
99 a -= b; a -= c; a ^= (c>>3); \
100 b -= c; b -= a; b ^= (a<<10); \
101 c -= a; c -= b; c ^= (b>>15); \
102 }
103
104 #define extract_word(k) \
105 (k[0]+((cl_index)k[1]<<8)+((cl_index)k[2]<<16)+((cl_index)k[3]<<24))
106
107 static cl_index
108 hash_string(cl_index initval, const unsigned char *k, cl_index length)
109 {
110 cl_index a = GOLDEN_RATIO, b = GOLDEN_RATIO, c = initval;
111 cl_index len;
112 for (len = length; len >= 12; len -= 12) {
113 a += extract_word(k); k += 4;
114 b += extract_word(k); k += 4;
115 c += extract_word(k); k += 4;
116 mix(a,b,c);
117 }
118
119 /*------------------------------------- handle the last 11 bytes */
120 c += length;
121 switch(len) {
122 /* all the case statements fall through */
123 case 11: c+=((cl_index)k[10]<<24);
124 case 10: c+=((cl_index)k[9]<<16);
125 case 9 : c+=((cl_index)k[8]<<8);
126 /* the first byte of c is reserved for the length */
127 case 8 : b+=((cl_index)k[7]<<24);
128 case 7 : b+=((cl_index)k[6]<<16);
129 case 6 : b+=((cl_index)k[5]<<8);
130 case 5 : b+=k[4];
131 case 4 : a+=((cl_index)k[3]<<24);
132 case 3 : a+=((cl_index)k[2]<<16);
133 case 2 : a+=((cl_index)k[1]<<8);
134 case 1 : a+=k[0];
135 /* case 0: nothing left to add */
136 }
137 mix(a,b,c);
138 /*-------------------------------------------- report the result */
139 return c;
140 }
141 #endif
142
143 static cl_index hash_word(cl_index c, cl_index w)
144 {
145 cl_index a = w + GOLDEN_RATIO, b = GOLDEN_RATIO;
146 mix(a, b, c);
147 return c;
148 }
149
150 static cl_index hash_base_string(const ecl_base_char *s, cl_index len, cl_index h)
151 {
152 cl_index a = GOLDEN_RATIO, b = GOLDEN_RATIO, i;
153 for (i = len; i >= 3; i -= 3) {
154 a += *s; s++;
155 b += *s; s++;
156 h += *s; s++;
157 mix(a, b, h);
158 }
159 switch (i) {
160 case 2: a += *s; s++;
161 case 1: b += *s;
162 default: h += len;
163 }
164 mix(a, b, h);
165 return h;
166 }
167
168 #ifdef ECL_UNICODE
169 static cl_index hash_full_string(const ecl_character *s, cl_index len, cl_index h)
170 {
171 cl_index a = GOLDEN_RATIO, b = GOLDEN_RATIO, i;
172 for (i = len; i >= 3; i -= 3) {
173 a += (*s); s++;
174 b += (*s); s++;
175 h += (*s); s++;
176 mix(a, b, h);
177 }
178 switch (i) {
179 case 2: a += (*s); s++;
180 case 1: b += (*s);
181 default: h += len;
182 }
183 mix(a, b, h);
184 return h;
185 }
186 #endif