/** * Returns a hash function implementing * <a href="https://github.com/Cyan4973/xxHash">xxHash algorithm</a> with the given seed value. * This implementation produce equal results for equal * input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms * than on little-endian. * * @see #xx() */ public static LongHashFunction xx(long seed) { return XxHash.asLongHashFunctionWithSeed(seed); }
/** * Returns a hash function implementing * <a href="https://github.com/Cyan4973/xxHash">xxHash algorithm</a> without seed value (0 is used * as default seed value). * This implementation produce equal results for equal * input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms * than on little-endian. * * @see #xx(long) */ public static LongHashFunction xx() { return XxHash.asLongHashFunctionWithoutSeed(); }
@Override <T> long fetch64(Access<T> access, T in, long off) { return Long.reverseBytes(super.fetch64(access, in, off)); }
v1 += fetch64(access, input, off) * P2; v1 = Long.rotateLeft(v1, 31); v1 *= P1; v2 += fetch64(access, input, off + 8) * P2; v2 = Long.rotateLeft(v2, 31); v2 *= P1; v3 += fetch64(access, input, off + 16) * P2; v3 = Long.rotateLeft(v3, 31); v3 *= P1; v4 += fetch64(access, input, off + 24) * P2; v4 = Long.rotateLeft(v4, 31); v4 *= P1; long k1 = fetch64(access, input, off); k1 *= P2; k1 = Long.rotateLeft(k1, 31); hash ^= fetch32(access, input, off) * P1; hash = Long.rotateLeft(hash, 23) * P2 + P3; off += 4; hash ^= fetch8(access, input, off) * P5; hash = Long.rotateLeft(hash, 11) * P1; --remaining; return finalize(hash);
@Override public long hashLong(long input) { input = NATIVE_XX.toLittleEndian(input); long hash = seed() + P5 + 8; input *= P2; input = Long.rotateLeft(input, 31); input *= P1; hash ^= input; hash = Long.rotateLeft(hash, 27) * P1 + P4; return XxHash.finalize(hash); }
@Override public <T> long hash(T input, Access<T> access, long off, long len) { long seed = seed(); if (access.byteOrder(input) == LITTLE_ENDIAN) { return XxHash.INSTANCE.xxHash64(seed, input, access, off, len); } else { return BigEndian.INSTANCE.xxHash64(seed, input, access, off, len); } } }
@Override public long hashInt(int input) { input = NATIVE_XX.toLittleEndian(input); long hash = seed() + P5 + 4; hash ^= Primitives.unsignedInt(input) * P1; hash = Long.rotateLeft(hash, 23) * P2 + P3; return XxHash.finalize(hash); }
@Override public long hashShort(short input) { input = NATIVE_XX.toLittleEndian(input); long hash = seed() + P5 + 2; hash ^= Primitives.unsignedByte(input) * P5; hash = Long.rotateLeft(hash, 11) * P1; hash ^= Primitives.unsignedByte(input >> 8) * P5; hash = Long.rotateLeft(hash, 11) * P1; return XxHash.finalize(hash); }