Skip to content

Commit

Permalink
fix serialization of some floating-point values
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Aug 15, 2024
1 parent 59ebb45 commit d2d440c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6309,7 +6309,9 @@ public static int WriteFloatingPointBits(
}
} else if (byteCount == 4) {
int bits =
CBORUtilities.SingleToHalfPrecisionIfSameValue(floatingBits);

CBORUtilities.SingleToHalfPrecisionIfSameValue(
unchecked((int)floatingBits));
if (bits != -1) {
return WriteFloatingPointBits(outputStream, bits, 2, false);
}
Expand Down
3 changes: 1 addition & 2 deletions CBOR/PeterO/Cbor/CBORUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,8 +1627,7 @@ public static int DoubleToRoundedSinglePrecision(long bits) {
}
}

public static int SingleToHalfPrecisionIfSameValue(float f) {
int bits = BitConverter.ToInt32(BitConverter.GetBytes((float)f), 0);
public static int SingleToHalfPrecisionIfSameValue(int bits) {
int exp = (bits >> 23) & 0xff;
int mant = bits & 0x7fffff;
int sign = (bits >> 16) & 0x8000;
Expand Down
43 changes: 43 additions & 0 deletions CBORTest/CBORObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6878,6 +6878,49 @@ public static void TestWriteExtraOne(long longValue) {
}
}

private void TestWriteUnchangedFloatBits(int bits) {
using (var ms = new Test.DelayingStream()) {
byte[] expectedBytes = {
(byte)0xfa,
(byte)((bits >> 24) & 0xff),
(byte)((bits >> 16) & 0xff),
(byte)((bits >> 8) & 0xff),
(byte)(bits & 0xff)

Check warning on line 6888 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (windows-latest)

Check warning on line 6888 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (ubuntu-latest)

Check warning on line 6888 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (macos-latest)

};
CBORObject.WriteFloatingPointBits(ms, bits, 4, true);
TestCommon.AssertByteArraysEqual(expectedBytes, ms.ToArray());
}
}

private void TestWriteUnchangedDoubleBits(long bits) {
using (var ms = new Test.DelayingStream()) {
byte[] expectedBytes = {
(byte)0xfb,
(byte)((bits >> 56) & 0xffL),
(byte)((bits >> 48) & 0xffL),
(byte)((bits >> 40) & 0xffL),
(byte)((bits >> 32) & 0xffL),
(byte)((bits >> 24) & 0xffL),
(byte)((bits >> 16) & 0xffL),
(byte)((bits >> 8) & 0xffL),
(byte)(bits & 0xffL)

Check warning on line 6906 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (windows-latest)

Check warning on line 6906 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (ubuntu-latest)

Check warning on line 6906 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (macos-latest)

};
CBORObject.WriteFloatingPointBits(ms, bits, 8, true);
TestCommon.AssertByteArraysEqual(expectedBytes, ms.ToArray());
}
}

[Test]
public void TestWriteSubnormalFloat() {
for (var i = 1; i <= 0x1fff; ++i) {
this.TestWriteUnchangedFloatBits(i);
this.TestWriteUnchangedFloatBits(0x2000 + i);
this.TestWriteUnchangedFloatBits(0x4000 + i);
this.TestWriteUnchangedFloatBits(0x8000 + i);
this.TestWriteUnchangedDoubleBits((long)i);
}
}

[Test]
public void TestWriteExtra() {
try {
Expand Down
2 changes: 1 addition & 1 deletion CBORTest/CBORTest.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<TargetFramework>netcoreapp8.0</TargetFramework>
<TargetFramework>netcoreapp7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=' &apos;$(Configuration)&apos;==&apos;Debug&apos; '>
<DebugType>full</DebugType>
Expand Down

0 comments on commit d2d440c

Please sign in to comment.