//
// Created by Dan Crosby on 10/13/22.
//

#ifndef EAGLE0_BYTEHASHER_HPP
#define EAGLE0_BYTEHASHER_HPP

#include <cstdint>

// FNV-1a 64-bit constants
constexpr uint64_t FNV_PRIME = 0x00000100000001B3ULL;
constexpr uint64_t FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL;

// FNV-1a algorithm: XOR first, then multiply
static inline auto MixIn(uint64_t& hash, const uint8_t byte) {
    hash ^= byte;
    hash *= FNV_PRIME;
}

// Hash an entire buffer using FNV-1a
static inline auto HashBuffer(const uint8_t* data, size_t size) -> uint64_t {
    uint64_t hash = FNV_OFFSET_BASIS;
    if (data != nullptr) {
        for (size_t i = 0; i < size; ++i) { MixIn(hash, data[i]); }
    }
    return hash;
}

#endif  // EAGLE0_BYTEHASHER_HPP
