|
| 1 | +/// Copyright 2012 Darren Kopp |
| 2 | +/// |
| 3 | +/// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +/// you may not use this file except in compliance with the License. |
| 5 | +/// You may obtain a copy of the License at |
| 6 | +/// |
| 7 | +/// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +/// |
| 9 | +/// Unless required by applicable law or agreed to in writing, software |
| 10 | +/// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +/// See the License for the specific language governing permissions and |
| 13 | +/// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Runtime.CompilerServices; |
| 17 | + |
| 18 | +namespace Murmur |
| 19 | +{ |
| 20 | + internal class Murmur128ManagedX86 : Murmur128 |
| 21 | + { |
| 22 | + const uint C1 = 0x239b961bU; |
| 23 | + const uint C2 = 0xab0e9789U; |
| 24 | + const uint C3 = 0x38b34ae5U; |
| 25 | + const uint C4 = 0xa1e38b93U; |
| 26 | + |
| 27 | + internal Murmur128ManagedX86(uint seed = 0) |
| 28 | + : base(seed) |
| 29 | + { |
| 30 | + Reset(); |
| 31 | + } |
| 32 | + |
| 33 | + private uint H1 { get; set; } |
| 34 | + private uint H2 { get; set; } |
| 35 | + private uint H3 { get; set; } |
| 36 | + private uint H4 { get; set; } |
| 37 | + private int Length { get; set; } |
| 38 | + |
| 39 | + private void Reset() |
| 40 | + { |
| 41 | + // initialize hash values to seed values |
| 42 | + H1 = H2 = H3 = H4 = Seed; |
| 43 | + Length = 0; |
| 44 | + } |
| 45 | + |
| 46 | + public override void Initialize() |
| 47 | + { |
| 48 | + Reset(); |
| 49 | + } |
| 50 | + |
| 51 | + protected override void HashCore(byte[] array, int ibStart, int cbSize) |
| 52 | + { |
| 53 | + // store the length of the hash (for use later) |
| 54 | + Length += cbSize; |
| 55 | + Body(array, ibStart, cbSize); |
| 56 | + } |
| 57 | + |
| 58 | +#if NETFX45 |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | +#endif |
| 61 | + private void Body(byte[] data, int start, int length) |
| 62 | + { |
| 63 | + int remainder = length & 15; |
| 64 | + int alignedLength = start + (length - remainder); |
| 65 | + for (int i = start; i < alignedLength; i += 16) |
| 66 | + { |
| 67 | + uint k1 = data.ToUInt32(i), |
| 68 | + k2 = data.ToUInt32(i + 4), |
| 69 | + k3 = data.ToUInt32(i + 8), |
| 70 | + k4 = data.ToUInt32(i + 12); |
| 71 | + |
| 72 | + H1 ^= (k1 * C1).RotateLeft(15) * C2; |
| 73 | + H1 = (H1.RotateLeft(19) + H2) * 5 + 0x561ccd1b; |
| 74 | + |
| 75 | + H2 ^= (k2 * C2).RotateLeft(16) * C3; |
| 76 | + H2 = (H2.RotateLeft(17) + H3) * 5 + 0x0bcaa747; |
| 77 | + |
| 78 | + H3 ^= (k3 * C3).RotateLeft(17) * C4; |
| 79 | + H3 = (H3.RotateLeft(15) + H4) * 5 + 0x96cd1c35; |
| 80 | + |
| 81 | + H4 ^= (k4 * C4).RotateLeft(18) * C1; |
| 82 | + H4 = (H4.RotateLeft(13) + H1) * 5 + 0x32ac3b17; |
| 83 | + } |
| 84 | + |
| 85 | + if (remainder > 0) |
| 86 | + Tail(data, alignedLength, remainder); |
| 87 | + } |
| 88 | + |
| 89 | +#if NETFX45 |
| 90 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 91 | +#endif |
| 92 | + private void Tail(byte[] tail, int position, int remainder) |
| 93 | + { |
| 94 | + // create our keys and initialize to 0 |
| 95 | + uint k1 = 0, k2 = 0, k3 = 0, k4 = 0; |
| 96 | + |
| 97 | + // determine how many bytes we have left to work with based on length |
| 98 | + switch (remainder) |
| 99 | + { |
| 100 | + case 15: k4 ^= (uint)tail[position + 14] << 16; goto case 14; |
| 101 | + case 14: k4 ^= (uint)tail[position + 13] << 8; goto case 13; |
| 102 | + case 13: k4 ^= (uint)tail[position + 12] << 0; goto case 12; |
| 103 | + case 12: k3 ^= (uint)tail[position + 11] << 24; goto case 11; |
| 104 | + case 11: k3 ^= (uint)tail[position + 10] << 16; goto case 10; |
| 105 | + case 10: k3 ^= (uint)tail[position + 9] << 8; goto case 9; |
| 106 | + case 9: k3 ^= (uint)tail[position + 8] << 0; goto case 8; |
| 107 | + case 8: k2 ^= (uint)tail[position + 7] << 24; goto case 7; |
| 108 | + case 7: k2 ^= (uint)tail[position + 6] << 16; goto case 6; |
| 109 | + case 6: k2 ^= (uint)tail[position + 5] << 8; goto case 5; |
| 110 | + case 5: k2 ^= (uint)tail[position + 4] << 0; goto case 4; |
| 111 | + case 4: k1 ^= (uint)tail[position + 3] << 24; goto case 3; |
| 112 | + case 3: k1 ^= (uint)tail[position + 2] << 16; goto case 2; |
| 113 | + case 2: k1 ^= (uint)tail[position + 1] << 8; goto case 1; |
| 114 | + case 1: k1 ^= (uint)tail[position] << 0; break; |
| 115 | + } |
| 116 | + |
| 117 | + H4 ^= (k4 * C4).RotateLeft(18) * C1; |
| 118 | + H3 ^= (k3 * C3).RotateLeft(17) * C4; |
| 119 | + H2 ^= (k2 * C2).RotateLeft(16) * C3; |
| 120 | + H1 ^= (k1 * C1).RotateLeft(15) * C2; |
| 121 | + } |
| 122 | + |
| 123 | + protected override byte[] HashFinal() |
| 124 | + { |
| 125 | + uint len = (uint)Length; |
| 126 | + H1 ^= len; H2 ^= len; H3 ^= len; H4 ^= len; |
| 127 | + |
| 128 | + H1 += (H2 + H3 + H4); |
| 129 | + H2 += H1; H3 += H1; H4 += H1; |
| 130 | + |
| 131 | + H1 = H1.FMix(); |
| 132 | + H2 = H2.FMix(); |
| 133 | + H3 = H3.FMix(); |
| 134 | + H4 = H4.FMix(); |
| 135 | + |
| 136 | + H1 += (H2 + H3 + H4); |
| 137 | + H2 += H1; H3 += H1; H4 += H1; |
| 138 | + |
| 139 | + var result = new byte[16]; |
| 140 | + Array.Copy(BitConverter.GetBytes(H1), 0, result, 0, 4); |
| 141 | + Array.Copy(BitConverter.GetBytes(H2), 0, result, 4, 4); |
| 142 | + Array.Copy(BitConverter.GetBytes(H3), 0, result, 8, 4); |
| 143 | + Array.Copy(BitConverter.GetBytes(H4), 0, result, 12, 4); |
| 144 | + |
| 145 | + return result; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments