18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * xxHash - Extremely Fast Hash algorithm
38c2ecf20Sopenharmony_ci * Copyright (C) 2012-2016, Yann Collet.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
88c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are
98c2ecf20Sopenharmony_ci * met:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
128c2ecf20Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
138c2ecf20Sopenharmony_ci *   * Redistributions in binary form must reproduce the above
148c2ecf20Sopenharmony_ci *     copyright notice, this list of conditions and the following disclaimer
158c2ecf20Sopenharmony_ci *     in the documentation and/or other materials provided with the
168c2ecf20Sopenharmony_ci *     distribution.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it under
318c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2 as published by the
328c2ecf20Sopenharmony_ci * Free Software Foundation. This program is dual-licensed; you may select
338c2ecf20Sopenharmony_ci * either version 2 of the GNU General Public License ("GPL") or BSD license
348c2ecf20Sopenharmony_ci * ("BSD").
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * You can contact the author at:
378c2ecf20Sopenharmony_ci * - xxHash homepage: https://cyan4973.github.io/xxHash/
388c2ecf20Sopenharmony_ci * - xxHash source repository: https://github.com/Cyan4973/xxHash
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * Notice extracted from xxHash homepage:
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
458c2ecf20Sopenharmony_ci * It also successfully passes all tests from the SMHasher suite.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2
488c2ecf20Sopenharmony_ci * Duo @3GHz)
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Name            Speed       Q.Score   Author
518c2ecf20Sopenharmony_ci * xxHash          5.4 GB/s     10
528c2ecf20Sopenharmony_ci * CrapWow         3.2 GB/s      2       Andrew
538c2ecf20Sopenharmony_ci * MumurHash 3a    2.7 GB/s     10       Austin Appleby
548c2ecf20Sopenharmony_ci * SpookyHash      2.0 GB/s     10       Bob Jenkins
558c2ecf20Sopenharmony_ci * SBox            1.4 GB/s      9       Bret Mulvey
568c2ecf20Sopenharmony_ci * Lookup3         1.2 GB/s      9       Bob Jenkins
578c2ecf20Sopenharmony_ci * SuperFastHash   1.2 GB/s      1       Paul Hsieh
588c2ecf20Sopenharmony_ci * CityHash64      1.05 GB/s    10       Pike & Alakuijala
598c2ecf20Sopenharmony_ci * FNV             0.55 GB/s     5       Fowler, Noll, Vo
608c2ecf20Sopenharmony_ci * CRC32           0.43 GB/s     9
618c2ecf20Sopenharmony_ci * MD5-32          0.33 GB/s    10       Ronald L. Rivest
628c2ecf20Sopenharmony_ci * SHA1-32         0.28 GB/s    10
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * Q.Score is a measure of quality of the hash function.
658c2ecf20Sopenharmony_ci * It depends on successfully passing SMHasher test set.
668c2ecf20Sopenharmony_ci * 10 is a perfect score.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * A 64-bits version, named xxh64 offers much better speed,
698c2ecf20Sopenharmony_ci * but for 64-bits applications only.
708c2ecf20Sopenharmony_ci * Name     Speed on 64 bits    Speed on 32 bits
718c2ecf20Sopenharmony_ci * xxh64       13.8 GB/s            1.9 GB/s
728c2ecf20Sopenharmony_ci * xxh32        6.8 GB/s            6.0 GB/s
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#ifndef XXHASH_H
768c2ecf20Sopenharmony_ci#define XXHASH_H
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#include <linux/types.h>
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*-****************************
818c2ecf20Sopenharmony_ci * Simple Hash Functions
828c2ecf20Sopenharmony_ci *****************************/
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/**
858c2ecf20Sopenharmony_ci * xxh32() - calculate the 32-bit hash of the input with a given seed.
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * @input:  The data to hash.
888c2ecf20Sopenharmony_ci * @length: The length of the data to hash.
898c2ecf20Sopenharmony_ci * @seed:   The seed can be used to alter the result predictably.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * Return:  The 32-bit hash of the data.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_ciuint32_t xxh32(const void *input, size_t length, uint32_t seed);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/**
988c2ecf20Sopenharmony_ci * xxh64() - calculate the 64-bit hash of the input with a given seed.
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * @input:  The data to hash.
1018c2ecf20Sopenharmony_ci * @length: The length of the data to hash.
1028c2ecf20Sopenharmony_ci * @seed:   The seed can be used to alter the result predictably.
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * Return:  The 64-bit hash of the data.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ciuint64_t xxh64(const void *input, size_t length, uint64_t seed);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/**
1118c2ecf20Sopenharmony_ci * xxhash() - calculate wordsize hash of the input with a given seed
1128c2ecf20Sopenharmony_ci * @input:  The data to hash.
1138c2ecf20Sopenharmony_ci * @length: The length of the data to hash.
1148c2ecf20Sopenharmony_ci * @seed:   The seed can be used to alter the result predictably.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * If the hash does not need to be comparable between machines with
1178c2ecf20Sopenharmony_ci * different word sizes, this function will call whichever of xxh32()
1188c2ecf20Sopenharmony_ci * or xxh64() is faster.
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * Return:  wordsize hash of the data.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline unsigned long xxhash(const void *input, size_t length,
1248c2ecf20Sopenharmony_ci				   uint64_t seed)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
1278c2ecf20Sopenharmony_ci       return xxh64(input, length, seed);
1288c2ecf20Sopenharmony_ci#else
1298c2ecf20Sopenharmony_ci       return xxh32(input, length, seed);
1308c2ecf20Sopenharmony_ci#endif
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*-****************************
1348c2ecf20Sopenharmony_ci * Streaming Hash Functions
1358c2ecf20Sopenharmony_ci *****************************/
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/*
1388c2ecf20Sopenharmony_ci * These definitions are only meant to allow allocation of XXH state
1398c2ecf20Sopenharmony_ci * statically, on stack, or in a struct for example.
1408c2ecf20Sopenharmony_ci * Do not use members directly.
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/**
1448c2ecf20Sopenharmony_ci * struct xxh32_state - private xxh32 state, do not use members directly
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_cistruct xxh32_state {
1478c2ecf20Sopenharmony_ci	uint32_t total_len_32;
1488c2ecf20Sopenharmony_ci	uint32_t large_len;
1498c2ecf20Sopenharmony_ci	uint32_t v1;
1508c2ecf20Sopenharmony_ci	uint32_t v2;
1518c2ecf20Sopenharmony_ci	uint32_t v3;
1528c2ecf20Sopenharmony_ci	uint32_t v4;
1538c2ecf20Sopenharmony_ci	uint32_t mem32[4];
1548c2ecf20Sopenharmony_ci	uint32_t memsize;
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/**
1588c2ecf20Sopenharmony_ci * struct xxh32_state - private xxh64 state, do not use members directly
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistruct xxh64_state {
1618c2ecf20Sopenharmony_ci	uint64_t total_len;
1628c2ecf20Sopenharmony_ci	uint64_t v1;
1638c2ecf20Sopenharmony_ci	uint64_t v2;
1648c2ecf20Sopenharmony_ci	uint64_t v3;
1658c2ecf20Sopenharmony_ci	uint64_t v4;
1668c2ecf20Sopenharmony_ci	uint64_t mem64[4];
1678c2ecf20Sopenharmony_ci	uint32_t memsize;
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/**
1718c2ecf20Sopenharmony_ci * xxh32_reset() - reset the xxh32 state to start a new hashing operation
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * @state: The xxh32 state to reset.
1748c2ecf20Sopenharmony_ci * @seed:  Initialize the hash state with this seed.
1758c2ecf20Sopenharmony_ci *
1768c2ecf20Sopenharmony_ci * Call this function on any xxh32_state to prepare for a new hashing operation.
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_civoid xxh32_reset(struct xxh32_state *state, uint32_t seed);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/**
1818c2ecf20Sopenharmony_ci * xxh32_update() - hash the data given and update the xxh32 state
1828c2ecf20Sopenharmony_ci *
1838c2ecf20Sopenharmony_ci * @state:  The xxh32 state to update.
1848c2ecf20Sopenharmony_ci * @input:  The data to hash.
1858c2ecf20Sopenharmony_ci * @length: The length of the data to hash.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * After calling xxh32_reset() call xxh32_update() as many times as necessary.
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * Return:  Zero on success, otherwise an error code.
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_ciint xxh32_update(struct xxh32_state *state, const void *input, size_t length);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/**
1948c2ecf20Sopenharmony_ci * xxh32_digest() - produce the current xxh32 hash
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * @state: Produce the current xxh32 hash of this state.
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * A hash value can be produced at any time. It is still possible to continue
1998c2ecf20Sopenharmony_ci * inserting input into the hash state after a call to xxh32_digest(), and
2008c2ecf20Sopenharmony_ci * generate new hashes later on, by calling xxh32_digest() again.
2018c2ecf20Sopenharmony_ci *
2028c2ecf20Sopenharmony_ci * Return: The xxh32 hash stored in the state.
2038c2ecf20Sopenharmony_ci */
2048c2ecf20Sopenharmony_ciuint32_t xxh32_digest(const struct xxh32_state *state);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/**
2078c2ecf20Sopenharmony_ci * xxh64_reset() - reset the xxh64 state to start a new hashing operation
2088c2ecf20Sopenharmony_ci *
2098c2ecf20Sopenharmony_ci * @state: The xxh64 state to reset.
2108c2ecf20Sopenharmony_ci * @seed:  Initialize the hash state with this seed.
2118c2ecf20Sopenharmony_ci */
2128c2ecf20Sopenharmony_civoid xxh64_reset(struct xxh64_state *state, uint64_t seed);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/**
2158c2ecf20Sopenharmony_ci * xxh64_update() - hash the data given and update the xxh64 state
2168c2ecf20Sopenharmony_ci * @state:  The xxh64 state to update.
2178c2ecf20Sopenharmony_ci * @input:  The data to hash.
2188c2ecf20Sopenharmony_ci * @length: The length of the data to hash.
2198c2ecf20Sopenharmony_ci *
2208c2ecf20Sopenharmony_ci * After calling xxh64_reset() call xxh64_update() as many times as necessary.
2218c2ecf20Sopenharmony_ci *
2228c2ecf20Sopenharmony_ci * Return:  Zero on success, otherwise an error code.
2238c2ecf20Sopenharmony_ci */
2248c2ecf20Sopenharmony_ciint xxh64_update(struct xxh64_state *state, const void *input, size_t length);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/**
2278c2ecf20Sopenharmony_ci * xxh64_digest() - produce the current xxh64 hash
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * @state: Produce the current xxh64 hash of this state.
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci * A hash value can be produced at any time. It is still possible to continue
2328c2ecf20Sopenharmony_ci * inserting input into the hash state after a call to xxh64_digest(), and
2338c2ecf20Sopenharmony_ci * generate new hashes later on, by calling xxh64_digest() again.
2348c2ecf20Sopenharmony_ci *
2358c2ecf20Sopenharmony_ci * Return: The xxh64 hash stored in the state.
2368c2ecf20Sopenharmony_ci */
2378c2ecf20Sopenharmony_ciuint64_t xxh64_digest(const struct xxh64_state *state);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/*-**************************
2408c2ecf20Sopenharmony_ci * Utils
2418c2ecf20Sopenharmony_ci ***************************/
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/**
2448c2ecf20Sopenharmony_ci * xxh32_copy_state() - copy the source state into the destination state
2458c2ecf20Sopenharmony_ci *
2468c2ecf20Sopenharmony_ci * @src: The source xxh32 state.
2478c2ecf20Sopenharmony_ci * @dst: The destination xxh32 state.
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_civoid xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/**
2528c2ecf20Sopenharmony_ci * xxh64_copy_state() - copy the source state into the destination state
2538c2ecf20Sopenharmony_ci *
2548c2ecf20Sopenharmony_ci * @src: The source xxh64 state.
2558c2ecf20Sopenharmony_ci * @dst: The destination xxh64 state.
2568c2ecf20Sopenharmony_ci */
2578c2ecf20Sopenharmony_civoid xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci#endif /* XXHASH_H */
260