PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hashfunc.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "utils/builtins.h"
Include dependency graph for hashfunc.c:

Go to the source code of this file.

Macros

#define UINT32_ALIGN_MASK   (sizeof(uint32) - 1)
 
#define rot(x, k)   (((x)<<(k)) | ((x)>>(32-(k))))
 
#define mix(a, b, c)
 
#define final(a, b, c)
 

Functions

Datum hashchar (PG_FUNCTION_ARGS)
 
Datum hashcharextended (PG_FUNCTION_ARGS)
 
Datum hashint2 (PG_FUNCTION_ARGS)
 
Datum hashint2extended (PG_FUNCTION_ARGS)
 
Datum hashint4 (PG_FUNCTION_ARGS)
 
Datum hashint4extended (PG_FUNCTION_ARGS)
 
Datum hashint8 (PG_FUNCTION_ARGS)
 
Datum hashint8extended (PG_FUNCTION_ARGS)
 
Datum hashoid (PG_FUNCTION_ARGS)
 
Datum hashoidextended (PG_FUNCTION_ARGS)
 
Datum hashenum (PG_FUNCTION_ARGS)
 
Datum hashenumextended (PG_FUNCTION_ARGS)
 
Datum hashfloat4 (PG_FUNCTION_ARGS)
 
Datum hashfloat4extended (PG_FUNCTION_ARGS)
 
Datum hashfloat8 (PG_FUNCTION_ARGS)
 
Datum hashfloat8extended (PG_FUNCTION_ARGS)
 
Datum hashoidvector (PG_FUNCTION_ARGS)
 
Datum hashoidvectorextended (PG_FUNCTION_ARGS)
 
Datum hashname (PG_FUNCTION_ARGS)
 
Datum hashnameextended (PG_FUNCTION_ARGS)
 
Datum hashtext (PG_FUNCTION_ARGS)
 
Datum hashtextextended (PG_FUNCTION_ARGS)
 
Datum hashvarlena (PG_FUNCTION_ARGS)
 
Datum hashvarlenaextended (PG_FUNCTION_ARGS)
 
Datum hash_any (register const unsigned char *k, register int keylen)
 
Datum hash_any_extended (register const unsigned char *k, register int keylen, uint64 seed)
 
Datum hash_uint32 (uint32 k)
 
Datum hash_uint32_extended (uint32 k, uint64 seed)
 

Macro Definition Documentation

#define final (   a,
  b,
  c 
)
Value:
{ \
c ^= b; c -= rot(b,14); \
a ^= c; a -= rot(c,11); \
b ^= a; b -= rot(a,25); \
c ^= b; c -= rot(b,16); \
a ^= c; a -= rot(c, 4); \
b ^= a; b -= rot(a,14); \
c ^= b; c -= rot(b,24); \
}
#define rot(x, k)
Definition: hashfunc.c:330
char * c

Definition at line 398 of file hashfunc.c.

#define mix (   a,
  b,
  c 
)
Value:
{ \
a -= c; a ^= rot(c, 4); c += b; \
b -= a; b ^= rot(a, 6); a += c; \
c -= b; c ^= rot(b, 8); b += a; \
a -= c; a ^= rot(c,16); c += b; \
b -= a; b ^= rot(a,19); a += c; \
c -= b; c ^= rot(b, 4); b += a; \
}
#define rot(x, k)
Definition: hashfunc.c:330
char * c

Definition at line 364 of file hashfunc.c.

Referenced by hash_any(), hash_any_extended(), hash_uint32_extended(), pgp_cfb_decrypt(), and pgp_cfb_encrypt().

#define rot (   x,
 
)    (((x)<<(k)) | ((x)>>(32-(k))))

Definition at line 330 of file hashfunc.c.

#define UINT32_ALIGN_MASK   (sizeof(uint32) - 1)

Definition at line 327 of file hashfunc.c.

Referenced by hash_any(), and hash_any_extended().

Function Documentation

Datum hash_any ( register const unsigned char *  k,
register int  keylen 
)

Definition at line 428 of file hashfunc.c.

References mix, UINT32_ALIGN_MASK, and UInt32GetDatum.

