-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

-- Basic hashing functions

local hash = {}

local FNV32_OFFSET = 2166136261
local FNV32_PRIME = 16777619

local FNV64_OFFSET = -3750763034362895579 -- 14695981039346656037 (uint64)
local FNV64_PRIME  = 1099511628211

--- Compute fast 32-bit FNV-1a hash (no allocations)
-- @param s string Input string to hash
-- @return integer 32-bit hash value
function hash.fnv1a32(s)
	local h = FNV32_OFFSET
	for i = 1, #s do
		-- XOR next byte, then keep multiplication in exact integer range
		h = h ~ s:byte(i)
		h = (h * FNV32_PRIME) & 0xffffffff
	end
	return h
end

--- Compute fast 64-bit FNV-1a hash for Lua 5.3 (native integers)
-- @param s string Input string to hash
-- @return integer 64-bit hash value as a Lua integer
function hash.fnv1a64(s)
	local h = FNV64_OFFSET
	for i = 1, #s do
		--- XOR next byte and multiply by 64-bit FNV prime
		h = (h ~ s:byte(i)) * FNV64_PRIME
	end
	return h
end

return hash
