Implementation:Ggml org Llama cpp XXHash
| Knowledge Sources | |
|---|---|
| Domains | Hashing, Utilities |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Single-header library for the xxHash extremely fast non-cryptographic hash algorithm (by Yann Collet, BSD-2-Clause), providing four hash variants across three families.
Description
This header provides XXH32 (32-bit), XXH64 (64-bit), and XXH3 (modern 64-bit and 128-bit) hash function families. It uses a single-header design where the XXH_IMPLEMENTATION macro controls whether definitions are emitted. The library includes both a streaming API (state-based incremental hashing via XXH*_createState, XXH*_reset, XXH*_update, XXH*_digest) and a one-shot API (XXH32(), XXH64(), XXH3_64bits(), XXH3_128bits()). The XXH3 family is optimized for SIMD and 64-bit operations, achieving up to 59.4 GB/s with AVX2.
Usage
Use this library as the core hashing dependency for the gguf-hash tool, providing the default fast hash (xxh64) for CI/testing scenarios where non-cryptographic speed is prioritized over collision resistance.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: examples/gguf-hash/deps/xxhash/xxhash.h
- Lines: 1-7093
Signature
/* One-shot API */
XXH32_hash_t XXH32(const void* input, size_t length, XXH32_hash_t seed);
XXH64_hash_t XXH64(const void* input, size_t length, XXH64_hash_t seed);
XXH64_hash_t XXH3_64bits(const void* input, size_t length);
XXH128_hash_t XXH3_128bits(const void* input, size_t length);
/* Streaming API */
XXH32_state_t* XXH32_createState(void);
XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed);
XXH_errorcode XXH32_update(XXH32_state_t* statePtr, const void* input, size_t length);
XXH32_hash_t XXH32_digest(const XXH32_state_t* statePtr);
XXH3_state_t* XXH3_createState(void);
XXH_errorcode XXH3_64bits_reset(XXH3_state_t* statePtr);
XXH_errorcode XXH3_64bits_update(XXH3_state_t* statePtr, const void* input, size_t length);
XXH64_hash_t XXH3_64bits_digest(const XXH3_state_t* statePtr);
Import
/* Single-header include */
#include "xxhash.h"
/* To include implementations (in exactly one .c file): */
#define XXH_IMPLEMENTATION
#include "xxhash.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | const void* | Yes | Pointer to data to hash (NULL allowed only if length is 0) |
| length | size_t | Yes | Size in bytes of the input data |
| seed | XXH32_hash_t / XXH64_hash_t | No | Seed value for hash computation (one-shot API only) |
| statePtr | XXH*_state_t* | Yes | State pointer for streaming API incremental hashing |
Outputs
| Name | Type | Description |
|---|---|---|
| hash32 | XXH32_hash_t (uint32_t) | 32-bit hash result from XXH32 |
| hash64 | XXH64_hash_t (uint64_t) | 64-bit hash result from XXH64 or XXH3_64bits |
| hash128 | XXH128_hash_t | 128-bit hash result from XXH3_128bits (struct with low64/high64) |
| error_code | XXH_errorcode | XXH_OK (0) on success, XXH_ERROR on failure |
Usage Examples
#include "xxhash.h"
/* One-shot hashing */
const char* data = "Hello, World!";
XXH64_hash_t hash = XXH64(data, strlen(data), 0 /* seed */);
/* Streaming hashing (for large files) */
XXH3_state_t* state = XXH3_createState();
XXH3_64bits_reset(state);
char buffer[4096];
size_t count;
while ((count = fread(buffer, 1, sizeof(buffer), file)) != 0) {
XXH3_64bits_update(state, buffer, count);
}
XXH64_hash_t result = XXH3_64bits_digest(state);
XXH3_freeState(state);