HashState.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.Graphics.Gpu.Shader.HashTable
  4. {
  5. /// <summary>
  6. /// State of a hash calculation.
  7. /// </summary>
  8. struct HashState
  9. {
  10. // This is using a slightly modified implementation of FastHash64.
  11. // Reference: https://github.com/ztanml/fast-hash/blob/master/fasthash.c
  12. private const ulong M = 0x880355f21e6d1965UL;
  13. private ulong _hash;
  14. private int _start;
  15. /// <summary>
  16. /// One shot hash calculation for a given data.
  17. /// </summary>
  18. /// <param name="data">Data to be hashed</param>
  19. /// <returns>Hash of the given data</returns>
  20. public static uint CalcHash(ReadOnlySpan<byte> data)
  21. {
  22. HashState state = new HashState();
  23. state.Initialize();
  24. state.Continue(data);
  25. return state.Finalize(data);
  26. }
  27. /// <summary>
  28. /// Initializes the hash state.
  29. /// </summary>
  30. public void Initialize()
  31. {
  32. _hash = 23;
  33. }
  34. /// <summary>
  35. /// Calculates the hash of the given data.
  36. /// </summary>
  37. /// <remarks>
  38. /// The full data must be passed on <paramref name="data"/>.
  39. /// If this is not the first time the method is called, then <paramref name="data"/> must start with the data passed on the last call.
  40. /// If a smaller slice of the data was already hashed before, only the additional data will be hashed.
  41. /// This can be used for additive hashing of data in chuncks.
  42. /// </remarks>
  43. /// <param name="data">Data to be hashed</param>
  44. public void Continue(ReadOnlySpan<byte> data)
  45. {
  46. ulong h = _hash;
  47. ReadOnlySpan<ulong> dataAsUlong = MemoryMarshal.Cast<byte, ulong>(data.Slice(_start));
  48. for (int i = 0; i < dataAsUlong.Length; i++)
  49. {
  50. ulong value = dataAsUlong[i];
  51. h ^= Mix(value);
  52. h *= M;
  53. }
  54. _hash = h;
  55. _start = data.Length & ~7;
  56. }
  57. /// <summary>
  58. /// Performs the hash finalization step, and returns the calculated hash.
  59. /// </summary>
  60. /// <remarks>
  61. /// The full data must be passed on <paramref name="data"/>.
  62. /// <paramref name="data"/> must start with the data passed on the last call to <see cref="Continue"/>.
  63. /// No internal state is changed, so one can still continue hashing data with <see cref="Continue"/>
  64. /// after calling this method.
  65. /// </remarks>
  66. /// <param name="data">Data to be hashed</param>
  67. /// <returns>Hash of all the data hashed with this <see cref="HashState"/></returns>
  68. public uint Finalize(ReadOnlySpan<byte> data)
  69. {
  70. ulong h = _hash;
  71. int remainder = data.Length & 7;
  72. if (remainder != 0)
  73. {
  74. ulong v = 0;
  75. for (int i = data.Length - remainder; i < data.Length; i++)
  76. {
  77. v |= (ulong)data[i] << ((i - remainder) * 8);
  78. }
  79. h ^= Mix(v);
  80. h *= M;
  81. }
  82. h = Mix(h);
  83. return (uint)(h - (h >> 32));
  84. }
  85. /// <summary>
  86. /// Hash mix function.
  87. /// </summary>
  88. /// <param name="h">Hash to mix</param>
  89. /// <returns>Mixed hash</returns>
  90. private static ulong Mix(ulong h)
  91. {
  92. h ^= h >> 23;
  93. h *= 0x2127599bf4325c37UL;
  94. h ^= h >> 47;
  95. return h;
  96. }
  97. }
  98. }