Пример #1
0
        private static void Marvin32Mix(ref Marvin32State state, uint value)
        {
            state.Lo += value;
            state.Hi ^= state.Lo;

            state.Lo = RotateLeft(state.Lo, 20) + state.Hi;
            state.Hi = RotateLeft(state.Hi, 09) ^ state.Lo;

            state.Lo = RotateLeft(state.Lo, 27) + state.Hi;
            state.Hi = RotateLeft(state.Hi, 19);
        }
Пример #2
0
        private static int GetMarvin32HashCore(string str, int startIndex, int length, ulong seed)
        {
            // NB: This code is adapted from the CoreCLR hash code in https://github.com/dotnet/coreclr/blob/master/src/vm/marvin32.cpp
            //     and is patented by Microsoft (US 20130262421 A1).

            var state = new Marvin32State
            {
                Lo = (uint)(seed),
                Hi = (uint)(seed >> 32)
            };

            var len = length;

            for (var i = startIndex; len >= 2; i += 2, len -= 2)
            {
                Marvin32Mix(ref state, TwoInt16ToInt32LittleEndian(str, i));
            }

            uint final = 0x80;

            if (len != 0)
            {
                Debug.Assert(len == 1);

                var c = (uint)str[length - 1];

                var b1 = c & 0x00FF;
                var b2 = c >> 8;

                final = (final << 8) | b1;
                final = (final << 8) | b2;
            }

            Marvin32Mix(ref state, final);
            Marvin32Mix(ref state, 0);

            return(unchecked ((int)(state.Lo ^ state.Hi)));
        }