Referenced by bernoulli_nextsampletuple(), bms_hash_value(), citext_hash(), hash_numeric(), hashbpchar(), hashfloat4(), hashfloat8(), hashinet(), hashmacaddr(), hashmacaddr8(), hashname(), hashoidvector(), hashtext(), hashvarlena(), hstore_hash(), JsonbHashScalarValue(), lexeme_hash(), make_text_key(), namehashfast(), ResourceArrayAdd(), ResourceArrayRemove(), sepgsql_avc_hash(), string_hash(), system_nextsampleblock(), tag_hash(), uuid_hash(), and varstr_abbrev_convert().

429 {
430  register uint32 a,
431  b,
432  c,
433  len;
434 
435  /* Set up the internal state */
436  len = keylen;
437  a = b = c = 0x9e3779b9 + len + 3923095;
438 
439  /* If the source pointer is word-aligned, we use word-wide fetches */
440  if (((uintptr_t) k & UINT32_ALIGN_MASK) == 0)
441  {
442  /* Code path for aligned source data */
443  register const uint32 *ka = (const uint32 *) k;
444 
445  /* handle most of the key */
446  while (len >= 12)
447  {
448  a += ka[0];
449  b += ka[1];
450  c += ka[2];
451  mix(a, b, c);
452  ka += 3;
453  len -= 12;
454  }
455 
456  /* handle the last 11 bytes */
457  k = (const unsigned char *) ka;
458 #ifdef WORDS_BIGENDIAN
459  switch (len)
460  {
461  case 11:
462  c += ((uint32) k[10] << 8);
463  /* fall through */
464  case 10:
465  c += ((uint32) k[9] << 16);
466  /* fall through */
467  case 9:
468  c += ((uint32) k[8] << 24);
469  /* the lowest byte of c is reserved for the length */
470  /* fall through */
471  case 8:
472  b += ka[1];
473  a += ka[0];
474  break;
475  case 7:
476  b += ((uint32) k[6] << 8);
477  /* fall through */
478  case 6:
479  b += ((uint32) k[5] << 16);
480  /* fall through */
481  case 5:
482  b += ((uint32) k[4] << 24);
483  /* fall through */
484  case 4:
485  a += ka[0];
486  break;
487  case 3:
488  a += ((uint32) k[2] << 8);
489  /* fall through */
490  case 2:
491  a += ((uint32) k[1] << 16);
492  /* fall through */
493  case 1:
494  a += ((uint32) k[0] << 24);
495  /* case 0: nothing left to add */
496  }
497 #else /* !WORDS_BIGENDIAN */
498  switch (len)
499  {
500  case 11:
501  c += ((uint32) k[10] << 24);
502  /* fall through */
503  case 10:
504  c += ((uint32) k[9] << 16);
505  /* fall through */
506  case 9:
507  c += ((uint32) k[8] << 8);
508  /* the lowest byte of c is reserved for the length */
509  /* fall through */
510  case 8:
511  b += ka[1];
512  a += ka[0];
513  break;
514  case 7:
515  b += ((uint32) k[6] << 16);
516  /* fall through */
517  case 6:
518  b += ((uint32) k[5] << 8);
519  /* fall through */
520  case 5:
521  b += k[4];
522  /* fall through */
523  case 4:
524  a += ka[0];
525  break;
526  case 3:
527  a += ((uint32) k[2] << 16);
528  /* fall through */
529  case 2:
530  a += ((uint32) k[1] << 8);
531  /* fall through */
532  case 1:
533  a += k[0];
534  /* case 0: nothing left to add */
535  }
536 #endif /* WORDS_BIGENDIAN */
537  }
538  else
539  {
540  /* Code path for non-aligned source data */
541 
542  /* handle most of the key */
543  while (len >= 12)
544  {
545 #ifdef WORDS_BIGENDIAN
546  a += (k[3] + ((uint32) k[2] << 8) + ((uint32) k[1] << 16) + ((uint32) k[0] << 24));
547  b += (k[7] + ((uint32) k[6] << 8) + ((uint32) k[5] << 16) + ((uint32) k[4] << 24));
548  c += (k[11] + ((uint32) k[10] << 8) + ((uint32) k[9] << 16) + ((uint32) k[8] << 24));
549 #else /* !WORDS_BIGENDIAN */
550  a += (k[0] + ((uint32) k[1] << 8) + ((uint32) k[2] << 16) + ((uint32) k[3] << 24));
551  b += (k[4] + ((uint32) k[5] << 8) + ((uint32) k[6] << 16) + ((uint32) k[7] << 24));
552  c += (k[8] + ((uint32) k[9] << 8) + ((uint32) k[10] << 16) + ((uint32) k[11] << 24));
553 #endif /* WORDS_BIGENDIAN */
554  mix(a, b, c);
555  k += 12;
556  len -= 12;
557  }
558 
559  /* handle the last 11 bytes */
560 #ifdef WORDS_BIGENDIAN
561  switch (len) /* all the case statements fall through */
562  {
563  case 11:
564  c += ((uint32) k[10] << 8);
565  case 10:
566  c += ((uint32) k[9] << 16);
567  case 9:
568  c += ((uint32) k[8] << 24);
569  /* the lowest byte of c is reserved for the length */
570  case 8:
571  b += k[7];
572  case 7:
573  b += ((uint32) k[6] << 8);
574  case 6:
575  b += ((uint32) k[5] << 16);
576  case 5:
577  b += ((uint32) k[4] << 24);
578  case 4:
579  a += k[3];
580  case 3:
581  a += ((uint32) k[2] << 8);
582  case 2:
583  a += ((uint32) k[1] << 16);
584  case 1:
585  a += ((uint32) k[0] << 24);
586  /* case 0: nothing left to add */
587  }
588 #else /* !WORDS_BIGENDIAN */
589  switch (len) /* all the case statements fall through */
590  {
591  case 11:
592  c += ((uint32) k[10] << 24);
593  case 10:
594  c += ((uint32) k[9] << 16);
595  case 9:
596  c += ((uint32) k[8] << 8);
597  /* the lowest byte of c is reserved for the length */
598  case 8:
599  b += ((uint32) k[7] << 24);
600  case 7:
601  b += ((uint32) k[6] << 16);
602  case 6:
603  b += ((uint32) k[5] << 8);
604  case 5:
605  b += k[4];
606  case 4:
607  a += ((uint32) k[3] << 24);
608  case 3:
609  a += ((uint32) k[2] << 16);
610  case 2:
611  a += ((uint32) k[1] << 8);
612  case 1:
613  a += k[0];
614  /* case 0: nothing left to add */
615  }
616 #endif /* WORDS_BIGENDIAN */
617  }
618 
619  final(a, b, c);
620 
621  /* report the result */
622  return UInt32GetDatum(c);
623 }
char * c
unsigned int uint32
Definition: c.h:258
#define UInt32GetDatum(X)
Definition: postgres.h:499
#define UINT32_ALIGN_MASK
Definition: hashfunc.c:327
#define mix(a, b, c)
Definition: hashfunc.c:364
Datum hash_any_extended ( register const unsigned char *  k,
register int  keylen,
uint64  seed 
)

