[Endianness] Make SpookyHash endianness-agnostic.

Try and make SpookyHash endianness-agnostic using macros copied from:
https://github.com/k0dai/spookyhash
This commit is contained in:
Suresh Sundriyal 2022-10-01 11:23:17 -07:00
parent e135cf3334
commit e2cddf28b2
2 changed files with 25 additions and 6 deletions

View File

@ -54,18 +54,18 @@ void SpookyHash::Short(
// handle all complete sets of 32 bytes
for (; u.p64 < end; u.p64 += 4)
{
c += u.p64[0];
d += u.p64[1];
c += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[0]);
d += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[1]);
ShortMix(a,b,c,d);
a += u.p64[2];
b += u.p64[3];
a += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[2]);
b += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[3]);
}
//Handle the case of 16+ remaining bytes.
if (remainder >= 16)
{
c += u.p64[0];
d += u.p64[1];
c += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[0]);
d += SPOOKYHASH_LITTLE_ENDIAN_64(u.p64[1]);
ShortMix(a,b,c,d);
u.p64 += 2;
remainder -= 16;

View File

@ -45,6 +45,25 @@
typedef uint8_t uint8;
#endif
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define SPOOKYHASH_LITTLE_ENDIAN_64(b) ((uint64_t)b)
#define SPOOKYHASH_LITTLE_ENDIAN_32(b) ((uint32_t)b)
#define SPOOKYHASH_LITTLE_ENDIAN_16(b) ((uint16_t)b)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#if __GNUC__ * 100 + __GNUC_MINOR__ >= 403 || defined(__clang__)
#define SPOOKYHASH_LITTLE_ENDIAN_64(b) __builtin_bswap64(b)
#define SPOOKYHASH_LITTLE_ENDIAN_32(b) __builtin_bswap32(b)
#define SPOOKYHASH_LITTLE_ENDIAN_16(b) __builtin_bswap16(b)
#else
#warning Using bulk byte swap routines. Expect performance issues.
#define SPOOKYHASH_LITTLE_ENDIAN_64(b) ((((b) & 0xFF00000000000000ull) >> 56) | (((b) & 0x00FF000000000000ull) >> 40) | (((b) & 0x0000FF0000000000ull) >> 24) | (((b) & 0x000000FF00000000ull) >> 8) | (((b) & 0x00000000FF000000ull) << 8) | (((b) & 0x0000000000FF0000ull) << 24ull) | (((b) & 0x000000000000FF00ull) << 40) | (((b) & 0x00000000000000FFull) << 56))
#define SPOOKYHASH_LITTLE_ENDIAN_32(b) ((((b) & 0xFF000000) >> 24) | (((b) & 0x00FF0000) >> 8) | (((b) & 0x0000FF00) << 8) | (((b) & 0x000000FF) << 24))
#define SPOOKYHASH_LITTLE_ENDIAN_16(b) ((((b) & 0xFF00) >> 8) | (((b) & 0x00FF) << 8))
#endif
#else
#error Unknow endianness
#endif
class SpookyHash
{