mirror of
https://github.com/trapexit/mergerfs.git
synced 2025-04-24 22:14:05 +08:00
Replace usage of wyhash with rapidhash
https://github.com/Nicoshev/rapidhash
This commit is contained in:
parent
764bd8f7be
commit
f74dc36e92
@ -3,7 +3,7 @@
|
||||
### included in repo
|
||||
|
||||
* libfuse: https://github.com/libfuse/libfuse (heavily modified fork of v2.x)
|
||||
* wyhash: https://github.com/wangyi-fudan/wyhash
|
||||
* rapidhash: https://github.com/Nicoshev/rapidhash
|
||||
* ghc::filesystem: https://github.com/gulrak/filesystem
|
||||
* nonstd::optional: https://github.com/martinmoene/optional-lite
|
||||
* fmt: https://github.com/fmtlib/fmt
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include "ef.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "wyhash.h"
|
||||
#include "rapidhash.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@ -61,10 +61,7 @@ path_hash(const char *fusepath_,
|
||||
const dev_t dev_,
|
||||
const ino_t ino_)
|
||||
{
|
||||
return wyhash(fusepath_,
|
||||
fusepath_len_,
|
||||
fs::inode::MAGIC,
|
||||
_wyp);
|
||||
return rapidhash(fusepath_,fusepath_len_);
|
||||
}
|
||||
|
||||
static
|
||||
@ -99,10 +96,7 @@ devino_hash(const char *fusepath_,
|
||||
buf[0] = dev_;
|
||||
buf[1] = ino_;
|
||||
|
||||
return wyhash((void*)&buf[0],
|
||||
sizeof(buf),
|
||||
fs::inode::MAGIC,
|
||||
_wyp);
|
||||
return rapidhash((void*)&buf[0],sizeof(buf));
|
||||
}
|
||||
|
||||
static
|
||||
|
@ -19,7 +19,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "khash.h"
|
||||
#include "wyhash.h"
|
||||
#include "rapidhash.h"
|
||||
|
||||
KHASH_SET_INIT_INT64(hashset);
|
||||
|
||||
@ -45,7 +45,7 @@ public:
|
||||
uint64_t h;
|
||||
khint_t key;
|
||||
|
||||
h = wyhash(str_,len_,0x7472617065786974,_wyp);
|
||||
h = rapidhash(str_,len_);
|
||||
|
||||
key = kh_put(hashset,_set,h,&rv);
|
||||
if(rv == 0)
|
||||
|
323
src/rapidhash.h
Normal file
323
src/rapidhash.h
Normal file
@ -0,0 +1,323 @@
|
||||
/*
|
||||
* rapidhash - Very fast, high quality, platform-independent hashing algorithm.
|
||||
* Copyright (C) 2024 Nicolas De Carli
|
||||
*
|
||||
* Based on 'wyhash', by Wang Yi <godspeed_china@yeah.net>
|
||||
*
|
||||
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* You can contact the author at:
|
||||
* - rapidhash source repository: https://github.com/Nicoshev/rapidhash
|
||||
*/
|
||||
|
||||
/*
|
||||
* Includes.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#if defined(_MSC_VER)
|
||||
#include <intrin.h>
|
||||
#if defined(_M_X64) && !defined(_M_ARM64EC)
|
||||
#pragma intrinsic(_umul128)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C++ macros.
|
||||
*
|
||||
* RAPIDHASH_INLINE can be overridden to be stronger than a hint, i.e. by adding __attribute__((always_inline)).
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
#define RAPIDHASH_NOEXCEPT noexcept
|
||||
#define RAPIDHASH_CONSTEXPR constexpr
|
||||
#ifndef RAPIDHASH_INLINE
|
||||
#define RAPIDHASH_INLINE inline
|
||||
#endif
|
||||
#else
|
||||
#define RAPIDHASH_NOEXCEPT
|
||||
#define RAPIDHASH_CONSTEXPR static const
|
||||
#ifndef RAPIDHASH_INLINE
|
||||
#define RAPIDHASH_INLINE static inline
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Protection macro, alters behaviour of rapid_mum multiplication function.
|
||||
*
|
||||
* RAPIDHASH_FAST: Normal behavior, max speed.
|
||||
* RAPIDHASH_PROTECTED: Extra protection against entropy loss.
|
||||
*/
|
||||
#ifndef RAPIDHASH_PROTECTED
|
||||
#define RAPIDHASH_FAST
|
||||
#elif defined(RAPIDHASH_FAST)
|
||||
#error "cannot define RAPIDHASH_PROTECTED and RAPIDHASH_FAST simultaneously."
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Unrolling macros, changes code definition for main hash function.
|
||||
*
|
||||
* RAPIDHASH_COMPACT: Legacy variant, each loop process 48 bytes.
|
||||
* RAPIDHASH_UNROLLED: Unrolled variant, each loop process 96 bytes.
|
||||
*
|
||||
* Most modern CPUs should benefit from having RAPIDHASH_UNROLLED.
|
||||
*
|
||||
* These macros do not alter the output hash.
|
||||
*/
|
||||
#ifndef RAPIDHASH_COMPACT
|
||||
#define RAPIDHASH_UNROLLED
|
||||
#elif defined(RAPIDHASH_UNROLLED)
|
||||
#error "cannot define RAPIDHASH_COMPACT and RAPIDHASH_UNROLLED simultaneously."
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Likely and unlikely macros.
|
||||
*/
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
#define _likely_(x) __builtin_expect(x,1)
|
||||
#define _unlikely_(x) __builtin_expect(x,0)
|
||||
#else
|
||||
#define _likely_(x) (x)
|
||||
#define _unlikely_(x) (x)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Endianness macros.
|
||||
*/
|
||||
#ifndef RAPIDHASH_LITTLE_ENDIAN
|
||||
#if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
||||
#define RAPIDHASH_LITTLE_ENDIAN
|
||||
#elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
#define RAPIDHASH_BIG_ENDIAN
|
||||
#else
|
||||
#warning "could not determine endianness! Falling back to little endian."
|
||||
#define RAPIDHASH_LITTLE_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Default seed.
|
||||
*/
|
||||
#define RAPID_SEED (0xbdd89aa982704029ull)
|
||||
|
||||
/*
|
||||
* Default secret parameters.
|
||||
*/
|
||||
RAPIDHASH_CONSTEXPR uint64_t rapid_secret[3] = {0x2d358dccaa6c78a5ull, 0x8bb84b93962eacc9ull, 0x4b33a62ed433d4a3ull};
|
||||
|
||||
/*
|
||||
* 64*64 -> 128bit multiply function.
|
||||
*
|
||||
* @param A Address of 64-bit number.
|
||||
* @param B Address of 64-bit number.
|
||||
*
|
||||
* Calculates 128-bit C = *A * *B.
|
||||
*
|
||||
* When RAPIDHASH_FAST is defined:
|
||||
* Overwrites A contents with C's low 64 bits.
|
||||
* Overwrites B contents with C's high 64 bits.
|
||||
*
|
||||
* When RAPIDHASH_PROTECTED is defined:
|
||||
* Xors and overwrites A contents with C's low 64 bits.
|
||||
* Xors and overwrites B contents with C's high 64 bits.
|
||||
*/
|
||||
RAPIDHASH_INLINE void rapid_mum(uint64_t *A, uint64_t *B) RAPIDHASH_NOEXCEPT {
|
||||
#if defined(__SIZEOF_INT128__)
|
||||
__uint128_t r=*A; r*=*B;
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
*A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
|
||||
#else
|
||||
*A=(uint64_t)r; *B=(uint64_t)(r>>64);
|
||||
#endif
|
||||
#elif defined(_MSC_VER) && (defined(_WIN64) || defined(_M_HYBRID_CHPE_ARM64))
|
||||
#if defined(_M_X64)
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
uint64_t a, b;
|
||||
a=_umul128(*A,*B,&b);
|
||||
*A^=a; *B^=b;
|
||||
#else
|
||||
*A=_umul128(*A,*B,B);
|
||||
#endif
|
||||
#else
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
uint64_t a, b;
|
||||
b = __umulh(*A, *B);
|
||||
a = *A * *B;
|
||||
*A^=a; *B^=b;
|
||||
#else
|
||||
uint64_t c = __umulh(*A, *B);
|
||||
*A = *A * *B;
|
||||
*B = c;
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo;
|
||||
uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
|
||||
lo=t+(rm1<<32); c+=lo<t; hi=rh+(rm0>>32)+(rm1>>32)+c;
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
*A^=lo; *B^=hi;
|
||||
#else
|
||||
*A=lo; *B=hi;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply and xor mix function.
|
||||
*
|
||||
* @param A 64-bit number.
|
||||
* @param B 64-bit number.
|
||||
*
|
||||
* Calculates 128-bit C = A * B.
|
||||
* Returns 64-bit xor between high and low 64 bits of C.
|
||||
*/
|
||||
RAPIDHASH_INLINE uint64_t rapid_mix(uint64_t A, uint64_t B) RAPIDHASH_NOEXCEPT { rapid_mum(&A,&B); return A^B; }
|
||||
|
||||
/*
|
||||
* Read functions.
|
||||
*/
|
||||
#ifdef RAPIDHASH_LITTLE_ENDIAN
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return v;}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return v;}
|
||||
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return __builtin_bswap64(v);}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return __builtin_bswap32(v);}
|
||||
#elif defined(_MSC_VER)
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return _byteswap_uint64(v);}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return _byteswap_ulong(v);}
|
||||
#else
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT {
|
||||
uint64_t v; memcpy(&v, p, 8);
|
||||
return (((v >> 56) & 0xff)| ((v >> 40) & 0xff00)| ((v >> 24) & 0xff0000)| ((v >> 8) & 0xff000000)| ((v << 8) & 0xff00000000)| ((v << 24) & 0xff0000000000)| ((v << 40) & 0xff000000000000)| ((v << 56) & 0xff00000000000000));
|
||||
}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT {
|
||||
uint32_t v; memcpy(&v, p, 4);
|
||||
return (((v >> 24) & 0xff)| ((v >> 8) & 0xff00)| ((v << 8) & 0xff0000)| ((v << 24) & 0xff000000));
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Reads and combines 3 bytes of input.
|
||||
*
|
||||
* @param p Buffer to read from.
|
||||
* @param k Length of @p, in bytes.
|
||||
*
|
||||
* Always reads and combines 3 bytes from memory.
|
||||
* Guarantees to read each buffer position at least once.
|
||||
*
|
||||
* Returns a 64-bit value containing all three bytes read.
|
||||
*/
|
||||
RAPIDHASH_INLINE uint64_t rapid_readSmall(const uint8_t *p, size_t k) RAPIDHASH_NOEXCEPT { return (((uint64_t)p[0])<<56)|(((uint64_t)p[k>>1])<<32)|p[k-1];}
|
||||
|
||||
/*
|
||||
* rapidhash main function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
* @param secret Triplet of 64-bit secrets used to alter hash result predictably.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE uint64_t rapidhash_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT {
|
||||
const uint8_t *p=(const uint8_t *)key; seed^=rapid_mix(seed^secret[0],secret[1])^len; uint64_t a, b;
|
||||
if(_likely_(len<=16)){
|
||||
if(_likely_(len>=4)){
|
||||
const uint8_t * plast = p + len - 4;
|
||||
a = (rapid_read32(p) << 32) | rapid_read32(plast);
|
||||
const uint64_t delta = ((len&24)>>(len>>3));
|
||||
b = ((rapid_read32(p + delta) << 32) | rapid_read32(plast - delta)); }
|
||||
else if(_likely_(len>0)){ a=rapid_readSmall(p,len); b=0;}
|
||||
else a=b=0;
|
||||
}
|
||||
else{
|
||||
size_t i=len;
|
||||
if(_unlikely_(i>48)){
|
||||
uint64_t see1=seed, see2=seed;
|
||||
#ifdef RAPIDHASH_UNROLLED
|
||||
while(_likely_(i>=96)){
|
||||
seed=rapid_mix(rapid_read64(p)^secret[0],rapid_read64(p+8)^seed);
|
||||
see1=rapid_mix(rapid_read64(p+16)^secret[1],rapid_read64(p+24)^see1);
|
||||
see2=rapid_mix(rapid_read64(p+32)^secret[2],rapid_read64(p+40)^see2);
|
||||
seed=rapid_mix(rapid_read64(p+48)^secret[0],rapid_read64(p+56)^seed);
|
||||
see1=rapid_mix(rapid_read64(p+64)^secret[1],rapid_read64(p+72)^see1);
|
||||
see2=rapid_mix(rapid_read64(p+80)^secret[2],rapid_read64(p+88)^see2);
|
||||
p+=96; i-=96;
|
||||
}
|
||||
if(_unlikely_(i>=48)){
|
||||
seed=rapid_mix(rapid_read64(p)^secret[0],rapid_read64(p+8)^seed);
|
||||
see1=rapid_mix(rapid_read64(p+16)^secret[1],rapid_read64(p+24)^see1);
|
||||
see2=rapid_mix(rapid_read64(p+32)^secret[2],rapid_read64(p+40)^see2);
|
||||
p+=48; i-=48;
|
||||
}
|
||||
#else
|
||||
do {
|
||||
seed=rapid_mix(rapid_read64(p)^secret[0],rapid_read64(p+8)^seed);
|
||||
see1=rapid_mix(rapid_read64(p+16)^secret[1],rapid_read64(p+24)^see1);
|
||||
see2=rapid_mix(rapid_read64(p+32)^secret[2],rapid_read64(p+40)^see2);
|
||||
p+=48; i-=48;
|
||||
} while (_likely_(i>=48));
|
||||
#endif
|
||||
seed^=see1^see2;
|
||||
}
|
||||
if(i>16){
|
||||
seed=rapid_mix(rapid_read64(p)^secret[2],rapid_read64(p+8)^seed^secret[1]);
|
||||
if(i>32)
|
||||
seed=rapid_mix(rapid_read64(p+16)^secret[2],rapid_read64(p+24)^seed);
|
||||
}
|
||||
a=rapid_read64(p+i-16); b=rapid_read64(p+i-8);
|
||||
}
|
||||
a^=secret[1]; b^=seed; rapid_mum(&a,&b);
|
||||
return rapid_mix(a^secret[0]^len,b^secret[1]);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhash default seeded hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
*
|
||||
* Calls rapidhash_internal using provided parameters and default secrets.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE uint64_t rapidhash_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhash_internal(key, len, seed, rapid_secret);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhash default hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
*
|
||||
* Calls rapidhash_withSeed using provided parameters and the default seed.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE uint64_t rapidhash(const void *key, size_t len) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhash_withSeed(key, len, RAPID_SEED);
|
||||
}
|
17
src/rnd.cpp
17
src/rnd.cpp
@ -18,7 +18,7 @@
|
||||
|
||||
#include "rnd.hpp"
|
||||
|
||||
#include "wyhash.h"
|
||||
#include "rapidhash.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
@ -41,21 +41,30 @@ _constructor()
|
||||
G_SEED |= tv.tv_usec;
|
||||
}
|
||||
|
||||
// Lifted from wyhash.h's wyrand()
|
||||
static
|
||||
uint64_t
|
||||
_rapidhash_rand(uint64_t *seed_)
|
||||
{
|
||||
*seed_ += 0x2d358dccaa6c78a5ull;
|
||||
return rapid_mix(*seed_,*seed_ ^ 0x8bb84b93962eacc9ull);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
RND::rand64(void)
|
||||
{
|
||||
return wyrand(&G_SEED);
|
||||
return _rapidhash_rand(&G_SEED);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
RND::rand64(const uint64_t max_)
|
||||
{
|
||||
return (wyrand(&G_SEED) % max_);
|
||||
return (RND::rand64() % max_);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
RND::rand64(const uint64_t min_,
|
||||
const uint64_t max_)
|
||||
{
|
||||
return (min_ + (wyrand(&G_SEED) % (max_ - min_)));
|
||||
return (min_ + (RND::rand64() % (max_ - min_)));
|
||||
}
|
||||
|
294
src/wyhash.h
294
src/wyhash.h
@ -1,294 +0,0 @@
|
||||
// This is free and unencumbered software released into the public domain under The Unlicense (http://unlicense.org/)
|
||||
// main repo: https://github.com/wangyi-fudan/wyhash
|
||||
// author: 王一 Wang Yi <godspeed_china@yeah.net>
|
||||
// contributors: Reini Urban, Dietrich Epp, Joshua Haberman, Tommy Ettinger, Daniel Lemire, Otmar Ertl, cocowalla, leo-yuriev, Diego Barrios Romero, paulie-g, dumblob, Yann Collet, ivte-ms, hyb, James Z.M. Gao, easyaspi314 (Devin), TheOneric
|
||||
|
||||
/* quick example:
|
||||
string s="fjsakfdsjkf";
|
||||
uint64_t hash=wyhash(s.c_str(), s.size(), 0, _wyp);
|
||||
*/
|
||||
|
||||
#ifndef wyhash_final_version_4_2
|
||||
#define wyhash_final_version_4_2
|
||||
|
||||
#ifndef WYHASH_CONDOM
|
||||
//protections that produce different results:
|
||||
//1: normal valid behavior
|
||||
//2: extra protection against entropy loss (probability=2^-63), aka. "blind multiplication"
|
||||
#define WYHASH_CONDOM 1
|
||||
#endif
|
||||
|
||||
#ifndef WYHASH_32BIT_MUM
|
||||
//0: normal version, slow on 32 bit systems
|
||||
//1: faster on 32 bit systems but produces different results, incompatible with wy2u0k function
|
||||
#define WYHASH_32BIT_MUM 0
|
||||
#endif
|
||||
|
||||
//includes
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#if defined(_MSC_VER) && defined(_M_X64)
|
||||
#include <intrin.h>
|
||||
#pragma intrinsic(_umul128)
|
||||
#endif
|
||||
|
||||
//likely and unlikely macros
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
#define _likely_(x) __builtin_expect(x,1)
|
||||
#define _unlikely_(x) __builtin_expect(x,0)
|
||||
#else
|
||||
#define _likely_(x) (x)
|
||||
#define _unlikely_(x) (x)
|
||||
#endif
|
||||
|
||||
//128bit multiply function
|
||||
static inline uint64_t _wyrot(uint64_t x) { return (x>>32)|(x<<32); }
|
||||
static inline void _wymum(uint64_t *A, uint64_t *B){
|
||||
#if(WYHASH_32BIT_MUM)
|
||||
uint64_t hh=(*A>>32)*(*B>>32), hl=(*A>>32)*(uint32_t)*B, lh=(uint32_t)*A*(*B>>32), ll=(uint64_t)(uint32_t)*A*(uint32_t)*B;
|
||||
#if(WYHASH_CONDOM>1)
|
||||
*A^=_wyrot(hl)^hh; *B^=_wyrot(lh)^ll;
|
||||
#else
|
||||
*A=_wyrot(hl)^hh; *B=_wyrot(lh)^ll;
|
||||
#endif
|
||||
#elif defined(__SIZEOF_INT128__)
|
||||
__uint128_t r=*A; r*=*B;
|
||||
#if(WYHASH_CONDOM>1)
|
||||
*A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
|
||||
#else
|
||||
*A=(uint64_t)r; *B=(uint64_t)(r>>64);
|
||||
#endif
|
||||
#elif defined(_MSC_VER) && defined(_M_X64)
|
||||
#if(WYHASH_CONDOM>1)
|
||||
uint64_t a, b;
|
||||
a=_umul128(*A,*B,&b);
|
||||
*A^=a; *B^=b;
|
||||
#else
|
||||
*A=_umul128(*A,*B,B);
|
||||
#endif
|
||||
#else
|
||||
uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo;
|
||||
uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
|
||||
lo=t+(rm1<<32); c+=lo<t; hi=rh+(rm0>>32)+(rm1>>32)+c;
|
||||
#if(WYHASH_CONDOM>1)
|
||||
*A^=lo; *B^=hi;
|
||||
#else
|
||||
*A=lo; *B=hi;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
//multiply and xor mix function, aka MUM
|
||||
static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B; }
|
||||
|
||||
//endian macros
|
||||
#ifndef WYHASH_LITTLE_ENDIAN
|
||||
#if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
||||
#define WYHASH_LITTLE_ENDIAN 1
|
||||
#elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
#define WYHASH_LITTLE_ENDIAN 0
|
||||
#else
|
||||
#warning could not determine endianness! Falling back to little endian.
|
||||
#define WYHASH_LITTLE_ENDIAN 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//read functions
|
||||
#if (WYHASH_LITTLE_ENDIAN)
|
||||
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;}
|
||||
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
|
||||
#elif defined(_MSC_VER)
|
||||
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return _byteswap_uint64(v);}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return _byteswap_ulong(v);}
|
||||
#else
|
||||
static inline uint64_t _wyr8(const uint8_t *p) {
|
||||
uint64_t v; memcpy(&v, p, 8);
|
||||
return (((v >> 56) & 0xff)| ((v >> 40) & 0xff00)| ((v >> 24) & 0xff0000)| ((v >> 8) & 0xff000000)| ((v << 8) & 0xff00000000)| ((v << 24) & 0xff0000000000)| ((v << 40) & 0xff000000000000)| ((v << 56) & 0xff00000000000000));
|
||||
}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) {
|
||||
uint32_t v; memcpy(&v, p, 4);
|
||||
return (((v >> 24) & 0xff)| ((v >> 8) & 0xff00)| ((v << 8) & 0xff0000)| ((v << 24) & 0xff000000));
|
||||
}
|
||||
#endif
|
||||
static inline uint64_t _wyr3(const uint8_t *p, size_t k) { return (((uint64_t)p[0])<<16)|(((uint64_t)p[k>>1])<<8)|p[k-1];}
|
||||
//wyhash main function
|
||||
static inline uint64_t wyhash(const void *key, size_t len, uint64_t seed, const uint64_t *secret){
|
||||
const uint8_t *p=(const uint8_t *)key; seed^=_wymix(seed^secret[0],secret[1]); uint64_t a, b;
|
||||
if(_likely_(len<=16)){
|
||||
if(_likely_(len>=4)){ a=(_wyr4(p)<<32)|_wyr4(p+((len>>3)<<2)); b=(_wyr4(p+len-4)<<32)|_wyr4(p+len-4-((len>>3)<<2)); }
|
||||
else if(_likely_(len>0)){ a=_wyr3(p,len); b=0;}
|
||||
else a=b=0;
|
||||
}
|
||||
else{
|
||||
size_t i=len;
|
||||
if(_unlikely_(i>=48)){
|
||||
uint64_t see1=seed, see2=seed;
|
||||
do{
|
||||
seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed);
|
||||
see1=_wymix(_wyr8(p+16)^secret[2],_wyr8(p+24)^see1);
|
||||
see2=_wymix(_wyr8(p+32)^secret[3],_wyr8(p+40)^see2);
|
||||
p+=48; i-=48;
|
||||
}while(_likely_(i>=48));
|
||||
seed^=see1^see2;
|
||||
}
|
||||
while(_unlikely_(i>16)){ seed=_wymix(_wyr8(p)^secret[1],_wyr8(p+8)^seed); i-=16; p+=16; }
|
||||
a=_wyr8(p+i-16); b=_wyr8(p+i-8);
|
||||
}
|
||||
a^=secret[1]; b^=seed; _wymum(&a,&b);
|
||||
return _wymix(a^secret[0]^len,b^secret[1]);
|
||||
}
|
||||
|
||||
//the default secret parameters
|
||||
static const uint64_t _wyp[4] = {0x2d358dccaa6c78a5ull, 0x8bb84b93962eacc9ull, 0x4b33a62ed433d4a3ull, 0x4d5a2da51de1aa47ull};
|
||||
|
||||
//a useful 64bit-64bit mix function to produce deterministic pseudo random numbers that can pass BigCrush and PractRand
|
||||
static inline uint64_t wyhash64(uint64_t A, uint64_t B){ A^=0x2d358dccaa6c78a5ull; B^=0x8bb84b93962eacc9ull; _wymum(&A,&B); return _wymix(A^0x2d358dccaa6c78a5ull,B^0x8bb84b93962eacc9ull);}
|
||||
|
||||
//The wyrand PRNG that pass BigCrush and PractRand
|
||||
static inline uint64_t wyrand(uint64_t *seed){ *seed+=0x2d358dccaa6c78a5ull; return _wymix(*seed,*seed^0x8bb84b93962eacc9ull);}
|
||||
|
||||
//convert any 64 bit pseudo random numbers to uniform distribution [0,1). It can be combined with wyrand, wyhash64 or wyhash.
|
||||
static inline double wy2u01(uint64_t r){ const double _wynorm=1.0/(1ull<<52); return (r>>12)*_wynorm;}
|
||||
|
||||
//convert any 64 bit pseudo random numbers to APPROXIMATE Gaussian distribution. It can be combined with wyrand, wyhash64 or wyhash.
|
||||
static inline double wy2gau(uint64_t r){ const double _wynorm=1.0/(1ull<<20); return ((r&0x1fffff)+((r>>21)&0x1fffff)+((r>>42)&0x1fffff))*_wynorm-3.0;}
|
||||
|
||||
#ifdef WYTRNG
|
||||
#include <sys/time.h>
|
||||
//The wytrand true random number generator, passed BigCrush.
|
||||
static inline uint64_t wytrand(uint64_t *seed){
|
||||
struct timeval t; gettimeofday(&t,0);
|
||||
uint64_t teed=(((uint64_t)t.tv_sec)<<32)|t.tv_usec;
|
||||
teed=_wymix(teed^_wyp[0],*seed^_wyp[1]);
|
||||
*seed=_wymix(teed^_wyp[0],_wyp[2]);
|
||||
return _wymix(*seed,*seed^_wyp[3]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if(!WYHASH_32BIT_MUM)
|
||||
//fast range integer random number generation on [0,k) credit to Daniel Lemire. May not work when WYHASH_32BIT_MUM=1. It can be combined with wyrand, wyhash64 or wyhash.
|
||||
static inline uint64_t wy2u0k(uint64_t r, uint64_t k){ _wymum(&r,&k); return k; }
|
||||
#endif
|
||||
|
||||
// modified from https://github.com/going-digital/Prime64
|
||||
static inline unsigned long long mul_mod(unsigned long long a, unsigned long long b, unsigned long long m) {
|
||||
unsigned long long r=0;
|
||||
while (b) {
|
||||
if (b & 1) {
|
||||
unsigned long long r2 = r + a;
|
||||
if (r2 < r) r2 -= m;
|
||||
r = r2 % m;
|
||||
}
|
||||
b >>= 1;
|
||||
if (b) {
|
||||
unsigned long long a2 = a + a;
|
||||
if (a2 < a) a2 -= m;
|
||||
a = a2 % m;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
static inline unsigned long long pow_mod(unsigned long long a, unsigned long long b, unsigned long long m) {
|
||||
unsigned long long r=1;
|
||||
while (b) {
|
||||
if (b&1) r=mul_mod(r,a,m);
|
||||
b>>=1;
|
||||
if (b) a=mul_mod(a,a,m);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
static inline unsigned sprp(unsigned long long n, unsigned long long a) {
|
||||
unsigned long long d=n-1;
|
||||
unsigned char s=0;
|
||||
while (!(d & 0xff)) { d>>=8; s+=8; }
|
||||
if (!(d & 0xf)) { d>>=4; s+=4; }
|
||||
if (!(d & 0x3)) { d>>=2; s+=2; }
|
||||
if (!(d & 0x1)) { d>>=1; s+=1; }
|
||||
unsigned long long b=pow_mod(a,d,n);
|
||||
if ((b==1) || (b==(n-1))) return 1;
|
||||
unsigned char r;
|
||||
for (r=1; r<s; r++) {
|
||||
b=mul_mod(b,b,n);
|
||||
if (b<=1) return 0;
|
||||
if (b==(n-1)) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static inline unsigned is_prime(unsigned long long n) {
|
||||
if (n<2||!(n&1)) return 0;
|
||||
if (n<4) return 1;
|
||||
if (!sprp(n,2)) return 0;
|
||||
if (n<2047) return 1;
|
||||
if (!sprp(n,3)) return 0;
|
||||
if (!sprp(n,5)) return 0;
|
||||
if (!sprp(n,7)) return 0;
|
||||
if (!sprp(n,11)) return 0;
|
||||
if (!sprp(n,13)) return 0;
|
||||
if (!sprp(n,17)) return 0;
|
||||
if (!sprp(n,19)) return 0;
|
||||
if (!sprp(n,23)) return 0;
|
||||
if (!sprp(n,29)) return 0;
|
||||
if (!sprp(n,31)) return 0;
|
||||
if (!sprp(n,37)) return 0;
|
||||
return 1;
|
||||
}
|
||||
//make your own secret
|
||||
static inline void make_secret(uint64_t seed, uint64_t *secret){
|
||||
uint8_t c[] = {15, 23, 27, 29, 30, 39, 43, 45, 46, 51, 53, 54, 57, 58, 60, 71, 75, 77, 78, 83, 85, 86, 89, 90, 92, 99, 101, 102, 105, 106, 108, 113, 114, 116, 120, 135, 139, 141, 142, 147, 149, 150, 153, 154, 156, 163, 165, 166, 169, 170, 172, 177, 178, 180, 184, 195, 197, 198, 201, 202, 204, 209, 210, 212, 216, 225, 226, 228, 232, 240 };
|
||||
for(size_t i=0;i<4;i++){
|
||||
uint8_t ok;
|
||||
do{
|
||||
ok=1; secret[i]=0;
|
||||
for(size_t j=0;j<64;j+=8) secret[i]|=((uint64_t)c[wyrand(&seed)%sizeof(c)])<<j;
|
||||
if(secret[i]%2==0){ ok=0; continue; }
|
||||
for(size_t j=0;j<i;j++) {
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
if(__builtin_popcountll(secret[j]^secret[i])!=32){ ok=0; break; }
|
||||
#elif defined(_MSC_VER) && defined(_M_X64)
|
||||
if(_mm_popcnt_u64(secret[j]^secret[i])!=32){ ok=0; break; }
|
||||
#else
|
||||
//manual popcount
|
||||
uint64_t x = secret[j]^secret[i];
|
||||
x -= (x >> 1) & 0x5555555555555555;
|
||||
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
|
||||
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
|
||||
x = (x * 0x0101010101010101) >> 56;
|
||||
if(x!=32){ ok=0; break; }
|
||||
#endif
|
||||
}
|
||||
if(ok&&!is_prime(secret[i])) ok=0;
|
||||
}while(!ok);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* The Unlicense
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user