Definition at line 634 of file hashfunc.c.

References mix, PG_RETURN_UINT64, and UINT32_ALIGN_MASK.

Referenced by AppendJumble(), hash_numeric_extended(), hashbpcharextended(), hashfloat4extended(), hashfloat8extended(), hashinetextended(), hashmacaddr8extended(), hashmacaddrextended(), hashnameextended(), hashoidvectorextended(), hashtextextended(), hashvarlenaextended(), JsonbHashScalarValueExtended(), pgss_hash_string(), pgss_post_parse_analyze(), and uuid_hash_extended().

636 {
637  register uint32 a,
638  b,
639  c,
640  len;
641 
642  /* Set up the internal state */
643  len = keylen;
644  a = b = c = 0x9e3779b9 + len + 3923095;
645 
646  /* If the seed is non-zero, use it to perturb the internal state. */
647  if (seed != 0)
648  {
649  /*
650  * In essence, the seed is treated as part of the data being hashed,
651  * but for simplicity, we pretend that it's padded with four bytes of
652  * zeroes so that the seed constitutes a 12-byte chunk.
653  */
654  a += (uint32) (seed >> 32);
655  b += (uint32) seed;
656  mix(a, b, c);
657  }
658 
659  /* If the source pointer is word-aligned, we use word-wide fetches */
660  if (((uintptr_t) k & UINT32_ALIGN_MASK) == 0)
661  {
662  /* Code path for aligned source data */
663  register const uint32 *ka = (const uint32 *) k;
664 
665  /* handle most of the key */
666  while (len >= 12)
667  {
668  a += ka[0];
669  b += ka[1];
670  c += ka[2];
671  mix(a, b, c);
672  ka += 3;
673  len -= 12;
674  }
675 
676  /* handle the last 11 bytes */
677  k = (const unsigned char *) ka;
678 #ifdef WORDS_BIGENDIAN
679  switch (len)
680  {
681  case 11:
682  c += ((uint32) k[10] << 8);
683  /* fall through */
684  case 10:
685  c += ((uint32) k[9] << 16);
686  /* fall through */
687  case 9:
688  c += ((uint32) k[8] << 24);
689  /* the lowest byte of c is reserved for the length */
690  /* fall through */
691  case 8:
692  b += ka[1];
693  a += ka[0];
694  break;
695  case 7:
696  b += ((uint32) k[6] << 8);
697  /* fall through */
698  case 6:
699  b += ((uint32) k[5] << 16);
700  /* fall through */
701  case 5:
702  b += ((uint32) k[4] << 24);
703  /* fall through */
704  case 4:
705  a += ka[0];
706  break;
707  case 3:
708  a += ((uint32) k[2] << 8);
709  /* fall through */
710  case 2:
711  a += ((uint32) k[1] << 16);
712  /* fall through */
713  case 1:
714  a += ((uint32) k[0] << 24);
715  /* case 0: nothing left to add */
716  }
717 #else /* !WORDS_BIGENDIAN */
718  switch (len)
719  {
720  case 11:
721  c += ((uint32) k[10] << 24);
722  /* fall through */
723  case 10:
724  c += ((uint32) k[9] << 16);
725  /* fall through */
726  case 9:
727  c += ((uint32) k[8] << 8);
728  /* the lowest byte of c is reserved for the length */
729  /* fall through */
730  case 8:
731  b += ka[1];
732  a += ka[0];
733  break;
734  case 7:
735  b += ((uint32) k[6] << 16);
736  /* fall through */
737  case 6:
738  b += ((uint32) k[5] << 8);
739  /* fall through */
740  case 5:
741  b += k[4];
742  /* fall through */
743  case 4:
744  a += ka[0];
745  break;
746  case 3:
747  a += ((uint32) k[2] << 16);
748  /* fall through */
749  case 2:
750  a += ((uint32) k[1] << 8);
751  /* fall through */
752  case 1:
753  a += k[0];
754  /* case 0: nothing left to add */
755  }
756 #endif /* WORDS_BIGENDIAN */
757  }
758  else
759  {
760  /* Code path for non-aligned source data */
761 
762  /* handle most of the key */
763  while (len >= 12)
764  {
765 #ifdef WORDS_BIGENDIAN
766  a += (k[3] + ((uint32) k[2] << 8) + ((uint32) k[1] << 16) + ((uint32) k[0] << 24));
767  b += (k[7] + ((uint32) k[6] << 8) + ((uint32) k[5] << 16) + ((uint32) k[4] << 24));
768  c += (k[11] + ((uint32) k[10] << 8) + ((uint32) k[9] << 16) + ((uint32) k[8] << 24));
769 #else /* !WORDS_BIGENDIAN */
770  a += (k[0] + ((uint32) k[1] << 8) + ((uint32) k[2] << 16) + ((uint32) k[3] << 24));
771  b += (k[4] + ((uint32) k[5] << 8) + ((uint32) k[6] << 16) + ((uint32) k[7] << 24));
772  c += (k[8] + ((uint32) k[9] << 8) + ((uint32) k[10] << 16) + ((uint32) k[11] << 24));
773 #endif /* WORDS_BIGENDIAN */
774  mix(a, b, c);
775  k += 12;
776  len -= 12;
777  }
778 
779  /* handle the last 11 bytes */
780 #ifdef WORDS_BIGENDIAN
781  switch (len) /* all the case statements fall through */
782  {
783  case 11:
784  c += ((uint32) k[10] << 8);
785  case 10:
786  c += ((uint32) k[9] << 16);
787  case 9:
788  c += ((uint32) k[8] << 24);
789  /* the lowest byte of c is reserved for the length */
790  case 8:
791  b += k[7];
792  case 7:
793  b += ((uint32) k[6] << 8);
794  case 6:
795  b += ((uint32) k[5] << 16);
796  case 5:
797  b += ((uint32) k[4] << 24);
798  case 4:
799  a += k[3];
800  case 3:
801  a += ((uint32) k[2] << 8);
802  case 2:
803  a += ((uint32) k[1] << 16);
804  case 1:
805  a += ((uint32) k[0] << 24);
806  /* case 0: nothing left to add */
807  }
808 #else /* !WORDS_BIGENDIAN */
809  switch (len) /* all the case statements fall through */
810  {
811  case 11:
812  c += ((uint32) k[10] << 24);
813  case 10:
814  c += ((uint32) k[9] << 16);
815  case 9:
816  c += ((uint32) k[8] << 8);
817  /* the lowest byte of c is reserved for the length */
818  case 8:
819  b += ((uint32) k[7] << 24);
820  case 7:
821  b += ((uint32) k[6] << 16);
822  case 6:
823  b += ((uint32) k[5] << 8);
824  case 5:
825  b += k[4];
826  case 4:
827  a += ((uint32) k[3] << 24);
828  case 3:
829  a += ((uint32) k[2] << 16);
830  case 2:
831  a += ((uint32) k[1] << 8);
832  case 1:
833  a += k[0];
834  /* case 0: nothing left to add */
835  }
836 #endif /* WORDS_BIGENDIAN */
837  }
838 
839  final(a, b, c);
840 
841  /* report the result */
842  PG_RETURN_UINT64(((uint64) b << 32) | c);
843 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:328
char * c
unsigned int uint32
Definition: c.h:258
#define UINT32_ALIGN_MASK
Definition: hashfunc.c:327
#define mix(a, b, c)
Definition: hashfunc.c:364
Datum hash_uint32 ( uint32  k)

Definition at line 853 of file hashfunc.c.

References UInt32GetDatum.

Referenced by BuildTupleHashTable(), hash_range(), hashchar(), hashenum(), hashint2(), hashint4(), hashint8(), hashoid(), hashTupleDesc(), macaddr_abbrev_convert(), timetz_hash(), uint32_hash(), uuid_abbrev_convert(), and varstr_abbrev_convert().

854 {
855  register uint32 a,
856  b,
857  c;
858 
859  a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095;
860  a += k;
861 
862  final(a, b, c);
863 
864  /* report the result */
865  return UInt32GetDatum(c);
866 }
char * c
unsigned int uint32
Definition: c.h:258
#define UInt32GetDatum(X)
Definition: postgres.h:499
Datum hash_uint32_extended ( uint32  k,
uint64  seed 
)

Definition at line 874 of file hashfunc.c.

References mix, and PG_RETURN_UINT64.

Referenced by hash_aclitem_extended(), hash_range_extended(), hashcharextended(), hashenumextended(), hashint2extended(), hashint4extended(), hashint8extended(), hashoidextended(), and timetz_hash_extended().

875 {
876  register uint32 a,
877  b,
878  c;
879 
880  a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095;
881 
882  if (seed != 0)
883  {
884  a += (uint32) (seed >> 32);
885  b += (uint32) seed;
886  mix(a, b, c);
887  }
888 
889  a += k;
890 
891  final(a, b, c);
892 
893  /* report the result */
894  PG_RETURN_UINT64(((uint64) b << 32) | c);
895 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:328
char * c
unsigned int uint32
Definition: c.h:258
#define mix(a, b, c)
Definition: hashfunc.c:364
Datum hashchar ( PG_FUNCTION_ARGS  )

Definition at line 44 of file hashfunc.c.

References hash_uint32(), and PG_GETARG_CHAR.

45 {
46  return hash_uint32((int32) PG_GETARG_CHAR(0));
47 }
signed int int32
Definition: c.h:246
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238
Datum hashcharextended ( PG_FUNCTION_ARGS  )

Definition at line 50 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_CHAR, and PG_GETARG_INT64.

Referenced by JsonbHashScalarValueExtended().

51 {
53 }
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
signed int int32
Definition: c.h:246
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:238
Datum hashenum ( PG_FUNCTION_ARGS  )

Definition at line 125 of file hashfunc.c.

References hash_uint32(), and PG_GETARG_OID.

126 {
127  return hash_uint32((uint32) PG_GETARG_OID(0));
128 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
unsigned int uint32
Definition: c.h:258
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
Datum hashenumextended ( PG_FUNCTION_ARGS  )

Definition at line 131 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_INT64, and PG_GETARG_OID.

132 {
134 }
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
unsigned int uint32
Definition: c.h:258
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hashfloat4 ( PG_FUNCTION_ARGS  )

Definition at line 137 of file hashfunc.c.

References hash_any(), PG_GETARG_FLOAT4, and PG_RETURN_UINT32.

138 {
139  float4 key = PG_GETARG_FLOAT4(0);
140  float8 key8;
141 
142  /*
143  * On IEEE-float machines, minus zero and zero have different bit patterns
144  * but should compare as equal. We must ensure that they have the same
145  * hash value, which is most reliably done this way:
146  */
147  if (key == (float4) 0)
148  PG_RETURN_UINT32(0);
149 
150  /*
151  * To support cross-type hashing of float8 and float4, we want to return
152  * the same hash value hashfloat8 would produce for an equal float8 value.
153  * So, widen the value to float8 and hash that. (We must do this rather
154  * than have hashfloat8 try to narrow its value to float4; that could fail
155  * on overflow.)
156  */
157  key8 = key;
158 
159  return hash_any((unsigned char *) &key8, sizeof(key8));
160 }
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:315
double float8
Definition: c.h:375
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
float float4
Definition: c.h:374
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
Datum hashfloat4extended ( PG_FUNCTION_ARGS  )

Definition at line 163 of file hashfunc.c.

References hash_any_extended(), PG_GETARG_FLOAT4, PG_GETARG_INT64, and PG_RETURN_UINT64.

164 {
165  float4 key = PG_GETARG_FLOAT4(0);
166  uint64 seed = PG_GETARG_INT64(1);
167  float8 key8;
168 
169  /* Same approach as hashfloat4 */
170  if (key == (float4) 0)
171  PG_RETURN_UINT64(seed);
172  key8 = key;
173 
174  return hash_any_extended((unsigned char *) &key8, sizeof(key8), seed);
175 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:328
double float8
Definition: c.h:375
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
float float4
Definition: c.h:374
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
Datum hashfloat8 ( PG_FUNCTION_ARGS  )

Definition at line 178 of file hashfunc.c.

References hash_any(), PG_GETARG_FLOAT8, and PG_RETURN_UINT32.

Referenced by tablesample_init().

179 {
180  float8 key = PG_GETARG_FLOAT8(0);
181 
182  /*
183  * On IEEE-float machines, minus zero and zero have different bit patterns
184  * but should compare as equal. We must ensure that they have the same
185  * hash value, which is most reliably done this way:
186  */
187  if (key == (float8) 0)
188  PG_RETURN_UINT32(0);
189 
190  return hash_any((unsigned char *) &key, sizeof(key));
191 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:315
double float8
Definition: c.h:375
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
Datum hashfloat8extended ( PG_FUNCTION_ARGS  )

Definition at line 194 of file hashfunc.c.

References hash_any_extended(), PG_GETARG_FLOAT8, PG_GETARG_INT64, and PG_RETURN_UINT64.

195 {
196  float8 key = PG_GETARG_FLOAT8(0);
197  uint64 seed = PG_GETARG_INT64(1);
198 
199  /* Same approach as hashfloat8 */
200  if (key == (float8) 0)
201  PG_RETURN_UINT64(seed);
202 
203  return hash_any_extended((unsigned char *) &key, sizeof(key), seed);
204 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:328
double float8
Definition: c.h:375
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
Datum hashint2 ( PG_FUNCTION_ARGS  )

Definition at line 56 of file hashfunc.c.

References hash_uint32(), and PG_GETARG_INT16.

57 {
58  return hash_uint32((int32) PG_GETARG_INT16(0));
59 }
signed int int32
Definition: c.h:246
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
Datum hashint2extended ( PG_FUNCTION_ARGS  )

Definition at line 62 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_INT16, and PG_GETARG_INT64.

63 {
65 }
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
signed int int32
Definition: c.h:246
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hashint4 ( PG_FUNCTION_ARGS  )

Definition at line 68 of file hashfunc.c.

References hash_uint32(), and PG_GETARG_INT32.

69 {
70  return hash_uint32(PG_GETARG_INT32(0));
71 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
Datum hashint4extended ( PG_FUNCTION_ARGS  )

Definition at line 74 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_INT32, and PG_GETARG_INT64.

75 {
77 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hashint8 ( PG_FUNCTION_ARGS  )

Definition at line 80 of file hashfunc.c.

References hash_uint32(), PG_GETARG_INT64, and val.

Referenced by interval_hash(), pg_lsn_hash(), time_hash(), timestamp_hash(), and timetz_hash().

81 {
82  /*
83  * The idea here is to produce a hash value compatible with the values
84  * produced by hashint4 and hashint2 for logically equal inputs; this is
85  * necessary to support cross-type hash joins across these input types.
86  * Since all three types are signed, we can xor the high half of the int8
87  * value if the sign is positive, or the complement of the high half when
88  * the sign is negative.
89  */
90  int64 val = PG_GETARG_INT64(0);
91  uint32 lohalf = (uint32) val;
92  uint32 hihalf = (uint32) (val >> 32);
93 
94  lohalf ^= (val >= 0) ? hihalf : ~hihalf;
95 
96  return hash_uint32(lohalf);
97 }
unsigned int uint32
Definition: c.h:258
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
long val
Definition: informix.c:689
Datum hashint8extended ( PG_FUNCTION_ARGS  )

Definition at line 100 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_INT64, and val.

Referenced by interval_hash_extended(), pg_lsn_hash_extended(), time_hash_extended(), timestamp_hash_extended(), and timetz_hash_extended().

101 {
102  /* Same approach as hashint8 */
103  int64 val = PG_GETARG_INT64(0);
104  uint32 lohalf = (uint32) val;
105  uint32 hihalf = (uint32) (val >> 32);
106 
107  lohalf ^= (val >= 0) ? hihalf : ~hihalf;
108 
109  return hash_uint32_extended(lohalf, PG_GETARG_INT64(1));
110 }
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
unsigned int uint32
Definition: c.h:258
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
long val
Definition: informix.c:689
Datum hashname ( PG_FUNCTION_ARGS  )

Definition at line 225 of file hashfunc.c.

References hash_any(), NameStr, and PG_GETARG_NAME.

226 {
227  char *key = NameStr(*PG_GETARG_NAME(0));
228 
229  return hash_any((unsigned char *) key, strlen(key));
230 }
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum hashnameextended ( PG_FUNCTION_ARGS  )

Definition at line 233 of file hashfunc.c.

References hash_any_extended(), NameStr, PG_GETARG_INT64, and PG_GETARG_NAME.

234 {
235  char *key = NameStr(*PG_GETARG_NAME(0));
236 
237  return hash_any_extended((unsigned char *) key, strlen(key),
238  PG_GETARG_INT64(1));
239 }
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum hashoid ( PG_FUNCTION_ARGS  )

Definition at line 113 of file hashfunc.c.

References hash_uint32(), and PG_GETARG_OID.

114 {
115  return hash_uint32((uint32) PG_GETARG_OID(0));
116 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
unsigned int uint32
Definition: c.h:258
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
Datum hashoidextended ( PG_FUNCTION_ARGS  )

Definition at line 119 of file hashfunc.c.

References hash_uint32_extended(), PG_GETARG_INT64, and PG_GETARG_OID.

120 {
122 }
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:874
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
unsigned int uint32
Definition: c.h:258
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hashoidvector ( PG_FUNCTION_ARGS  )

Definition at line 207 of file hashfunc.c.

References oidvector::dim1, hash_any(), PG_GETARG_POINTER, and oidvector::values.

Referenced by oidvectorhashfast().

208 {
209  oidvector *key = (oidvector *) PG_GETARG_POINTER(0);
210 
211  return hash_any((unsigned char *) key->values, key->dim1 * sizeof(Oid));
212 }
Definition: c.h:472
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:480
int dim1
Definition: c.h:478
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
Datum hashoidvectorextended ( PG_FUNCTION_ARGS  )

Definition at line 215 of file hashfunc.c.

References oidvector::dim1, hash_any_extended(), PG_GETARG_INT64, PG_GETARG_POINTER, and oidvector::values.

216 {
217  oidvector *key = (oidvector *) PG_GETARG_POINTER(0);
218 
219  return hash_any_extended((unsigned char *) key->values,
220  key->dim1 * sizeof(Oid),
221  PG_GETARG_INT64(1));
222 }
Definition: c.h:472
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:480
int dim1
Definition: c.h:478
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
Datum hashtext ( PG_FUNCTION_ARGS  )

Definition at line 242 of file hashfunc.c.

References hash_any(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by texthashfast().

243 {
244  text *key = PG_GETARG_TEXT_PP(0);
245  Datum result;
246 
247  /*
248  * Note: this is currently identical in behavior to hashvarlena, but keep
249  * it as a separate function in case we someday want to do something
250  * different in non-C locales. (See also hashbpchar, if so.)
251  */
252  result = hash_any((unsigned char *) VARDATA_ANY(key),
253  VARSIZE_ANY_EXHDR(key));
254 
255  /* Avoid leaking memory for toasted inputs */
256  PG_FREE_IF_COPY(key, 0);
257 
258  return result;
259 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
uintptr_t Datum
Definition: postgres.h:372
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum hashtextextended ( PG_FUNCTION_ARGS  )

Definition at line 262 of file hashfunc.c.

References hash_any_extended(), PG_FREE_IF_COPY, PG_GETARG_INT64, PG_GETARG_TEXT_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

263 {
264  text *key = PG_GETARG_TEXT_PP(0);
265  Datum result;
266 
267  /* Same approach as hashtext */
268  result = hash_any_extended((unsigned char *) VARDATA_ANY(key),
269  VARSIZE_ANY_EXHDR(key),
270  PG_GETARG_INT64(1));
271 
272  PG_FREE_IF_COPY(key, 0);
273 
274  return result;
275 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
uintptr_t Datum
Definition: postgres.h:372
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634
Datum hashvarlena ( PG_FUNCTION_ARGS  )

Definition at line 282 of file hashfunc.c.

References hash_any(), PG_FREE_IF_COPY, PG_GETARG_VARLENA_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

283 {
284  struct varlena *key = PG_GETARG_VARLENA_PP(0);
285  Datum result;
286 
287  result = hash_any((unsigned char *) VARDATA_ANY(key),
288  VARSIZE_ANY_EXHDR(key));
289 
290  /* Avoid leaking memory for toasted inputs */
291  PG_FREE_IF_COPY(key, 0);
292 
293  return result;
294 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_VARLENA_PP(n)
Definition: fmgr.h:253
uintptr_t Datum
Definition: postgres.h:372
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum hashvarlenaextended ( PG_FUNCTION_ARGS  )

Definition at line 297 of file hashfunc.c.

References hash_any_extended(), PG_FREE_IF_COPY, PG_GETARG_INT64, PG_GETARG_VARLENA_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

298 {
299  struct varlena *key = PG_GETARG_VARLENA_PP(0);
300  Datum result;
301 
302  result = hash_any_extended((unsigned char *) VARDATA_ANY(key),
303  VARSIZE_ANY_EXHDR(key),
304  PG_GETARG_INT64(1));
305 
306  PG_FREE_IF_COPY(key, 0);
307 
308  return result;
309 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_GETARG_VARLENA_PP(n)
Definition: fmgr.h:253
uintptr_t Datum
Definition: postgres.h:372
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634