UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Fnv.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
5#include "CoreTypes.h"
6#include "HAL/Platform.h"
7
8namespace UE
9{
18 template<typename HashType, typename CharType>
19 constexpr HashType HashStringFNV1a(TStringView<CharType> String)
20 {
21 static_assert(std::is_same_v<HashType, uint32> || std::is_same_v<HashType, uint64>, "HashType must be an unsigned 32 or 64 bit integer.");
22 static_assert(sizeof(CharType) <= 4, "FNV1a only works with characters up to 32 bits.");
23
24 // See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
25 constexpr HashType Offset = []() -> HashType
26 {
27 if constexpr (sizeof(HashType) == 4)
28 {
29 return 0x811c9dc5;
30 }
31 else
32 {
33 return 0xcbf29ce484222325;
34 }
35 }();
36 constexpr HashType Prime = []() -> HashType
37 {
38 if constexpr (sizeof(HashType) == 4)
39 {
40 return 0x01000193;
41 }
42 else
43 {
44 return 0x100000001b3;
45 }
46 }();
47
48 HashType Fnv = Offset;
49 for (CharType Char : String)
50 {
51 // Operate on every character as if it's 4 bytes.
52 // Characters < 4 bytes will be padded out with zeros.
53 const uint32 Ch = static_cast<uint32>(Char);
54
55 Fnv ^= (Ch >> 24) & 0xff;
56 Fnv *= Prime;
57 Fnv ^= (Ch >> 16) & 0xff;
58 Fnv *= Prime;
59 Fnv ^= (Ch >> 8) & 0xff;
60 Fnv *= Prime;
61 Fnv ^= (Ch) & 0xff;
62 Fnv *= Prime;
63 }
64
65 return Fnv;
66 }
67
68 /* Version taking a string literal. Use this version to force hashing at compile time. */
69 template<typename HashType, typename CharType, int32 Len>
70 UE_CONSTEVAL HashType HashStringFNV1a(const CharType(&StringLiteral)[Len])
71 {
72 return HashStringFNV1a<HashType>(MakeStringView(StringLiteral, Len - 1));
73 }
74
75 /* Creates a 32-bit FNV1a hash for the given string. */
76 template<typename CharType>
81
82 template<typename CharType, int32 Len>
83 UE_CONSTEVAL uint32 HashStringFNV1a32(const CharType(&StringLiteral)[Len])
84 {
85 return HashStringFNV1a<uint32>(StringLiteral);
86 }
87
88 /* Creates a 64-bit FNV1a hash for the given string. */
89 template<typename CharType>
94
95 template<typename CharType, int32 Len>
96 UE_CONSTEVAL uint64 HashStringFNV1a64(const CharType(&StringLiteral)[Len])
97 {
98 return HashStringFNV1a<uint64>(StringLiteral);
99 }
100
101} // namespace UE
#define UE_CONSTEVAL
Definition Platform.h:777
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Char
Character type.
constexpr auto MakeStringView(const CharPtrOrRangeType &CharPtrOrRange UE_LIFETIMEBOUND) -> decltype(TStringView(CharPtrOrRange))
Definition StringView.h:508
uint32 Offset
Definition VulkanMemory.cpp:4033
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition StringView.h:107
Definition AdvancedWidgetsModule.cpp:13
constexpr uint32 HashStringFNV1a32(TStringView< CharType > String)
Definition Fnv.h:77
constexpr HashType HashStringFNV1a(TStringView< CharType > String)
Definition Fnv.h:19
constexpr uint64 HashStringFNV1a64(TStringView< CharType > String)
Definition Fnv.h:90