/// <summary> /// Reads the next data item as a single-precision floating point number (major type 7). /// </summary> /// <returns>The decoded value.</returns> /// <exception cref="InvalidOperationException"> /// the next data item does not have the correct major type. -or- /// the next simple value is not a floating-point number encoding. -or- /// the encoded value is a double-precision float /// </exception> /// <exception cref="CborContentException"> /// the next value has an invalid CBOR encoding. -or- /// there was an unexpected end of CBOR encoding data. -or- /// the next value uses a CBOR encoding that is not valid under the current conformance mode. /// </exception> public float ReadSingle() { CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.Simple); ReadOnlySpan <byte> buffer = GetRemainingBytes(); float result; switch (header.AdditionalInfo) { case CborAdditionalInfo.Additional16BitData: EnsureReadCapacity(buffer, 1 + HalfHelpers.SizeOfHalf); result = (float)HalfHelpers.ReadHalfBigEndian(buffer.Slice(1)); AdvanceBuffer(1 + HalfHelpers.SizeOfHalf); AdvanceDataItemCounters(); return(result); case CborAdditionalInfo.Additional32BitData: EnsureReadCapacity(buffer, 1 + sizeof(float)); result = BinaryPrimitives.ReadSingleBigEndian(buffer.Slice(1)); AdvanceBuffer(1 + sizeof(float)); AdvanceDataItemCounters(); return(result); case CborAdditionalInfo.Additional64BitData: throw new InvalidOperationException(SR.Cbor_Reader_ReadingAsLowerPrecision); default: throw new InvalidOperationException(SR.Cbor_Reader_NotAFloatEncoding); } }
/// <summary>Reads the next data item as a double-precision floating point number (major type 7).</summary> /// <returns>The decoded <see cref="double" /> value.</returns> /// <exception cref="InvalidOperationException"><para>The next data item does not have the correct major type.</para> /// <para>-or-</para> /// <para>The next simple value is not a floating-point number encoding.</para></exception> /// <exception cref="CborContentException"><para>The next value has an invalid CBOR encoding.</para> /// <para>-or-</para> /// <para>There was an unexpected end of CBOR encoding data.</para> /// <para>-or-</para> /// <para>The next value uses a CBOR encoding that is not valid under the current conformance mode.</para></exception> public double ReadDouble() { CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.Simple); ReadOnlySpan <byte> buffer = GetRemainingBytes(); double result; switch (header.AdditionalInfo) { case CborAdditionalInfo.Additional16BitData: EnsureReadCapacity(buffer, 1 + sizeof(short)); result = HalfHelpers.HalfToDouble(CborHelpers.ReadHalfBigEndian(buffer.Slice(1))); AdvanceBuffer(1 + sizeof(short)); AdvanceDataItemCounters(); return(result); case CborAdditionalInfo.Additional32BitData: EnsureReadCapacity(buffer, 1 + sizeof(float)); result = CborHelpers.ReadSingleBigEndian(buffer.Slice(1)); AdvanceBuffer(1 + sizeof(float)); AdvanceDataItemCounters(); return(result); case CborAdditionalInfo.Additional64BitData: EnsureReadCapacity(buffer, 1 + sizeof(double)); result = CborHelpers.ReadDoubleBigEndian(buffer.Slice(1)); AdvanceBuffer(1 + sizeof(double)); AdvanceDataItemCounters(); return(result); default: throw new InvalidOperationException(SR.Cbor_Reader_NotAFloatEncoding); } }
// Implements major type 7 encoding per https://tools.ietf.org/html/rfc7049#section-2.1 /// <summary> /// Writes a half-precision floating point number (major type 7). /// </summary> /// <param name="value">The value to write.</param> /// <exception cref="InvalidOperationException"> /// Writing a new value exceeds the definite length of the parent data item. -or- /// The major type of the encoded value is not permitted in the parent data item. -or- /// The written data is not accepted under the current conformance mode /// </exception> public void WriteHalf(Half value) { EnsureWriteCapacity(1 + HalfHelpers.SizeOfHalf); WriteInitialByte(new CborInitialByte(CborMajorType.Simple, CborAdditionalInfo.Additional16BitData)); HalfHelpers.WriteHalfBigEndian(_buffer.AsSpan(_offset), value); _offset += HalfHelpers.SizeOfHalf; AdvanceDataItemCounters(); }
internal static bool TryConvertSingleToHalf(float value, out ushort result) { result = HalfHelpers.FloatToHalf(value); return(CborHelpers.SingleToInt32Bits(HalfHelpers.HalfToFloat(result)) == CborHelpers.SingleToInt32Bits(value)); }