Top Description Fields Constructors Methods
org.lwjgl.util.xxhash

public Class XXHash

extends Object
Class Inheritance
Imports
javax.annotation.*, java.nio.*, org.lwjgl.system.*

Native bindings to xxhash.

xxHash is an extremely fast Hash algorithm, running at RAM speed limits. It also successfully passes all tests from the SMHasher suite.

A 64-bit version, named XXH64, is available since r35. It offers much better speed, but for 64-bit applications only.

Streaming

Streaming functions generate the xxHash value from an incremental input. This method is slower than single-call functions, due to state management. For small inputs, prefer 32 and 64, which are better optimized.

XXH state must first be allocated, using 32_createState.

Start a new hash by initializing state with a seed, using 32_reset.

Then, feed the hash state by calling 32_update as many times as necessary. Obviously, input must be allocated and read accessible. The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.

Finally, a hash value can be produced anytime, by using 32_digest. This function returns the 32-bits hash as an int.

It's still possible to continue inserting input into the hash state after a digest, and generate some new hash values later on, by calling again 32_digest.

When done, release the state, using 32_freeState.

Example code for incrementally hashing a file:


#include <stdio.h>
#include <xxhash.h>
#define BUFFER_SIZE 256

// Note: XXH64 and XXH3 use the same interface.
XXH32_hash_t
hashFile(FILE* stream)
{
    XXH32_state_t* state;
    unsigned char buf[BUFFER_SIZE];
    size_t amt;
    XXH32_hash_t hash;

    state = XXH32_createState();       // Create a state
    assert(state != NULL);             // Error check here
    XXH32_reset(state, 0xbaad5eed);    // Reset state with our seed
    while ((amt = fread(buf, 1, sizeof(buf), stream)) != 0) {
        XXH32_update(state, buf, amt); // Hash the file in chunks
    }
    hash = XXH32_digest(state);        // Finalize the hash
    XXH32_freeState(state);            // Clean up
    return hash;
}

Canonical representation

The default return values from XXH functions are unsigned 32 and 64 bit integers. This the simplest and fastest format for further post-processing.

However, this leaves open the question of what is the order on the byte level, since little and big endian conventions will store the same number differently.

The canonical representation settles this issue by mandating big-endian convention, the same convention as human-readable numbers (large digits first).

When writing hash values to storage, sending them over a network, or printing them, it's highly recommended to use the canonical representation to ensure portability across a wider range of systems, present and future.

XXH3

XXH3 is a more recent hash algorithm featuring:

Speed analysis methodology is explained here:

https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html

Compared to XXH64, expect XXH3 to run approximately ~2x faster on large inputs and >3x faster on small ones, exact differences vary depending on platform.

XXH3's speed benefits greatly from SIMD and 64-bit arithmetic, but does not require it. Most 32-bit and 64-bit targets that can run XXH32 smoothly can run XXH3 at competitive speeds, even without vector support. Further details are explained in the implementation.

Optimized implementations are provided for AVX512, AVX2, SSE2, NEON, POWER8, ZVector and scalar targets. This can be controlled via the XXH_VECTOR macro.

XXH3 implementation is portable:

XXH3 offers 2 variants, _64bits and _128bits. When only 64 bits are needed, prefer invoking the _64bits variant, as it reduces the amount of mixing, resulting in faster speed on small inputs. It's also generally simpler to manipulate a scalar return type than a struct.

The API supports one-shot hashing, streaming mode, and custom secrets.

*_withSecretandSeed()

These variants generate hash values using either seed for "short" keys (< XXH3_MIDSIZE_MAX = 240 bytes) or secret for "large" keys (≥ XXH3_MIDSIZE_MAX).

This generally benefits speed, compared to _withSeed() or _withSecret(). _withSeed() has to generate the secret on the fly for "large" keys. It's fast, but can be perceptible for "not so large" keys (< 1 KB). _withSecret() has to generate the masks on the fly for "small" keys, which requires more instructions than _withSeed() variants. Therefore, _withSecretandSeed variant combines the best of both worlds.

