-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5603: Add Big Endian support in BinaryVectorReader and BinaryVectorWriter #1682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff89368
Add Big Endian Support for Float32 in BinaryVectorWriter.WriteToBytes…
2c2cae1
Added comments for clarity
medhatiwari 2c4a16a
Fix BinaryVectorSerializerTests to generate little-endian test data f…
medhatiwari 0ee1694
Add BinaryPrimitivesCompat methods for float32 little-endian serializ…
medhatiwari f43d935
Add float32 BinaryVector serialization/deserialization with endian ha…
medhatiwari 92b7ed2
added tests for new methods in BinaryPrimitivesCompat
medhatiwari 530ecda
resolved all the review comments
medhatiwari 02579c1
resolved all the comments
medhatiwari c547a15
another set of changes to resolve minor issues
medhatiwari c47d4bb
hardcoded ParamName to length
medhatiwari ed4f5f0
removed hardocode length to nameof(source.Length)
medhatiwari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
using System; | ||
using System.Buffers.Binary; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace MongoDB.Bson.IO | ||
{ | ||
|
@@ -31,5 +32,55 @@ public static void WriteDoubleLittleEndian(Span<byte> destination, double value) | |
{ | ||
BinaryPrimitives.WriteInt64LittleEndian(destination, BitConverter.DoubleToInt64Bits(value)); | ||
} | ||
|
||
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> source) | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (source.Length < 4) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(source.Length), "Source span is too small to contain a float."); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
return BinaryPrimitives.ReadSingleLittleEndian(source); | ||
#else | ||
// Constructs a 32-bit float from 4 Little Endian bytes in a platform-agnostic way. | ||
// Ensures correct bit pattern regardless of system endianness. | ||
int intValue = | ||
source[0] | | ||
(source[1] << 8) | | ||
(source[2] << 16) | | ||
(source[3] << 24); | ||
|
||
// This struct emulates BitConverter.Int32BitsToSingle for platforms like net472. | ||
return new FloatIntUnion { IntValue = intValue }.FloatValue; | ||
#endif | ||
} | ||
|
||
public static void WriteSingleLittleEndian(Span<byte> destination, float value) | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (destination.Length < 4) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(destination.Length), "Destination span is too small to hold a float."); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
BinaryPrimitives.WriteSingleLittleEndian(destination, value); | ||
#else | ||
// This struct emulates BitConverter.SingleToInt32Bits for platforms like net472. | ||
int intValue = new FloatIntUnion { FloatValue = value }.IntValue; | ||
|
||
destination[0] = (byte)(intValue); | ||
destination[1] = (byte)(intValue >> 8); | ||
destination[2] = (byte)(intValue >> 16); | ||
destination[3] = (byte)(intValue >> 24); | ||
#endif | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
private struct FloatIntUnion | ||
{ | ||
[FieldOffset(0)] public float FloatValue; | ||
[FieldOffset(0)] public int IntValue; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice solution! |
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/MongoDB.Bson.Tests/IO/BinaryPrimitivesCompatTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using Xunit; | ||
using FluentAssertions; | ||
using MongoDB.Bson.IO; | ||
|
||
namespace MongoDB.Bson.Tests.IO | ||
{ | ||
public class BinaryPrimitivesCompatTests | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
[Fact] | ||
public void ReadSingleLittleEndian_should_read_correctly() | ||
{ | ||
var bytes = new byte[] { 0x00, 0x00, 0x80, 0x3F }; // 1.0f in little endian | ||
var result = BinaryPrimitivesCompat.ReadSingleLittleEndian(bytes); | ||
result.Should().Be(1.0f); | ||
} | ||
|
||
[Fact] | ||
public void ReadSingleLittleEndian_should_throw_on_insufficient_length() | ||
{ | ||
var shortBuffer = new byte[3]; | ||
var exception = Record.Exception(() => | ||
BinaryPrimitivesCompat.ReadSingleLittleEndian(shortBuffer)); | ||
|
||
var e = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject; | ||
e.ParamName.Should().Be("Length"); | ||
} | ||
|
||
[Fact] | ||
public void WriteSingleLittleEndian_should_throw_on_insufficient_length() | ||
{ | ||
var shortBuffer = new byte[3]; | ||
var exception = Record.Exception(() => | ||
BinaryPrimitivesCompat.WriteSingleLittleEndian(shortBuffer, 1.23f)); | ||
|
||
var e = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject; | ||
e.ParamName.Should().Be("Length"); | ||
} | ||
|
||
[Fact] | ||
public void WriteSingleLittleEndian_should_write_correctly() | ||
{ | ||
Span<byte> buffer = new byte[4]; | ||
BinaryPrimitivesCompat.WriteSingleLittleEndian(buffer, 1.0f); | ||
buffer.ToArray().Should().Equal(0x00, 0x00, 0x80, 0x3F); // 1.0f little-endian | ||
} | ||
|
||
[Theory] | ||
[InlineData(0f)] | ||
[InlineData(1.0f)] | ||
[InlineData(-1.5f)] | ||
[InlineData(float.MaxValue)] | ||
[InlineData(float.MinValue)] | ||
[InlineData(float.NaN)] | ||
[InlineData(float.PositiveInfinity)] | ||
[InlineData(float.NegativeInfinity)] | ||
public void WriteAndReadSingleLittleEndian_should_roundtrip_correctly(float value) | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Span<byte> buffer = new byte[4]; | ||
|
||
BinaryPrimitivesCompat.WriteSingleLittleEndian(buffer, value); | ||
float result = BinaryPrimitivesCompat.ReadSingleLittleEndian(buffer); | ||
|
||
if (float.IsNaN(value)) | ||
{ | ||
Assert.True(float.IsNaN(result)); | ||
} | ||
else | ||
{ | ||
Assert.Equal(value, result); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.