Created
September 27, 2023 19:32
-
-
Save Blokyk/476e36ccbc35a961c4efe859ef7d6002 to your computer and use it in GitHub Desktop.
Reverse 4-bytes endianness of Span of bytes (not vectorized)
This file contains 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
using System; | |
using System.Collections.Generic; | |
static class SpanExtensions | |
{ | |
// just a quick and dirty version, could be a lot faster | |
// by just using Vector<byte>.Shuffle if available | |
public static void Reverse4ByteEndianness(Span<byte> bytes) { | |
byte b0, b1, b2, b3; | |
for (int i = 3; i < bytes.Length; i += 4) { | |
b0 = bytes[i]; | |
b1 = bytes[i - 1]; | |
b2 = bytes[i - 2]; | |
b3 = bytes[i - 3]; | |
bytes[i] = b3; | |
bytes[i-1] = b2; | |
bytes[i-2] = b1; | |
bytes[i-3] = b0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment