private static void ReadStructFieldByFieldBE() { Span <byte> spanBE = TestHelpers.GetSpanBE(); var readStruct = new TestHelpers.TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = new TestHelpers.TestStructExplicit { S0 = ReadInt16BigEndian(spanBE), I0 = ReadInt32BigEndian(spanBE.Slice(2)), L0 = ReadInt64BigEndian(spanBE.Slice(6)), US0 = ReadUInt16BigEndian(spanBE.Slice(14)), UI0 = ReadUInt32BigEndian(spanBE.Slice(16)), UL0 = ReadUInt64BigEndian(spanBE.Slice(20)), S1 = ReadInt16BigEndian(spanBE.Slice(28)), I1 = ReadInt32BigEndian(spanBE.Slice(30)), L1 = ReadInt64BigEndian(spanBE.Slice(34)), US1 = ReadUInt16BigEndian(spanBE.Slice(42)), UI1 = ReadUInt32BigEndian(spanBE.Slice(44)), UL1 = ReadUInt64BigEndian(spanBE.Slice(48)) }; } } } Assert.Equal(TestHelpers.testExplicitStruct, readStruct); }
private static void ReadStructFieldByFieldUsingBitConverterLE() { Span <byte> spanLE = TestHelpers.GetSpanLE(); byte[] arrayLE = spanLE.ToArray(); var readStruct = new TestHelpers.TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = new TestHelpers.TestStructExplicit { S0 = BitConverter.ToInt16(arrayLE, 0), I0 = BitConverter.ToInt32(arrayLE, 2), L0 = BitConverter.ToInt64(arrayLE, 6), US0 = BitConverter.ToUInt16(arrayLE, 14), UI0 = BitConverter.ToUInt32(arrayLE, 16), UL0 = BitConverter.ToUInt64(arrayLE, 20), S1 = BitConverter.ToInt16(arrayLE, 28), I1 = BitConverter.ToInt32(arrayLE, 30), L1 = BitConverter.ToInt64(arrayLE, 34), US1 = BitConverter.ToUInt16(arrayLE, 42), UI1 = BitConverter.ToUInt32(arrayLE, 44), UL1 = BitConverter.ToUInt64(arrayLE, 48), }; } } } Assert.Equal(TestHelpers.testExplicitStruct, readStruct); }
private static void ReadStructAndReverseLE() { Span <byte> spanLE = TestHelpers.GetSpanLE(); var readStruct = new TestHelpers.TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = ReadMachineEndian <TestHelpers.TestStructExplicit>(spanLE); if (!BitConverter.IsLittleEndian) { readStruct.S0 = ReverseEndianness(readStruct.S0); readStruct.I0 = ReverseEndianness(readStruct.I0); readStruct.L0 = ReverseEndianness(readStruct.L0); readStruct.US0 = ReverseEndianness(readStruct.US0); readStruct.UI0 = ReverseEndianness(readStruct.UI0); readStruct.UL0 = ReverseEndianness(readStruct.UL0); readStruct.S1 = ReverseEndianness(readStruct.S1); readStruct.I1 = ReverseEndianness(readStruct.I1); readStruct.L1 = ReverseEndianness(readStruct.L1); readStruct.US1 = ReverseEndianness(readStruct.US1); readStruct.UI1 = ReverseEndianness(readStruct.UI1); readStruct.UL1 = ReverseEndianness(readStruct.UL1); } } } } Assert.Equal(TestHelpers.testExplicitStruct, readStruct); }
private static void ReadStructFieldByFieldUsingBitConverterBE() { Span <byte> spanBE = TestHelpers.GetSpanBE(); byte[] arrayBE = spanBE.ToArray(); var readStruct = new TestHelpers.TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = new TestHelpers.TestStructExplicit { S0 = BitConverter.ToInt16(arrayBE, 0), I0 = BitConverter.ToInt32(arrayBE, 2), L0 = BitConverter.ToInt64(arrayBE, 6), US0 = BitConverter.ToUInt16(arrayBE, 14), UI0 = BitConverter.ToUInt32(arrayBE, 16), UL0 = BitConverter.ToUInt64(arrayBE, 20), S1 = BitConverter.ToInt16(arrayBE, 28), I1 = BitConverter.ToInt32(arrayBE, 30), L1 = BitConverter.ToInt64(arrayBE, 34), US1 = BitConverter.ToUInt16(arrayBE, 42), UI1 = BitConverter.ToUInt32(arrayBE, 44), UL1 = BitConverter.ToUInt64(arrayBE, 48), }; if (BitConverter.IsLittleEndian) { readStruct.S0 = ReverseEndianness(readStruct.S0); readStruct.I0 = ReverseEndianness(readStruct.I0); readStruct.L0 = ReverseEndianness(readStruct.L0); readStruct.US0 = ReverseEndianness(readStruct.US0); readStruct.UI0 = ReverseEndianness(readStruct.UI0); readStruct.UL0 = ReverseEndianness(readStruct.UL0); readStruct.S1 = ReverseEndianness(readStruct.S1); readStruct.I1 = ReverseEndianness(readStruct.I1); readStruct.L1 = ReverseEndianness(readStruct.L1); readStruct.US1 = ReverseEndianness(readStruct.US1); readStruct.UI1 = ReverseEndianness(readStruct.UI1); readStruct.UL1 = ReverseEndianness(readStruct.UL1); } } } } Assert.Equal(TestHelpers.s_testExplicitStruct, readStruct); }
public void ReadingStructFieldByFieldOrReadAndReverseEndianness() { Assert.True(BitConverter.IsLittleEndian); Span <byte> spanBE = new byte[Unsafe.SizeOf <TestHelpers.TestStructExplicit>()]; var testExplicitStruct = new TestHelpers.TestStructExplicit { S0 = short.MaxValue, I0 = int.MaxValue, L0 = long.MaxValue, US0 = ushort.MaxValue, UI0 = uint.MaxValue, UL0 = ulong.MaxValue, S1 = short.MinValue, I1 = int.MinValue, L1 = long.MinValue, US1 = ushort.MinValue, UI1 = uint.MinValue, UL1 = ulong.MinValue }; WriteInt16BigEndian(spanBE, testExplicitStruct.S0); WriteInt32BigEndian(spanBE.Slice(2), testExplicitStruct.I0); WriteInt64BigEndian(spanBE.Slice(6), testExplicitStruct.L0); WriteUInt16BigEndian(spanBE.Slice(14), testExplicitStruct.US0); WriteUInt32BigEndian(spanBE.Slice(16), testExplicitStruct.UI0); WriteUInt64BigEndian(spanBE.Slice(20), testExplicitStruct.UL0); WriteInt16BigEndian(spanBE.Slice(28), testExplicitStruct.S1); WriteInt32BigEndian(spanBE.Slice(30), testExplicitStruct.I1); WriteInt64BigEndian(spanBE.Slice(34), testExplicitStruct.L1); WriteUInt16BigEndian(spanBE.Slice(42), testExplicitStruct.US1); WriteUInt32BigEndian(spanBE.Slice(44), testExplicitStruct.UI1); WriteUInt64BigEndian(spanBE.Slice(48), testExplicitStruct.UL1); Assert.Equal(56, spanBE.Length); ReadOnlySpan <byte> readOnlySpanBE = new ReadOnlySpan <byte>(spanBE.ToArray()); var readStructAndReverse = ReadMachineEndian <TestHelpers.TestStructExplicit>(spanBE); if (BitConverter.IsLittleEndian) { readStructAndReverse.S0 = ReverseEndianness(readStructAndReverse.S0); readStructAndReverse.I0 = ReverseEndianness(readStructAndReverse.I0); readStructAndReverse.L0 = ReverseEndianness(readStructAndReverse.L0); readStructAndReverse.US0 = ReverseEndianness(readStructAndReverse.US0); readStructAndReverse.UI0 = ReverseEndianness(readStructAndReverse.UI0); readStructAndReverse.UL0 = ReverseEndianness(readStructAndReverse.UL0); readStructAndReverse.S1 = ReverseEndianness(readStructAndReverse.S1); readStructAndReverse.I1 = ReverseEndianness(readStructAndReverse.I1); readStructAndReverse.L1 = ReverseEndianness(readStructAndReverse.L1); readStructAndReverse.US1 = ReverseEndianness(readStructAndReverse.US1); readStructAndReverse.UI1 = ReverseEndianness(readStructAndReverse.UI1); readStructAndReverse.UL1 = ReverseEndianness(readStructAndReverse.UL1); } var readStructFieldByField = new TestHelpers.TestStructExplicit { S0 = ReadInt16BigEndian(spanBE), I0 = ReadInt32BigEndian(spanBE.Slice(2)), L0 = ReadInt64BigEndian(spanBE.Slice(6)), US0 = ReadUInt16BigEndian(spanBE.Slice(14)), UI0 = ReadUInt32BigEndian(spanBE.Slice(16)), UL0 = ReadUInt64BigEndian(spanBE.Slice(20)), S1 = ReadInt16BigEndian(spanBE.Slice(28)), I1 = ReadInt32BigEndian(spanBE.Slice(30)), L1 = ReadInt64BigEndian(spanBE.Slice(34)), US1 = ReadUInt16BigEndian(spanBE.Slice(42)), UI1 = ReadUInt32BigEndian(spanBE.Slice(44)), UL1 = ReadUInt64BigEndian(spanBE.Slice(48)) }; var readStructAndReverseFromReadOnlySpan = ReadMachineEndian <TestHelpers.TestStructExplicit>(readOnlySpanBE); if (BitConverter.IsLittleEndian) { readStructAndReverseFromReadOnlySpan.S0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.S0); readStructAndReverseFromReadOnlySpan.I0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.I0); readStructAndReverseFromReadOnlySpan.L0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.L0); readStructAndReverseFromReadOnlySpan.US0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.US0); readStructAndReverseFromReadOnlySpan.UI0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.UI0); readStructAndReverseFromReadOnlySpan.UL0 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.UL0); readStructAndReverseFromReadOnlySpan.S1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.S1); readStructAndReverseFromReadOnlySpan.I1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.I1); readStructAndReverseFromReadOnlySpan.L1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.L1); readStructAndReverseFromReadOnlySpan.US1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.US1); readStructAndReverseFromReadOnlySpan.UI1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.UI1); readStructAndReverseFromReadOnlySpan.UL1 = ReverseEndianness(readStructAndReverseFromReadOnlySpan.UL1); } var readStructFieldByFieldFromReadOnlySpan = new TestHelpers.TestStructExplicit { S0 = ReadInt16BigEndian(readOnlySpanBE), I0 = ReadInt32BigEndian(readOnlySpanBE.Slice(2)), L0 = ReadInt64BigEndian(readOnlySpanBE.Slice(6)), US0 = ReadUInt16BigEndian(readOnlySpanBE.Slice(14)), UI0 = ReadUInt32BigEndian(readOnlySpanBE.Slice(16)), UL0 = ReadUInt64BigEndian(readOnlySpanBE.Slice(20)), S1 = ReadInt16BigEndian(readOnlySpanBE.Slice(28)), I1 = ReadInt32BigEndian(readOnlySpanBE.Slice(30)), L1 = ReadInt64BigEndian(readOnlySpanBE.Slice(34)), US1 = ReadUInt16BigEndian(readOnlySpanBE.Slice(42)), UI1 = ReadUInt32BigEndian(readOnlySpanBE.Slice(44)), UL1 = ReadUInt64BigEndian(readOnlySpanBE.Slice(48)) }; Assert.Equal(testExplicitStruct, readStructAndReverse); Assert.Equal(testExplicitStruct, readStructFieldByField); Assert.Equal(testExplicitStruct, readStructAndReverseFromReadOnlySpan); Assert.Equal(testExplicitStruct, readStructFieldByFieldFromReadOnlySpan); }