chore: call common djb2 implementations in game hashing funcs

This commit is contained in:
Jan Laupetin
2026-02-23 08:15:05 +01:00
parent 9502ebfc26
commit f7e0cb3c45
10 changed files with 318 additions and 164 deletions
-18
View File
@@ -2,26 +2,8 @@
#include "Utils/Pack.h"
#include <cctype>
using namespace IW4;
int Common::StringTable_HashString(const char* str)
{
if (!str)
return 0;
auto result = 0;
auto offset = 0;
while (str[offset])
{
const auto c = tolower(str[offset++]);
result = c + 31 * result;
}
return result;
}
PackedTexCoords Common::Vec2PackTexCoords(const float (&in)[2])
{
return PackedTexCoords{pack32::Vec2PackTexCoordsVU(in)};
+20 -12
View File
@@ -1,33 +1,41 @@
#pragma once
#include "IW4.h"
#include "Utils/Djb2.h"
#include <cctype>
namespace IW4
{
class Common
{
public:
static constexpr uint32_t R_HashString(const char* string, const uint32_t hash)
static constexpr int StringTable_HashString(const char* str)
{
const char* v2 = string; // edx@1
char v3 = *string; // cl@1
uint32_t result = hash;
if (!str)
return 0;
for (; *v2; v3 = *v2)
{
++v2;
result = 33 * result ^ (v3 | 0x20);
}
return result;
// Lets do djb2 with 31 instead of 33 because why not
// and leave out the starting value while we are at it
uint32_t hash = 0;
for (char c = *str; c; c = *++str)
hash = hash * 31 + std::tolower(c);
return static_cast<int>(hash);
}
static constexpr uint32_t R_HashString(const char* str, const uint32_t hash)
{
return djb2_xor_nocase(str, hash);
}
static constexpr uint32_t R_HashString(const char* string)
{
// Using djb2 with a 0 starting value makes a worse hash func apparently
// but who am I to judge
return R_HashString(string, 0u);
}
static int StringTable_HashString(const char* str);
static PackedTexCoords Vec2PackTexCoords(const float (&in)[2]);
static PackedUnitVec Vec3PackUnitVec(const float (&in)[3]);
static GfxColor Vec4PackGfxColor(const float (&in)[4]);