Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created July 31, 2022 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeeto/01c95fc2c0f748b2eded260662f0d63e to your computer and use it in GitHub Desktop.
Save skeeto/01c95fc2c0f748b2eded260662f0d63e to your computer and use it in GitHub Desktop.
K-HASHV benchmark
#include <stdio.h>
#include <stdint.h>
#include "khashv.h"
static uint64_t dumbhash(uint64_t s[2])
{
uint64_t x = s[0];
x += 0x3243f6a8885a308d;
x *= 1111111111111111111;
x += s[1];
x ^= x >> 33;
x *= 1111111111111111111;
x ^= x >> 33;
x *= 1111111111111111111;
x ^= x >> 33;
return x;
}
static void salsa12(uint32_t s[16])
{
#define SALSA_ROTATE(v,n) (((v) << (n)) | ((v) >> (32 - (n))))
#define SALSA_QUARTERROUND(a,b,c,d) \
x[a] += x[b]; x[d] = SALSA_ROTATE(x[d] ^ x[a],16); \
x[c] += x[d]; x[b] = SALSA_ROTATE(x[b] ^ x[c],12); \
x[a] += x[b]; x[d] = SALSA_ROTATE(x[d] ^ x[a], 8); \
x[c] += x[d]; x[b] = SALSA_ROTATE(x[b] ^ x[c], 7)
uint32_t x[16];
for (int i = 0; i < 16; ++i) {
x[i] = s[i];
}
for (int i = 12; i > 0; i -= 2) {
SALSA_QUARTERROUND( 0, 4, 8,12);
SALSA_QUARTERROUND( 1, 5, 9,13);
SALSA_QUARTERROUND( 2, 6,10,14);
SALSA_QUARTERROUND( 3, 7,11,15);
SALSA_QUARTERROUND( 0, 5,10,15);
SALSA_QUARTERROUND( 1, 6,11,12);
SALSA_QUARTERROUND( 2, 7, 8,13);
SALSA_QUARTERROUND( 3, 4, 9,14);
}
for (int i = 0; i < 16; i++) {
s[i] += x[i];
}
}
int main(void)
{
khashvSeed seed[1];
uint64_t input[2] = {0, 0};
static uint64_t buf[1<<12];
khashv_prep_seed64(seed, 0);
for (;; input[1]++) {
for (int i = 0; i < 1<<12; i++) {
input[0] = i;
#if DUMB
buf[i] = dumbhash(input);
#elif SALSA
uint32_t salsa[16] = {
0x885a308d, 0x3243f6a8,
input[0] >> 0, input[0] >> 32,
input[1] >> 0, input[1] >> 32
};
salsa12(salsa);
buf[i] = (uint64_t)salsa[0]<<32 | salsa[1];
#else
buf[i] = khashv64(seed, (void *)&input, sizeof(input));
#endif
}
if (!fwrite(buf, sizeof(buf), 1, stdout)) {
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment