Skip to content

Instantly share code, notes, and snippets.

@Blokyk
Created September 27, 2023 19:32
Show Gist options
  • Save Blokyk/476e36ccbc35a961c4efe859ef7d6002 to your computer and use it in GitHub Desktop.
Save Blokyk/476e36ccbc35a961c4efe859ef7d6002 to your computer and use it in GitHub Desktop.
Reverse 4-bytes endianness of Span of bytes (not vectorized)
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