Example #1
0
        private static void PostProcess(ref UInt64 h, UInt64[] initValues, int dataCount, byte[] remainder)
        {
            if (dataCount >= 32)
            {
                h = initValues[0].RotateLeft(1) +
                    initValues[1].RotateLeft(7) +
                    initValues[2].RotateLeft(12) +
                    initValues[3].RotateLeft(18);


                for (var x = 0; x < initValues.Length; ++x)
                {
                    initValues[x] *= _primes64[1];
                    initValues[x] = initValues[x].RotateLeft(31);
                    initValues[x] *= _primes64[0];

                    h ^= initValues[x];
                    h = (h * _primes64[0]) + _primes64[3];
                }
            }

            h += (UInt64) dataCount;

            if (remainder != null)
            { 
                // In 8-byte chunks, process all full chunks
                for (int x = 0; x < remainder.Length / 8; ++x)
                {
                    h ^= (BitConverter.ToUInt64(remainder, x * 8) * _primes64[1]).RotateLeft(31) * _primes64[0];
                    h  = (h.RotateLeft(27) * _primes64[0]) + _primes64[3];
                }


                // Process a 4-byte chunk if it exists
                if ((remainder.Length % 8) > 4)
                {
                    h ^= ((UInt64) BitConverter.ToUInt32(remainder, remainder.Length - (remainder.Length % 8))) * _primes64[0];
                    h  = (h.RotateLeft(23) * _primes64[1]) + _primes64[2];
                }

                // Process last 4 bytes in 1-byte chunks (only runs if data.Length % 4 != 0)
                for (int x = remainder.Length - (remainder.Length % 4); x < remainder.Length; ++x)
                {
                    h ^= (UInt64) remainder[x] * _primes64[4];
                    h  = h.RotateLeft(11) * _primes64[0];
                }
            }


            h ^= h >> 33;
            h *= _primes64[1];
            h ^= h >> 29;
            h *= _primes64[2];
            h ^= h >> 32;
        }
        private static void ProcessGroup(ref UInt64 h1, ref UInt64 h2, byte[] dataGroup, int position, int length)
        {
            for (var x = position; x < position + length; x += 16)
            {
                UInt64 k1 = BitConverter.ToUInt64(dataGroup, 0);
                UInt64 k2 = BitConverter.ToUInt64(dataGroup, 8);

                k1 *= c1_128;
                k1 = k1.RotateLeft(31);
                k1 *= c2_128;
                h1 ^= k1;

                h1 = h1.RotateLeft(27);
                h1 += h2;
                h1 = (h1 * 5) + 0x52dce729;

                k2 *= c2_128;
                k2 = k2.RotateLeft(33);
                k2 *= c1_128;
                h2 ^= k2;

                h2 = h2.RotateLeft(31);
                h2 += h1;
                h2 = (h2 * 5) + 0x38495ab5;
            }
        }