Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to rapidhash #17422

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/actions/spelling/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
charinfo
CHARSETINFO
chh
CHPE
chshdng
CHT
CLASSSTRING
Expand Down Expand Up @@ -783,7 +784,7 @@
Hostx
HPA
hpcon
HPCON

Check warning on line 787 in .github/actions/spelling/expect/expect.txt

View workflow job for this annotation

GitHub Actions / Spell checking

`HPCON` is ignored by check spelling because another more general variant is also in expect. (ignored-expect-variant)
hpen
HPR
HProvider
Expand Down Expand Up @@ -857,6 +858,7 @@
INTERCEPTCOPYPASTE
INTERNALNAME
Interner
intrin
intsafe
INVALIDARG
INVALIDATERECT
Expand Down Expand Up @@ -1336,6 +1338,7 @@
pixelheight
PIXELSLIST
PJOBOBJECT
plast
platforming
playsound
ploc
Expand Down Expand Up @@ -1440,6 +1443,8 @@
qzmp
RAII
RALT
RAPIDHASH

Check warning on line 1446 in .github/actions/spelling/expect/expect.txt

View workflow job for this annotation

GitHub Actions / Spell checking

`RAPIDHASH` is ignored by check spelling because another more general variant is also in expect. (ignored-expect-variant)
rapidhash
rasterbar
rasterfont
rasterization
Expand Down Expand Up @@ -1690,6 +1695,7 @@
stdc
stdcpp
STDEXT
stdint
STDMETHODCALLTYPE
STDMETHODIMP
STGM
Expand Down Expand Up @@ -2127,6 +2133,7 @@
XManifest
XMath
xorg
Xors
XPan
XResource
xsi
Expand Down
34 changes: 34 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,40 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
```

## rapidhash

**Source**: [https://github.com/Nicoshev/rapidhash](https://github.com/Nicoshev/rapidhash)

### License

```
Copyright (C) 2024 Nicolas De Carli
BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

## stb

**Source**: [https://github.com/nothings/stb](https://github.com/nothings/stb)
Expand Down
34 changes: 34 additions & 0 deletions oss/rapidhash/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* rapidhash - Very fast, high quality, platform independant hashing algorithm.
* Copyright (C) 2024 Nicolas De Carli
*
* Based on 'wyhash', by Wang Yi <[email protected]>
*
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at:
* - rapidhash source repository: https://github.com/Nicoshev/rapidhash
*/
4 changes: 4 additions & 0 deletions oss/rapidhash/MAINTAINER_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Notes for Future Maintainers

[rapidhash](https://github.com/Nicoshev/rapidhash) is used as the 64-bit hash algorithm for `<til/hash.h>` and its `til::hasher`.
The source code can be found in `/src/inc/til/rapidhash.h`.
15 changes: 15 additions & 0 deletions oss/rapidhash/cgmanifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/component-detection-manifest.json",
"Registrations": [
{
"component": {
"type": "git",
"git": {
"repositoryUrl": "https://github.com/Nicoshev/rapidhash",
"commitHash": "168b4bc916e2f6913780904d1422c62fd0b3d8b2"
}
}
}
],
"Version": 1
}
2 changes: 1 addition & 1 deletion oss/wyhash/MAINTAINER_README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Notes for Future Maintainers

[wyhash](https://github.com/wangyi-fudan/wyhash) is used as the hash algorithm for `<til/hash.h>` and its `til::hasher`.
[wyhash](https://github.com/wangyi-fudan/wyhash) is used as the 32-bit hash algorithm for `<til/hash.h>` and its `til::hasher`.
The source code was directly integrated into that header file and can be found in `/src/inc/til/hash.h`.
4 changes: 2 additions & 2 deletions src/cascadia/UnitTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ using namespace winrt::Microsoft::Terminal::Control;
#if defined(_M_IX86)
#define SEND_INPUT_ARCH_SPECIFIC_ACTION_HASH "56911147"
#else
#define SEND_INPUT_ARCH_SPECIFIC_ACTION_HASH "A020D2"
#define SEND_INPUT_ARCH_SPECIFIC_ACTION_HASH "96E19A0C"
#endif

#if defined(_M_IX86)
#define SEND_INPUT2_ARCH_SPECIFIC_ACTION_HASH "35488AA6"
#else
#define SEND_INPUT2_ARCH_SPECIFIC_ACTION_HASH "58D1971"
#define SEND_INPUT2_ARCH_SPECIFIC_ACTION_HASH "F40ECD1A"
#endif

namespace SettingsModelUnitTests
Expand Down
108 changes: 14 additions & 94 deletions src/inc/til/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#error "Unsupported architecture for til::hash"
#endif

#if !defined(TIL_HASH_32BIT)
#include "rapidhash.h"
#endif

namespace til
{
template<typename T>
Expand All @@ -49,7 +53,11 @@ namespace til

hasher& write(const void* data, size_t len) noexcept
{
_hash = _wyhash(data, len, _hash);
#if defined(TIL_HASH_32BIT)
_hash = _wyhash32(data, len, _hash);
#else
_hash = rapidhash_withSeed(data, len, _hash);
#endif
return *this;
}

Expand Down Expand Up @@ -81,7 +89,7 @@ namespace til
*b = static_cast<uint32_t>(c >> 32);
}

static uint32_t _wyhash(const void* data, uint32_t len, uint32_t seed) noexcept
static uint32_t _wyhash32(const void* data, uint32_t len, uint32_t seed) noexcept
{
auto p = static_cast<const uint8_t*>(data);
auto i = len;
Expand Down Expand Up @@ -109,101 +117,13 @@ namespace til
return seed ^ see1;
}

#else // defined(TIL_HASH_32BIT)

static uint64_t _wyr3(const uint8_t* p, size_t k) noexcept
{
return (static_cast<uint64_t>(p[0]) << 16) | (static_cast<uint64_t>(p[k >> 1]) << 8) | p[k - 1];
}

static uint64_t _wyr4(const uint8_t* p) noexcept
{
uint32_t v;
memcpy(&v, p, 4);
return v;
}

static uint64_t _wyr8(const uint8_t* p) noexcept
{
uint64_t v;
memcpy(&v, p, 8);
return v;
}

static uint64_t _wymix(uint64_t lhs, uint64_t rhs) noexcept
{
#if defined(TIL_HASH_X64)
uint64_t hi;
uint64_t lo = _umul128(lhs, rhs, &hi);
#elif defined(TIL_HASH_ARM64)
const uint64_t lo = lhs * rhs;
const uint64_t hi = __umulh(lhs, rhs);
#endif
return lo ^ hi;
}

static uint64_t _wyhash(const void* data, uint64_t len, uint64_t seed) noexcept
{
static constexpr auto s0 = UINT64_C(0xa0761d6478bd642f);
static constexpr auto s1 = UINT64_C(0xe7037ed1a0b428db);
static constexpr auto s2 = UINT64_C(0x8ebc6af09c88c6e3);
static constexpr auto s3 = UINT64_C(0x589965cc75374cc3);

auto p = static_cast<const uint8_t*>(data);
seed ^= s0;
uint64_t a;
uint64_t b;

if (len <= 16)
{
if (len >= 4)
{
a = (_wyr4(p) << 32) | _wyr4(p + ((len >> 3) << 2));
b = (_wyr4(p + len - 4) << 32) | _wyr4(p + len - 4 - ((len >> 3) << 2));
}
else if (len > 0)
{
a = _wyr3(p, len);
b = 0;
}
else
{
a = b = 0;
}
}
else
{
auto i = len;
if (i > 48)
{
auto seed1 = seed;
auto seed2 = seed;
do
{
seed = _wymix(_wyr8(p) ^ s1, _wyr8(p + 8) ^ seed);
seed1 = _wymix(_wyr8(p + 16) ^ s2, _wyr8(p + 24) ^ seed1);
seed2 = _wymix(_wyr8(p + 32) ^ s3, _wyr8(p + 40) ^ seed2);
p += 48;
i -= 48;
} while (i > 48);
seed ^= seed1 ^ seed2;
}
while (i > 16)
{
seed = _wymix(_wyr8(p) ^ s1, _wyr8(p + 8) ^ seed);
i -= 16;
p += 16;
}
a = _wyr8(p + i - 16);
b = _wyr8(p + i - 8);
}

return _wymix(s1 ^ len, _wymix(a ^ s1, b ^ seed));
}

#endif // defined(TIL_HASH_32BIT)

#if defined(TIL_HASH_32BIT)
size_t _hash = 0;
#else
size_t _hash = RAPID_SEED;
#endif
};

namespace details
Expand Down
Loading
Loading