-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathpalMetroHash.h
114 lines (103 loc) · 3.98 KB
/
palMetroHash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
***********************************************************************************************************************
*
* Copyright (c) 2017-2025 Advanced Micro Devices, Inc. All Rights Reserved.
*
* 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.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palMetroHash.h
* @brief PAL utility collection MetroHash namespace declarations.
***********************************************************************************************************************
*/
#pragma once
#include "metrohash.h"
#include "palAssert.h"
#include "palUtil.h"
namespace Util
{
/// Namespace containing functions that provide support for MetroHash.
namespace MetroHash
{
/// 128-bit hash structure
struct Hash
{
union
{
uint32 dwords[4]; ///< Output hash in dwords.
uint64 qwords[2]; ///< Output hash in qwords.
uint8 bytes[16]; ///< Output hash in bytes.
};
};
/// Compacts a 128-bit hash into a 64-bit one by XOR'ing the low and high 64-bits together
/// and then swapping the two 32-bit words.
///
/// @param [in] pHash 128-bit hash to be compacted.
///
/// @returns 64-bit hash value based on the inputted 128-bit hash.
constexpr uint64 Compact64(
const Hash* pHash)
{
return (static_cast<uint64>(pHash->dwords[3] ^ pHash->dwords[1]) |
(static_cast<uint64>(pHash->dwords[2] ^ pHash->dwords[0]) << 32));
}
/// Compacts a 64-bit hash checksum into a 32-bit one by XOR'ing each 32-bit chunk together.
///
/// @param [in] pHash 128-bit hash to be compacted.
///
/// @returns 32-bit hash value based on the inputted 128-bit hash.
constexpr uint32 Compact32(
const Hash* pHash)
{
return pHash->dwords[3] ^ pHash->dwords[2] ^ pHash->dwords[1] ^ pHash->dwords[0];
}
/// Compacts a 64-bit hash checksum into a 32-bit one by XOR'ing each 32-bit chunk together.
///
/// @param [in] hash 64-bit hash to be compacted.
///
/// @returns 32-bit hash value based on the inputted 64-bit hash.
constexpr uint32 Compact32(
uint64 hash)
{
return static_cast<uint32>(hash) ^ static_cast<uint32>(hash >> 32);
}
/// Hash functor.
///
/// Helpful when a 128-bit hash is being used as a key in another container.
template<typename T>
struct HashFunc
{
/// Hashes the specified 128-bit key value by XOR'ing each 32-bit chunk.
///
/// @param [in] pVoidKey Pointer to the key to be hashed.
/// @param [in] keyLen Amount of data at pVoidKey to hash, in bytes.
///
/// @returns 32-bit uint hash value.
uint32 operator()(const void* pVoidKey, uint32 keyLen) const
{
PAL_ASSERT(keyLen == sizeof(Hash));
return Compact32(static_cast<const Hash*>(pVoidKey));
}
/// No init job. Defined to be compatible with default hash func.
void Init(uint32) const { }
};
} // MetroHash
} // Util