ToDouble() public static method

public static ToDouble ( byte buffer, int offset ) : double
buffer byte
offset int
return double
        public void PerformanceTest()
        {
            var dummy = new byte[8];

            // Let's load and JIT
            BigEndianBinary.ToInt16(dummy, 0);
            BigEndianBinary.ToInt32(dummy, 0);
            BigEndianBinary.ToInt64(dummy, 0);
            BigEndianBinary.ToUInt16(dummy, 0);
            BigEndianBinary.ToUInt32(dummy, 0);
            BigEndianBinary.ToUInt64(dummy, 0);
            BigEndianBinary.ToSingle(dummy, 0);
            BigEndianBinary.ToDouble(dummy, 0);
            BitConverter.ToInt16(dummy, 0);
            BitConverter.ToInt32(dummy, 0);
            BitConverter.ToInt64(dummy, 0);
            BitConverter.ToUInt16(dummy, 0);
            BitConverter.ToUInt32(dummy, 0);
            BitConverter.ToUInt64(dummy, 0);
            BitConverter.ToSingle(dummy, 0);
            BitConverter.ToDouble(dummy, 0);

            // Go
            const int iteration = 1000000;

            PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0 }, BigEndianBinary.ToInt16, BitConverter.ToInt16, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0 }, BigEndianBinary.ToUInt16, BitConverter.ToUInt16, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0xff, 0xff, 0 }, BigEndianBinary.ToInt32, BitConverter.ToInt32, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0xff, 0xff, 0 }, BigEndianBinary.ToUInt32, BitConverter.ToUInt32, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0 }, BigEndianBinary.ToInt64, BitConverter.ToInt64, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0 }, BigEndianBinary.ToUInt64, BitConverter.ToUInt64, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0x00, 0xff, 0 }, BigEndianBinary.ToSingle, BitConverter.ToSingle, iteration);
            PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0 }, BigEndianBinary.ToDouble, BitConverter.ToDouble, iteration);
        }
 public void TestToDouble()
 {
     Assert.AreEqual(0.0, BigEndianBinary.ToDouble(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(-0.0, BigEndianBinary.ToDouble(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(Double.Epsilon, BigEndianBinary.ToDouble(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, 0));
     Assert.AreEqual(-1.0 * Double.Epsilon, BigEndianBinary.ToDouble(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, 0));
     Assert.AreEqual(Double.MinValue, BigEndianBinary.ToDouble(new byte[] { 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 0));
     Assert.AreEqual(Double.MaxValue, BigEndianBinary.ToDouble(new byte[] { 0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 0));
     Assert.AreEqual(Double.NaN, BigEndianBinary.ToDouble(new byte[] { 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(Double.NaN, BigEndianBinary.ToDouble(new byte[] { 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(Double.PositiveInfinity, BigEndianBinary.ToDouble(new byte[] { 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(Double.NegativeInfinity, BigEndianBinary.ToDouble(new byte[] { 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(1.0, BigEndianBinary.ToDouble(new byte[] { 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(-1.0, BigEndianBinary.ToDouble(new byte[] { 0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(2.0, BigEndianBinary.ToDouble(new byte[] { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(-2.0, BigEndianBinary.ToDouble(new byte[] { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0));
     Assert.AreEqual(1.0, BigEndianBinary.ToDouble(new byte[] { 0xff, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff }, 1));
 }
        private ReadValueResult ReadValue(out byte header, out long integral, out float real32, out double real64)
        {
            var readHeader = this.ReadByteFromSource();

            // This is BAD practice for out, but it reduces IL size very well for this method.
            integral = default(long);
            real32   = default(float);
            real64   = default(double);

            if (readHeader < 0)
            {
                header = 0;
                return(ReadValueResult.Eof);
            }

            header = unchecked (( byte )readHeader);

            switch (header >> 4)
            {
            case 0x0:
            case 0x1:
            case 0x2:
            case 0x3:
            case 0x4:
            case 0x5:
            case 0x6:
            case 0x7:
            {
                // PositiveFixNum
                this.InternalCollectionType = CollectionType.None;
                integral = header;
                return(ReadValueResult.Byte);
            }

            case 0x8:
            {
                // FixMap
                integral = header & 0xF;
                return(ReadValueResult.MapLength);
            }

            case 0x9:
            {
                // FixArray
                integral = header & 0xF;
                return(ReadValueResult.ArrayLength);
            }

            case 0xA:
            case 0xB:
            {
                // FixRaw
                integral = header & 0x1F;
                return(ReadValueResult.String);
            }

            case 0xE:
            case 0xF:
            {
                // NegativeFixNum
                this.InternalCollectionType = CollectionType.None;
                integral = header | unchecked (( long )0xFFFFFFFFFFFFFF00);
                return(ReadValueResult.SByte);
            }
            }

            switch (header)
            {
            case MessagePackCode.NilValue:
            {
                return(ReadValueResult.Nil);
            }

            case MessagePackCode.TrueValue:
            {
                integral = 1;
                return(ReadValueResult.Boolean);
            }

            case MessagePackCode.FalseValue:
            {
                integral = 0;
                return(ReadValueResult.Boolean);
            }

            case MessagePackCode.SignedInt8:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(sbyte));
                integral = BigEndianBinary.ToSByte(this._scalarBuffer, 0);
                return(ReadValueResult.SByte);
            }

            case MessagePackCode.SignedInt16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(short));
                integral = BigEndianBinary.ToInt16(this._scalarBuffer, 0);
                return(ReadValueResult.Int16);
            }

            case MessagePackCode.SignedInt32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(int));
                integral = BigEndianBinary.ToInt32(this._scalarBuffer, 0);
                return(ReadValueResult.Int32);
            }

            case MessagePackCode.SignedInt64:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(long));
                integral = BigEndianBinary.ToInt64(this._scalarBuffer, 0);
                return(ReadValueResult.Int64);
            }

            case MessagePackCode.UnsignedInt8:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(byte));
                integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                return(ReadValueResult.Byte);
            }

            case MessagePackCode.UnsignedInt16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                return(ReadValueResult.UInt16);
            }

            case MessagePackCode.UnsignedInt32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                return(ReadValueResult.UInt32);
            }

            case MessagePackCode.UnsignedInt64:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ulong));
                integral = unchecked (( long )BigEndianBinary.ToUInt64(this._scalarBuffer, 0));
                return(ReadValueResult.UInt64);
            }

            case MessagePackCode.Real32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(float));
                real32 = BigEndianBinary.ToSingle(this._scalarBuffer, 0);
                return(ReadValueResult.Single);
            }

            case MessagePackCode.Real64:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(double));
                real64 = BigEndianBinary.ToDouble(this._scalarBuffer, 0);
                return(ReadValueResult.Double);
            }

            case MessagePackCode.Bin8:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(byte));
                integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                return(ReadValueResult.Binary);
            }

            case MessagePackCode.Str8:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(byte));
                integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                return(ReadValueResult.String);
            }

            case MessagePackCode.Bin16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                return(ReadValueResult.Binary);
            }

            case MessagePackCode.Raw16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                return(ReadValueResult.String);
            }

            case MessagePackCode.Bin32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                return(ReadValueResult.Binary);
            }

            case MessagePackCode.Raw32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                return(ReadValueResult.String);
            }

            case MessagePackCode.Array16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                return(ReadValueResult.ArrayLength);
            }

            case MessagePackCode.Array32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                return(ReadValueResult.ArrayLength);
            }

            case MessagePackCode.Map16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                return(ReadValueResult.MapLength);
            }

            case MessagePackCode.Map32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                return(ReadValueResult.MapLength);
            }

            case MessagePackCode.FixExt1:
            {
                return(ReadValueResult.FixExt1);
            }

            case MessagePackCode.FixExt2:
            {
                return(ReadValueResult.FixExt2);
            }

            case MessagePackCode.FixExt4:
            {
                return(ReadValueResult.FixExt4);
            }

            case MessagePackCode.FixExt8:
            {
                return(ReadValueResult.FixExt8);
            }

            case MessagePackCode.FixExt16:
            {
                return(ReadValueResult.FixExt16);
            }

            case MessagePackCode.Ext8:
            {
                return(ReadValueResult.Ext8);
            }

            case MessagePackCode.Ext16:
            {
                return(ReadValueResult.Ext16);
            }

            case MessagePackCode.Ext32:
            {
                return(ReadValueResult.Ext32);
            }

            default:
            {
                this.ThrowUnassignedMessageTypeException(readHeader);
                // Never reach
                return(ReadValueResult.Eof);
            }
            }
        }
        private async Task <AsyncReadValueResult> ReadValueAsync(CancellationToken cancellationToken)
        {
            var readHeader = await this.ReadByteFromSourceAsync(cancellationToken).ConfigureAwait(false);

            var result = default(AsyncReadValueResult);

            if (readHeader < 0)
            {
                return(result);
            }

            var header = unchecked (( byte )readHeader);

            result.header = header;

            switch (header >> 4)
            {
            case 0x0:
            case 0x1:
            case 0x2:
            case 0x3:
            case 0x4:
            case 0x5:
            case 0x6:
            case 0x7:
            {
                // PositiveFixNum
                this.InternalCollectionType = CollectionType.None;
                result.integral             = header;
                result.type = ReadValueResult.Byte;
                return(result);
            }

            case 0x8:
            {
                // FixMap
                result.integral = header & 0xF;
                result.type     = ReadValueResult.MapLength;
                return(result);
            }

            case 0x9:
            {
                // FixArray
                result.integral = header & 0xF;
                result.type     = ReadValueResult.ArrayLength;
                return(result);
            }

            case 0xA:
            case 0xB:
            {
                // FixRaw
                result.integral = header & 0x1F;
                result.type     = ReadValueResult.String;
                return(result);
            }

            case 0xE:
            case 0xF:
            {
                // NegativeFixNum
                this.InternalCollectionType = CollectionType.None;
                result.integral             = header | unchecked (( long )0xFFFFFFFFFFFFFF00);
                result.type = ReadValueResult.SByte;
                return(result);
            }
            }

            switch (header)
            {
            case MessagePackCode.NilValue:
            {
                result.type = ReadValueResult.Nil;
                return(result);
            }

            case MessagePackCode.TrueValue:
            {
                result.integral = 1;
                result.type     = ReadValueResult.Boolean;
                return(result);
            }

            case MessagePackCode.FalseValue:
            {
                result.integral = 0;
                result.type     = ReadValueResult.Boolean;
                return(result);
            }

            case MessagePackCode.SignedInt8:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(sbyte), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToSByte(this._scalarBuffer, 0);
                result.type     = ReadValueResult.SByte;
                return(result);
            }

            case MessagePackCode.SignedInt16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(short), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Int16;
                return(result);
            }

            case MessagePackCode.SignedInt32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(int), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Int32;
                return(result);
            }

            case MessagePackCode.SignedInt64:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(long), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToInt64(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Int64;
                return(result);
            }

            case MessagePackCode.UnsignedInt8:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Byte;
                return(result);
            }

            case MessagePackCode.UnsignedInt16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.UInt16;
                return(result);
            }

            case MessagePackCode.UnsignedInt32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.UInt32;
                return(result);
            }

            case MessagePackCode.UnsignedInt64:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ulong), cancellationToken).ConfigureAwait(false);

                result.integral = unchecked (( long )BigEndianBinary.ToUInt64(this._scalarBuffer, 0));
                result.type     = ReadValueResult.UInt64;
                return(result);
            }

            case MessagePackCode.Real32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(float), cancellationToken).ConfigureAwait(false);

                result.real32 = BigEndianBinary.ToSingle(this._scalarBuffer, 0);
                result.type   = ReadValueResult.Single;
                return(result);
            }

            case MessagePackCode.Real64:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(double), cancellationToken).ConfigureAwait(false);

                result.real64 = BigEndianBinary.ToDouble(this._scalarBuffer, 0);
                result.type   = ReadValueResult.Double;
                return(result);
            }

            case MessagePackCode.Bin8:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Binary;
                return(result);
            }

            case MessagePackCode.Str8:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                result.type     = ReadValueResult.String;
                return(result);
            }

            case MessagePackCode.Bin16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Binary;
                return(result);
            }

            case MessagePackCode.Raw16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.String;
                return(result);
            }

            case MessagePackCode.Bin32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.Binary;
                return(result);
            }

            case MessagePackCode.Raw32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.String;
                return(result);
            }

            case MessagePackCode.Array16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.ArrayLength;
                return(result);
            }

            case MessagePackCode.Array32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.ArrayLength;
                return(result);
            }

            case MessagePackCode.Map16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                result.type     = ReadValueResult.MapLength;
                return(result);
            }

            case MessagePackCode.Map32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                result.type     = ReadValueResult.MapLength;
                return(result);
            }

            case MessagePackCode.FixExt1:
            {
                result.type = ReadValueResult.FixExt1;
                return(result);
            }

            case MessagePackCode.FixExt2:
            {
                result.type = ReadValueResult.FixExt2;
                return(result);
            }

            case MessagePackCode.FixExt4:
            {
                result.type = ReadValueResult.FixExt4;
                return(result);
            }

            case MessagePackCode.FixExt8:
            {
                result.type = ReadValueResult.FixExt8;
                return(result);
            }

            case MessagePackCode.FixExt16:
            {
                result.type = ReadValueResult.FixExt16;
                return(result);
            }

            case MessagePackCode.Ext8:
            {
                result.type = ReadValueResult.Ext8;
                return(result);
            }

            case MessagePackCode.Ext16:
            {
                result.type = ReadValueResult.Ext16;
                return(result);
            }

            case MessagePackCode.Ext32:
            {
                result.type = ReadValueResult.Ext32;
                return(result);
            }

            default:
            {
                this.ThrowUnassignedMessageTypeException(readHeader);
                // Never reach
                result.type = ReadValueResult.Eof;
                return(result);
            }
            }
        }