Skip to content

Commit

Permalink
refactor: unroll hash value
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN authored and ShrBox committed May 31, 2024
1 parent b99e41f commit 6de9ecf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 47 deletions.
11 changes: 9 additions & 2 deletions src/ll/api/utils/HashUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <type_traits>
#include <vector>

#include "ll/api/base/Meta.h"
#include "ll/api/base/StdInt.h"

namespace ll::inline utils::hash_utils {
Expand All @@ -26,7 +27,7 @@ constexpr void hashCombine(T const& v, size_t& seed) {
uint64 hash = 0xcbf29ce484222325;
constexpr uint64 prime = 0x100000001b3;
for (char c : x) {
hash = hash ^ c;
hash ^= c;
hash *= prime;
}
return hash;
Expand All @@ -43,7 +44,13 @@ constexpr void hashCombine(T const& v, size_t& seed) {
template <class T>
requires(std::is_trivially_destructible_v<T>)
[[nodiscard]] constexpr uint64 rawHashType(T const& v) {
return doHash2({reinterpret_cast<char const*>(std::addressof(v)), sizeof(T)});
uint64 hash = 0xcbf29ce484222325;
constexpr uint64 prime = 0x100000001b3;
meta::unroll<sizeof(T)>([&](size_t i) {
hash ^= reinterpret_cast<char const*>(std::addressof(v))[i];
hash *= prime;
});
return hash;
}

template <class T>
Expand Down
2 changes: 1 addition & 1 deletion src/mc/math/vector/base/VectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct LL_EBO VectorBase : concepts::VectorBaseTag {
[[nodiscard]] constexpr std::string toString() const noexcept {
std::string res("(");
forEachComponent([&]<typename axis_type>(size_t iter) constexpr {
res = std::move(std::format("{}{}", res, static_cast<T const*>(this)->template get<axis_type>(iter)));
res = std::format("{}{}", res, static_cast<T const*>(this)->template get<axis_type>(iter));
res += ((iter < size() - 1) ? ", " : ")");
});
return res;
Expand Down
50 changes: 6 additions & 44 deletions src/mc/math/vector/impl/VecImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,11 @@
} \
template <typename T> \
[[nodiscard]] constexpr T& get(size_t index) noexcept { \
if (index == 1) { \
return (T&)z; \
} \
return (T&)x; \
return (T&)((&x)[index]); \
} \
template <typename T> \
[[nodiscard]] constexpr T get(size_t index) const noexcept { \
if (index == 1) { \
return (T)z; \
} \
return (T)x; \
return (T&)((&x)[index]); \
} \
}

Expand Down Expand Up @@ -105,25 +99,11 @@
} \
template <typename T> \
[[nodiscard]] constexpr T& get(size_t index) noexcept { \
switch (index) { \
case 1: \
return (T&)y; \
case 2: \
return (T&)z; \
default: \
return (T&)x; \
} \
return (T&)((&x)[index]); \
} \
template <typename T> \
[[nodiscard]] constexpr T get(size_t index) const noexcept { \
switch (index) { \
case 1: \
return (T)y; \
case 2: \
return (T)z; \
default: \
return (T)x; \
} \
return (T&)((&x)[index]); \
} \
}

Expand Down Expand Up @@ -167,29 +147,11 @@
} \
template <typename T> \
[[nodiscard]] constexpr T& get(size_t index) noexcept { \
switch (index) { \
case 1: \
return (T&)y; \
case 2: \
return (T&)z; \
case 3: \
return (T&)w; \
default: \
return (T&)x; \
} \
return (T&)((&x)[index]); \
} \
template <typename T> \
[[nodiscard]] constexpr T get(size_t index) const noexcept { \
switch (index) { \
case 1: \
return (T)y; \
case 2: \
return (T)z; \
case 3: \
return (T)w; \
default: \
return (T)x; \
} \
return (T&)((&x)[index]); \
} \
}

Expand Down

0 comments on commit 6de9ecf

Please sign in to comment.