|
| 1 | +package com.tuannh.phantom.offheap.hashtable.hash; |
| 2 | + |
| 3 | +import com.tuannh.phantom.commons.unsafe.UnsafeWrapper; |
| 4 | + |
| 5 | +import java.nio.ByteBuffer; |
| 6 | + |
| 7 | +@SuppressWarnings("all") |
| 8 | +// from HaloDB |
| 9 | +public class Murmur3 implements Hasher { |
| 10 | + @Override |
| 11 | + public long hash(byte[] array) { |
| 12 | + int o = 0; |
| 13 | + int r = array.length; |
| 14 | + |
| 15 | + long h1 = 0L; |
| 16 | + long h2 = 0L; |
| 17 | + long k1, k2; |
| 18 | + |
| 19 | + for (; r >= 16; r -= 16) { |
| 20 | + k1 = getLong(array, o); |
| 21 | + o += 8; |
| 22 | + k2 = getLong(array, o); |
| 23 | + o += 8; |
| 24 | + |
| 25 | + // bmix64() |
| 26 | + |
| 27 | + h1 ^= mixK1(k1); |
| 28 | + |
| 29 | + h1 = Long.rotateLeft(h1, 27); |
| 30 | + h1 += h2; |
| 31 | + h1 = h1 * 5 + 0x52dce729; |
| 32 | + |
| 33 | + h2 ^= mixK2(k2); |
| 34 | + |
| 35 | + h2 = Long.rotateLeft(h2, 31); |
| 36 | + h2 += h1; |
| 37 | + h2 = h2 * 5 + 0x38495ab5; |
| 38 | + } |
| 39 | + |
| 40 | + if (r > 0) { |
| 41 | + k1 = 0; |
| 42 | + k2 = 0; |
| 43 | + switch (r) { |
| 44 | + case 15: |
| 45 | + k2 ^= toLong(array[o + 14]) << 48; // fall through |
| 46 | + case 14: |
| 47 | + k2 ^= toLong(array[o + 13]) << 40; // fall through |
| 48 | + case 13: |
| 49 | + k2 ^= toLong(array[o + 12]) << 32; // fall through |
| 50 | + case 12: |
| 51 | + k2 ^= toLong(array[o + 11]) << 24; // fall through |
| 52 | + case 11: |
| 53 | + k2 ^= toLong(array[o + 10]) << 16; // fall through |
| 54 | + case 10: |
| 55 | + k2 ^= toLong(array[o + 9]) << 8; // fall through |
| 56 | + case 9: |
| 57 | + k2 ^= toLong(array[o + 8]); // fall through |
| 58 | + case 8: |
| 59 | + k1 ^= getLong(array, o); |
| 60 | + break; |
| 61 | + case 7: |
| 62 | + k1 ^= toLong(array[o + 6]) << 48; // fall through |
| 63 | + case 6: |
| 64 | + k1 ^= toLong(array[o + 5]) << 40; // fall through |
| 65 | + case 5: |
| 66 | + k1 ^= toLong(array[o + 4]) << 32; // fall through |
| 67 | + case 4: |
| 68 | + k1 ^= toLong(array[o + 3]) << 24; // fall through |
| 69 | + case 3: |
| 70 | + k1 ^= toLong(array[o + 2]) << 16; // fall through |
| 71 | + case 2: |
| 72 | + k1 ^= toLong(array[o + 1]) << 8; // fall through |
| 73 | + case 1: |
| 74 | + k1 ^= toLong(array[o]); |
| 75 | + break; |
| 76 | + default: |
| 77 | + throw new AssertionError("Should never get here."); |
| 78 | + } |
| 79 | + |
| 80 | + h1 ^= mixK1(k1); |
| 81 | + h2 ^= mixK2(k2); |
| 82 | + } |
| 83 | + |
| 84 | + // makeHash() |
| 85 | + |
| 86 | + h1 ^= array.length; |
| 87 | + h2 ^= array.length; |
| 88 | + |
| 89 | + h1 += h2; |
| 90 | + h2 += h1; |
| 91 | + |
| 92 | + h1 = fmix64(h1); |
| 93 | + h2 = fmix64(h2); |
| 94 | + |
| 95 | + h1 += h2; |
| 96 | + //h2 += h1; |
| 97 | + |
| 98 | + // padToLong() |
| 99 | + return h1; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public long hash(ByteBuffer buffer) { |
| 104 | + return hash(buffer.array()); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public long hash(long adr, long offset, int length) { |
| 109 | + long o = offset; |
| 110 | + long r = length; |
| 111 | + |
| 112 | + long h1 = 0L; |
| 113 | + long h2 = 0L; |
| 114 | + long k1, k2; |
| 115 | + |
| 116 | + for (; r >= 16; r -= 16) { |
| 117 | + k1 = getLong(adr, o); |
| 118 | + o += 8; |
| 119 | + k2 = getLong(adr, o); |
| 120 | + o += 8; |
| 121 | + |
| 122 | + // bmix64() |
| 123 | + |
| 124 | + h1 ^= mixK1(k1); |
| 125 | + |
| 126 | + h1 = Long.rotateLeft(h1, 27); |
| 127 | + h1 += h2; |
| 128 | + h1 = h1 * 5 + 0x52dce729; |
| 129 | + |
| 130 | + h2 ^= mixK2(k2); |
| 131 | + |
| 132 | + h2 = Long.rotateLeft(h2, 31); |
| 133 | + h2 += h1; |
| 134 | + h2 = h2 * 5 + 0x38495ab5; |
| 135 | + } |
| 136 | + |
| 137 | + if (r > 0) { |
| 138 | + k1 = 0; |
| 139 | + k2 = 0; |
| 140 | + switch ((int) r) { |
| 141 | + case 15: |
| 142 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 14)) << 48; // fall through |
| 143 | + case 14: |
| 144 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 13)) << 40; // fall through |
| 145 | + case 13: |
| 146 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 12)) << 32; // fall through |
| 147 | + case 12: |
| 148 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 11)) << 24; // fall through |
| 149 | + case 11: |
| 150 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 10)) << 16; // fall through |
| 151 | + case 10: |
| 152 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 9)) << 8; // fall through |
| 153 | + case 9: |
| 154 | + k2 ^= toLong(UnsafeWrapper.getByte(adr, o + 8)); // fall through |
| 155 | + case 8: |
| 156 | + k1 ^= getLong(adr, o); |
| 157 | + break; |
| 158 | + case 7: |
| 159 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 6)) << 48; // fall through |
| 160 | + case 6: |
| 161 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 5)) << 40; // fall through |
| 162 | + case 5: |
| 163 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 4)) << 32; // fall through |
| 164 | + case 4: |
| 165 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 3)) << 24; // fall through |
| 166 | + case 3: |
| 167 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 2)) << 16; // fall through |
| 168 | + case 2: |
| 169 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o + 1)) << 8; // fall through |
| 170 | + case 1: |
| 171 | + k1 ^= toLong(UnsafeWrapper.getByte(adr, o)); |
| 172 | + break; |
| 173 | + default: |
| 174 | + throw new AssertionError("Should never get here."); |
| 175 | + } |
| 176 | + |
| 177 | + h1 ^= mixK1(k1); |
| 178 | + h2 ^= mixK2(k2); |
| 179 | + } |
| 180 | + |
| 181 | + // makeHash() |
| 182 | + |
| 183 | + h1 ^= length; |
| 184 | + h2 ^= length; |
| 185 | + |
| 186 | + h1 += h2; |
| 187 | + h2 += h1; |
| 188 | + |
| 189 | + h1 = fmix64(h1); |
| 190 | + h2 = fmix64(h2); |
| 191 | + |
| 192 | + h1 += h2; |
| 193 | + //h2 += h1; |
| 194 | + |
| 195 | + // padToLong() |
| 196 | + |
| 197 | + return h1; |
| 198 | + } |
| 199 | + |
| 200 | + private static long getLong(byte[] array, int o) { |
| 201 | + long l = toLong(array[o + 7]) << 56; |
| 202 | + l |= toLong(array[o + 6]) << 48; |
| 203 | + l |= toLong(array[o + 5]) << 40; |
| 204 | + l |= toLong(array[o + 4]) << 32; |
| 205 | + l |= toLong(array[o + 3]) << 24; |
| 206 | + l |= toLong(array[o + 2]) << 16; |
| 207 | + l |= toLong(array[o + 1]) << 8; |
| 208 | + l |= toLong(array[o]); |
| 209 | + return l; |
| 210 | + } |
| 211 | + |
| 212 | + private static long getLong(long adr, long o) { |
| 213 | + long l = toLong(UnsafeWrapper.getByte(adr, o + 7)) << 56; |
| 214 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 6)) << 48; |
| 215 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 5)) << 40; |
| 216 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 4)) << 32; |
| 217 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 3)) << 24; |
| 218 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 2)) << 16; |
| 219 | + l |= toLong(UnsafeWrapper.getByte(adr, o + 1)) << 8; |
| 220 | + l |= toLong(UnsafeWrapper.getByte(adr, o)); |
| 221 | + return l; |
| 222 | + } |
| 223 | + |
| 224 | + static final long C1 = 0x87c37b91114253d5L; |
| 225 | + static final long C2 = 0x4cf5ad432745937fL; |
| 226 | + |
| 227 | + static long fmix64(long k) { |
| 228 | + k ^= k >>> 33; |
| 229 | + k *= 0xff51afd7ed558ccdL; |
| 230 | + k ^= k >>> 33; |
| 231 | + k *= 0xc4ceb9fe1a85ec53L; |
| 232 | + k ^= k >>> 33; |
| 233 | + return k; |
| 234 | + } |
| 235 | + |
| 236 | + static long mixK1(long k1) { |
| 237 | + k1 *= C1; |
| 238 | + k1 = Long.rotateLeft(k1, 31); |
| 239 | + k1 *= C2; |
| 240 | + return k1; |
| 241 | + } |
| 242 | + |
| 243 | + static long mixK2(long k2) { |
| 244 | + k2 *= C2; |
| 245 | + k2 = Long.rotateLeft(k2, 33); |
| 246 | + k2 *= C1; |
| 247 | + return k2; |
| 248 | + } |
| 249 | + |
| 250 | + static long toLong(byte value) { |
| 251 | + return value & 0xff; |
| 252 | + } |
| 253 | +} |
0 commit comments