Vowpal Wabbit
Namespaces | Functions
hash.h File Reference
#include "future_compat.h"
#include <sys/types.h>
#include <cstdint>

Go to the source code of this file.

Namespaces

 MURMUR_HASH_3
 

Functions

constexpr uint32_t rotl32 (uint32_t x, int8_t r) noexcept
 
static VW_STD14_CONSTEXPR uint32_t MURMUR_HASH_3::fmix (uint32_t h) noexcept
 
static constexpr uint32_t MURMUR_HASH_3::getblock (const uint32_t *p, int i) noexcept
 
VW_STD14_CONSTEXPR uint64_t uniform_hash (const void *key, size_t len, uint64_t seed)
 

Function Documentation

◆ rotl32()

constexpr uint32_t rotl32 ( uint32_t  x,
int8_t  r 
)
inlinenoexcept

Definition at line 38 of file hash.h.

Referenced by uniform_hash().

39 {
40  return (x << r) | (x >> (32 - r));
41 }

◆ uniform_hash()

VW_STD14_CONSTEXPR uint64_t uniform_hash ( const void *  key,
size_t  len,
uint64_t  seed 
)
inline

Definition at line 67 of file hash.h.

References MURMUR_HASH_3::fmix(), MURMUR_HASH_3::getblock(), and rotl32().

Referenced by io_buf::bin_read_fixed(), io_buf::bin_write_fixed(), Search::cached_action_store_or_find(), cbify_setup(), cbifyldf_setup(), namedlabels::get(), hashall(), hashstring(), VW::cb_sample_data::learn_or_predict(), TC_parser< audit >::maybeFeature(), namedlabels::namedlabels(), TC_parser< audit >::nameSpace(), parse_dictionary_argument(), vw_slim::model_parser::read(), vw_slim::model_parser::read_string(), exploration::sample_after_normalizing(), save_load_header(), vw_slim::model_parser::skip(), MultiState< audit >::StartArray(), and warm_cb_setup().

68 {
69  const uint8_t* data = (const uint8_t*)key;
70  const int nblocks = (int)len / 4;
71 
72  uint32_t h1 = (uint32_t)seed;
73 
74  const uint32_t c1 = 0xcc9e2d51;
75  const uint32_t c2 = 0x1b873593;
76 
77  // --- body
78  const uint32_t* blocks = (const uint32_t *)(data + nblocks * 4);
79 
80  for (int i = -nblocks; i; i++)
81  {
82  uint32_t k1 = MURMUR_HASH_3::getblock(blocks, i);
83 
84  k1 *= c1;
85  k1 = rotl32(k1, 15);
86  k1 *= c2;
87 
88  h1 ^= k1;
89  h1 = rotl32(h1, 13);
90  h1 = h1 * 5 + 0xe6546b64;
91  }
92 
93  // --- tail
94  const uint8_t * tail = (const uint8_t*)(data + nblocks * 4);
95 
96  uint32_t k1 = 0;
97 
98  // The 'fall through' comments below silence the implicit-fallthrough warning introduced in GCC 7.
99  // Once we move to C++17 these should be replaced with the [[fallthrough]] attribute.
100  switch (len & 3u)
101  {
102  case 3:
103  k1 ^= tail[2] << 16;
104  // fall through
105  case 2:
106  k1 ^= tail[1] << 8;
107  // fall through
108  case 1: k1 ^= tail[0];
109  k1 *= c1;
110  k1 = rotl32(k1, 15);
111  k1 *= c2; h1 ^= k1;
112  default:
113  break;
114  }
115 
116  // --- finalization
117  h1 ^= len;
118 
119  return MURMUR_HASH_3::fmix(h1);
120 }
constexpr uint32_t rotl32(uint32_t x, int8_t r) noexcept
Definition: hash.h:38
static VW_STD14_CONSTEXPR uint32_t fmix(uint32_t h) noexcept
Definition: hash.h:47
static constexpr uint32_t getblock(const uint32_t *p, int i) noexcept
Definition: hash.h:61