PostgreSQL Source Code  git master
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

◆ final

#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.

◆ mix

#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().

◆ rot

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

Definition at line 330 of file hashfunc.c.

◆ UINT32_ALIGN_MASK

#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

◆ hash_any()

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(), ChooseTablespace(), 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  /* fall through */
470  case 8:
471  /* the lowest byte of c is reserved for the length */
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  /* fall through */
509  case 8:
510  /* the lowest byte of c is reserved for the length */
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)
562  {
563  case 11:
564  c += ((uint32) k[10] << 8);
565  /* fall through */
566  case 10:
567  c += ((uint32) k[9] << 16);
568  /* fall through */
569  case 9:
570  c += ((uint32) k[8] << 24);
571  /* fall through */
572  case 8:
573  /* the lowest byte of c is reserved for the length */
574  b += k[7];
575  /* fall through */
576  case 7:
577  b += ((uint32) k[6] << 8);
578  /* fall through */
579  case 6:
580  b += ((uint32) k[5] << 16);
581  /* fall through */
582  case 5:
583  b += ((uint32) k[4] << 24);
584  /* fall through */
585  case 4:
586  a += k[3];
587  /* fall through */
588  case 3:
589  a += ((uint32) k[2] << 8);
590  /* fall through */
591  case 2:
592  a += ((uint32) k[1] << 16);
593  /* fall through */
594  case 1:
595  a += ((uint32) k[0] << 24);
596  /* case 0: nothing left to add */
597  }
598 #else /* !WORDS_BIGENDIAN */
599  switch (len)
600  {
601  case 11:
602  c += ((uint32) k[10] << 24);
603  /* fall through */
604  case 10:
605  c += ((uint32) k[9] << 16);
606  /* fall through */
607  case 9:
608  c += ((uint32) k[8] << 8);
609  /* fall through */
610  case 8:
611  /* the lowest byte of c is reserved for the length */
612  b += ((uint32) k[7] << 24);
613  /* fall through */
614  case 7:
615  b += ((uint32) k[6] << 16);
616  /* fall through */
617  case 6:
618  b += ((uint32) k[5] << 8);
619  /* fall through */
620  case 5:
621  b += k[4];
622  /* fall through */
623  case 4:
624  a += ((uint32) k[3] << 24);
625  /* fall through */
626  case 3:
627  a += ((uint32) k[2] << 16);
628  /* fall through */
629  case 2:
630  a += ((uint32) k[1] << 8);
631  /* fall through */
632  case 1:
633  a += k[0];
634  /* case 0: nothing left to add */
635  }
636 #endif /* WORDS_BIGENDIAN */
637  }
638 
639  final(a, b, c);
640 
641  /* report the result */
642  return UInt32GetDatum(c);
643 }
char * c
unsigned int uint32
Definition: c.h:325
#define UInt32GetDatum(X)
Definition: postgres.h:478
#define UINT32_ALIGN_MASK
Definition: hashfunc.c:327
#define mix(a, b, c)
Definition: hashfunc.c:364

◆ hash_any_extended()

Datum hash_any_extended ( register const unsigned char *  k,
register int  keylen,
uint64  seed 
)

Definition at line 654 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(), k_hashes(), pgss_hash_string(), pgss_post_parse_analyze(), and uuid_hash_extended().

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

◆ hash_uint32()

Datum hash_uint32 ( uint32  k)

Definition at line 893 of file hashfunc.c.

References UInt32GetDatum.

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

894 {
895  register uint32 a,
896  b,
897  c;
898 
899  a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095;
900  a += k;
901 
902  final(a, b, c);
903 
904  /* report the result */
905  return UInt32GetDatum(c);
906 }
char * c
unsigned int uint32
Definition: c.h:325
#define UInt32GetDatum(X)
Definition: postgres.h:478

◆ hash_uint32_extended()

Datum hash_uint32_extended ( uint32  k,
uint64  seed 
)

Definition at line 914 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().

915 {
916  register uint32 a,
917  b,
918  c;
919 
920  a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095;
921 
922  if (seed != 0)
923  {
924  a += (uint32) (seed >> 32);
925  b += (uint32) seed;
926  mix(a, b, c);
927  }
928 
929  a += k;
930 
931  final(a, b, c);
932 
933  /* report the result */
934  PG_RETURN_UINT64(((uint64) b << 32) | c);
935 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:333
char * c
unsigned int uint32
Definition: c.h:325
#define mix(a, b, c)
Definition: hashfunc.c:364

◆ hashchar()

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:313
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:243

◆ hashcharextended()

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:914
signed int int32
Definition: c.h:313
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:243

◆ hashenum()

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:245
unsigned int uint32
Definition: c.h:325
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893

◆ hashenumextended()

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:914
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
unsigned int uint32
Definition: c.h:325
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252

◆ hashfloat4()

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:320
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428

◆ hashfloat4extended()

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:333
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654

◆ hashfloat8()

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:251
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:320
double float8
Definition: c.h:458
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428

◆ hashfloat8extended()

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:251
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:333
double float8
Definition: c.h:458
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654

◆ hashint2()

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:313
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893
#define PG_GETARG_INT16(n)
Definition: fmgr.h:241

◆ hashint2extended()

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:914
signed int int32
Definition: c.h:313
#define PG_GETARG_INT16(n)
Definition: fmgr.h:241
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252

◆ hashint4()

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:239
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893

◆ hashint4extended()

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:239
Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfunc.c:914
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252

◆ hashint8()

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:325
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
long val
Definition: informix.c:689

◆ hashint8extended()

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:914
unsigned int uint32
Definition: c.h:325
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
long val
Definition: informix.c:689

◆ hashname()

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:576
#define PG_GETARG_NAME(n)
Definition: fmgr.h:248

◆ hashnameextended()

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:576
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654
#define PG_GETARG_NAME(n)
Definition: fmgr.h:248

◆ hashoid()

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:245
unsigned int uint32
Definition: c.h:325
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:893

◆ hashoidextended()

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:914
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
unsigned int uint32
Definition: c.h:325
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252

◆ hashoidvector()

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:555
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
unsigned int Oid
Definition: postgres_ext.h:31
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:563
int dim1
Definition: c.h:561
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:428

◆ hashoidvectorextended()

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:555
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
unsigned int Oid
Definition: postgres_ext.h:31
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:563
int dim1
Definition: c.h:561
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654

◆ hashtext()

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:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
uintptr_t Datum
Definition: postgres.h:367
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:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ hashtextextended()

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:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
uintptr_t Datum
Definition: postgres.h:367
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654

◆ hashvarlena()

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:348
#define PG_GETARG_VARLENA_PP(n)
Definition: fmgr.h:258
uintptr_t Datum
Definition: postgres.h:367
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:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516

◆ hashvarlenaextended()

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:348
#define PG_GETARG_VARLENA_PP(n)
Definition: fmgr.h:258
uintptr_t Datum
Definition: postgres.h:367
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:230
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:516
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:654