Othello
 All Classes Files Functions Variables Macros
hash.h
Go to the documentation of this file.
1 
6 #pragma once
7 #include <functional>
9 template <class keyType>
10 class Hasher32 {
11 public:
12  uint32_t mask;
13  uint32_t s;
14 private:
15  std::hash<keyType> fallback;
16  uint32_t hashshr;
17 public:
19  void setMaskSeed(uint32_t _mask, uint32_t _seed){
20  mask = _mask;
21  s = _seed;
22  hashshr = s & 7;
23  }
24  template <class T = keyType>
25  typename std::enable_if<std::is_integral<T>::value, uint32_t>::type
26  operator()(const keyType& k0) const {
27 #if defined(__SSE4_2__)
28 #pragma message("Hasher CRC32c")
29  uint32_t crc1 = ~0;
30  uint64_t *k;
31  k = (uint64_t* ) &k0;
32  uint32_t s1 = s;
33  if (sizeof(keyType)>=8)
34  asm (".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1), "c" ((*k)+s1) );
35 
36  if (sizeof(keyType)>=16) {
37  s1 = ((((uint64_t) s1) * s1 >> 16) ^ (s1 << 2));
38  k++;
39  asm (".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1), "c" ((*k)+s1) );
40  }
41  if (sizeof(keyType)>=24) {
42  s1 = ((((uint64_t) s1) * s1 >> 16) ^ (s1 << 2));
43  k++;
44  asm (".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1), "c" ((*k)+s1) );
45  }
46  if (sizeof(keyType)>=32) {
47  s1 = ((((uint64_t) s1) * s1 >> 16) ^ (s1 << 2));
48  k++;
49  asm (".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1), "c" ((*k)+s1) );
50  }
51  if (sizeof(keyType) & 4) {
52  uint64_t k32;
53  k32 = ((*k)) & 0xFFFFFFFFULL;
54  asm ( ".byte 0xf2, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1),"c" ((k32)+s1) );
55 
56  }
57 
58  asm ( ".byte 0xf2, 0xf, 0x38, 0xf1, 0xf1;" : "=S" (crc1) : "0" (crc1),"c" (s1) );
59 // crc1 ^= (crc1 >> (HASHLENGTH ^ (7&s1)));
60  crc1 ^= (crc1 >> (hashshr));
61  if (sizeof(keyType)==4)
62  return mask & (crc1 ^ ((uint32_t) *k));
63  else
64  return mask & (crc1 ^ (*k >> 32) ^ ((uint32_t) *k));
65 #else
66 #pragma message("Build without SSE4.2 support ")
67  return mask & fallback(k0);
68 #endif
69  }
70 
71  template <class T = keyType>
72  typename std::enable_if<!std::is_integral<T>::value, uint32_t>::type
73  operator()(const keyType& k0) const {
74  return mask & fallback(k0^s ^ (s <<(8+hashshr)));
75  }
76 
77 };
78 
A hash function that hashes keyType to uint32_t. When SSE4.2 support is found, use sse4...
Definition: hash.h:10
void setMaskSeed(uint32_t _mask, uint32_t _seed)
set bitmask and seed
Definition: hash.h:19
uint32_t s
hash seed.
Definition: hash.h:13
uint32_t mask
a bitmask for the return value. return value must be within [0..mask]
Definition: hash.h:12