private static void ReadStructAndReverseLE() { Span <byte> spanLE = GetSpanLE(); TestStructExplicit readStruct = new TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = spanLE.Read <TestStructExplicit>(); if (!BitConverter.IsLittleEndian) { readStruct.S0 = readStruct.S0.Reverse(); readStruct.I0 = readStruct.I0.Reverse(); readStruct.L0 = readStruct.L0.Reverse(); readStruct.US0 = readStruct.US0.Reverse(); readStruct.UI0 = readStruct.UI0.Reverse(); readStruct.UL0 = readStruct.UL0.Reverse(); readStruct.S1 = readStruct.S1.Reverse(); readStruct.I1 = readStruct.I1.Reverse(); readStruct.L1 = readStruct.L1.Reverse(); readStruct.US1 = readStruct.US1.Reverse(); readStruct.UI1 = readStruct.UI1.Reverse(); readStruct.UL1 = readStruct.UL1.Reverse(); } } } } Assert.Equal(testExplicitStruct, readStruct); }
private static void ReadStructFieldByFieldBE() { Span <byte> spanBE = GetSpanBE(); TestStructExplicit readStruct = new TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = new TestStructExplicit { S0 = spanBE.ReadInt16BigEndian(), I0 = spanBE.Slice(2).ReadInt32BigEndian(), L0 = spanBE.Slice(6).ReadInt64BigEndian(), US0 = spanBE.Slice(14).ReadUInt16BigEndian(), UI0 = spanBE.Slice(16).ReadUInt32BigEndian(), UL0 = spanBE.Slice(20).ReadUInt64BigEndian(), S1 = spanBE.Slice(28).ReadInt16BigEndian(), I1 = spanBE.Slice(30).ReadInt32BigEndian(), L1 = spanBE.Slice(34).ReadInt64BigEndian(), US1 = spanBE.Slice(42).ReadUInt16BigEndian(), UI1 = spanBE.Slice(44).ReadUInt32BigEndian(), UL1 = spanBE.Slice(48).ReadUInt64BigEndian() }; } } } Assert.Equal(testExplicitStruct, readStruct); }
private static void ReadStructFieldByFieldUsingBitConverterLE() { Span <byte> spanLE = GetSpanLE(); byte[] arrayLE = spanLE.ToArray(); TestStructExplicit readStruct = new TestStructExplicit(); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { readStruct = new 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(testExplicitStruct, readStruct); }
public unsafe static void Main() { // Explicit class var testClassExplicit = new TestClassExplicit(); testClassExplicit.A = 32; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); testClassExplicit.B = 64; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); // Explicit struct var testStructExplicit = new TestStructExplicit(); testStructExplicit.A = 32; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); testStructExplicit.B = 64; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); // Sequential class (with pack) var testClassSequential = new TestClassSequential { D = 18 }; fixed(int *a = &testClassSequential.A) fixed(int *b = &testClassSequential.B) fixed(byte *c = &testClassSequential.C) fixed(int *d = &testClassSequential.D) { System.Console.WriteLine((byte *)b - (byte *)a); System.Console.WriteLine((byte *)c - (byte *)a); System.Console.WriteLine((byte *)d - (byte *)a); System.Console.WriteLine(testClassSequential.D); } // Sequential struct (with pack) var testStructSequential = new TestStructSequential() { D = 22 }; // System.Console.WriteLine((byte*)&testStructSequential.B - (byte*)&testStructSequential.A); //For some reason this doesnt work ... the address are pretty random in D // System.Console.WriteLine((byte*)&testStructSequential.C - (byte*)&testStructSequential.A); // System.Console.WriteLine((byte*)&testStructSequential.D - (byte*)&testStructSequential.A); System.Console.WriteLine(testStructSequential.D); }
public unsafe static void Main() { // Explicit class var testClassExplicit = new TestClassExplicit(); testClassExplicit.A = 32; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); testClassExplicit.B = 64; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); // Explicit struct var testStructExplicit = new TestStructExplicit(); testStructExplicit.A = 32; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); testStructExplicit.B = 64; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); // Sequential class (with pack) var testClassSequential = new TestClassSequential { D = 18 }; fixed(int *a = &testClassSequential.A) fixed(int *b = &testClassSequential.B) fixed(byte *c = &testClassSequential.C) fixed(int *d = &testClassSequential.D) { System.Console.WriteLine((byte *)b - (byte *)a); System.Console.WriteLine((byte *)c - (byte *)a); System.Console.WriteLine((byte *)d - (byte *)a); System.Console.WriteLine(testClassSequential.D); } // Sequential struct (with pack) var testStructSequential = new TestStructSequential() { D = 22 }; System.Console.WriteLine((byte *)&testStructSequential.B - (byte *)&testStructSequential.A); System.Console.WriteLine((byte *)&testStructSequential.C - (byte *)&testStructSequential.A); System.Console.WriteLine((byte *)&testStructSequential.D - (byte *)&testStructSequential.A); System.Console.WriteLine(testStructSequential.D); }
public unsafe static void Main() { // Explicit class var testClassExplicit = new TestClassExplicit(); testClassExplicit.A = 32; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); testClassExplicit.B = 64; System.Console.WriteLine(testClassExplicit.A); System.Console.WriteLine(testClassExplicit.B); // Explicit struct var testStructExplicit = new TestStructExplicit(); testStructExplicit.A = 32; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); testStructExplicit.B = 64; System.Console.WriteLine(testStructExplicit.A); System.Console.WriteLine(testStructExplicit.B); // Sequential class (with pack) var testClassSequential = new TestClassSequential { D = 18 }; fixed (int* a = &testClassSequential.A) fixed (int* b = &testClassSequential.B) fixed (byte* c = &testClassSequential.C) fixed (int* d = &testClassSequential.D) { System.Console.WriteLine((byte*)b - (byte*)a); System.Console.WriteLine((byte*)c - (byte*)a); System.Console.WriteLine((byte*)d - (byte*)a); System.Console.WriteLine(testClassSequential.D); } // Sequential struct (with pack) var testStructSequential = new TestStructSequential() { D = 22 }; // System.Console.WriteLine((byte*)&testStructSequential.B - (byte*)&testStructSequential.A); //For some reason this doesnt work ... the address are pretty random in D // System.Console.WriteLine((byte*)&testStructSequential.C - (byte*)&testStructSequential.A); // System.Console.WriteLine((byte*)&testStructSequential.D - (byte*)&testStructSequential.A); System.Console.WriteLine(testStructSequential.D); }
public void ReadingStructByFieldOrReadAndReverse() { Assert.True(BitConverter.IsLittleEndian); Span <byte> spanBE = GetSpanBE(); TestStructExplicit readStructAndReverse = spanBE.Read <TestStructExplicit>(); if (BitConverter.IsLittleEndian) { readStructAndReverse.S0 = readStructAndReverse.S0.Reverse(); readStructAndReverse.I0 = readStructAndReverse.I0.Reverse(); readStructAndReverse.L0 = readStructAndReverse.L0.Reverse(); readStructAndReverse.US0 = readStructAndReverse.US0.Reverse(); readStructAndReverse.UI0 = readStructAndReverse.UI0.Reverse(); readStructAndReverse.UL0 = readStructAndReverse.UL0.Reverse(); readStructAndReverse.S1 = readStructAndReverse.S1.Reverse(); readStructAndReverse.I1 = readStructAndReverse.I1.Reverse(); readStructAndReverse.L1 = readStructAndReverse.L1.Reverse(); readStructAndReverse.US1 = readStructAndReverse.US1.Reverse(); readStructAndReverse.UI1 = readStructAndReverse.UI1.Reverse(); readStructAndReverse.UL1 = readStructAndReverse.UL1.Reverse(); } TestStructExplicit readStructByField = new TestStructExplicit { S0 = spanBE.ReadInt16BigEndian(), I0 = spanBE.Slice(2).ReadInt32BigEndian(), L0 = spanBE.Slice(6).ReadInt64BigEndian(), US0 = spanBE.Slice(14).ReadUInt16BigEndian(), UI0 = spanBE.Slice(16).ReadUInt32BigEndian(), UL0 = spanBE.Slice(20).ReadUInt64BigEndian(), S1 = spanBE.Slice(28).ReadInt16BigEndian(), I1 = spanBE.Slice(30).ReadInt32BigEndian(), L1 = spanBE.Slice(34).ReadInt64BigEndian(), US1 = spanBE.Slice(42).ReadUInt16BigEndian(), UI1 = spanBE.Slice(44).ReadUInt32BigEndian(), UL1 = spanBE.Slice(48).ReadUInt64BigEndian() }; Assert.Equal(testExplicitStruct, readStructAndReverse); Assert.Equal(testExplicitStruct, readStructByField); }
private static Span <byte> GetSpanLE() { Span <byte> spanLE = new byte[Unsafe.SizeOf <TestStructExplicit>()]; TestStructExplicit testExplicitStruct = CreateTestExplicitStruct(); WriteInt16LittleEndian(spanLE, testExplicitStruct.S0); WriteInt32LittleEndian(spanLE.Slice(2), testExplicitStruct.I0); WriteInt64LittleEndian(spanLE.Slice(6), testExplicitStruct.L0); WriteUInt16LittleEndian(spanLE.Slice(14), testExplicitStruct.US0); WriteUInt32LittleEndian(spanLE.Slice(16), testExplicitStruct.UI0); WriteUInt64LittleEndian(spanLE.Slice(20), testExplicitStruct.UL0); WriteInt16LittleEndian(spanLE.Slice(28), testExplicitStruct.S1); WriteInt32LittleEndian(spanLE.Slice(30), testExplicitStruct.I1); WriteInt64LittleEndian(spanLE.Slice(34), testExplicitStruct.L1); WriteUInt16LittleEndian(spanLE.Slice(42), testExplicitStruct.US1); WriteUInt32LittleEndian(spanLE.Slice(44), testExplicitStruct.UI1); WriteUInt64LittleEndian(spanLE.Slice(48), testExplicitStruct.UL1); return(spanLE); }
public TestStructExplicit ReadStructFieldByFieldLE() { Span <byte> spanLE = new Span <byte>(_arrayLE); var readStruct = new TestStructExplicit { S0 = ReadInt16LittleEndian(spanLE), I0 = ReadInt32LittleEndian(spanLE.Slice(2)), L0 = ReadInt64LittleEndian(spanLE.Slice(6)), US0 = ReadUInt16LittleEndian(spanLE.Slice(14)), UI0 = ReadUInt32LittleEndian(spanLE.Slice(16)), UL0 = ReadUInt64LittleEndian(spanLE.Slice(20)), S1 = ReadInt16LittleEndian(spanLE.Slice(28)), I1 = ReadInt32LittleEndian(spanLE.Slice(30)), L1 = ReadInt64LittleEndian(spanLE.Slice(34)), US1 = ReadUInt16LittleEndian(spanLE.Slice(42)), UI1 = ReadUInt32LittleEndian(spanLE.Slice(44)), UL1 = ReadUInt64LittleEndian(spanLE.Slice(48)) }; return(readStruct); }
public TestStructExplicit ReadStructFieldByFieldUsingBitConverterLE() { byte[] arrayLE = _arrayLE; var readStruct = new 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), }; return(readStruct); }
public TestStructExplicit ReadStructFieldByFieldUsingBitConverterBE() { byte[] arrayBE = _arrayBE; var readStruct = new 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); } return(readStruct); }
private static void ReadStructFieldByFieldUsingBitConverter() { Assert.True(BitConverter.IsLittleEndian); Span <byte> spanLE = GetSpanLE(); byte[] arrayLE = spanLE.ToArray(); TestStructExplicit readStructLE = new 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), }; Span <byte> spanBE = GetSpanBE(); byte[] arrayBE = spanBE.ToArray(); TestStructExplicit readStructBE = new 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) { readStructBE.S0 = IPAddress.NetworkToHostOrder(readStructBE.S0); readStructBE.I0 = IPAddress.NetworkToHostOrder(readStructBE.I0); readStructBE.L0 = IPAddress.NetworkToHostOrder(readStructBE.L0); readStructBE.US0 = readStructBE.US0.Reverse(); readStructBE.UI0 = readStructBE.UI0.Reverse(); readStructBE.UL0 = readStructBE.UL0.Reverse(); readStructBE.S1 = IPAddress.NetworkToHostOrder(readStructBE.S1); readStructBE.I1 = IPAddress.NetworkToHostOrder(readStructBE.I1); readStructBE.L1 = IPAddress.NetworkToHostOrder(readStructBE.L1); readStructBE.US1 = readStructBE.US1.Reverse(); readStructBE.UI1 = readStructBE.UI1.Reverse(); readStructBE.UL1 = readStructBE.UL1.Reverse(); } Assert.Equal(testExplicitStruct, readStructLE); Assert.Equal(testExplicitStruct, readStructBE); }