Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/xxhash.h
48775 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/*2* xxHash - Extremely Fast Hash algorithm3* Header File4* Copyright (c) 2012-2020, Yann Collet, Facebook, Inc.5*6* You can contact the author at :7* - xxHash source repository : https://github.com/Cyan4973/xxHash8*9* This source code is licensed under both the BSD-style license (found in the10* LICENSE file in the root directory of this source tree) and the GPLv2 (found11* in the COPYING file in the root directory of this source tree).12* You may select, at your option, one of the above-listed licenses.13*/1415/* Notice extracted from xxHash homepage :1617xxHash is an extremely fast Hash algorithm, running at RAM speed limits.18It also successfully passes all tests from the SMHasher suite.1920Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)2122Name Speed Q.Score Author23xxHash 5.4 GB/s 1024CrapWow 3.2 GB/s 2 Andrew25MumurHash 3a 2.7 GB/s 10 Austin Appleby26SpookyHash 2.0 GB/s 10 Bob Jenkins27SBox 1.4 GB/s 9 Bret Mulvey28Lookup3 1.2 GB/s 9 Bob Jenkins29SuperFastHash 1.2 GB/s 1 Paul Hsieh30CityHash64 1.05 GB/s 10 Pike & Alakuijala31FNV 0.55 GB/s 5 Fowler, Noll, Vo32CRC32 0.43 GB/s 933MD5-32 0.33 GB/s 10 Ronald L. Rivest34SHA1-32 0.28 GB/s 103536Q.Score is a measure of quality of the hash function.37It depends on successfully passing SMHasher test set.3810 is a perfect score.3940A 64-bits version, named XXH64, is available since r35.41It offers much better speed, but for 64-bits applications only.42Name Speed on 64 bits Speed on 32 bits43XXH64 13.8 GB/s 1.9 GB/s44XXH32 6.8 GB/s 6.0 GB/s45*/4647#if defined (__cplusplus)48extern "C" {49#endif5051#ifndef XXHASH_H_562713558566617952#define XXHASH_H_5627135585666179 1535455/* ****************************56* Definitions57******************************/58#include <stddef.h> /* size_t */59typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;606162/* ****************************63* API modifier64******************************/65/** XXH_PRIVATE_API66* This is useful if you want to include xxhash functions in `static` mode67* in order to inline them, and remove their symbol from the public list.68* Methodology :69* #define XXH_PRIVATE_API70* #include "xxhash.h"71* `xxhash.c` is automatically included.72* It's not useful to compile and link it as a separate module anymore.73*/74#ifdef XXH_PRIVATE_API75# ifndef XXH_STATIC_LINKING_ONLY76# define XXH_STATIC_LINKING_ONLY77# endif78# if defined(__GNUC__)79# define XXH_PUBLIC_API static __inline __attribute__((unused))80# elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)81# define XXH_PUBLIC_API static inline82# elif defined(_MSC_VER)83# define XXH_PUBLIC_API static __inline84# else85# define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */86# endif87#else88# define XXH_PUBLIC_API /* do nothing */89#endif /* XXH_PRIVATE_API */9091/*!XXH_NAMESPACE, aka Namespace Emulation :9293If you want to include _and expose_ xxHash functions from within your own library,94but also want to avoid symbol collisions with another library which also includes xxHash,9596you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library97with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).9899Note that no change is required within the calling program as long as it includes `xxhash.h` :100regular symbol name will be automatically translated by this header.101*/102#ifdef XXH_NAMESPACE103# define XXH_CAT(A,B) A##B104# define XXH_NAME2(A,B) XXH_CAT(A,B)105# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)106# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)107# define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)108# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)109# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)110# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)111# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)112# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)113# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)114# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)115# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)116# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)117# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)118# define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)119# define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)120# define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)121# define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)122# define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)123# define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)124#endif125126127/* *************************************128* Version129***************************************/130#define XXH_VERSION_MAJOR 0131#define XXH_VERSION_MINOR 6132#define XXH_VERSION_RELEASE 2133#define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)134XXH_PUBLIC_API unsigned XXH_versionNumber (void);135136137/* ****************************138* Simple Hash Functions139******************************/140typedef unsigned int XXH32_hash_t;141typedef unsigned long long XXH64_hash_t;142143XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);144XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);145146/*!147XXH32() :148Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".149The memory between input & input+length must be valid (allocated and read-accessible).150"seed" can be used to alter the result predictably.151Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s152XXH64() :153Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".154"seed" can be used to alter the result predictably.155This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark).156*/157158159/* ****************************160* Streaming Hash Functions161******************************/162typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */163typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */164165/*! State allocation, compatible with dynamic libraries */166167XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);168XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);169170XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);171XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);172173174/* hash streaming */175176XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);177XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);178XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);179180XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);181XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);182XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);183184/*185These functions generate the xxHash of an input provided in multiple segments.186Note that, for small input, they are slower than single-call functions, due to state management.187For small input, prefer `XXH32()` and `XXH64()` .188189XXH state must first be allocated, using XXH*_createState() .190191Start a new hash by initializing state with a seed, using XXH*_reset().192193Then, feed the hash state by calling XXH*_update() as many times as necessary.194Obviously, input must be allocated and read accessible.195The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.196197Finally, a hash value can be produced anytime, by using XXH*_digest().198This function returns the nn-bits hash as an int or long long.199200It's still possible to continue inserting input into the hash state after a digest,201and generate some new hashes later on, by calling again XXH*_digest().202203When done, free XXH state space if it was allocated dynamically.204*/205206207/* **************************208* Utils209****************************/210#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */211# define restrict /* disable restrict */212#endif213214XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state);215XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state);216217218/* **************************219* Canonical representation220****************************/221/* Default result type for XXH functions are primitive unsigned 32 and 64 bits.222* The canonical representation uses human-readable write convention, aka big-endian (large digits first).223* These functions allow transformation of hash result into and from its canonical format.224* This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.225*/226typedef struct { unsigned char digest[4]; } XXH32_canonical_t;227typedef struct { unsigned char digest[8]; } XXH64_canonical_t;228229XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);230XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);231232XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);233XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);234235#endif /* XXHASH_H_5627135585666179 */236237238239/* ================================================================================================240This section contains definitions which are not guaranteed to remain stable.241They may change in future versions, becoming incompatible with a different version of the library.242They shall only be used with static linking.243Never use these definitions in association with dynamic linking !244=================================================================================================== */245#if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345)246#define XXH_STATIC_H_3543687687345247248/* These definitions are only meant to allow allocation of XXH state249statically, on stack, or in a struct for example.250Do not use members directly. */251252struct XXH32_state_s {253unsigned total_len_32;254unsigned large_len;255unsigned v1;256unsigned v2;257unsigned v3;258unsigned v4;259unsigned mem32[4]; /* buffer defined as U32 for alignment */260unsigned memsize;261unsigned reserved; /* never read nor write, will be removed in a future version */262}; /* typedef'd to XXH32_state_t */263264struct XXH64_state_s {265unsigned long long total_len;266unsigned long long v1;267unsigned long long v2;268unsigned long long v3;269unsigned long long v4;270unsigned long long mem64[4]; /* buffer defined as U64 for alignment */271unsigned memsize;272unsigned reserved[2]; /* never read nor write, will be removed in a future version */273}; /* typedef'd to XXH64_state_t */274275276# ifdef XXH_PRIVATE_API277# include "xxhash.c" /* include xxhash functions as `static`, for inlining */278# endif279280#endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */281282283#if defined (__cplusplus)284}285#endif286287288