When secret has been generated by 3_generateSecret_fromSeed, this variant produces exactly the same results as _withSeed()` variant, hence offering only a pure speed benefit on "large" input, by skipping the need to regenerate the secret for every large input.

Another usage scenario is to hash the secret to a 64-bit hash value, for example with 3_64bits, which then becomes the seed, and then employ both the seed and the secret in _withSecretandSeed(). On top of speed, an added benefit is that each bit in the secret has a 50% chance to swap each bit in the output, via its impact to the seed. This is not guaranteed when using the secret directly in "small data" scenarios, because only portions of the secret are employed for small data.

Field Summary

Modifier and TypeField and Description
public static final int
XXH3_INTERNALBUFFER_SIZE

The size of the internal XXH3 buffer.

public static final int
XXH3_SECRET_DEFAULT_SIZE

Default size of the secret buffer (and XXH3_kSecret).

public static final int
XXH3_SECRET_SIZE_MIN

The bare minimum size for a custom secret.

public static final int
XXH_ERROR

Exit code for the streaming API.

public static final int
XXH_OK

Exit code for the streaming API.

public static final int
XXH_VERSION_MAJOR

The major version number.

public static final int
XXH_VERSION_MINOR

The minor version number.

public static final int
XXH_VERSION_NUMBER

The version number

public static final int
XXH_VERSION_RELEASE

The release version number.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public static native void
nXXH128(long data, long len, long seed, long __result)

Unsafe version of: XXH128

public static native void
nXXH128_canonicalFromHash(long dst, long hash)

Unsafe version of: XXH128_canonicalFromHash

public static native int
nXXH128_cmp(long h128_1, long h128_2)

Unsafe version of: XXH128_cmp

public static native void
nXXH128_hashFromCanonical(long src, long __result)

Unsafe version of: XXH128_hashFromCanonical

public static native int
nXXH128_isEqual(long h1, long h2)

Unsafe version of: XXH128_isEqual

public static native int
nXXH32(long input, long
the length of input, in bytes
length
,
int seed)

Unsafe version of: XXH32

public static native void
nXXH32_canonicalFromHash(long dst, int hash)

Unsafe version of: XXH32_canonicalFromHash

public static native void
nXXH32_copyState(long dst_state, long src_state)

Unsafe version of: XXH32_copyState

public static native long
public static native int
nXXH32_digest(long statePtr)

Unsafe version of: XXH32_digest

public static native int
nXXH32_freeState(long statePtr)

Unsafe version of: XXH32_freeState

public static native int
public static native int
nXXH32_reset(long statePtr, int seed)

Unsafe version of: XXH32_reset

public static native int
nXXH32_update(long statePtr, long input, long
the length of input, in bytes
length
)

Unsafe version of: XXH32_update

public static native void
nXXH3_128bits(long data, long len, long __result)

Unsafe version of: XXH3_128bits

public static native void
nXXH3_128bits_digest(long statePtr, long __result)

Unsafe version of: XXH3_128bits_digest

public static native int
nXXH3_128bits_reset(long statePtr)

Unsafe version of: XXH3_128bits_reset

public static native int
nXXH3_128bits_reset_withSecret(long statePtr, long secret, long secretSize)

Unsafe version of: XXH3_128bits_reset_withSecret

public static native int
nXXH3_128bits_reset_withSecretandSeed(long statePtr, long secret, long secretSize, long seed64)

public static native int
nXXH3_128bits_reset_withSeed(long statePtr, long seed)

Unsafe version of: XXH3_128bits_reset_withSeed

public static native int
nXXH3_128bits_update(long statePtr, long input, long
the length of input, in bytes
length
)

Unsafe version of: XXH3_128bits_update

public static native void
nXXH3_128bits_withSecret(long data, long len, long secret, long secretSize, long __result)

Unsafe version of: XXH3_128bits_withSecret

public static native void
nXXH3_128bits_withSecretandSeed(long input, long length, long secret, long secretSize, long seed, long __result)

public static native void
nXXH3_128bits_withSeed(long data, long len, long seed, long __result)

Unsafe version of: XXH3_128bits_withSeed

public static native long
nXXH3_64bits(long data, long len)

Unsafe version of: XXH3_64bits

public static native long
nXXH3_64bits_digest(long statePtr)

Unsafe version of: XXH3_64bits_digest

public static native int
nXXH3_64bits_reset(long statePtr)

Unsafe version of: XXH3_64bits_reset

public static native int
nXXH3_64bits_reset_withSecret(long statePtr, long secret, long secretSize)

Unsafe version of: XXH3_64bits_reset_withSecret

public static native int
nXXH3_64bits_reset_withSecretandSeed(long statePtr, long secret, long secretSize, long seed64)

public static native int
nXXH3_64bits_reset_withSeed(long statePtr, long seed)

Unsafe version of: XXH3_64bits_reset_withSeed

public static native int
nXXH3_64bits_update(long statePtr, long input, long
the length of input, in bytes
length
)

Unsafe version of: XXH3_64bits_update

public static native long
nXXH3_64bits_withSecret(long data, long len, long secret, long secretSize)

Unsafe version of: XXH3_64bits_withSecret

public static native long
nXXH3_64bits_withSecretandSeed(long data, long len, long secret, long secretSize, long seed)

public static native long
nXXH3_64bits_withSeed(long data, long len, long seed)

Unsafe version of: XXH3_64bits_withSeed

public static native void
nXXH3_copyState(long dst_state, long srct_state)

public static native long
public static native int
nXXH3_freeState(long statePtr)

public static native int
nXXH3_generateSecret(long secretBuffer, long secretSize, long customSeed, long customSeedSize)

Unsafe version of: XXH3_generateSecret

public static native void
nXXH3_generateSecret_fromSeed(long secretBuffer, long seed)

Unsafe version of: XXH3_generateSecret_fromSeed

public static native void
nXXH3_INITSTATE(long statePtr)

Unsafe version of: XXH3_INITSTATE

public static native long
nXXH64(long input, long
the length of input, in bytes
length
,
long seed)

Unsafe version of: XXH64

public static native void
nXXH64_canonicalFromHash(long dst, long hash)

Unsafe version of: XXH64_canonicalFromHash

public static native void
nXXH64_copyState(long dst_state, long src_state)

Unsafe version of: XXH64_copyState

public static native long
public static native long
nXXH64_digest(long statePtr)

Unsafe version of: XXH64_digest

public static native int
nXXH64_freeState(long statePtr)

Unsafe version of: XXH64_freeState

public static native long
public static native int
nXXH64_reset(long statePtr, long seed)

Unsafe version of: XXH64_reset

public static native int
nXXH64_update(long statePtr, long input, long
the length of input, in bytes
length
)

Unsafe version of: XXH64_update

public static XXH128Hash
XXH128(ByteBuffer data, long seed, XXH128Hash __result)

Simple alias to pre-selected XXH3_128bits variant.

public static void
XXH128_canonicalFromHash(XXH128Canonical
the XXH128_canonical_t pointer to be stored to
dst
,
XXH128Hash
the XXH128_hash_t to be converted
hash
)

Converts an XXH128_hash_t to a big endian XXH128_canonical_t.

public static int
XXH128_cmp(ByteBuffer h128_1, ByteBuffer h128_2)

This comparator is compatible with stdlib's qsort()/bsearch().

public static XXH128Hash
XXH128_hashFromCanonical(XXH128Canonical
the XXH128_canonical_t to convert
src
,
XXH128Hash __result)

Converts an XXH128_canonical_t to a native XXH128_hash_t.

public static boolean
XXH128_isEqual(XXH128Hash h1, XXH128Hash h2)

Returns 1 if equal, 0 if different.

public static int

Returns:

the calculated 32-bit hash value
XXH32
(ByteBuffer
the block of data to be hashed, at least length bytes in size
input
,
int
the 32-bit seed to alter the hash's output predictably
seed
)

Calculates the 32-bit hash of input using xxHash32.

public static void
XXH32_canonicalFromHash(XXH32Canonical
the XXH32_canonical_t pointer to be stored to.
dst
,
int
the XXH32_hash_t to be converted
hash
)

Converts an XXH32_hash_t to a big endian XXH32_canonical_t.

public static void
XXH32_copyState(XXH32State
the state to copy to
dst_state
,
XXH32State
the state to copy from
src_state
)

Copies one XXH32_state_t to another.

public static XXH32State

Returns:

an allocated XXH32_state_t on success, NULL on failure
XXH32_createState
()

Allocates an XXH32_state_t.

public static int

Returns:

the calculated xxHash32 value from that state
XXH32_digest
(XXH32State
the state struct to calculate the hash from
statePtr
)

Returns the calculated hash value from an XXH32_state_t.

public static int
XXH32_freeState(XXH32State
the state to free
statePtr
)

Frees an XXH32_state_t.

public static int
XXH32_hashFromCanonical(XXH32Canonical
the XXH32_canonical_t to convert
src
)

Converts an XXH32_canonical_t to a native XXH32_hash_t.

public static int

Returns:

OK on success, ERROR on failure
XXH32_reset
(XXH32State
the state struct to reset
statePtr
,
int
the 32-bit seed to alter the hash result predictably
seed
)

Resets an XXH32_state_t to begin a new hash.

public static int

Returns:

OK on success, ERROR on failure
XXH32_update
(XXH32State
the state struct to update
statePtr
,
ByteBuffer
the block of data to be hashed, at least length bytes in size
input
)

Consumes a block of input to an XXH32_state_t.

public static XXH128Hash
XXH3_128bits(ByteBuffer data, XXH128Hash __result)

Unseeded 128-bit variant of XXH3.

public static XXH128Hash
XXH3_128bits_digest(XXH3State
the state struct to calculate the hash from
statePtr
,
XXH128Hash __result)

Returns the calculated XXH3 128-bit hash value from an XXH3_state_t.

public static int
XXH3_128bits_reset(XXH3State
the state struct to reset
statePtr
)

Resets an XXH3_state_t to begin a new hash.

public static int
XXH3_128bits_reset_withSecret(XXH3State statePtr, ByteBuffer secret)

Custom secret 128-bit variant of XXH3.

public static int
public static int
XXH3_128bits_reset_withSeed(XXH3State
the state struct to reset
statePtr
,
long
he 64-bit seed to alter the state
seed
)

Resets an XXH3_state_t with 64-bit seed to begin a new hash.

public static int
XXH3_128bits_update(XXH3State
the state struct to update
statePtr
,
ByteBuffer
the block of data to be hashed, at least length bytes in size
input
)

Consumes a block of input to an XXH3_state_t.

public static XXH128Hash
XXH3_128bits_withSecret(ByteBuffer data, ByteBuffer secret, XXH128Hash __result)

Custom secret 128-bit variant of XXH3.

public static XXH128Hash
XXH3_128bits_withSecretandSeed(ByteBuffer input, ByteBuffer secret, long seed, XXH128Hash __result)

public static XXH128Hash
XXH3_128bits_withSeed(ByteBuffer data, long seed, XXH128Hash __result)

Seeded 128-bit variant of XXH3.

public static long
XXH3_64bits(ByteBuffer data)

64-bit unseeded variant of XXH3.

public static long
XXH3_64bits_digest(XXH3State
the state struct to calculate the hash from
statePtr
)

Returns the calculated XXH3 64-bit hash value from an XXH3_state_t.

public static int
XXH3_64bits_reset(XXH3State
the state struct to reset
statePtr
)

Resets an XXH3_state_t to begin a new hash.

public static int
XXH3_64bits_reset_withSecret(XXH3State statePtr, ByteBuffer secret)

secret is referenced, and must outlive the hash streaming session.

public static int
public static int
XXH3_64bits_reset_withSeed(XXH3State
the state struct to reset
statePtr
,
long
the 64-bit seed to alter the state
seed
)

Resets an XXH3_state_t with 64-bit seed to begin a new hash.

public static int
XXH3_64bits_update(XXH3State
the state struct to update
statePtr
,
ByteBuffer
the block of data to be hashed, at least length bytes in size
input
)

Consumes a block of input to an XXH3_state_t.

public static long
XXH3_64bits_withSecret(ByteBuffer data, ByteBuffer secret)

64-bit variant of XXH3 with a custom "secret".

public static long
public static long
XXH3_64bits_withSeed(ByteBuffer data, long seed)

64-bit seeded variant of XXH3.

public static void
XXH3_copyState(XXH3State dst_state, XXH3State srct_state)

public static XXH3State
public static int
public static int
XXH3_generateSecret(ByteBuffer secretBuffer, ByteBuffer customSeed)

Derives a high-entropy secret from any user-defined content, named customSeed.

public static void
XXH3_generateSecret_fromSeed(ByteBuffer secretBuffer, long seed)

Generate the same secret as the _withSeed() variants.

public static void
XXH3_INITSTATE(XXH3State statePtr)

Initializes a stack-allocated XXH3_state_t.

public static long

Returns:

the calculated 64-bit hash
XXH64
(ByteBuffer
the block of data to be hashed, at least length bytes in size
input
,
long
the 64-bit seed to alter the hash's output predictably
seed
)

Calculates the 64-bit hash of input using xxHash64.

public static void
XXH64_canonicalFromHash(XXH64Canonical
the XXH64_canonical_t pointer to be stored to.
dst
,
long
the XXH64_hash_t to be converted
hash
)

Converts an XXH64_hash_t to a big endian XXH64_canonical_t.

public static void
XXH64_copyState(XXH64State
the state to copy to
dst_state
,
XXH64State
the state to copy from
src_state
)

Copies one XXH64_state_t to another.

public static XXH64State

Returns:

an allocated XXH64_state_t on success, NULL on failure
XXH64_createState
()

Allocates an XXH64_state_t.

public static long

Returns:

the calculated xxHash64 value from that state
XXH64_digest
(XXH64State
the state struct to calculate the hash from
statePtr
)

Returns the calculated hash value from an XXH64_state_t.

public static int
XXH64_freeState(XXH64State
the state to free
statePtr
)

Frees an XXH64_state_t.

public static long
XXH64_hashFromCanonical(XXH64Canonical
the XXH64_canonical_t to convert
src
)

Converts an XXH64_canonical_t to a native XXH64_hash_t.

public static int
XXH64_reset(XXH64State
the state struct to reset
statePtr
,
long
the 64-bit seed to alter the hash result predictably
seed
)

Resets an XXH64_state_t to begin a new hash.

public static int
XXH64_update(XXH64State
the state struct to update
statePtr
,
ByteBuffer
the block of data to be hashed, at least length bytes in size
input
)

Consumes a block of input to an XXH64_state_t.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait