Skip to content

Commit

Permalink
Remove unnecessary endianness dependent logic (#104332)
Browse files Browse the repository at this point in the history
* Remove unnecessary endianness logic

* Revert back to the old more performant approach

* Write 2 values once
  • Loading branch information
buyaa-n authored Jul 8, 2024
1 parent e336326 commit 101c0da
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,12 @@ private static unsafe uint Encode(byte* threeBytes, ref byte encodingMap)
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
uint i3 = Unsafe.Add(ref encodingMap, (IntPtr)(i & 0x3F));

return ConstructResult(i0, i1, i2, i3);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ConstructResult(uint i0, uint i1, uint i2, uint i3)
{
if (BitConverter.IsLittleEndian)
{
return i0 | (i1 << 8) | (i2 << 16) | (i3 << 24);
Expand Down Expand Up @@ -677,20 +683,8 @@ public unsafe void EncodeOneOptionallyPadTwo(byte* oneByte, byte* dest, ref byte
uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 10));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 4) & 0x3F));

if (BitConverter.IsLittleEndian)
{
dest[0] = (byte)i0;
dest[1] = (byte)i1;
dest[2] = (byte)EncodingPad;
dest[3] = (byte)EncodingPad;
}
else
{
dest[3] = (byte)i0;
dest[2] = (byte)i1;
dest[1] = (byte)EncodingPad;
dest[0] = (byte)EncodingPad;
}
uint result = ConstructResult(i0, i1, EncodingPad, EncodingPad);
Unsafe.WriteUnaligned(dest, result);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -705,20 +699,8 @@ public unsafe void EncodeTwoOptionallyPadOne(byte* twoBytes, byte* dest, ref byt
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));

if (BitConverter.IsLittleEndian)
{
dest[0] = (byte)i0;
dest[1] = (byte)i1;
dest[2] = (byte)i2;
dest[3] = (byte)EncodingPad;
}
else
{
dest[3] = (byte)i0;
dest[2] = (byte)i1;
dest[1] = (byte)i2;
dest[0] = (byte)EncodingPad;
}
uint result = ConstructResult(i0, i1, i2, EncodingPad);
Unsafe.WriteUnaligned(dest, result);
}

#if NET
Expand Down Expand Up @@ -757,33 +739,9 @@ public unsafe void StoreArmVector128x4ToDestination(byte* dest, byte* destStart,
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void EncodeThreeAndWrite(byte* threeBytes, byte* destination, ref byte encodingMap)
{
uint t0 = threeBytes[0];
uint t1 = threeBytes[1];
uint t2 = threeBytes[2];

uint i = (t0 << 16) | (t1 << 8) | t2;

byte i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
byte i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
byte i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
byte i3 = Unsafe.Add(ref encodingMap, (IntPtr)(i & 0x3F));

if (BitConverter.IsLittleEndian)
{
destination[0] = i0;
destination[1] = i1;
destination[2] = i2;
destination[3] = i3;
}
else
{
destination[3] = i0;
destination[2] = i1;
destination[1] = i2;
destination[0] = i3;
}
uint result = Encode(threeBytes, ref encodingMap);
Unsafe.WriteUnaligned(destination, result);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,21 @@ public unsafe void EncodeOneOptionallyPadTwo(byte* oneByte, byte* dest, ref byte

uint i = t0 << 8;

uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 10));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 4) & 0x3F));
byte i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 10));
byte i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 4) & 0x3F));

ushort result;

if (BitConverter.IsLittleEndian)
{
dest[0] = (byte)i0;
dest[1] = (byte)i1;
result = (ushort)(i0 | (i1 << 8));
}
else
{
dest[1] = (byte)i0;
dest[0] = (byte)i1;
result = (ushort)((i0 << 8) | i1);
}

Unsafe.WriteUnaligned(dest, result);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -274,22 +276,13 @@ public unsafe void EncodeTwoOptionallyPadOne(byte* twoBytes, byte* dest, ref byt

uint i = (t0 << 16) | (t1 << 8);

uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
byte i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
byte i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
byte i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));

if (BitConverter.IsLittleEndian)
{
dest[0] = (byte)i0;
dest[1] = (byte)i1;
dest[2] = (byte)i2;
}
else
{
dest[2] = (byte)i0;
dest[1] = (byte)i1;
dest[0] = (byte)i2;
}
dest[0] = i0;
dest[1] = i1;
dest[2] = i2;
}

#if NET
Expand Down Expand Up @@ -351,16 +344,18 @@ public unsafe void EncodeOneOptionallyPadTwo(byte* oneByte, ushort* dest, ref by
uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 10));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 4) & 0x3F));

uint result;

if (BitConverter.IsLittleEndian)
{
dest[0] = (ushort)i0;
dest[1] = (ushort)i1;
result = (i0 | (i1 << 16));
}
else
{
dest[1] = (ushort)i0;
dest[0] = (ushort)i1;
result = ((i0 << 16) | i1);
}

Unsafe.WriteUnaligned(dest, result);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -371,22 +366,13 @@ public unsafe void EncodeTwoOptionallyPadOne(byte* twoBytes, ushort* dest, ref b

uint i = (t0 << 16) | (t1 << 8);

uint i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
uint i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
uint i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
ushort i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
ushort i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
ushort i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));

if (BitConverter.IsLittleEndian)
{
dest[0] = (ushort)i0;
dest[1] = (ushort)i1;
dest[2] = (ushort)i2;
}
else
{
dest[2] = (ushort)i0;
dest[1] = (ushort)i1;
dest[0] = (ushort)i2;
}
dest[0] = i0;
dest[1] = i1;
dest[2] = i2;
}

#if NET
Expand Down Expand Up @@ -441,25 +427,22 @@ public unsafe void EncodeThreeAndWrite(byte* threeBytes, ushort* destination, re

uint i = (t0 << 16) | (t1 << 8) | t2;

byte i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
byte i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
byte i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
byte i3 = Unsafe.Add(ref encodingMap, (IntPtr)(i & 0x3F));
ulong i0 = Unsafe.Add(ref encodingMap, (IntPtr)(i >> 18));
ulong i1 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 12) & 0x3F));
ulong i2 = Unsafe.Add(ref encodingMap, (IntPtr)((i >> 6) & 0x3F));
ulong i3 = Unsafe.Add(ref encodingMap, (IntPtr)(i & 0x3F));

ulong result;
if (BitConverter.IsLittleEndian)
{
destination[0] = i0;
destination[1] = i1;
destination[2] = i2;
destination[3] = i3;
result = i0 | (i1 << 16) | (i2 << 32) | (i3 << 48);
}
else
{
destination[3] = i0;
destination[2] = i1;
destination[1] = i2;
destination[0] = i3;
result = (i0 << 48) | (i1 << 32) | (i2 << 16) | i3;
}

Unsafe.WriteUnaligned(destination, result);
}
}
}
Expand Down

0 comments on commit 101c0da

Please sign in to comment.