HashCode.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. // See the LICENSE file in the project root for more information.
  4. /*
  5. The xxHash32 implementation is based on the code published by Yann Collet:
  6. https://raw.githubusercontent.com/Cyan4973/xxHash/5c174cfa4e45a42f94082dc0d4539b39696afea1/xxhash.c
  7. xxHash - Fast Hash algorithm
  8. Copyright (C) 2012-2016, Yann Collet
  9. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above
  16. copyright notice, this list of conditions and the following disclaimer
  17. in the documentation and/or other materials provided with the
  18. distribution.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. You can contact the author at :
  31. - xxHash homepage: http://www.xxhash.com
  32. - xxHash source repository : https://github.com/Cyan4973/xxHash
  33. */
  34. using System.Collections.Generic;
  35. using System.ComponentModel;
  36. using System.Runtime.CompilerServices;
  37. namespace System
  38. {
  39. // xxHash32 is used for the hash code.
  40. // https://github.com/Cyan4973/xxHash
  41. public struct HashCode
  42. {
  43. private static readonly uint s_seed = GenerateGlobalSeed();
  44. private const uint Prime1 = 2654435761U;
  45. private const uint Prime2 = 2246822519U;
  46. private const uint Prime3 = 3266489917U;
  47. private const uint Prime4 = 668265263U;
  48. private const uint Prime5 = 374761393U;
  49. private uint _v1, _v2, _v3, _v4;
  50. private uint _queue1, _queue2, _queue3;
  51. private uint _length;
  52. private static unsafe uint GenerateGlobalSeed()
  53. {
  54. uint result;
  55. Interop.GetRandomBytes((byte*)&result, sizeof(uint));
  56. return result;
  57. }
  58. public static int Combine<T1>(T1 value1)
  59. {
  60. // Provide a way of diffusing bits from something with a limited
  61. // input hash space. For example, many enums only have a few
  62. // possible hashes, only using the bottom few bits of the code. Some
  63. // collections are built on the assumption that hashes are spread
  64. // over a larger space, so diffusing the bits may help the
  65. // collection work more efficiently.
  66. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  67. uint hash = MixEmptyState();
  68. hash += 4;
  69. hash = QueueRound(hash, hc1);
  70. hash = MixFinal(hash);
  71. return (int)hash;
  72. }
  73. public static int Combine<T1, T2>(T1 value1, T2 value2)
  74. {
  75. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  76. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  77. uint hash = MixEmptyState();
  78. hash += 8;
  79. hash = QueueRound(hash, hc1);
  80. hash = QueueRound(hash, hc2);
  81. hash = MixFinal(hash);
  82. return (int)hash;
  83. }
  84. public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
  85. {
  86. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  87. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  88. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  89. uint hash = MixEmptyState();
  90. hash += 12;
  91. hash = QueueRound(hash, hc1);
  92. hash = QueueRound(hash, hc2);
  93. hash = QueueRound(hash, hc3);
  94. hash = MixFinal(hash);
  95. return (int)hash;
  96. }
  97. public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4)
  98. {
  99. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  100. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  101. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  102. var hc4 = (uint)(value4?.GetHashCode() ?? 0);
  103. Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
  104. v1 = Round(v1, hc1);
  105. v2 = Round(v2, hc2);
  106. v3 = Round(v3, hc3);
  107. v4 = Round(v4, hc4);
  108. uint hash = MixState(v1, v2, v3, v4);
  109. hash += 16;
  110. hash = MixFinal(hash);
  111. return (int)hash;
  112. }
  113. public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
  114. {
  115. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  116. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  117. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  118. var hc4 = (uint)(value4?.GetHashCode() ?? 0);
  119. var hc5 = (uint)(value5?.GetHashCode() ?? 0);
  120. Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
  121. v1 = Round(v1, hc1);
  122. v2 = Round(v2, hc2);
  123. v3 = Round(v3, hc3);
  124. v4 = Round(v4, hc4);
  125. uint hash = MixState(v1, v2, v3, v4);
  126. hash += 20;
  127. hash = QueueRound(hash, hc5);
  128. hash = MixFinal(hash);
  129. return (int)hash;
  130. }
  131. public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
  132. {
  133. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  134. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  135. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  136. var hc4 = (uint)(value4?.GetHashCode() ?? 0);
  137. var hc5 = (uint)(value5?.GetHashCode() ?? 0);
  138. var hc6 = (uint)(value6?.GetHashCode() ?? 0);
  139. Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
  140. v1 = Round(v1, hc1);
  141. v2 = Round(v2, hc2);
  142. v3 = Round(v3, hc3);
  143. v4 = Round(v4, hc4);
  144. uint hash = MixState(v1, v2, v3, v4);
  145. hash += 24;
  146. hash = QueueRound(hash, hc5);
  147. hash = QueueRound(hash, hc6);
  148. hash = MixFinal(hash);
  149. return (int)hash;
  150. }
  151. public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
  152. {
  153. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  154. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  155. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  156. var hc4 = (uint)(value4?.GetHashCode() ?? 0);
  157. var hc5 = (uint)(value5?.GetHashCode() ?? 0);
  158. var hc6 = (uint)(value6?.GetHashCode() ?? 0);
  159. var hc7 = (uint)(value7?.GetHashCode() ?? 0);
  160. Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
  161. v1 = Round(v1, hc1);
  162. v2 = Round(v2, hc2);
  163. v3 = Round(v3, hc3);
  164. v4 = Round(v4, hc4);
  165. uint hash = MixState(v1, v2, v3, v4);
  166. hash += 28;
  167. hash = QueueRound(hash, hc5);
  168. hash = QueueRound(hash, hc6);
  169. hash = QueueRound(hash, hc7);
  170. hash = MixFinal(hash);
  171. return (int)hash;
  172. }
  173. public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
  174. {
  175. var hc1 = (uint)(value1?.GetHashCode() ?? 0);
  176. var hc2 = (uint)(value2?.GetHashCode() ?? 0);
  177. var hc3 = (uint)(value3?.GetHashCode() ?? 0);
  178. var hc4 = (uint)(value4?.GetHashCode() ?? 0);
  179. var hc5 = (uint)(value5?.GetHashCode() ?? 0);
  180. var hc6 = (uint)(value6?.GetHashCode() ?? 0);
  181. var hc7 = (uint)(value7?.GetHashCode() ?? 0);
  182. var hc8 = (uint)(value8?.GetHashCode() ?? 0);
  183. Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
  184. v1 = Round(v1, hc1);
  185. v2 = Round(v2, hc2);
  186. v3 = Round(v3, hc3);
  187. v4 = Round(v4, hc4);
  188. v1 = Round(v1, hc5);
  189. v2 = Round(v2, hc6);
  190. v3 = Round(v3, hc7);
  191. v4 = Round(v4, hc8);
  192. uint hash = MixState(v1, v2, v3, v4);
  193. hash += 32;
  194. hash = MixFinal(hash);
  195. return (int)hash;
  196. }
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. private static uint Rol(uint value, int count)
  199. => (value << count) | (value >> (32 - count));
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
  202. {
  203. v1 = s_seed + Prime1 + Prime2;
  204. v2 = s_seed + Prime2;
  205. v3 = s_seed;
  206. v4 = s_seed - Prime1;
  207. }
  208. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  209. private static uint Round(uint hash, uint input)
  210. {
  211. hash += input * Prime2;
  212. hash = Rol(hash, 13);
  213. hash *= Prime1;
  214. return hash;
  215. }
  216. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  217. private static uint QueueRound(uint hash, uint queuedValue)
  218. {
  219. hash += queuedValue * Prime3;
  220. return Rol(hash, 17) * Prime4;
  221. }
  222. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  223. private static uint MixState(uint v1, uint v2, uint v3, uint v4)
  224. {
  225. return Rol(v1, 1) + Rol(v2, 7) + Rol(v3, 12) + Rol(v4, 18);
  226. }
  227. private static uint MixEmptyState()
  228. {
  229. return s_seed + Prime5;
  230. }
  231. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  232. private static uint MixFinal(uint hash)
  233. {
  234. hash ^= hash >> 15;
  235. hash *= Prime2;
  236. hash ^= hash >> 13;
  237. hash *= Prime3;
  238. hash ^= hash >> 16;
  239. return hash;
  240. }
  241. public void Add<T>(T value)
  242. {
  243. Add(value?.GetHashCode() ?? 0);
  244. }
  245. public void Add<T>(T value, IEqualityComparer<T> comparer)
  246. {
  247. Add(comparer != null ? comparer.GetHashCode(value) : (value?.GetHashCode() ?? 0));
  248. }
  249. private void Add(int value)
  250. {
  251. // The original xxHash works as follows:
  252. // 0. Initialize immediately. We can't do this in a struct (no
  253. // default ctor).
  254. // 1. Accumulate blocks of length 16 (4 uints) into 4 accumulators.
  255. // 2. Accumulate remaining blocks of length 4 (1 uint) into the
  256. // hash.
  257. // 3. Accumulate remaining blocks of length 1 into the hash.
  258. // There is no need for #3 as this type only accepts ints. _queue1,
  259. // _queue2 and _queue3 are basically a buffer so that when
  260. // ToHashCode is called we can execute #2 correctly.
  261. // We need to initialize the xxHash32 state (_v1 to _v4) lazily (see
  262. // #0) nd the last place that can be done if you look at the
  263. // original code is just before the first block of 16 bytes is mixed
  264. // in. The xxHash32 state is never used for streams containing fewer
  265. // than 16 bytes.
  266. // To see what's really going on here, have a look at the Combine
  267. // methods.
  268. var val = (uint)value;
  269. // Storing the value of _length locally shaves of quite a few bytes
  270. // in the resulting machine code.
  271. uint previousLength = _length++;
  272. uint position = previousLength % 4;
  273. // Switch can't be inlined.
  274. if (position == 0)
  275. _queue1 = val;
  276. else if (position == 1)
  277. _queue2 = val;
  278. else if (position == 2)
  279. _queue3 = val;
  280. else // position == 3
  281. {
  282. if (previousLength == 3)
  283. Initialize(out _v1, out _v2, out _v3, out _v4);
  284. _v1 = Round(_v1, _queue1);
  285. _v2 = Round(_v2, _queue2);
  286. _v3 = Round(_v3, _queue3);
  287. _v4 = Round(_v4, val);
  288. }
  289. }
  290. public int ToHashCode()
  291. {
  292. // Storing the value of _length locally shaves of quite a few bytes
  293. // in the resulting machine code.
  294. uint length = _length;
  295. // position refers to the *next* queue position in this method, so
  296. // position == 1 means that _queue1 is populated; _queue2 would have
  297. // been populated on the next call to Add.
  298. uint position = length % 4;
  299. // If the length is less than 4, _v1 to _v4 don't contain anything
  300. // yet. xxHash32 treats this differently.
  301. uint hash = length < 4 ? MixEmptyState() : MixState(_v1, _v2, _v3, _v4);
  302. // _length is incremented once per Add(Int32) and is therefore 4
  303. // times too small (xxHash length is in bytes, not ints).
  304. hash += length * 4;
  305. // Mix what remains in the queue
  306. // Switch can't be inlined right now, so use as few branches as
  307. // possible by manually excluding impossible scenarios (position > 1
  308. // is always false if position is not > 0).
  309. if (position > 0)
  310. {
  311. hash = QueueRound(hash, _queue1);
  312. if (position > 1)
  313. {
  314. hash = QueueRound(hash, _queue2);
  315. if (position > 2)
  316. hash = QueueRound(hash, _queue3);
  317. }
  318. }
  319. hash = MixFinal(hash);
  320. return (int)hash;
  321. }
  322. #pragma warning disable 0809
  323. // Obsolete member 'memberA' overrides non-obsolete member 'memberB'.
  324. // Disallowing GetHashCode and Equals is by design
  325. // * We decided to not override GetHashCode() to produce the hash code
  326. // as this would be weird, both naming-wise as well as from a
  327. // behavioral standpoint (GetHashCode() should return the object's
  328. // hash code, not the one being computed).
  329. // * Even though ToHashCode() can be called safely multiple times on
  330. // this implementation, it is not part of the contract. If the
  331. // implementation has to change in the future we don't want to worry
  332. // about people who might have incorrectly used this type.
  333. [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", error: true)]
  334. [EditorBrowsable(EditorBrowsableState.Never)]
  335. public override int GetHashCode() => throw new NotSupportedException(SR.HashCode_HashCodeNotSupported);
  336. [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", error: true)]
  337. [EditorBrowsable(EditorBrowsableState.Never)]
  338. public override bool Equals(object obj) => throw new NotSupportedException(SR.HashCode_EqualityNotSupported);
  339. #pragma warning restore 0809
  340. }
  341. }