|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +// --------------------------------------------------------------------------- |
| 5 | +// xxHash32 primitives and helpers |
| 6 | +// |
| 7 | +// Based on the xxHash32 logic implemented in System.HashCode, which is in |
| 8 | +// turn based on the code published by Yann Collet: |
| 9 | +// https://raw.githubusercontent.com/Cyan4973/xxHash/5c174cfa4e45a42f94082dc0d4539b39696afea1/xxhash.c |
| 10 | +// |
| 11 | +// xxHash - Fast Hash algorithm |
| 12 | +// Copyright (C) 2012-2016, Yann Collet |
| 13 | +// |
| 14 | +// BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 15 | +// |
| 16 | +// Redistribution and use in source and binary forms, with or without |
| 17 | +// modification, are permitted provided that the following conditions are |
| 18 | +// met: |
| 19 | +// |
| 20 | +// * Redistributions of source code must retain the above copyright |
| 21 | +// notice, this list of conditions and the following disclaimer. |
| 22 | +// * Redistributions in binary form must reproduce the above |
| 23 | +// copyright notice, this list of conditions and the following disclaimer |
| 24 | +// in the documentation and/or other materials provided with the |
| 25 | +// distribution. |
| 26 | +// |
| 27 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | +// |
| 39 | +// You can contact the author at : |
| 40 | +// - xxHash homepage: http://www.xxhash.com |
| 41 | +// - xxHash source repository : https://github.com/Cyan4973/xxHash |
| 42 | +// --------------------------------------------------------------------------- |
| 43 | + |
| 44 | +#pragma once |
| 45 | +#include <stdlib.h> |
| 46 | +#include <type_traits> |
| 47 | +#include "clrtypes.h" |
| 48 | + |
| 49 | +struct xxHashDefaultTraits |
| 50 | +{ |
| 51 | + static uint32_t GenerateGlobalSeed(); |
| 52 | +}; |
| 53 | + |
| 54 | +// This is a port of the System.HashCode logic for computing a hashcode using the xxHash algorithm. |
| 55 | +// The traits type T must provide a static GenerateGlobalSeed() method that returns the seed value. |
| 56 | +template <typename T> |
| 57 | +class xxHash |
| 58 | +{ |
| 59 | + static_assert(std::is_same<decltype(T::GenerateGlobalSeed()), uint32_t>::value, |
| 60 | + "T must provide a static uint32_t GenerateGlobalSeed() method"); |
| 61 | + |
| 62 | + static constexpr uint32_t Prime1 = 2654435761U; |
| 63 | + static constexpr uint32_t Prime2 = 2246822519U; |
| 64 | + static constexpr uint32_t Prime3 = 3266489917U; |
| 65 | + static constexpr uint32_t Prime4 = 668265263U; |
| 66 | + static constexpr uint32_t Prime5 = 374761393U; |
| 67 | + |
| 68 | +public: // static |
| 69 | + static uint32_t MixEmptyState() |
| 70 | + { |
| 71 | + return T::GenerateGlobalSeed() + Prime5; |
| 72 | + } |
| 73 | + |
| 74 | + static uint32_t MixState(uint32_t v1, uint32_t v2, uint32_t v3, uint32_t v4) |
| 75 | + { |
| 76 | + return (uint32_t)_rotl(v1, 1) + (uint32_t)_rotl(v2, 7) + (uint32_t)_rotl(v3, 12) + (uint32_t)_rotl(v4, 18); |
| 77 | + } |
| 78 | + |
| 79 | + static uint32_t QueueRound(uint32_t hash, uint32_t queuedValue) |
| 80 | + { |
| 81 | + return ((uint32_t)_rotl((int)(hash + queuedValue * Prime3), 17)) * Prime4; |
| 82 | + } |
| 83 | + |
| 84 | + static uint32_t Round(uint32_t hash, uint32_t input) |
| 85 | + { |
| 86 | + return ((uint32_t)_rotl((int)(hash + input * Prime2), 13)) * Prime1; |
| 87 | + } |
| 88 | + |
| 89 | + static uint32_t MixFinal(uint32_t hash) |
| 90 | + { |
| 91 | + hash ^= hash >> 15; |
| 92 | + hash *= Prime2; |
| 93 | + hash ^= hash >> 13; |
| 94 | + hash *= Prime3; |
| 95 | + hash ^= hash >> 16; |
| 96 | + return hash; |
| 97 | + } |
| 98 | + |
| 99 | +private: |
| 100 | + static constexpr uint32_t MixEmptyState(uint32_t seed) |
| 101 | + { |
| 102 | + return seed + Prime5; |
| 103 | + } |
| 104 | + |
| 105 | + const uint32_t seed; |
| 106 | + uint32_t _v1; |
| 107 | + uint32_t _v2; |
| 108 | + uint32_t _v3; |
| 109 | + uint32_t _v4; |
| 110 | + uint32_t _queue1 = 0; |
| 111 | + uint32_t _queue2 = 0; |
| 112 | + uint32_t _queue3 = 0; |
| 113 | + uint32_t _length = 0; |
| 114 | + |
| 115 | +public: |
| 116 | + xxHash() |
| 117 | + : seed(T::GenerateGlobalSeed()) |
| 118 | + , _v1{seed + Prime1 + Prime2} |
| 119 | + , _v2{seed + Prime2} |
| 120 | + , _v3{seed} |
| 121 | + , _v4{seed - Prime1} |
| 122 | + { |
| 123 | + } |
| 124 | + |
| 125 | + void Add(uint32_t val) |
| 126 | + { |
| 127 | + // The original xxHash works as follows: |
| 128 | + // 0. Initialize immediately. We can't do this in a struct (no |
| 129 | + // default ctor). |
| 130 | + // 1. Accumulate blocks of length 16 (4 uints) into 4 accumulators. |
| 131 | + // 2. Accumulate remaining blocks of length 4 (1 uint) into the |
| 132 | + // hash. |
| 133 | + // 3. Accumulate remaining blocks of length 1 into the hash. |
| 134 | + |
| 135 | + // There is no need for #3 as this type only accepts ints. _queue1, |
| 136 | + // _queue2 and _queue3 are basically a buffer so that when |
| 137 | + // ToHashCode is called we can execute #2 correctly. |
| 138 | + |
| 139 | + // Storing the value of _length locally shaves of quite a few bytes |
| 140 | + // in the resulting machine code. |
| 141 | + uint32_t previousLength = _length++; |
| 142 | + uint32_t position = previousLength % 4; |
| 143 | + |
| 144 | + // Switch can't be inlined. |
| 145 | + |
| 146 | + if (position == 0) |
| 147 | + _queue1 = val; |
| 148 | + else if (position == 1) |
| 149 | + _queue2 = val; |
| 150 | + else if (position == 2) |
| 151 | + _queue3 = val; |
| 152 | + else // position == 3 |
| 153 | + { |
| 154 | + _v1 = Round(_v1, _queue1); |
| 155 | + _v2 = Round(_v2, _queue2); |
| 156 | + _v3 = Round(_v3, _queue3); |
| 157 | + _v4 = Round(_v4, val); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + void AddPointer(void* ptr) |
| 162 | + { |
| 163 | +#ifdef HOST_64BIT |
| 164 | + Add((uint32_t)(UINT_PTR)ptr); |
| 165 | + Add((uint32_t)(((UINT_PTR)ptr) >> 32)); |
| 166 | +#else |
| 167 | + Add((uint32_t)(UINT_PTR)ptr); |
| 168 | +#endif |
| 169 | + } |
| 170 | + |
| 171 | + uint32_t ToHashCode() |
| 172 | + { |
| 173 | + // Storing the value of _length locally shaves of quite a few bytes |
| 174 | + // in the resulting machine code. |
| 175 | + uint32_t length = _length; |
| 176 | + |
| 177 | + // position refers to the *next* queue position in this method, so |
| 178 | + // position == 1 means that _queue1 is populated; _queue2 would have |
| 179 | + // been populated on the next call to Add. |
| 180 | + uint32_t position = length % 4; |
| 181 | + |
| 182 | + // If the length is less than 4, _v1 to _v4 don't contain anything |
| 183 | + // yet. xxHash32 treats this differently. |
| 184 | + |
| 185 | + uint32_t hash = length < 4 ? MixEmptyState(seed) : MixState(_v1, _v2, _v3, _v4); |
| 186 | + |
| 187 | + // _length is incremented once per Add(Int32) and is therefore 4 |
| 188 | + // times too small (xxHash length is in bytes, not ints). |
| 189 | + |
| 190 | + hash += length * 4; |
| 191 | + |
| 192 | + // Mix what remains in the queue |
| 193 | + |
| 194 | + // Switch can't be inlined right now, so use as few branches as |
| 195 | + // possible by manually excluding impossible scenarios (position > 1 |
| 196 | + // is always false if position is not > 0). |
| 197 | + if (position > 0) |
| 198 | + { |
| 199 | + hash = QueueRound(hash, _queue1); |
| 200 | + if (position > 1) |
| 201 | + { |
| 202 | + hash = QueueRound(hash, _queue2); |
| 203 | + if (position > 2) |
| 204 | + hash = QueueRound(hash, _queue3); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + hash = MixFinal(hash); |
| 209 | + return hash; |
| 210 | + } |
| 211 | +}; |
| 212 | + |
| 213 | +template <typename T = xxHashDefaultTraits> |
| 214 | +inline static uint32_t MixOneValueIntoHash(uint32_t value1) |
| 215 | +{ |
| 216 | + // This matches the behavior of System.HashCode.Combine(value1) as of the time of authoring |
| 217 | + |
| 218 | + // Provide a way of diffusing bits from something with a limited |
| 219 | + // input hash space. For example, many enums only have a few |
| 220 | + // possible hashes, only using the bottom few bits of the code. Some |
| 221 | + // collections are built on the assumption that hashes are spread |
| 222 | + // over a larger space, so diffusing the bits may help the |
| 223 | + // collection work more efficiently. |
| 224 | + |
| 225 | + uint32_t hash = xxHash<T>::MixEmptyState(); |
| 226 | + hash += 4; |
| 227 | + hash = xxHash<T>::QueueRound(hash, value1); |
| 228 | + hash = xxHash<T>::MixFinal(hash); |
| 229 | + return hash; |
| 230 | +} |
| 231 | + |
| 232 | +template <typename T = xxHashDefaultTraits> |
| 233 | +inline static uint32_t CombineTwoValuesIntoHash(uint32_t value1, uint32_t value2) |
| 234 | +{ |
| 235 | + // This matches the behavior of System.HashCode.Combine(value1, value2) as of the time of authoring |
| 236 | + uint32_t hash = xxHash<T>::MixEmptyState(); |
| 237 | + hash += 8; |
| 238 | + hash = xxHash<T>::QueueRound(hash, value1); |
| 239 | + hash = xxHash<T>::QueueRound(hash, value2); |
| 240 | + hash = xxHash<T>::MixFinal(hash); |
| 241 | + return hash; |
| 242 | +} |
| 243 | + |
| 244 | +template <typename T = xxHashDefaultTraits> |
| 245 | +inline static uint32_t MixPointerIntoHash(void* ptr) |
| 246 | +{ |
| 247 | + // This matches the behavior of System.HashCode.Combine(ptr) as of the time of authoring |
| 248 | +#ifdef HOST_64BIT |
| 249 | + return CombineTwoValuesIntoHash<T>((uint32_t)(UINT_PTR)ptr, (uint32_t)(((UINT64)(UINT_PTR)ptr) >> 32)); |
| 250 | +#else |
| 251 | + return MixOneValueIntoHash<T>((uint32_t)ptr); |
| 252 | +#endif |
| 253 | +} |
0 commit comments