[util] Update constexpr hash to be FNV-1a

The constexpr_hash.h header has fallen out of use. As constexpr hashing
will be used for IDs with the service locator protocol, update these
hashes to be 32 and 64 bit FNV-1a, and replace the _h user-defined
literal with _id (a 64-bit hash), and _id8 (a 32-bit hash folded down to
8 bits). These are now in the util/hash.h header along with the runtime
hash functions.
This commit is contained in:
Justin C. Miller
2022-02-22 00:20:00 -08:00
parent 63265728d4
commit 2640cea175
6 changed files with 94 additions and 70 deletions

View File

@@ -9,7 +9,6 @@
#include <j6/syscalls.h>
#include <j6/sysconf.h>
#include <j6/types.h>
#include <util/constexpr_hash.h>
#include "io.h"
#include "serial.h"

View File

@@ -1,4 +1,4 @@
#include <util/constexpr_hash.h>
#include <util/hash.h>
#include "test_case.h"
class hash_tests :
@@ -6,19 +6,46 @@ class hash_tests :
{
};
TEST_CASE( hash_tests, equality_test )
TEST_CASE( hash_tests, equality_test64 )
{
const unsigned hash1 = static_cast<unsigned>("hash1!"_h);
const auto hash1 = static_cast<unsigned>("hash1!"_id);
CHECK( hash1 == 210, "hash gave unexpected value");
const unsigned hash2 = static_cast<unsigned>("hash1!"_h);
const auto hash2 = static_cast<unsigned>("hash1!"_id);
CHECK(hash1 == hash2, "hashes of equal strings should be equal");
const unsigned hash3 = static_cast<unsigned>("not hash1!"_h);
const auto hash3 = static_cast<unsigned>("not hash1!"_id);
CHECK(hash1 != hash3, "hashes of different strings should not be equal");
CHECK(hash3 == 37, "hash gave unexpected value");
const unsigned hash4 = static_cast<unsigned>("another thing that's longer"_h);
const auto hash4 = static_cast<unsigned>("another thing that's longer"_id);
CHECK(hash1 != hash4, "hashes of different strings should not be equal");
CHECK(hash4 == 212, "hash gave unexpected value");
}
TEST_CASE( hash_tests, equality_test8 )
{
const uint8_t type_ids[] = {
"system"_id8,
"event"_id8,
"channel"_id8,
"endpoint"_id8,
"mailbox"_id8,
"vma"_id8,
"process"_id8,
"thread"_id8
};
constexpr unsigned id_count = sizeof(type_ids) / sizeof(type_ids[0]);
const uint8_t system = "system"_id8;
CHECK_BARE( type_ids[0] == system );
for (unsigned i = 0; i < id_count; ++i) {
for (unsigned j = i + 1; j < id_count; ++j) {
CHECK_BARE( type_ids[i] != type_ids[j] );
}
}
}