readability code clean-up, added 64-bit hash (#538)

This commit is contained in:
Logan oos Even 2020-12-19 17:12:14 +05:45 committed by GitHub
parent 599e424b5d
commit df716c9da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 311 additions and 244 deletions

View File

@ -21,17 +21,21 @@
#include <stdint.h>
#if defined (__SSSE3__) && defined (__AES__) // AES-NI & SSSE3 -----------------------------
#if defined (__SSSE3__) && defined (__AES__) // AES-NI & SSSE3 ----------------------------------------------------
#include <immintrin.h>
#endif // AES-NI & SSSE3 -------------------------------------------------------------------
#endif // AES-NI & SSSE3 ------------------------------------------------------------------------------------------
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len);
void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len);
uint64_t pearson_hash_64 (const uint8_t *in, size_t len);
uint32_t pearson_hash_32 (const uint8_t *in, size_t len);
uint16_t pearson_hash_16 (const uint8_t *in, size_t len);

View File

@ -16,6 +16,7 @@
*
*/
// taken from https://github.com/Logan007/pearson
// This is free and unencumbered software released into the public domain.
@ -65,7 +66,7 @@ static const uint8_t t[256] ={
*/
#if defined (__AES__) && defined (__SSSE3__) // AES-NI & SSSE3 ----------------------------
#if defined (__AES__) && defined (__SSSE3__) // AES-NI & SSSE3 ----------------------------------------------------
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
@ -83,16 +84,18 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
__m128i hash_mask = _mm_set_epi64((__m64)lower_hash_mask, (__m64)upper_hash_mask);
__m128i high_hash_mask = _mm_xor_si128(tmp, hash_mask);
__m128i hash= _mm_setzero_si128();
__m128i high_hash= _mm_setzero_si128(); hash;
__m128i high_hash= _mm_setzero_si128();
__m128i ZERO = _mm_setzero_si128();
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00);
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
__m128i cc = _mm_set1_epi8(in[i]);
// xor into hash
hash = _mm_xor_si128(hash, cc);
high_hash = _mm_xor_si128(high_hash, cc);
// make them different permutations
hash = _mm_xor_si128(hash, hash_mask);
high_hash = _mm_xor_si128(high_hash, high_hash_mask);
// table lookup
@ -101,6 +104,7 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
hash = _mm_aesenclast_si128(hash, ZERO);
high_hash = _mm_aesenclast_si128(high_hash, ZERO);
}
// store output
_mm_store_si128((__m128i*)out , high_hash);
_mm_store_si128((__m128i*)&out[16] , hash);
@ -118,27 +122,29 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
uint64_t lower_hash_mask = *(uint64_t*)&lower;
__m128i hash_mask = _mm_set_epi64((__m64)lower_hash_mask, (__m64)upper_hash_mask);
__m128i hash = _mm_setzero_si128(); // _mm_xor_si128 (hash, hash);
__m128i hash = _mm_setzero_si128();
__m128i ZERO = _mm_setzero_si128();
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00);
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
__m128i cc = _mm_set1_epi8(in[i]);
// xor into hash
hash = _mm_xor_si128(hash, cc);
// make them different permutations
hash = _mm_xor_si128(hash, hash_mask);
// table lookup
hash = _mm_shuffle_epi8(hash, ISOLATE_SBOX_MASK); // re-order along AES round
hash = _mm_aesenclast_si128(hash, ZERO);
}
// store output
_mm_store_si128((__m128i*)out , hash);
}
// so far, only used internally
static uint64_t pearson_hash_64 (const uint8_t *in, size_t len) {
uint64_t pearson_hash_64 (const uint8_t *in, size_t len) {
size_t i;
@ -150,32 +156,37 @@ static uint64_t pearson_hash_64 (const uint8_t *in, size_t len) {
__m128i ISOLATE_SBOX_MASK = _mm_set_epi32(0x0306090C, 0x0F020508, 0x0B0E0104, 0x070A0D00);
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
__m128i cc = _mm_set1_epi8(in[i]);
// xor into hash
hash = _mm_xor_si128(hash, cc);
// make them different permutations
hash = _mm_xor_si128(hash, hash_mask);
// table lookup
hash = _mm_shuffle_epi8(hash, ISOLATE_SBOX_MASK); // re-order along AES round
hash = _mm_aesenclast_si128(hash, ZERO);
}
// store output
// return lower 64 bits
return _mm_cvtsi128_si64(hash);
}
uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
// return lower 32 bits (type casted)
return pearson_hash_64(in, len);
}
uint16_t pearson_hash_16 (const uint8_t *in, size_t len) {
// return lower 16 bits (type casted)
return pearson_hash_64(in, len);
}
#else // plain C --------------------------------------------------------------------------
#else // plain C --------------------------------------------------------------------------------------------------
static uint16_t t16[65536]; // 16-bit look-up table
@ -205,16 +216,16 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
uint64_t high_lower_hash = 0;
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
uint64_t c = (uint8_t)in[i];
c |= c << 8;
c |= c << 16;
c |= c << 32;
// xor into hash & make them different permutations
upper_hash ^= c ^ upper_hash_mask;
lower_hash ^= c ^ lower_hash_mask;
high_upper_hash ^= c ^ high_upper_hash_mask;
high_lower_hash ^= c ^ high_lower_hash_mask;
// table lookup
uint64_t h = 0;
uint16_t x;
@ -245,6 +256,7 @@ void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len) {
x = high_lower_hash; x = t16[x]; high_lower_hash >>= 16; h |= x; h=ROR64(h,16);
high_lower_hash = h;
}
// store output
uint64_t *o;
o = (uint64_t*)&out[0];
@ -275,11 +287,12 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
uint64_t lower_hash = 0;
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
uint64_t c = (uint8_t)in[i];
c |= c << 8;
c |= c << 16;
c |= c << 32;
// xor into hash, make them different permutations
upper_hash ^= c ^ upper_hash_mask;
lower_hash ^= c ^ lower_hash_mask;
// table lookup
@ -298,6 +311,7 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
x = lower_hash; x = t16[x]; lower_hash >>= 16; h |= x; h=ROR64(h,16);
lower_hash = h;
}
// store output
uint64_t *o;
o = (uint64_t*)&out[0];
@ -307,6 +321,49 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
}
// 64-bit hash: the return value has to be interpreted as uint64_t and
// follows machine-specific endianess in memory
uint64_t pearson_hash_64 (const uint8_t *in, size_t len) {
size_t i;
uint64_t hash_mask = 0x0706050403020100;
uint64_t hash = 0;
uint32_t h1 = 0;
uint32_t h2 = 0;
uint32_t hash1 = hash;
uint32_t hash2 = (hash >> 32);
uint8_t x = 0;
uint8_t y = 0;
for(i = 0; i < len; i++) {
// broadcast the character
uint64_t c = (uint8_t)in[i];
c |= c << 8;
c |= c << 16;
c |= c << 32;
// into hash, make them different permutations
hash ^= c ^ hash_mask;
// table lookup
x = hash1; x = t[x]; hash1 >>= 8; h1 = x; h1 = ROR32 (h1, 8);
x = hash1; x = t[x]; hash1 >>= 8; h1 |= x; h1 = ROR32 (h1, 8);
x = hash1; x = t[x]; hash1 >>= 8; h1 |= x; h1 = ROR32 (h1, 8);
x = hash1; x = t[x]; ; h1 |= x; h1 = ROR32 (h1, 8);
hash1 = h1;
y = hash2; y = t[y]; hash2 >>= 8; h2 = y; h2 = ROR32 (h2, 8);
y = hash2; y = t[y]; hash2 >>= 8; h2 |= y; h2 = ROR32 (h2, 8);
y = hash2; y = t[y]; hash2 >>= 8; h2 |= y; h2 = ROR32 (h2, 8);
y = hash2; y = t[y]; ; h2 |= y; h2 = ROR32 (h2, 8);
hash2 = h2;
hash = h1 | ((uint64_t)h2 << 32);
}
// return value
return hash;
}
// 32-bit hash: the return value has to be interpreted as uint32_t and
// follows machine-specific endianess in memory
uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
@ -316,10 +373,11 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
uint32_t hash_mask = 0x03020100;
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
uint32_t c = (uint8_t)in[i];
c |= c << 8;
c |= c << 16;
// xor into hash, make them different permutations
hash ^= c ^ hash_mask;
// table lookup
uint32_t h = 0;
@ -330,7 +388,8 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
x = hash; x = t[x]; hash >>= 8; h |= x; h=ROR32(h,8);
hash = h;
}
// output
// return value
return hash;
}
@ -338,19 +397,22 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len) {
// 16-bit hash: the return value has to be interpreted as uint16_t and
// follows machine-specific endianess in memory
uint16_t pearson_hash_16 (const uint8_t *in, size_t len) {
size_t i;
uint16_t hash = 0;
uint16_t hash_mask = 0x0100;
for(i = 0; i < len; i++) {
// broadcast the character, xor into hash, make them different permutations
// broadcast the character
uint16_t c = (uint8_t)in[i];
c |= c << 8;
// xor into hash, make them different permutations
hash ^= c ^ hash_mask;
// table lookup
hash = t[(uint8_t)hash] + (t[hash >> 8] << 8);
}
// output
// return value
return hash;
}
@ -361,10 +423,11 @@ uint16_t pearson_hash_16 (const uint8_t *in, size_t len) {
void pearson_hash_init () {
#if defined (__AES__) && (__SSSE3__)
// no initialization required for SSSE/AES-NI
#else
size_t i;
// lookup table for 16-bit lookups
for(i = 0; i < 65536; i++)
t16[i] = (t[i >> 8] << 8) + t[(uint8_t)i];
#endif