Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Hash.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include "Types.hpp"
6
7#include <cstring>
8#include <string_view>
9
10namespace esengine::hash {
11
12inline constexpr u64 kFnv64Offset = 0xcbf29ce484222325ull;
13inline constexpr u64 kFnv64Prime = 0x100000001b3ull;
14inline constexpr u32 kFnv32Offset = 0x811c9dc5u;
15inline constexpr u32 kFnv32Prime = 0x01000193u;
16
17constexpr u64 fnv1aWord(u64 h, u64 word) {
18 return (h ^ word) * kFnv64Prime;
19}
20
21inline u64 fnv1aBytes(u64 h, const void* data, usize bytes) {
22 const auto* p = static_cast<const u8*>(data);
23 for (usize i = 0; i < bytes; ++i) h = (h ^ p[i]) * kFnv64Prime;
24 return h;
25}
26
27constexpr u32 fnv1a32(std::string_view text) {
28 u32 h = kFnv32Offset;
29 for (const char c : text) h = (h ^ static_cast<u8>(c)) * kFnv32Prime;
30 return h;
31}
32
33constexpr u64 combine(u64 h, u64 v) {
34 return h ^ (v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2));
35}
36
37inline u64 combineWords(u64 h, const void* data, usize bytes) {
38 const auto* p = static_cast<const u8*>(data);
39 usize at = 0;
40 for (; at + 8 <= bytes; at += 8) {
41 u64 w;
42 std::memcpy(&w, p + at, 8);
43 h = combine(h, w);
44 }
45 if (at < bytes) {
46 u64 tail = 0;
47 std::memcpy(&tail, p + at, bytes - at);
48 h = combine(h, tail);
49 }
50 return h;
51}
52
53} // namespace esengine::hash
Core type definitions and utilities for ESEngine.
constexpr u64 fnv1aWord(u64 h, u64 word)
Definition Hash.hpp:17
constexpr u32 kFnv32Prime
Definition Hash.hpp:15
constexpr u32 kFnv32Offset
Definition Hash.hpp:14
u64 fnv1aBytes(u64 h, const void *data, usize bytes)
Definition Hash.hpp:21
constexpr u64 combine(u64 h, u64 v)
Definition Hash.hpp:33
constexpr u64 kFnv64Prime
Definition Hash.hpp:13
constexpr u64 kFnv64Offset
Definition Hash.hpp:12
constexpr u32 fnv1a32(std::string_view text)
Definition Hash.hpp:27
u64 combineWords(u64 h, const void *data, usize bytes)
Definition Hash.hpp:37
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41