Skip to content

Commit

Permalink
hash: more work (also add meowhash)
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Jun 9, 2024
1 parent 9af2058 commit 62ed07f
Show file tree
Hide file tree
Showing 10 changed files with 919 additions and 67 deletions.
3 changes: 2 additions & 1 deletion cmake/msvc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ endfunction()

if(DEFINED floormat-64bit)
if(floormat-64bit)
set(floormat-simd "AVX")
set(floormat-simd "AVX ")
else()
set(floormat-simd "SSE2")
endif()
Expand All @@ -100,6 +100,7 @@ set(_CXXFLAGS "${_CFLAGS} -Zc:throwingNew -Zc:lambda")
set(_CFLAGS_RELEASE "-O2 -Oit -Oy -Ox -Ob3 -fp:fast -GS- -GF -GL -Gw -Gy")
if(NOT floormat-simd STREQUAL "")
set(_CFLAGS_RELEASE "${_CFLAGS_RELEASE} -arch:${floormat-simd}")
set(_CFLAGS_DEBUG "${_CFLAGS_DEBUG} -arch:${floormat-simd}")
endif()
set(_CFLAGS_DEBUG "-guard:cf -MDd -RTCsu")
set(_CXXFLAGS_RELEASE "${_CFLAGS_RELEASE}")
Expand Down
16 changes: 16 additions & 0 deletions hash/hash-impl.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
#pragma once
#include "compat/defs.hpp"

#if (defined __x86_64__ || defined __i386__ || defined _M_AMD64 || defined _M_IX86) && \
(defined __AES__ || defined _MSC_VER && defined __AVX__)
#define FM_HASH_HAVE_MEOWHASH 1
#else
#define FM_HASH_HAVE_MEOWHASH 0
#endif

#if FM_HASH_HAVE_MEOWHASH
namespace floormat::meow_hash {
size_t hash_buf(const void* __restrict buf, size_t size) noexcept;
size_t hash_int(uint32_t x) noexcept;
size_t hash_int(uint64_t x) noexcept;
} // namespace floormat::meow_hash
#endif

namespace floormat::xxHash {
size_t hash_buf(const void* __restrict buf, size_t size) noexcept;
Expand Down
10 changes: 6 additions & 4 deletions hash/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <cr/StringView.h>

namespace floormat {
namespace Hash = xxHash;

using Hash::hash_buf;
using Hash::hash_int;
namespace Hash_int = xxHash;
namespace Hash_str = xxHash;

using Hash_int::hash_int;
using Hash_str::hash_buf;

struct hash_string_view { [[nodiscard]] CORRADE_ALWAYS_INLINE size_t operator()(StringView str) const noexcept; };
size_t hash_string_view::operator()(StringView str) const noexcept { return hash_buf(str.data(), str.size()); }
size_t hash_string_view::operator()(StringView str) const noexcept { return Hash_str::hash_buf(str.data(), str.size()); }

} // namespace floormat
19 changes: 19 additions & 0 deletions hash/meow_hash-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
zlib License

(C) Copyright 2018 Molly Rocket, Inc.

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
48 changes: 48 additions & 0 deletions hash/meow_hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "hash-impl.hpp"
#if FM_HASH_HAVE_MEOWHASH
#include "meow_hash_x64_aesni.h"
#include "compat/assert.hpp"

#ifdef __GNUG__
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wunused-macros"
#endif

#define fm_MeowU64From(A, I) (*(meow_u64 *)&(A) + (I))

namespace floormat::meow_hash {
namespace {

CORRADE_ALWAYS_INLINE size_t hash(void* __restrict buf, size_t size)
{
#ifndef fm_NO_DEBUG
if (size % 16 != 0) [[unlikely]]
fm_abort("size %zu %% 16 == %zu", size, size % 16);
#endif
meow_u128 hash = MeowHash(MeowDefaultSeed, size, buf);
if constexpr(sizeof nullptr > 4)
return fm_MeowU64From(hash, 0);
else
return MeowU32From(hash, 0);
}

} // namespace

size_t hash_buf(const void* __restrict buf, size_t size) noexcept
{
return hash(const_cast<void*>(buf), size);
}

size_t hash_int(uint32_t x) noexcept
{
return hash(&x, sizeof x);
}

size_t hash_int(uint64_t x) noexcept
{
return hash(&x, sizeof x);
}

} // namespace floormat::meow_hash

#endif
Loading

0 comments on commit 62ed07f

Please sign in to comment.