00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "Hasher.H"
00029 #include "HasherInt.H"
00030
00031 #if defined (__GLIBC__)
00032 # include <endian.h>
00033 #endif
00034
00035
00036 #define bitRotateLeft(x, nBits) (((x) << (nBits)) | ((x) >> (32 - (nBits))))
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 #define bitMixer(a, b, c) \
00117 { \
00118 a -= c; a ^= bitRotateLeft(c, 4); c += b; \
00119 b -= a; b ^= bitRotateLeft(a, 6); a += c; \
00120 c -= b; c ^= bitRotateLeft(b, 8); b += a; \
00121 a -= c; a ^= bitRotateLeft(c,16); c += b; \
00122 b -= a; b ^= bitRotateLeft(a,19); a += c; \
00123 c -= b; c ^= bitRotateLeft(b, 4); b += a; \
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 #define bitMixerFinal(a, b, c) \
00152 { \
00153 c ^= b; c -= bitRotateLeft(b, 14); \
00154 a ^= c; a -= bitRotateLeft(c, 11); \
00155 b ^= a; b -= bitRotateLeft(a, 25); \
00156 c ^= b; c -= bitRotateLeft(b, 16); \
00157 a ^= c; a -= bitRotateLeft(c, 4); \
00158 b ^= a; b -= bitRotateLeft(a, 14); \
00159 c ^= b; c -= bitRotateLeft(b, 24); \
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __LITTLE_ENDIAN)
00192 static unsigned jenkins_hashlittle
00193 (
00194 const void *key,
00195 size_t length,
00196 unsigned initval
00197 )
00198 {
00199 uint32_t a, b, c;
00200 union { const void *ptr; size_t i; } u;
00201
00202
00203 a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
00204
00205 u.ptr = key;
00206 if ((u.i & 0x3) == 0)
00207 {
00208
00209 const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
00210
00211
00212 while (length > 12)
00213 {
00214 a += k[0];
00215 b += k[1];
00216 c += k[2];
00217 bitMixer(a,b,c);
00218 length -= 12;
00219 k += 3;
00220 }
00221
00222
00223 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00224 switch (length)
00225 {
00226 case 12: c += k[2]; b += k[1]; a += k[0]; break;
00227 case 11: c += static_cast<uint32_t>(k8[10]) << 16;
00228 case 10: c += static_cast<uint32_t>(k8[9]) << 8;
00229 case 9 : c += k8[8];
00230 case 8 : b += k[1]; a += k[0]; break;
00231 case 7 : b += static_cast<uint32_t>(k8[6]) << 16;
00232 case 6 : b += static_cast<uint32_t>(k8[5]) << 8;
00233 case 5 : b += k8[4];
00234 case 4 : a += k[0]; break;
00235 case 3 : a += static_cast<uint32_t>(k8[2]) << 16;
00236 case 2 : a += static_cast<uint32_t>(k8[1]) << 8;
00237 case 1 : a += k8[0]; break;
00238 case 0 : return c;
00239 }
00240 }
00241 else if ((u.i & 0x1) == 0)
00242 {
00243
00244 const uint16_t *k = reinterpret_cast<const uint16_t*>(key);
00245
00246
00247 while (length > 12)
00248 {
00249 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00250 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00251 c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
00252 bitMixer(a,b,c);
00253 length -= 12;
00254 k += 6;
00255 }
00256
00257
00258 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00259 switch (length)
00260 {
00261 case 12:
00262 c += k[4] + (static_cast<uint32_t>(k[5]) << 16);
00263 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00264 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00265 break;
00266 case 11:
00267 c += static_cast<uint32_t>(k8[10]) << 16;
00268
00269 case 10:
00270 c += k[4];
00271 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00272 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00273 break;
00274 case 9 :
00275 c += k8[8];
00276
00277 case 8 :
00278 b += k[2] + (static_cast<uint32_t>(k[3]) << 16);
00279 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00280 break;
00281 case 7 :
00282 b += static_cast<uint32_t>(k8[6]) << 16;
00283
00284 case 6 :
00285 b += k[2];
00286 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00287 break;
00288 case 5 :
00289 b += k8[4];
00290
00291 case 4 :
00292 a += k[0] + (static_cast<uint32_t>(k[1]) << 16);
00293 break;
00294 case 3 :
00295 a += static_cast<uint32_t>(k8[2]) << 16;
00296
00297 case 2 :
00298 a += k[0];
00299 break;
00300 case 1 :
00301 a += k8[0];
00302 break;
00303 case 0 : return c;
00304 }
00305 }
00306 else
00307 {
00308 const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
00309
00310
00311 while (length > 12)
00312 {
00313 a += k[0];
00314 a += static_cast<uint32_t>(k[1]) << 8;
00315 a += static_cast<uint32_t>(k[2]) << 16;
00316 a += static_cast<uint32_t>(k[3]) << 24;
00317 b += k[4];
00318 b += static_cast<uint32_t>(k[5]) << 8;
00319 b += static_cast<uint32_t>(k[6]) << 16;
00320 b += static_cast<uint32_t>(k[7]) << 24;
00321 c += k[8];
00322 c += static_cast<uint32_t>(k[9]) << 8;
00323 c += static_cast<uint32_t>(k[10]) << 16;
00324 c += static_cast<uint32_t>(k[11]) << 24;
00325
00326 bitMixer(a,b,c);
00327 length -= 12;
00328 k += 12;
00329 }
00330
00331
00332 switch (length)
00333 {
00334 case 12: c += static_cast<uint32_t>(k[11]) << 24;
00335 case 11: c += static_cast<uint32_t>(k[10]) << 16;
00336 case 10: c += static_cast<uint32_t>(k[9]) << 8;
00337 case 9 : c += k[8];
00338
00339 case 8 : b += static_cast<uint32_t>(k[7]) << 24;
00340 case 7 : b += static_cast<uint32_t>(k[6]) << 16;
00341 case 6 : b += static_cast<uint32_t>(k[5]) << 8;
00342 case 5 : b += k[4];
00343
00344 case 4 : a += static_cast<uint32_t>(k[3]) << 24;
00345 case 3 : a += static_cast<uint32_t>(k[2]) << 16;
00346 case 2 : a += static_cast<uint32_t>(k[1]) << 8;
00347 case 1 : a += k[0];
00348 break;
00349
00350 case 0 : return c;
00351 }
00352 }
00353
00354 bitMixerFinal(a,b,c);
00355 return c;
00356 }
00357 #endif
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 #if !defined (__BYTE_ORDER) || (__BYTE_ORDER == __BIG_ENDIAN)
00370 static unsigned jenkins_hashbig
00371 (
00372 const void *key,
00373 size_t length,
00374 unsigned initval
00375 )
00376 {
00377 uint32_t a, b, c;
00378 union { const void *ptr; size_t i; } u;
00379
00380
00381 a = b = c = 0xdeadbeef + static_cast<uint32_t>(length) + initval;
00382
00383 u.ptr = key;
00384 if ((u.i & 0x3) == 0)
00385 {
00386
00387 const uint32_t *k = reinterpret_cast<const uint32_t*>(key);
00388
00389
00390 while (length > 12)
00391 {
00392 a += k[0];
00393 b += k[1];
00394 c += k[2];
00395 bitMixer(a,b,c);
00396 length -= 12;
00397 k += 3;
00398 }
00399
00400
00401 const uint8_t *k8 = reinterpret_cast<const uint8_t*>(k);
00402
00403 switch (length)
00404 {
00405 case 12: c += k[2]; b += k[1]; a += k[0]; break;
00406 case 11: c += static_cast<uint32_t>(k8[10]) << 8;
00407 case 10: c += static_cast<uint32_t>(k8[9]) << 16;
00408 case 9 : c += static_cast<uint32_t>(k8[8]) << 24;
00409 case 8 : b += k[1]; a += k[0]; break;
00410 case 7 : b += static_cast<uint32_t>(k8[6]) << 8;
00411 case 6 : b += static_cast<uint32_t>(k8[5]) << 16;
00412 case 5 : b += static_cast<uint32_t>(k8[4]) << 24;
00413 case 4 : a += k[0]; break;
00414 case 3 : a += static_cast<uint32_t>(k8[2]) << 8;
00415 case 2 : a += static_cast<uint32_t>(k8[1]) << 16;
00416 case 1 : a += static_cast<uint32_t>(k8[0]) << 24; break;
00417 case 0 : return c;
00418 }
00419 }
00420 else
00421 {
00422
00423 const uint8_t *k = reinterpret_cast<const uint8_t*>(key);
00424
00425
00426 while (length > 12)
00427 {
00428 a += static_cast<uint32_t>(k[0]) << 24;
00429 a += static_cast<uint32_t>(k[1]) << 16;
00430 a += static_cast<uint32_t>(k[2]) << 8;
00431 a += static_cast<uint32_t>(k[3]);
00432 b += static_cast<uint32_t>(k[4]) << 24;
00433 b += static_cast<uint32_t>(k[5]) << 16;
00434 b += static_cast<uint32_t>(k[6]) << 8;
00435 b += static_cast<uint32_t>(k[7]);
00436 c += static_cast<uint32_t>(k[8]) << 24;
00437 c += static_cast<uint32_t>(k[9]) << 16;
00438 c += static_cast<uint32_t>(k[10]) << 8;
00439 c += static_cast<uint32_t>(k[11]);
00440
00441 bitMixer(a,b,c);
00442 length -= 12;
00443 k += 12;
00444 }
00445
00446
00447 switch (length)
00448 {
00449 case 12: c += k[11];
00450 case 11: c += static_cast<uint32_t>(k[10]) << 8;
00451 case 10: c += static_cast<uint32_t>(k[9]) << 16;
00452 case 9 : c += static_cast<uint32_t>(k[8]) << 24;
00453 case 8 : b += k[7];
00454 case 7 : b += static_cast<uint32_t>(k[6]) << 8;
00455 case 6 : b += static_cast<uint32_t>(k[5]) << 16;
00456 case 5 : b += static_cast<uint32_t>(k[4]) << 24;
00457 case 4 : a += k[3];
00458 case 3 : a += static_cast<uint32_t>(k[2]) << 8;
00459 case 2 : a += static_cast<uint32_t>(k[1]) << 16;
00460 case 1 : a += static_cast<uint32_t>(k[0]) << 24;
00461 break;
00462 case 0 : return c;
00463 }
00464 }
00465
00466 bitMixerFinal(a,b,c);
00467 return c;
00468 }
00469 #endif
00470
00471
00472
00473
00474
00475 unsigned Foam::Hasher
00476 (
00477 const void *key,
00478 size_t length,
00479 unsigned initval
00480 )
00481 {
00482 #ifdef __BYTE_ORDER
00483 # if (__BYTE_ORDER == __BIG_ENDIAN)
00484 return jenkins_hashbig(key, length, initval);
00485 # else
00486 return jenkins_hashlittle(key, length, initval);
00487 # endif
00488 #else
00489
00490 const short endianTest = 0x0100;
00491
00492
00493 if (*(reinterpret_cast<const char *>(&endianTest)))
00494 {
00495 return jenkins_hashbig(key, length, initval);
00496 }
00497 else
00498 {
00499 return jenkins_hashlittle(key, length, initval);
00500 }
00501 #endif
00502 }
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 unsigned Foam::HasherInt
00517 (
00518 const uint32_t *k,
00519 size_t length,
00520 unsigned seed
00521 )
00522 {
00523 uint32_t a, b, c;
00524
00525
00526 a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + seed;
00527
00528
00529 while (length > 3)
00530 {
00531 a += k[0];
00532 b += k[1];
00533 c += k[2];
00534 bitMixer(a,b,c);
00535 length -= 3;
00536 k += 3;
00537 }
00538
00539
00540 switch (length)
00541 {
00542 case 3 : c += k[2];
00543 case 2 : b += k[1];
00544 case 1 : a += k[0];
00545 bitMixerFinal(a,b,c);
00546 case 0 :
00547 break;
00548 }
00549
00550 return c;
00551 }
00552
00553
00554
00555
00556
00557
00558
00559
00560 unsigned Foam::HasherDual
00561 (
00562 const uint32_t *k,
00563 size_t length,
00564 unsigned& hash1,
00565 unsigned& hash2
00566 )
00567 {
00568 uint32_t a, b, c;
00569
00570
00571 a = b = c = 0xdeadbeef + (static_cast<uint32_t>(length) << 2) + hash1;
00572 c += hash2;
00573
00574
00575 while (length > 3)
00576 {
00577 a += k[0];
00578 b += k[1];
00579 c += k[2];
00580 bitMixer(a,b,c);
00581 length -= 3;
00582 k += 3;
00583 }
00584
00585
00586 switch (length)
00587 {
00588 case 3 : c += k[2];
00589 case 2 : b += k[1];
00590 case 1 : a += k[0];
00591 bitMixerFinal(a,b,c);
00592 case 0 :
00593 break;
00594 }
00595
00596
00597 hash1 = c;
00598 hash2 = b;
00599
00600
00601 return c;
00602 }
00603
00604
00605