Add Big Endian Support for Float32 in BinaryVectorWriter.WriteToBytes<T>() #1682
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.
Description
This PR adds Big Endian support for System.Single (Float32) to the BinaryVectorWriter.WriteToBytes() method.
Background
While running the MongoDB.Bson.Tests test suite on a Big Endian (s390x) system, we encountered 34 consistent test failures within the BinaryVectorSerializerTests class.
Each failure was caused by a System.NotSupportedException indicating that binary vector data of float32 type is not yet supported on Big Endian architectures.
Exception Observed
Sample Failing Tests
Some of the test cases that failed due to this limitation include:
BinaryVectorSerializerTests.BinaryVectorSerializer_should_deserialize_bson_vector<Float32>
BinaryVectorSerializerTests.BinaryVectorSerializer_should_serialize_bson_vector<Float32>
BinaryVectorSerializerTests.ArrayAsBinaryVectorSerializer_should_deserialize_bson_vector<Float32>
BinaryVectorSerializerTests.ArrayAsBinaryVectorSerializer_should_serialize_bson_vector<Float32>
BinaryVectorSerializerTests.MemoryAsBinaryVectorSerializer_should_serialize_bson_vector<Float32>
BinaryVectorSerializerTests.MemoryAsBinaryVectorSerializer_should_deserialize_bson_vector<Float32>
BinaryVectorSerializerTests.ReadOnlyMemoryAsBinaryVectorSerializer_should_serialize_bson_vector<Float32>
BinaryVectorSerializerTests.ReadOnlyMemoryAsBinaryVectorSerializer_should_deserialize_bson_vector<Float32>
Why This Fix Is Necessary
This limitation was blocking test pass status on Big Endian platforms such as s390x. Adding support for float32 serialization in Big Endian format:
Enables consistent behavior across architectures
Completes existing deserialization support added earlier in BinaryVectorReader.cs
Changes Introduced
Added Big Endian branch to BinaryVectorWriter.WriteToBytes() for T == float.
Used
BinaryPrimitives.WriteSingleBigEndian()
to write bytes in the correct order.Left existing Little Endian logic untouched to preserve behavior.
cc: @giritrivedi