Feb 12, 2025

[Algorithm] Fowler–Noll–Vo (FNV) hash

static uint64_t FNVhash64(uint64_t val) {
  const uint64_t FNV_offset_basis_64 = 0xCBF29CE484222325L;
  const uint64_t FNV_prime_64 = 1099511628211L;

  uint64_t hashval = FNV_offset_basis_64;
  for (int i = 0; i < 8; i++) {
    hashval = hashval ^ (val & 0xff);
    hashval = hashval * FNV_prime_64;
    val >>= 8;
  }
  return (hashval & 0x8000000000000000) ? -hashval : hashval;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.