Skip to content

Commit

Permalink
KFL: Simplify the hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
gongminmin committed Dec 25, 2023
1 parent accfba2 commit 0462bc6
Show file tree
Hide file tree
Showing 28 changed files with 341 additions and 359 deletions.
26 changes: 8 additions & 18 deletions KFL/include/KFL/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,28 @@

namespace KlayGE
{
#define PRIME_NUM 0x9e3779b9

#ifdef KLAYGE_COMPILER_MSVC
#pragma warning(disable: 4307) // The hash here could cause integral constant overflow
#endif

size_t constexpr CTHashImpl(char const* str, size_t seed) noexcept
constexpr size_t HashCombineImpl(size_t value, size_t seed) noexcept
{
return 0 == *str ? seed : CTHashImpl(str + 1, seed ^ (static_cast<size_t>(*str) + PRIME_NUM + (seed << 6) + (seed >> 2)));
size_t constexpr PRIME_NUM = 0x9e3779b9U;
return seed ^ (value + PRIME_NUM + (seed << 6) + (seed >> 2));
}

#define CT_HASH(x) (CTHashImpl(x, 0))

template <typename SizeT>
inline void HashCombineImpl(SizeT& seed, SizeT value) noexcept
constexpr size_t CtHash(char const* str, size_t seed = 0) noexcept
{
seed ^= value + PRIME_NUM + (seed << 6) + (seed >> 2);
return 0 == *str ? seed : CtHash(str + 1, HashCombineImpl(*str, seed));
}

inline size_t RT_HASH(char const* str) noexcept
inline size_t RtHash(char const* str) noexcept
{
size_t seed = 0;
while (*str != 0)
{
HashCombineImpl(seed, static_cast<size_t>(*str));
seed = HashCombineImpl(*str, seed);
++ str;
}
return seed;
}

#undef PRIME_NUM

template <typename T>
inline size_t HashValue(T v) noexcept
{
Expand All @@ -87,7 +77,7 @@ namespace KlayGE
template <typename T>
inline void HashCombine(size_t& seed, T const& v) noexcept
{
return HashCombineImpl(seed, HashValue(v));
seed = HashCombineImpl(HashValue(v), seed);
}

template <typename T>
Expand Down
40 changes: 20 additions & 20 deletions KlayGE/Core/Src/Base/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,34 +389,34 @@ namespace KlayGE
keep_screen_on = attr->ValueBool();
}

size_t const color_fmt_str_hash = RT_HASH(color_fmt_str.c_str());
if (CT_HASH("ARGB8") == color_fmt_str_hash)
size_t const color_fmt_str_hash = RtHash(color_fmt_str.c_str());
if (CtHash("ARGB8") == color_fmt_str_hash)
{
color_fmt = EF_ARGB8;
}
else if (CT_HASH("ABGR8") == color_fmt_str_hash)
else if (CtHash("ABGR8") == color_fmt_str_hash)
{
color_fmt = EF_ABGR8;
}
else if (CT_HASH("A2BGR10") == color_fmt_str_hash)
else if (CtHash("A2BGR10") == color_fmt_str_hash)
{
color_fmt = EF_A2BGR10;
}
else if (CT_HASH("ABGR16F") == color_fmt_str_hash)
else if (CtHash("ABGR16F") == color_fmt_str_hash)
{
color_fmt = EF_ABGR16F;
}

size_t const depth_stencil_fmt_str_hash = RT_HASH(depth_stencil_fmt_str.c_str());
if (CT_HASH("D16") == depth_stencil_fmt_str_hash)
size_t const depth_stencil_fmt_str_hash = RtHash(depth_stencil_fmt_str.c_str());
if (CtHash("D16") == depth_stencil_fmt_str_hash)
{
depth_stencil_fmt = EF_D16;
}
else if (CT_HASH("D24S8") == depth_stencil_fmt_str_hash)
else if (CtHash("D24S8") == depth_stencil_fmt_str_hash)
{
depth_stencil_fmt = EF_D24S8;
}
else if (CT_HASH("D32F") == depth_stencil_fmt_str_hash)
else if (CtHash("D32F") == depth_stencil_fmt_str_hash)
{
depth_stencil_fmt = EF_D32F;
}
Expand Down Expand Up @@ -477,43 +477,43 @@ namespace KlayGE
if (XMLAttribute const* attr = stereo_node->Attrib("method"))
{
size_t const method_str_hash = HashValue(attr->ValueString());
if (CT_HASH("none") == method_str_hash)
if (CtHash("none") == method_str_hash)
{
stereo_method = STM_None;
}
else if (CT_HASH("red_cyan") == method_str_hash)
else if (CtHash("red_cyan") == method_str_hash)
{
stereo_method = STM_ColorAnaglyph_RedCyan;
}
else if (CT_HASH("yellow_blue") == method_str_hash)
else if (CtHash("yellow_blue") == method_str_hash)
{
stereo_method = STM_ColorAnaglyph_YellowBlue;
}
else if (CT_HASH("green_red") == method_str_hash)
else if (CtHash("green_red") == method_str_hash)
{
stereo_method = STM_ColorAnaglyph_GreenRed;
}
else if (CT_HASH("lcd_shutter") == method_str_hash)
else if (CtHash("lcd_shutter") == method_str_hash)
{
stereo_method = STM_LCDShutter;
}
else if (CT_HASH("hor_interlacing") == method_str_hash)
else if (CtHash("hor_interlacing") == method_str_hash)
{
stereo_method = STM_HorizontalInterlacing;
}
else if (CT_HASH("ver_interlacing") == method_str_hash)
else if (CtHash("ver_interlacing") == method_str_hash)
{
stereo_method = STM_VerticalInterlacing;
}
else if (CT_HASH("horizontal") == method_str_hash)
else if (CtHash("horizontal") == method_str_hash)
{
stereo_method = STM_Horizontal;
}
else if (CT_HASH("vertical") == method_str_hash)
else if (CtHash("vertical") == method_str_hash)
{
stereo_method = STM_Vertical;
}
else if (CT_HASH("oculus_vr") == method_str_hash)
else if (CtHash("oculus_vr") == method_str_hash)
{
stereo_method = STM_OculusVR;
}
Expand All @@ -535,7 +535,7 @@ namespace KlayGE
if (XMLAttribute const* attr = output_node->Attrib("method"))
{
size_t const method_str_hash = HashValue(attr->ValueString());
if (CT_HASH("hdr10") == method_str_hash)
if (CtHash("hdr10") == method_str_hash)
{
display_output_method = DOM_HDR10;
}
Expand Down
2 changes: 1 addition & 1 deletion KlayGE/Core/Src/Base/ResLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ namespace KlayGE
local_path_ = exe_path_;
#endif

paths_.emplace_back(PathInfo{CT_HASH(""), 0U, std::filesystem::path(), PackagePtr()});
paths_.emplace_back(PathInfo{CtHash(""), 0U, std::filesystem::path(), PackagePtr()});

#if defined KLAYGE_PLATFORM_WINDOWS_STORE
this->AddPath("Assets/");
Expand Down
8 changes: 4 additions & 4 deletions KlayGE/Core/Src/Render/CameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,19 +807,19 @@ namespace KlayGE
{
size_t const type_str_hash = HashValue(curve_node->Attrib("type")->ValueString());
CameraPathController::InterpolateType type;
if (CT_HASH("linear") == type_str_hash)
if (CtHash("linear") == type_str_hash)
{
type = CameraPathController::IT_Linear;
}
else if (CT_HASH("catmull_rom") == type_str_hash)
else if (CtHash("catmull_rom") == type_str_hash)
{
type = CameraPathController::IT_CatmullRom;
}
else if (CT_HASH("b_spline") == type_str_hash)
else if (CtHash("b_spline") == type_str_hash)
{
type = CameraPathController::IT_BSpline;
}
else if (CT_HASH("bezier") == type_str_hash)
else if (CtHash("bezier") == type_str_hash)
{
type = CameraPathController::IT_Bezier;
}
Expand Down
3 changes: 1 addition & 2 deletions KlayGE/Core/Src/Render/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,7 @@ namespace

uint64_t Type() const override
{
static uint64_t const type = CT_HASH("FontLoadingDesc");
return type;
return CtHash("FontLoadingDesc");
}

bool StateLess() const override
Expand Down
3 changes: 1 addition & 2 deletions KlayGE/Core/Src/Render/Imposter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ namespace KlayGE

uint64_t Type() const override
{
static uint64_t const type = CT_HASH("ImposterLoadingDesc");
return type;
return CtHash("ImposterLoadingDesc");
}

bool StateLess() const override
Expand Down
3 changes: 1 addition & 2 deletions KlayGE/Core/Src/Render/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ namespace

uint64_t Type() const override
{
static uint64_t const type = CT_HASH("RenderModelLoadingDesc");
return type;
return CtHash("RenderModelLoadingDesc");
}

bool StateLess() const override
Expand Down
9 changes: 4 additions & 5 deletions KlayGE/Core/Src/Render/ParticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ namespace

uint64_t Type() const override
{
static uint64_t const type = CT_HASH("ParticleSystemLoadingDesc");
return type;
return CtHash("ParticleSystemLoadingDesc");
}

bool StateLess() const override
Expand Down Expand Up @@ -290,15 +289,15 @@ namespace

XMLAttribute const* attr = node->Attrib("name");
size_t const name_hash = HashValue(attr->ValueString());
if (CT_HASH("size_over_life") == name_hash)
if (CtHash("size_over_life") == name_hash)
{
ps_desc_.ps_data->size_over_life_ctrl_pts = xys;
}
else if (CT_HASH("mass_over_life") == name_hash)
else if (CtHash("mass_over_life") == name_hash)
{
ps_desc_.ps_data->mass_over_life_ctrl_pts = xys;
}
else if (CT_HASH("opacity_over_life") == name_hash)
else if (CtHash("opacity_over_life") == name_hash)
{
ps_desc_.ps_data->opacity_over_life_ctrl_pts = xys;
}
Expand Down
3 changes: 1 addition & 2 deletions KlayGE/Core/Src/Render/PostProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ namespace

uint64_t Type() const override
{
static uint64_t const type = CT_HASH("PostProcessLoadingDesc");
return type;
return CtHash("PostProcessLoadingDesc");
}

bool StateLess() const override
Expand Down
Loading

0 comments on commit 0462bc6

Please sign in to comment.