143a63c18SDmitri Tikhonov/* 243a63c18SDmitri Tikhonov xxHash - Extremely Fast Hash algorithm 343a63c18SDmitri Tikhonov Header File 443a63c18SDmitri Tikhonov Copyright (C) 2012-2014, Yann Collet. 543a63c18SDmitri Tikhonov BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 643a63c18SDmitri Tikhonov 743a63c18SDmitri Tikhonov Redistribution and use in source and binary forms, with or without 843a63c18SDmitri Tikhonov modification, are permitted provided that the following conditions are 943a63c18SDmitri Tikhonov met: 1043a63c18SDmitri Tikhonov 1143a63c18SDmitri Tikhonov * Redistributions of source code must retain the above copyright 1243a63c18SDmitri Tikhonov notice, this list of conditions and the following disclaimer. 1343a63c18SDmitri Tikhonov * Redistributions in binary form must reproduce the above 1443a63c18SDmitri Tikhonov copyright notice, this list of conditions and the following disclaimer 1543a63c18SDmitri Tikhonov in the documentation and/or other materials provided with the 1643a63c18SDmitri Tikhonov distribution. 1743a63c18SDmitri Tikhonov 1843a63c18SDmitri Tikhonov THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1943a63c18SDmitri Tikhonov "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2043a63c18SDmitri Tikhonov LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2143a63c18SDmitri Tikhonov A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2243a63c18SDmitri Tikhonov OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2343a63c18SDmitri Tikhonov SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2443a63c18SDmitri Tikhonov LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2543a63c18SDmitri Tikhonov DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2643a63c18SDmitri Tikhonov THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2743a63c18SDmitri Tikhonov (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2843a63c18SDmitri Tikhonov OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2943a63c18SDmitri Tikhonov 3043a63c18SDmitri Tikhonov You can contact the author at : 3143a63c18SDmitri Tikhonov - xxHash source repository : http://code.google.com/p/xxhash/ 3243a63c18SDmitri Tikhonov*/ 3343a63c18SDmitri Tikhonov 3443a63c18SDmitri Tikhonov/* Notice extracted from xxHash homepage : 3543a63c18SDmitri Tikhonov 3643a63c18SDmitri TikhonovxxHash is an extremely fast Hash algorithm, running at RAM speed limits. 3743a63c18SDmitri TikhonovIt also successfully passes all tests from the SMHasher suite. 3843a63c18SDmitri Tikhonov 3943a63c18SDmitri TikhonovComparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) 4043a63c18SDmitri Tikhonov 4143a63c18SDmitri TikhonovName Speed Q.Score Author 4243a63c18SDmitri TikhonovxxHash 5.4 GB/s 10 4343a63c18SDmitri TikhonovCrapWow 3.2 GB/s 2 Andrew 4443a63c18SDmitri TikhonovMumurHash 3a 2.7 GB/s 10 Austin Appleby 4543a63c18SDmitri TikhonovSpookyHash 2.0 GB/s 10 Bob Jenkins 4643a63c18SDmitri TikhonovSBox 1.4 GB/s 9 Bret Mulvey 4743a63c18SDmitri TikhonovLookup3 1.2 GB/s 9 Bob Jenkins 4843a63c18SDmitri TikhonovSuperFastHash 1.2 GB/s 1 Paul Hsieh 4943a63c18SDmitri TikhonovCityHash64 1.05 GB/s 10 Pike & Alakuijala 5043a63c18SDmitri TikhonovFNV 0.55 GB/s 5 Fowler, Noll, Vo 5143a63c18SDmitri TikhonovCRC32 0.43 GB/s 9 5243a63c18SDmitri TikhonovMD5-32 0.33 GB/s 10 Ronald L. Rivest 5343a63c18SDmitri TikhonovSHA1-32 0.28 GB/s 10 5443a63c18SDmitri Tikhonov 5543a63c18SDmitri TikhonovQ.Score is a measure of quality of the hash function. 5643a63c18SDmitri TikhonovIt depends on successfully passing SMHasher test set. 5743a63c18SDmitri Tikhonov10 is a perfect score. 5843a63c18SDmitri Tikhonov*/ 5943a63c18SDmitri Tikhonov 6043a63c18SDmitri Tikhonov#pragma once 6143a63c18SDmitri Tikhonov 6243a63c18SDmitri Tikhonov#if defined (__cplusplus) 6343a63c18SDmitri Tikhonovextern "C" { 6443a63c18SDmitri Tikhonov#endif 6543a63c18SDmitri Tikhonov 6643a63c18SDmitri Tikhonov 6743a63c18SDmitri Tikhonov/***************************** 6843a63c18SDmitri Tikhonov Includes 6943a63c18SDmitri Tikhonov*****************************/ 7043a63c18SDmitri Tikhonov#include <stddef.h> /* size_t */ 7143a63c18SDmitri Tikhonov 7243a63c18SDmitri Tikhonov 7343a63c18SDmitri Tikhonov/***************************** 7443a63c18SDmitri Tikhonov Type 7543a63c18SDmitri Tikhonov*****************************/ 7643a63c18SDmitri Tikhonovtypedef enum { XXH_OK = 0, XXH_ERROR } XXH_errorcode; 7743a63c18SDmitri Tikhonov 7843a63c18SDmitri Tikhonov 7943a63c18SDmitri Tikhonov 8043a63c18SDmitri Tikhonov/***************************** 8143a63c18SDmitri Tikhonov Simple Hash Functions 8243a63c18SDmitri Tikhonov*****************************/ 8343a63c18SDmitri Tikhonov 8443a63c18SDmitri Tikhonovunsigned int XXH32(const void *input, size_t length, unsigned seed); 8543a63c18SDmitri Tikhonovunsigned long long XXH64(const void *input, size_t length, 8643a63c18SDmitri Tikhonov unsigned long long seed); 8743a63c18SDmitri Tikhonov 8843a63c18SDmitri Tikhonov/* 8943a63c18SDmitri TikhonovXXH32() : 9043a63c18SDmitri Tikhonov Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". 9143a63c18SDmitri Tikhonov The memory between input & input+length must be valid (allocated and read-accessible). 9243a63c18SDmitri Tikhonov "seed" can be used to alter the result predictably. 9343a63c18SDmitri Tikhonov This function successfully passes all SMHasher tests. 9443a63c18SDmitri Tikhonov Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s 9543a63c18SDmitri TikhonovXXH64() : 9643a63c18SDmitri Tikhonov Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". 9743a63c18SDmitri Tikhonov*/ 9843a63c18SDmitri Tikhonov 9943a63c18SDmitri Tikhonov 10043a63c18SDmitri Tikhonov 10143a63c18SDmitri Tikhonov/***************************** 10243a63c18SDmitri Tikhonov Advanced Hash Functions 10343a63c18SDmitri Tikhonov*****************************/ 10443a63c18SDmitri Tikhonovtypedef struct { long long ll[ 6]; } XXH32_state_t; 10543a63c18SDmitri Tikhonovtypedef struct { long long ll[11]; } XXH64_state_t; 10643a63c18SDmitri Tikhonov 10743a63c18SDmitri Tikhonov/* 10843a63c18SDmitri TikhonovThese structures allow static allocation of XXH states. 10943a63c18SDmitri TikhonovStates must then be initialized using XXHnn_reset() before first use. 11043a63c18SDmitri Tikhonov 11143a63c18SDmitri TikhonovIf you prefer dynamic allocation, please refer to functions below. 11243a63c18SDmitri Tikhonov*/ 11343a63c18SDmitri Tikhonov 11443a63c18SDmitri TikhonovXXH32_state_t *XXH32_createState(void); 11543a63c18SDmitri TikhonovXXH_errorcode XXH32_freeState(XXH32_state_t *statePtr); 11643a63c18SDmitri Tikhonov 11743a63c18SDmitri TikhonovXXH64_state_t *XXH64_createState(void); 11843a63c18SDmitri TikhonovXXH_errorcode XXH64_freeState(XXH64_state_t *statePtr); 11943a63c18SDmitri Tikhonov 12043a63c18SDmitri Tikhonov/* 12143a63c18SDmitri TikhonovThese functions create and release memory for XXH state. 12243a63c18SDmitri TikhonovStates must then be initialized using XXHnn_reset() before first use. 12343a63c18SDmitri Tikhonov*/ 12443a63c18SDmitri Tikhonov 12543a63c18SDmitri Tikhonov 12643a63c18SDmitri TikhonovXXH_errorcode XXH32_reset(XXH32_state_t *statePtr, unsigned seed); 12743a63c18SDmitri TikhonovXXH_errorcode XXH32_update(XXH32_state_t *statePtr, const void *input, 12843a63c18SDmitri Tikhonov size_t length); 12943a63c18SDmitri Tikhonovunsigned int XXH32_digest(const XXH32_state_t *statePtr); 13043a63c18SDmitri Tikhonov 13143a63c18SDmitri TikhonovXXH_errorcode XXH64_reset(XXH64_state_t *statePtr, 13243a63c18SDmitri Tikhonov unsigned long long seed); 13343a63c18SDmitri TikhonovXXH_errorcode XXH64_update(XXH64_state_t *statePtr, const void *input, 13443a63c18SDmitri Tikhonov size_t length); 13543a63c18SDmitri Tikhonovunsigned long long XXH64_digest(const XXH64_state_t *statePtr); 13643a63c18SDmitri Tikhonov 13743a63c18SDmitri Tikhonov/* 13843a63c18SDmitri TikhonovThese functions calculate the xxHash of an input provided in multiple smaller packets, 13943a63c18SDmitri Tikhonovas opposed to an input provided as a single block. 14043a63c18SDmitri Tikhonov 14143a63c18SDmitri TikhonovXXH state space must first be allocated, using either static or dynamic method provided above. 14243a63c18SDmitri Tikhonov 14343a63c18SDmitri TikhonovStart a new hash by initializing state with a seed, using XXHnn_reset(). 14443a63c18SDmitri Tikhonov 14543a63c18SDmitri TikhonovThen, feed the hash state by calling XXHnn_update() as many times as necessary. 14643a63c18SDmitri TikhonovObviously, input must be valid, meaning allocated and read accessible. 14743a63c18SDmitri TikhonovThe function returns an error code, with 0 meaning OK, and any other value meaning there is an error. 14843a63c18SDmitri Tikhonov 14943a63c18SDmitri TikhonovFinally, you can produce a hash anytime, by using XXHnn_digest(). 15043a63c18SDmitri TikhonovThis function returns the final nn-bits hash. 15143a63c18SDmitri TikhonovYou can nonetheless continue feeding the hash state with more input, 15243a63c18SDmitri Tikhonovand therefore get some new hashes, by calling again XXHnn_digest(). 15343a63c18SDmitri Tikhonov 15443a63c18SDmitri TikhonovWhen you are done, don't forget to free XXH state space, using typically XXHnn_freeState(). 15543a63c18SDmitri Tikhonov*/ 15643a63c18SDmitri Tikhonov 15743a63c18SDmitri Tikhonov 15843a63c18SDmitri Tikhonov#if defined (__cplusplus) 15943a63c18SDmitri Tikhonov} 16043a63c18SDmitri Tikhonov#endif 161