pixwhile/thirdparty/FastHash.h

77 lines
2.4 KiB
C
Raw Normal View History

2023-05-12 09:56:22 +00:00
// https://github.com/KoneLinx/small_ECS/blob/master/FastHash.h
2023-05-12 10:32:09 +00:00
// but slightly modified to abandon ranges
2023-05-12 09:56:22 +00:00
// Copyright(C) 2021 Kobe Vrijsen <kobevrijsen@posteo.be>
//
// A simple ECS example
//
// This file is free software and distributed under the terms of the European Union
// Public Lincense as published by the European Commision; either version 1.2 of the
// License, or , at your option, any later version.
/* The MIT License
Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Source: fast-hash https://github.com/ztanml/fast-hash (26 Nov 2021)
// Changes: General uplift and providing a general constant hash for ranges
#pragma once
// Compression function for Merkle-Damgard construction.
// This function is generated using the framework provided.
//#define mix(h) ({
// (h) ^= (h) >> 23;
// (h) *= 0x2127599bf4325c37ULL;
// (h) ^= (h) >> 47; })
constexpr uint64_t FastHash(const char* str, size_t size, uint64_t seed/*, uint64_t back = {}*/)
{
auto const mix = [](uint64_t h)
{
h ^= h >> 23;
h *= 0x2127599bf4325c37ULL;
h ^= h >> 47;
return h;
};
const uint64_t m = 0x880355f21e6d1965ULL;
uint64_t h = seed ^ (size * m);
for (size_t i = 0; i < size; i++)
{
h ^= mix(static_cast<uint64_t>(str[i]));
h *= m;
}
//if (back != uint64_t())
//{
// h ^= mix(back);
// h *= m;
//}
return mix(h);
}