warpcore 0.0.1
Hashing at the Speed of Light on modern CUDA-accelerators
hashers.cuh
Go to the documentation of this file.
1 #ifndef WARPCORE_HASHERS_CUH
2 #define WARPCORE_HASHERS_CUH
3 
4 namespace warpcore
5 {
6 
7 /*! \brief hash functions
8 */
9 namespace hashers
10 {
11 
12 /*! \brief hash function proposed by NVIDIA
13 */
15 {
16 
17 public:
18  using key_type = std::uint32_t;
20  using tag = tags::hasher;
21 
22  /*! \brief deleted hash function for types other than explicitly defined
23  * \tparam T key type
24  */
25  template<class T>
27  static hash_type hash(T) = delete;
28 
29  /*! \brief hash function
30  * \param[in] x key to be hashed
31  * \return hash of \c x
32  */
33  HOSTDEVICEQUALIFIER INLINEQUALIFIER
34  static hash_type hash(key_type x) noexcept
35  {
36  x = (x + 0x7ed55d16) + (x << 12);
37  x = (x ^ 0xc761c23c) ^ (x >> 19);
38  x = (x + 0x165667b1) + (x << 5);
39  x = (x + 0xd3a2646c) ^ (x << 9);
40  x = (x + 0xfd7046c5) + (x << 3);
41  x = (x ^ 0xb55a4f09) ^ (x >> 16);
42 
43  return x;
44  }
45 
46 }; // class NvidiaHash
47 
48 /*! \brief hash function proposed by Mueller
49  */
51 {
52 
53 public:
54  using key_type = std::uint32_t;
57 
58  /*! \brief deleted hash function for types other than explicitly defined
59  * \tparam T key type
60  */
61  template<class T>
63  static hash_type hash(T) = delete;
64 
65  /*! \brief hash function
66  * \param[in] x key to be hashed
67  * \return hash of \c x
68  */
69  HOSTDEVICEQUALIFIER INLINEQUALIFIER
70  static hash_type hash(key_type x) noexcept
71  {
72  x = ((x >> 16) ^ x) * 0x45d9f3b;
73  x = ((x >> 16) ^ x) * 0x45d9f3b;
74  x = ((x >> 16) ^ x);
75 
76  return x;
77  }
78 
79 }; // class MuellerHash
80 
81 
82 /*! \brief murmur integer finalizer
83  * \tparam K key type (\c std::uint32_t or std::uint64_t)
84  */
85 template<class K>
87 {
88 
89 public:
90  using key_type = K;
91  using hash_type = K;
93 
94  /*! \brief hash function
95  * \tparam T key type
96  * \param[in] x key to be hashed
97  * \return hash of \c x
98  */
99  template<class T>
101  static T hash(T x) noexcept
102  {
103  static_assert(
104  std::is_same<T, key_type>::value,
105  "invalid key type");
106 
107  return hash_(x);
108  }
109 
110 private:
111  /*! \brief hash function
112  * \param[in] x key to be hashed
113  * \return hash of \c x
114  */
116  static std::uint32_t hash_(std::uint32_t x) noexcept
117  {
118  x ^= x >> 16;
119  x *= 0x85ebca6b;
120  x ^= x >> 13;
121  x *= 0xc2b2ae35;
122  x ^= x >> 16;
123  return x;
124  }
125 
126  /*! \brief hash function
127  * \param[in] x key to be hashed
128  * \return hash of \c x
129  */
131  static std::uint64_t hash_(std::uint64_t x) noexcept
132  {
133  x ^= x >> 33;
134  x *= 0xff51afd7ed558ccd;
135  x ^= x >> 33;
136  x *= 0xc4ceb9fe1a85ec53;
137  x ^= x >> 33;
138  return x;
139  }
140 
141 }; // class MurmurHash
142 
143 /*! \brief identity hash
144  * \tparam K key type
145  * \tparam H hash type
146  */
147 template<class K, class H = std::uint32_t>
149 {
150 
151 public:
152  using key_type = K;
153  using hash_type = H;
155 
156  static_assert(
159  "invalid hash type");
160 
161  static_assert(
163  "key type not convertible to hash type");
164 
165  /*! \brief hash function
166  * \param[in] x key to be hashed
167  * \return hash of \c x
168  */
169  HOSTDEVICEQUALIFIER INLINEQUALIFIER
170  static hash_type hash(key_type x) noexcept
171  {
172  return hash_type{x};
173  }
174 
175 }; // class IdentityMap
176 
177 } // namespace hashers
178 
179 } // namespace warpcore
180 
181 #endif /* WARPCORE_HASHERS_CUH */