TsnTables/Src/PHash/XXHash/XU.mqh
2026-06-25 17:29:51 -05:00

139 lines
3.7 KiB
MQL5

//+------------------------------------------------------------------+
//| XU.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property strict
#ifndef TSNTABES_SRC_PHASH_XXHASH_XU_MQH
#define TSNTABES_SRC_PHASH_XXHASH_XU_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Def.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace XU
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
ulong XXHash_Final(const uchar& str[], int i, int len, ulong hash)
{
//---
len &= 31;
//---
while(len >= 8)
{
ulong ul = XXH_GET_64(str, i);
ulong acc = 0;
XXH64_round(acc, ul, k1)
i += 8;
hash ^= k1;
hash = XXH_rotl64(hash, 27) * XXH_PRIME64_1 + XXH_PRIME64_4;
len -= 8;
}
if(len >= 4)
{
hash ^= (ulong)(XXH_GET_32(str, i)) * XXH_PRIME64_1;
i += 4;
hash = XXH_rotl64(hash, 23) * XXH_PRIME64_2 + XXH_PRIME64_3;
len -= 4;
}
while(len > 0)
{
hash ^= (str[i]) * XXH_PRIME64_5;
i++;
hash = XXH_rotl64(hash, 11) * XXH_PRIME64_1;
--len;
}
//--- Avalanche (inline)
hash ^= hash >> 33;
hash *= XXH_PRIME64_2;
hash ^= hash >> 29;
hash *= XXH_PRIME64_3;
hash ^= hash >> 32;
return hash;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
ulong XXH64(const uchar& str[], int len, ulong seed)
{
//---
ulong h64;
int i = 0;
//---
if(len >= 32) /* Process a large block of data */
{
ulong acc[4];
//---
acc[0] = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
acc[1] = seed + XXH_PRIME64_2;
acc[2] = seed;
acc[3] = seed - XXH_PRIME64_1;
//--- Consume long inline
const int bEnd = i + len;
const int limit = bEnd - 31;
// It
do
{
XXH64_round_i(acc[0], XXH_GET_64(str, i), acc[0]);
i += 8;
XXH64_round_i(acc[1], XXH_GET_64(str, i), acc[1]);
i += 8;
XXH64_round_i(acc[2], XXH_GET_64(str, i), acc[2]);
i += 8;
XXH64_round_i(acc[3], XXH_GET_64(str, i), acc[3]);
i += 8;
}
while(i < limit);
//--- parte de XXH64_mergeAccs
//h64 = XXH64_mergeAccs(acc);
//---
h64 = XXH_rotl64(acc[0], 1) + XXH_rotl64(acc[1], 7)
+ XXH_rotl64(acc[2], 12) + XXH_rotl64(acc[3], 18);
//---
h64 = XXH64_MergeRound(h64, acc[0]);
h64 = XXH64_MergeRound(h64, acc[1]);
h64 = XXH64_MergeRound(h64, acc[2]);
h64 = XXH64_MergeRound(h64, acc[3]);
}
else
{
h64 = seed + XXH_PRIME64_5;
}
//---
h64 += len;
//---
return XXHash_Final(str, i, len, h64);
}
}
}
#endif // TSNTABES_SRC_PHASH_XXHASH_XU_MQH