#include <stddef.h>
#include <stdint.h>
+#include <string.h>
/* default: SipHash-2-4 */
#ifndef cROUNDS
#endif
__attribute__((always_inline))
-static inline uint64_t siphash(const void *in, const size_t inlen, const uint8_t k[16]) {
- const unsigned char *ni = (const unsigned char *)in;
+static inline uint64_t siphash(const uint64_t *in, const size_t inwords, const uint8_t k[16]) {
const unsigned char *kk = (const unsigned char *)k;
uint64_t v0 = UINT64_C(0x736f6d6570736575);
uint64_t k1 = U8TO64_LE(kk + 8);
uint64_t m;
int i;
- const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t));
- const int left = inlen & 7;
- uint64_t b = ((uint64_t)inlen) << 56;
+ size_t j;
+ uint64_t b = ((uint64_t)inwords) << (56 + 3);
v3 ^= k1;
v2 ^= k0;
v1 ^= k1;
v0 ^= k0;
- for (; ni != end; ni += 8) {
- m = U8TO64_LE(ni);
+ for (j = 0; j < inwords; ++j) {
+ m = *in;
+ in += 1;
v3 ^= m;
TRACE;
v0 ^= m;
}
- switch (left) {
- case 7:
- b |= ((uint64_t)ni[6]) << 48;
- case 6:
- b |= ((uint64_t)ni[5]) << 40;
- case 5:
- b |= ((uint64_t)ni[4]) << 32;
- case 4:
- b |= ((uint64_t)ni[3]) << 24;
- case 3:
- b |= ((uint64_t)ni[2]) << 16;
- case 2:
- b |= ((uint64_t)ni[1]) << 8;
- case 1:
- b |= ((uint64_t)ni[0]);
- break;
- case 0:
- break;
- }
-
v3 ^= b;
TRACE;
#include "rand.h"
static uint64_t siphash_uint64_t(const uint64_t in) {
- return siphash(&in, sizeof(uint64_t), COMPILE_TIME_RAND);
+ return siphash(&in, 1, COMPILE_TIME_RAND);
}
static uint64_t siphash_uint128_t(const __uint128_t in) {
- return siphash(&in, sizeof(__uint128_t), COMPILE_TIME_RAND);
+ uint64_t words[2];
+ memcpy(words, &in, sizeof(__uint128_t));
+ return siphash(words, 2, COMPILE_TIME_RAND);
}