Skip to content

Commit

Permalink
Ensure floating-point types write the exponent/significand as little-…
Browse files Browse the repository at this point in the history
…endian (#68331)
  • Loading branch information
tannergooding authored Apr 22, 2022
1 parent 9e9cfcb commit 0e18cfd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**
===========================================================*/

using System.Buffers.Binary;

This comment has been minimized.

Copy link
@Blackironman797
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
Expand Down Expand Up @@ -684,6 +685,12 @@ bool IFloatingPoint<double>.TryWriteExponentLittleEndian(Span<byte> destination,
if (destination.Length >= sizeof(short))
{
short exponent = Exponent;

if (!BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

bytesWritten = sizeof(short);
Expand All @@ -708,6 +715,12 @@ bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destinati
if (destination.Length >= sizeof(ulong))
{
ulong significand = Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ulong);
Expand Down
7 changes: 7 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -929,6 +930,12 @@ bool IFloatingPoint<Half>.TryWriteSignificandLittleEndian(Span<byte> destination
if (destination.Length >= sizeof(ushort))
{
ushort significand = Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(ushort);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
Expand Down Expand Up @@ -884,6 +885,12 @@ bool IFloatingPoint<NFloat>.TryWriteExponentLittleEndian(Span<byte> destination,
if (destination.Length >= sizeof(NativeExponentType))
{
NativeExponentType exponent = _value.Exponent;

if (!BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);

bytesWritten = sizeof(NativeExponentType);
Expand Down Expand Up @@ -915,6 +922,12 @@ bool IFloatingPoint<NFloat>.TryWriteSignificandLittleEndian(Span<byte> destinati
if (destination.Length >= sizeof(NativeSignificandType))
{
NativeSignificandType significand = _value.Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(NativeSignificandType);
Expand Down
11 changes: 9 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
**
===========================================================*/

using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
Expand Down Expand Up @@ -174,9 +175,9 @@ public static unsafe bool IsNaN(float f)
// A NaN will never equal itself so this is an
// easy and efficient way to check for NaN.

#pragma warning disable CS1718
#pragma warning disable CS1718
return f != f;
#pragma warning restore CS1718
#pragma warning restore CS1718
}

/// <summary>Determines whether the specified value is negative.</summary>
Expand Down Expand Up @@ -703,6 +704,12 @@ bool IFloatingPoint<float>.TryWriteSignificandLittleEndian(Span<byte> destinatio
if (destination.Length >= sizeof(uint))
{
uint significand = Significand;

if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}

Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);

bytesWritten = sizeof(uint);
Expand Down

0 comments on commit 0e18cfd

Please sign in to comment.