Example #1
0
        /// <summary>
        /// Returns a non-negative, random 64-bit integer
        /// </summary>
        /// <returns>A non-negative, random 64-bit integer</returns>
        public ulong NextLong()
        {
            this.seed += WyCore.Prime0;
            var result = WyCore.Mum(this.seed ^ WyCore.Prime1, this.seed);

            return(result);
        }
Example #2
0
        /// <summary>
        /// Returns a non-negative, random 64-bit integer
        /// </summary>
        /// <returns>A non-negative, random 64-bit integer</returns>
        public ulong NextLong()
        {
            this.seed += WyCore.Prime0;
            this.seed  = WyCore.Mum(this.seed ^ WyCore.Prime1, this.seed);

            return(seed);
        }
Example #3
0
        private static unsafe ulong WyHashCore(byte[] array, int ibStart, int cbSize, ulong seed)
        {
            fixed(byte *pData = array)
            {
                byte *ptr = pData;

                var len = cbSize - ibStart;
                var p   = 0;

                for (int i = ibStart; i + 32 <= len; i += 32, p += 32)
                {
                    // Storing these in temp variables is slightly more performant (presumably it gives some kind of hint to the jitter)
                    var m1x = WyCore.Read64(ptr, p) ^ WyCore.Prime1;
                    var m1y = WyCore.Read64(ptr, p + 8) ^ WyCore.Prime2;
                    var m2x = WyCore.Read64(ptr, p + 16) ^ WyCore.Prime3;
                    var m2y = WyCore.Read64(ptr, p + 24) ^ WyCore.Prime4;

                    seed = WyCore.Mum(seed ^ WyCore.Prime0, WyCore.Mum(m1x, m1y) ^ WyCore.Mum(m2x, m2y));
                }

                seed ^= WyCore.Prime0;

                // After the loop we have between 1 and 31 bytes left to process
                switch (len & 31)
                {
                case 1:
                    seed = WyCore.Mum(seed, WyCore.Read8(ptr, p) ^ WyCore.Prime1);
                    break;

                case 2:
                    seed = WyCore.Mum(seed, WyCore.Read16(ptr, p) ^ WyCore.Prime1);
                    break;

                case 3:
                    seed = WyCore.Mum(seed, ((WyCore.Read16(ptr, p) << 8) | WyCore.Read8(ptr, p + 2)) ^ WyCore.Prime1);
                    break;

                case 4:
                    seed = WyCore.Mum(seed, WyCore.Read32(ptr, p) ^ WyCore.Prime1);
                    break;

                case 5:
                    seed = WyCore.Mum(seed, ((WyCore.Read32(ptr, p) << 8) | WyCore.Read8(ptr, p + 4)) ^ WyCore.Prime1);
                    break;

                case 6:
                    seed = WyCore.Mum(seed, ((WyCore.Read32(ptr, p) << 16) | WyCore.Read16(ptr, p + 4)) ^ WyCore.Prime1);
                    break;

                case 7:
                    seed = WyCore.Mum(seed, ((WyCore.Read32(ptr, p) << 24) | (WyCore.Read16(ptr, p + 4) << 8) | WyCore.Read8(ptr, p + 6)) ^ WyCore.Prime1);
                    break;

                case 8:
                    seed = WyCore.Mum(seed, WyCore.Read64Swapped(ptr, p) ^ WyCore.Prime1);
                    break;

                case 9:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read8(ptr, p + 8) ^ WyCore.Prime2);
                    break;

                case 10:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read16(ptr, p + 8) ^ WyCore.Prime2);
                    break;

                case 11:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, ((WyCore.Read16(ptr, p + 8) << 8) | WyCore.Read8(ptr, p + 10)) ^ WyCore.Prime2);
                    break;

                case 12:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read32(ptr, p + 8) ^ WyCore.Prime2);
                    break;

                case 13:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, ((WyCore.Read32(ptr, p + 8) << 8) | WyCore.Read8(ptr, p + 12)) ^ WyCore.Prime2);
                    break;

                case 14:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, ((WyCore.Read32(ptr, p + 8) << 16) | WyCore.Read16(ptr, p + 12)) ^ WyCore.Prime2);
                    break;

                case 15:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, ((WyCore.Read32(ptr, p + 8) << 24) | (WyCore.Read16(ptr, p + 12) << 8) | WyCore.Read8(ptr, p + 14)) ^ WyCore.Prime2);
                    break;

                case 16:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2);
                    break;

                case 17:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, WyCore.Read8(ptr, p + 16) ^ WyCore.Prime3);
                    break;

                case 18:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, WyCore.Read16(ptr, p + 16) ^ WyCore.Prime3);
                    break;

                case 19:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, ((WyCore.Read16(ptr, p + 16) << 8) | WyCore.Read8(ptr, p + 18)) ^ WyCore.Prime3);
                    break;

                case 20:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, WyCore.Read32(ptr, p + 16) ^ WyCore.Prime3);
                    break;

                case 21:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, ((WyCore.Read32(ptr, p + 16) << 8) | WyCore.Read8(ptr, p + 20)) ^ WyCore.Prime3);
                    break;

                case 22:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, ((WyCore.Read32(ptr, p + 16) << 16) | WyCore.Read16(ptr, p + 20)) ^ WyCore.Prime3);
                    break;

                case 23:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, ((WyCore.Read32(ptr, p + 16) << 24) | (WyCore.Read16(ptr, p + 20) << 8) | WyCore.Read8(ptr, p + 22)) ^ WyCore.Prime3);
                    break;

                case 24:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(seed, WyCore.Read64Swapped(ptr, p + 16) ^ WyCore.Prime3);
                    break;

                case 25:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, WyCore.Read8(ptr, p + 24) ^ WyCore.Prime4);
                    break;

                case 26:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, WyCore.Read16(ptr, p + 24) ^ WyCore.Prime4);
                    break;

                case 27:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, ((WyCore.Read16(ptr, p + 24) << 8) | WyCore.Read8(ptr, p + 26)) ^ WyCore.Prime4);
                    break;

                case 28:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, WyCore.Read32(ptr, p + 24) ^ WyCore.Prime4);
                    break;

                case 29:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, ((WyCore.Read32(ptr, p + 24) << 8) | WyCore.Read8(ptr, p + 28)) ^ WyCore.Prime4);
                    break;

                case 30:
                    seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                           WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, ((WyCore.Read32(ptr, p + 24) << 16) | WyCore.Read16(ptr, p + 28)) ^ WyCore.Prime4);
                    break;

                case 31: seed = WyCore.Mum(WyCore.Read64Swapped(ptr, p) ^ seed, WyCore.Read64Swapped(ptr, p + 8) ^ WyCore.Prime2) ^
                                WyCore.Mum(WyCore.Read64Swapped(ptr, p + 16) ^ seed, ((WyCore.Read32(ptr, p + 24) << 24) | (WyCore.Read16(ptr, p + 28) << 8) | WyCore.Read8(ptr, p + 30)) ^ WyCore.Prime4);
                    break;
                }
            }

            return(seed);
        }
Example #4
0
 private static ulong HashFinal(ulong seed, ulong length) =>
 WyCore.Mum(seed, length ^ WyCore.Prime5);