Skip to content

Commit

Permalink
✨ added hash seeding and changed hashvalue identifier to hash64
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Nov 30, 2024
1 parent d082659 commit a7da96e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
21 changes: 11 additions & 10 deletions ashura/engine/gpu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,26 @@ struct Framebuffer
gpu::Extent extent = {};
};

// [ ] use: enum to bits, and float to bits, not casted
struct SamplerHasher
{
constexpr Hash operator()(gpu::SamplerInfo const & info) const
constexpr hash64 operator()(gpu::SamplerInfo const & info) const
{
return hash_combine_n(
(Hash) info.mag_filter, (Hash) info.min_filter,
(Hash) info.mip_map_mode, (Hash) info.address_mode_u,
(Hash) info.address_mode_v, (Hash) info.address_mode_w,
(Hash) info.mip_lod_bias, (Hash) info.anisotropy_enable,
(Hash) info.max_anisotropy, (Hash) info.compare_enable,
(Hash) info.compare_op, (Hash) info.min_lod, (Hash) info.max_lod,
(Hash) info.border_color, (Hash) info.unnormalized_coordinates);
(hash64) info.mag_filter, (hash64) info.min_filter,
(hash64) info.mip_map_mode, (hash64) info.address_mode_u,
(hash64) info.address_mode_v, (hash64) info.address_mode_w,
(hash64) info.mip_lod_bias, (hash64) info.anisotropy_enable,
(hash64) info.max_anisotropy, (hash64) info.compare_enable,
(hash64) info.compare_op, (hash64) info.min_lod, (hash64) info.max_lod,
(hash64) info.border_color, (hash64) info.unnormalized_coordinates);
}
};

struct SamplerEq
{
constexpr Hash operator()(gpu::SamplerInfo const & a,
gpu::SamplerInfo const & b) const
constexpr hash64 operator()(gpu::SamplerInfo const & a,
gpu::SamplerInfo const & b) const
{
return a.mag_filter == b.mag_filter && a.mip_map_mode == b.mip_map_mode &&
a.address_mode_u == b.address_mode_u &&
Expand Down
5 changes: 3 additions & 2 deletions ashura/std/hash.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/// SPDX-License-Identifier: MIT
#include "ashura/std/hash.h"
#include "ashura/std/types.h"
#include "xxh3.h"

namespace ash
{

Hash hash_bytes(Span<u8 const> bytes)
hash64 hash_bytes(Span<u8 const> bytes, hash64 seed)
{
return XXH3_64bits(bytes.data(), bytes.size());
return XXH3_64bits_withSeed(bytes.data(), bytes.size_bytes(), seed);
}

} // namespace ash
25 changes: 20 additions & 5 deletions ashura/std/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@
namespace ash
{

constexpr Hash hash_combine(Hash hash_a, Hash hash_b)
constexpr hash64 hash_combine(hash64 hash_a, hash64 hash_b)
{
hash_a ^= hash_b + 0x9e37'79b9 + (hash_a << 6) + (hash_a >> 2);
return hash_a;
}

template <typename... H>
constexpr Hash hash_combine_n(Hash hash_a, H... hash_b)
constexpr hash64 hash_combine_n(hash64 hash_a, H... hash_b)
{
((hash_a = hash_combine(hash_a, hash_b)), ...);
return hash_a;
}

Hash hash_bytes(Span<u8 const> bytes);
hash64 hash_bytes(Span<u8 const> bytes, hash64 seed = 0);

struct StrHasher
{
Hash operator()(Span<char const> str) const
hash64 operator()(Span<char const> str) const
{
return hash_bytes(str.as_u8());
}

hash64 operator()(Span<c8 const> str) const
{
return hash_bytes(str.as_u8());
}

hash64 operator()(Span<c16 const> str) const
{
return hash_bytes(str.as_u8());
}

hash64 operator()(Span<c32 const> str) const
{
return hash_bytes(str.as_u8());
}
Expand All @@ -31,7 +46,7 @@ struct StrHasher
struct BitHasher
{
template <typename T>
Hash operator()(T const & a) const
hash64 operator()(T const & a) const
{
return hash_bytes(Span<T const>{&a, 1}.as_u8());
}
Expand Down
18 changes: 9 additions & 9 deletions ashura/std/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct [[nodiscard]] Map
return num_probes_;
}

[[nodiscard]] constexpr V * try_get(auto const & key, Hash hash) const
[[nodiscard]] constexpr V * try_get(auto const & key, hash64 hash) const
{
if (num_probes_ == 0 || num_entries_ == 0)
{
Expand All @@ -200,7 +200,7 @@ struct [[nodiscard]] Map

[[nodiscard]] constexpr V * try_get(auto const & key) const
{
Hash const hash = hasher_(key);
hash64 const hash = hasher_(key);
return try_get(key, hash);
}

Expand All @@ -216,7 +216,7 @@ struct [[nodiscard]] Map
return try_get(key) != nullptr;
}

[[nodiscard]] constexpr bool has(auto const & key, Hash hash) const
[[nodiscard]] constexpr bool has(auto const & key, hash64 hash) const
{
return try_get(key, hash) != nullptr;
}
Expand All @@ -236,7 +236,7 @@ struct [[nodiscard]] Map
{
Entry entry{static_cast<Entry &&>(src_probes[src_probe_idx])};
src_probes[src_probe_idx].~Entry();
Hash hash = hasher_(entry.key);
hash64 hash = hasher_(entry.key);
usize probe_idx = hash & (num_probes_ - 1);
Distance probe_dist = 0;
while (true)
Expand Down Expand Up @@ -339,10 +339,10 @@ struct [[nodiscard]] Map
return Err{};
}

Hash const hash = hasher_(key);
usize probe_idx = hash & (num_probes_ - 1);
usize insert_idx = USIZE_MAX;
Distance probe_dist = 0;
hash64 const hash = hasher_(key);
usize probe_idx = hash & (num_probes_ - 1);
usize insert_idx = USIZE_MAX;
Distance probe_dist = 0;
Entry entry{.key{static_cast<K &&>(key)}, .value{static_cast<V &&>(value)}};

while (true)
Expand Down Expand Up @@ -419,7 +419,7 @@ struct [[nodiscard]] Map
{
return false;
}
Hash hash = hasher_(key);
hash64 hash = hasher_(key);
usize probe_idx = hash & (num_probes_ - 1);
Distance probe_dist = 0;

Expand Down
2 changes: 1 addition & 1 deletion ashura/std/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef size_t usize;
typedef ptrdiff_t isize;
typedef uintptr_t uptr;
typedef intptr_t iptr;
typedef u64 Hash;
typedef u64 hash64;

constexpr u8 U8_MIN = 0;
constexpr u8 U8_MAX = 0xFF;
Expand Down

0 comments on commit a7da96e

Please sign in to comment.