hashfn.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Utilities for working with hash values.
  3. *
  4. * Portions Copyright (c) 2017-2022, PostgreSQL Global Development Group
  5. */
  6. #ifndef HASHFN_H
  7. #define HASHFN_H
  8. /*
  9. * Rotate the high 32 bits and the low 32 bits separately. The standard
  10. * hash function sometimes rotates the low 32 bits by one bit when
  11. * combining elements. We want extended hash functions to be compatible with
  12. * that algorithm when the seed is 0, so we can't just do a normal rotation.
  13. * This works, though.
  14. */
  15. #define ROTATE_HIGH_AND_LOW_32BITS(v) \
  16. ((((v) << 1) & UINT64CONST(0xfffffffefffffffe)) | \
  17. (((v) >> 31) & UINT64CONST(0x100000001)))
  18. extern uint32 hash_bytes(const unsigned char *k, int keylen);
  19. extern uint64 hash_bytes_extended(const unsigned char *k,
  20. int keylen, uint64 seed);
  21. extern uint32 hash_bytes_uint32(uint32 k);
  22. extern uint64 hash_bytes_uint32_extended(uint32 k, uint64 seed);
  23. #ifndef FRONTEND
  24. static inline Datum
  25. hash_any(const unsigned char *k, int keylen)
  26. {
  27. return UInt32GetDatum(hash_bytes(k, keylen));
  28. }
  29. static inline Datum
  30. hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
  31. {
  32. return UInt64GetDatum(hash_bytes_extended(k, keylen, seed));
  33. }
  34. static inline Datum
  35. hash_uint32(uint32 k)
  36. {
  37. return UInt32GetDatum(hash_bytes_uint32(k));
  38. }
  39. static inline Datum
  40. hash_uint32_extended(uint32 k, uint64 seed)
  41. {
  42. return UInt64GetDatum(hash_bytes_uint32_extended(k, seed));
  43. }
  44. #endif
  45. extern uint32 string_hash(const void *key, Size keysize);
  46. extern uint32 tag_hash(const void *key, Size keysize);
  47. extern uint32 uint32_hash(const void *key, Size keysize);
  48. #define oid_hash uint32_hash /* Remove me eventually */
  49. /*
  50. * Combine two 32-bit hash values, resulting in another hash value, with
  51. * decent bit mixing.
  52. *
  53. * Similar to boost's hash_combine().
  54. */
  55. static inline uint32
  56. hash_combine(uint32 a, uint32 b)
  57. {
  58. a ^= b + 0x9e3779b9 + (a << 6) + (a >> 2);
  59. return a;
  60. }
  61. /*
  62. * Combine two 64-bit hash values, resulting in another hash value, using the
  63. * same kind of technique as hash_combine(). Testing shows that this also
  64. * produces good bit mixing.
  65. */
  66. static inline uint64
  67. hash_combine64(uint64 a, uint64 b)
  68. {
  69. /* 0x49a0f4dd15e5a8e3 is 64bit random data */
  70. a ^= b + UINT64CONST(0x49a0f4dd15e5a8e3) + (a << 54) + (a >> 7);
  71. return a;
  72. }
  73. /*
  74. * Simple inline murmur hash implementation hashing a 32 bit integer, for
  75. * performance.
  76. */
  77. static inline uint32
  78. murmurhash32(uint32 data)
  79. {
  80. uint32 h = data;
  81. h ^= h >> 16;
  82. h *= 0x85ebca6b;
  83. h ^= h >> 13;
  84. h *= 0xc2b2ae35;
  85. h ^= h >> 16;
  86. return h;
  87. }
  88. #endif /* HASHFN_H */