/// <summary> /// Writes a <see cref="MessagePackCode.Float64"/> value. /// </summary> /// <param name="value">The value.</param> public void Write(double value) { var span = writer.GetSpan(9); span[0] = MessagePackCode.Float64; var num = new Float64Bits(value); if (BitConverter.IsLittleEndian) { span[1] = num.Byte7; span[2] = num.Byte6; span[3] = num.Byte5; span[4] = num.Byte4; span[5] = num.Byte3; span[6] = num.Byte2; span[7] = num.Byte1; span[8] = num.Byte0; } else { span[1] = num.Byte0; span[2] = num.Byte1; span[3] = num.Byte2; span[4] = num.Byte3; span[5] = num.Byte4; span[6] = num.Byte5; span[7] = num.Byte6; span[8] = num.Byte7; } writer.Advance(9); }
/// <summary> /// Reads an <see cref="double"/> value from any value encoded with: /// <see cref="MessagePackCode.Float64"/>, /// <see cref="MessagePackCode.Float32"/>, /// <see cref="MessagePackCode.Int8"/>, /// <see cref="MessagePackCode.Int16"/>, /// <see cref="MessagePackCode.Int32"/>, /// <see cref="MessagePackCode.Int64"/>, /// <see cref="MessagePackCode.UInt8"/>, /// <see cref="MessagePackCode.UInt16"/>, /// <see cref="MessagePackCode.UInt32"/>, /// <see cref="MessagePackCode.UInt64"/>, /// or some value between <see cref="MessagePackCode.MinNegativeFixInt"/> and <see cref="MessagePackCode.MaxNegativeFixInt"/>, /// or some value between <see cref="MessagePackCode.MinFixInt"/> and <see cref="MessagePackCode.MaxFixInt"/>. /// </summary> /// <returns>The value.</returns> public unsafe double ReadDouble() { ThrowInsufficientBufferUnless(this.reader.TryRead(out byte code)); switch (code) { case MessagePackCode.Float64: byte * pScratch8 = stackalloc byte[8]; Span <byte> scratch8 = new Span <byte>(pScratch8, 8); ThrowInsufficientBufferUnless(this.reader.TryCopyTo(scratch8)); this.reader.Advance(scratch8.Length); var doubleValue = new Float64Bits(scratch8); return(doubleValue.Value); case MessagePackCode.Float32: byte * pScratch4 = stackalloc byte[4]; Span <byte> scratch4 = new Span <byte>(pScratch4, 4); ThrowInsufficientBufferUnless(this.reader.TryCopyTo(scratch4)); this.reader.Advance(scratch4.Length); var floatValue = new Float32Bits(scratch4); return(floatValue.Value); case MessagePackCode.Int8: ThrowInsufficientBufferUnless(this.reader.TryRead(out byte byteValue)); return(unchecked ((sbyte)byteValue)); case MessagePackCode.Int16: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out short shortValue)); return(shortValue); case MessagePackCode.Int32: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out int intValue)); return(intValue); case MessagePackCode.Int64: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out long longValue)); return(longValue); case MessagePackCode.UInt8: ThrowInsufficientBufferUnless(this.reader.TryRead(out byteValue)); return(byteValue); case MessagePackCode.UInt16: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out shortValue)); return(unchecked ((ushort)shortValue)); case MessagePackCode.UInt32: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out intValue)); return(unchecked ((uint)intValue)); case MessagePackCode.UInt64: ThrowInsufficientBufferUnless(this.reader.TryReadBigEndian(out longValue)); return(unchecked ((ulong)longValue)); default: if (code >= MessagePackCode.MinNegativeFixInt && code <= MessagePackCode.MaxNegativeFixInt) { return(unchecked ((sbyte)code)); } else if (code >= MessagePackCode.MinFixInt && code <= MessagePackCode.MaxFixInt) { return(code); } throw ThrowInvalidCode(code); } }