Go to the documentation of this file.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 #include "postgres.h"
00028
00029 #include "access/hash.h"
00030
00031
00032
00033 Datum
00034 hashchar(PG_FUNCTION_ARGS)
00035 {
00036 return hash_uint32((int32) PG_GETARG_CHAR(0));
00037 }
00038
00039 Datum
00040 hashint2(PG_FUNCTION_ARGS)
00041 {
00042 return hash_uint32((int32) PG_GETARG_INT16(0));
00043 }
00044
00045 Datum
00046 hashint4(PG_FUNCTION_ARGS)
00047 {
00048 return hash_uint32(PG_GETARG_INT32(0));
00049 }
00050
00051 Datum
00052 hashint8(PG_FUNCTION_ARGS)
00053 {
00054
00055
00056
00057
00058
00059
00060
00061
00062 int64 val = PG_GETARG_INT64(0);
00063 uint32 lohalf = (uint32) val;
00064 uint32 hihalf = (uint32) (val >> 32);
00065
00066 lohalf ^= (val >= 0) ? hihalf : ~hihalf;
00067
00068 return hash_uint32(lohalf);
00069 }
00070
00071 Datum
00072 hashoid(PG_FUNCTION_ARGS)
00073 {
00074 return hash_uint32((uint32) PG_GETARG_OID(0));
00075 }
00076
00077 Datum
00078 hashenum(PG_FUNCTION_ARGS)
00079 {
00080 return hash_uint32((uint32) PG_GETARG_OID(0));
00081 }
00082
00083 Datum
00084 hashfloat4(PG_FUNCTION_ARGS)
00085 {
00086 float4 key = PG_GETARG_FLOAT4(0);
00087 float8 key8;
00088
00089
00090
00091
00092
00093
00094 if (key == (float4) 0)
00095 PG_RETURN_UINT32(0);
00096
00097
00098
00099
00100
00101
00102
00103
00104 key8 = key;
00105
00106 return hash_any((unsigned char *) &key8, sizeof(key8));
00107 }
00108
00109 Datum
00110 hashfloat8(PG_FUNCTION_ARGS)
00111 {
00112 float8 key = PG_GETARG_FLOAT8(0);
00113
00114
00115
00116
00117
00118
00119 if (key == (float8) 0)
00120 PG_RETURN_UINT32(0);
00121
00122 return hash_any((unsigned char *) &key, sizeof(key));
00123 }
00124
00125 Datum
00126 hashoidvector(PG_FUNCTION_ARGS)
00127 {
00128 oidvector *key = (oidvector *) PG_GETARG_POINTER(0);
00129
00130 return hash_any((unsigned char *) key->values, key->dim1 * sizeof(Oid));
00131 }
00132
00133 Datum
00134 hashint2vector(PG_FUNCTION_ARGS)
00135 {
00136 int2vector *key = (int2vector *) PG_GETARG_POINTER(0);
00137
00138 return hash_any((unsigned char *) key->values, key->dim1 * sizeof(int16));
00139 }
00140
00141 Datum
00142 hashname(PG_FUNCTION_ARGS)
00143 {
00144 char *key = NameStr(*PG_GETARG_NAME(0));
00145 int keylen = strlen(key);
00146
00147 Assert(keylen < NAMEDATALEN);
00148
00149 return hash_any((unsigned char *) key, keylen);
00150 }
00151
00152 Datum
00153 hashtext(PG_FUNCTION_ARGS)
00154 {
00155 text *key = PG_GETARG_TEXT_PP(0);
00156 Datum result;
00157
00158
00159
00160
00161
00162
00163 result = hash_any((unsigned char *) VARDATA_ANY(key),
00164 VARSIZE_ANY_EXHDR(key));
00165
00166
00167 PG_FREE_IF_COPY(key, 0);
00168
00169 return result;
00170 }
00171
00172
00173
00174
00175
00176 Datum
00177 hashvarlena(PG_FUNCTION_ARGS)
00178 {
00179 struct varlena *key = PG_GETARG_VARLENA_PP(0);
00180 Datum result;
00181
00182 result = hash_any((unsigned char *) VARDATA_ANY(key),
00183 VARSIZE_ANY_EXHDR(key));
00184
00185
00186 PG_FREE_IF_COPY(key, 0);
00187
00188 return result;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 #define UINT32_ALIGN_MASK (sizeof(uint32) - 1)
00208
00209
00210 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 #define mix(a,b,c) \
00245 { \
00246 a -= c; a ^= rot(c, 4); c += b; \
00247 b -= a; b ^= rot(a, 6); a += c; \
00248 c -= b; c ^= rot(b, 8); b += a; \
00249 a -= c; a ^= rot(c,16); c += b; \
00250 b -= a; b ^= rot(a,19); a += c; \
00251 c -= b; c ^= rot(b, 4); b += a; \
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 #define final(a,b,c) \
00279 { \
00280 c ^= b; c -= rot(b,14); \
00281 a ^= c; a -= rot(c,11); \
00282 b ^= a; b -= rot(a,25); \
00283 c ^= b; c -= rot(b,16); \
00284 a ^= c; a -= rot(c, 4); \
00285 b ^= a; b -= rot(a,14); \
00286 c ^= b; c -= rot(b,24); \
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 Datum
00305 hash_any(register const unsigned char *k, register int keylen)
00306 {
00307 register uint32 a,
00308 b,
00309 c,
00310 len;
00311
00312
00313 len = keylen;
00314 a = b = c = 0x9e3779b9 + len + 3923095;
00315
00316
00317 if (((intptr_t) k & UINT32_ALIGN_MASK) == 0)
00318 {
00319
00320 register const uint32 *ka = (const uint32 *) k;
00321
00322
00323 while (len >= 12)
00324 {
00325 a += ka[0];
00326 b += ka[1];
00327 c += ka[2];
00328 mix(a, b, c);
00329 ka += 3;
00330 len -= 12;
00331 }
00332
00333
00334 k = (const unsigned char *) ka;
00335 #ifdef WORDS_BIGENDIAN
00336 switch (len)
00337 {
00338 case 11:
00339 c += ((uint32) k[10] << 8);
00340
00341 case 10:
00342 c += ((uint32) k[9] << 16);
00343
00344 case 9:
00345 c += ((uint32) k[8] << 24);
00346
00347
00348 case 8:
00349 b += ka[1];
00350 a += ka[0];
00351 break;
00352 case 7:
00353 b += ((uint32) k[6] << 8);
00354
00355 case 6:
00356 b += ((uint32) k[5] << 16);
00357
00358 case 5:
00359 b += ((uint32) k[4] << 24);
00360
00361 case 4:
00362 a += ka[0];
00363 break;
00364 case 3:
00365 a += ((uint32) k[2] << 8);
00366
00367 case 2:
00368 a += ((uint32) k[1] << 16);
00369
00370 case 1:
00371 a += ((uint32) k[0] << 24);
00372
00373 }
00374 #else
00375 switch (len)
00376 {
00377 case 11:
00378 c += ((uint32) k[10] << 24);
00379
00380 case 10:
00381 c += ((uint32) k[9] << 16);
00382
00383 case 9:
00384 c += ((uint32) k[8] << 8);
00385
00386
00387 case 8:
00388 b += ka[1];
00389 a += ka[0];
00390 break;
00391 case 7:
00392 b += ((uint32) k[6] << 16);
00393
00394 case 6:
00395 b += ((uint32) k[5] << 8);
00396
00397 case 5:
00398 b += k[4];
00399
00400 case 4:
00401 a += ka[0];
00402 break;
00403 case 3:
00404 a += ((uint32) k[2] << 16);
00405
00406 case 2:
00407 a += ((uint32) k[1] << 8);
00408
00409 case 1:
00410 a += k[0];
00411
00412 }
00413 #endif
00414 }
00415 else
00416 {
00417
00418
00419
00420 while (len >= 12)
00421 {
00422 #ifdef WORDS_BIGENDIAN
00423 a += (k[3] + ((uint32) k[2] << 8) + ((uint32) k[1] << 16) + ((uint32) k[0] << 24));
00424 b += (k[7] + ((uint32) k[6] << 8) + ((uint32) k[5] << 16) + ((uint32) k[4] << 24));
00425 c += (k[11] + ((uint32) k[10] << 8) + ((uint32) k[9] << 16) + ((uint32) k[8] << 24));
00426 #else
00427 a += (k[0] + ((uint32) k[1] << 8) + ((uint32) k[2] << 16) + ((uint32) k[3] << 24));
00428 b += (k[4] + ((uint32) k[5] << 8) + ((uint32) k[6] << 16) + ((uint32) k[7] << 24));
00429 c += (k[8] + ((uint32) k[9] << 8) + ((uint32) k[10] << 16) + ((uint32) k[11] << 24));
00430 #endif
00431 mix(a, b, c);
00432 k += 12;
00433 len -= 12;
00434 }
00435
00436
00437 #ifdef WORDS_BIGENDIAN
00438 switch (len)
00439 {
00440 case 11:
00441 c += ((uint32) k[10] << 8);
00442 case 10:
00443 c += ((uint32) k[9] << 16);
00444 case 9:
00445 c += ((uint32) k[8] << 24);
00446
00447 case 8:
00448 b += k[7];
00449 case 7:
00450 b += ((uint32) k[6] << 8);
00451 case 6:
00452 b += ((uint32) k[5] << 16);
00453 case 5:
00454 b += ((uint32) k[4] << 24);
00455 case 4:
00456 a += k[3];
00457 case 3:
00458 a += ((uint32) k[2] << 8);
00459 case 2:
00460 a += ((uint32) k[1] << 16);
00461 case 1:
00462 a += ((uint32) k[0] << 24);
00463
00464 }
00465 #else
00466 switch (len)
00467 {
00468 case 11:
00469 c += ((uint32) k[10] << 24);
00470 case 10:
00471 c += ((uint32) k[9] << 16);
00472 case 9:
00473 c += ((uint32) k[8] << 8);
00474
00475 case 8:
00476 b += ((uint32) k[7] << 24);
00477 case 7:
00478 b += ((uint32) k[6] << 16);
00479 case 6:
00480 b += ((uint32) k[5] << 8);
00481 case 5:
00482 b += k[4];
00483 case 4:
00484 a += ((uint32) k[3] << 24);
00485 case 3:
00486 a += ((uint32) k[2] << 16);
00487 case 2:
00488 a += ((uint32) k[1] << 8);
00489 case 1:
00490 a += k[0];
00491
00492 }
00493 #endif
00494 }
00495
00496 final(a, b, c);
00497
00498
00499 return UInt32GetDatum(c);
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509 Datum
00510 hash_uint32(uint32 k)
00511 {
00512 register uint32 a,
00513 b,
00514 c;
00515
00516 a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095;
00517 a += k;
00518
00519 final(a, b, c);
00520
00521
00522 return UInt32GetDatum(c);
00523 }