microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
cmake/externals/farmhash/dev/farmhashmk.cc
112lines · modecode
| 1 | #undef Fetch |
| 2 | #define Fetch Fetch32 |
| 3 | |
| 4 | #undef Rotate |
| 5 | #define Rotate Rotate32 |
| 6 | |
| 7 | #undef Bswap |
| 8 | #define Bswap Bswap32 |
| 9 | |
| 10 | STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len, uint32_t seed = 0) { |
| 11 | uint32_t a = Fetch(s - 4 + (len >> 1)); |
| 12 | uint32_t b = Fetch(s + 4); |
| 13 | uint32_t c = Fetch(s + len - 8); |
| 14 | uint32_t d = Fetch(s + (len >> 1)); |
| 15 | uint32_t e = Fetch(s); |
| 16 | uint32_t f = Fetch(s + len - 4); |
| 17 | uint32_t h = d * c1 + len + seed; |
| 18 | a = Rotate(a, 12) + f; |
| 19 | h = Mur(c, h) + a; |
| 20 | a = Rotate(a, 3) + c; |
| 21 | h = Mur(e, h) + a; |
| 22 | a = Rotate(a + f, 12) + d; |
| 23 | h = Mur(b ^ seed, h) + a; |
| 24 | return fmix(h); |
| 25 | } |
| 26 | |
| 27 | STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len, uint32_t seed = 0) { |
| 28 | uint32_t b = seed; |
| 29 | uint32_t c = 9; |
| 30 | for (size_t i = 0; i < len; i++) { |
| 31 | signed char v = s[i]; |
| 32 | b = b * c1 + v; |
| 33 | c ^= b; |
| 34 | } |
| 35 | return fmix(Mur(b, Mur(len, c))); |
| 36 | } |
| 37 | |
| 38 | STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len, uint32_t seed = 0) { |
| 39 | uint32_t a = len, b = len * 5, c = 9, d = b + seed; |
| 40 | a += Fetch(s); |
| 41 | b += Fetch(s + len - 4); |
| 42 | c += Fetch(s + ((len >> 1) & 4)); |
| 43 | return fmix(seed ^ Mur(c, Mur(b, Mur(a, d)))); |
| 44 | } |
| 45 | |
| 46 | uint32_t Hash32(const char *s, size_t len) { |
| 47 | if (len <= 24) { |
| 48 | return len <= 12 ? |
| 49 | (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) : |
| 50 | Hash32Len13to24(s, len); |
| 51 | } |
| 52 | |
| 53 | // len > 24 |
| 54 | uint32_t h = len, g = c1 * len, f = g; |
| 55 | uint32_t a0 = Rotate(Fetch(s + len - 4) * c1, 17) * c2; |
| 56 | uint32_t a1 = Rotate(Fetch(s + len - 8) * c1, 17) * c2; |
| 57 | uint32_t a2 = Rotate(Fetch(s + len - 16) * c1, 17) * c2; |
| 58 | uint32_t a3 = Rotate(Fetch(s + len - 12) * c1, 17) * c2; |
| 59 | uint32_t a4 = Rotate(Fetch(s + len - 20) * c1, 17) * c2; |
| 60 | h ^= a0; |
| 61 | h = Rotate(h, 19); |
| 62 | h = h * 5 + 0xe6546b64; |
| 63 | h ^= a2; |
| 64 | h = Rotate(h, 19); |
| 65 | h = h * 5 + 0xe6546b64; |
| 66 | g ^= a1; |
| 67 | g = Rotate(g, 19); |
| 68 | g = g * 5 + 0xe6546b64; |
| 69 | g ^= a3; |
| 70 | g = Rotate(g, 19); |
| 71 | g = g * 5 + 0xe6546b64; |
| 72 | f += a4; |
| 73 | f = Rotate(f, 19) + 113; |
| 74 | size_t iters = (len - 1) / 20; |
| 75 | do { |
| 76 | uint32_t a = Fetch(s); |
| 77 | uint32_t b = Fetch(s + 4); |
| 78 | uint32_t c = Fetch(s + 8); |
| 79 | uint32_t d = Fetch(s + 12); |
| 80 | uint32_t e = Fetch(s + 16); |
| 81 | h += a; |
| 82 | g += b; |
| 83 | f += c; |
| 84 | h = Mur(d, h) + e; |
| 85 | g = Mur(c, g) + a; |
| 86 | f = Mur(b + e * c1, f) + d; |
| 87 | f += g; |
| 88 | g += f; |
| 89 | s += 20; |
| 90 | } while (--iters != 0); |
| 91 | g = Rotate(g, 11) * c1; |
| 92 | g = Rotate(g, 17) * c1; |
| 93 | f = Rotate(f, 11) * c1; |
| 94 | f = Rotate(f, 17) * c1; |
| 95 | h = Rotate(h + g, 19); |
| 96 | h = h * 5 + 0xe6546b64; |
| 97 | h = Rotate(h, 17) * c1; |
| 98 | h = Rotate(h + f, 19); |
| 99 | h = h * 5 + 0xe6546b64; |
| 100 | h = Rotate(h, 17) * c1; |
| 101 | return h; |
| 102 | } |
| 103 | |
| 104 | uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { |
| 105 | if (len <= 24) { |
| 106 | if (len >= 13) return Hash32Len13to24(s, len, seed * c1); |
| 107 | else if (len >= 5) return Hash32Len5to12(s, len, seed); |
| 108 | else return Hash32Len0to4(s, len, seed); |
| 109 | } |
| 110 | uint32_t h = Hash32Len13to24(s, 24, seed ^ len); |
| 111 | return Mur(Hash32(s + 24, len - 24) + seed, h); |
| 112 | } |
| 113 | |