0

I've got 64 bytes of continuous memory organized into big-endian DWORDs. I'd like to copy this data and and organize as little-endian elsewhere. I could do that DWORD after DWORD, e. g.:

mov eax, [rsi + rcx]
bswap eax                ; setting proper endianness
mov [rdi + rcx], eax

But I feel there's a more efficient way, using xmm/ymm/zmm registers and instructions such as vmovdqa/vmovdqu. The problem is, those only cover the copying part, without setting the endianness. So my question is - is there any smart way of organizing such a large chunk of data into DWORDs, or do I have to do it manually, as shown in the snippet? Or perhaps there's another efficient way that doesn't involve any of the above?

Edit: After @Erik Eidt's comment, I altered the question, because the original wording was confusing and didn't exactly describe the problem at hand properly.

mdx
  • 534
  • 3
  • 16
  • 2
    You can use [`vpshufb`](https://www.felixcloutier.com/x86/pshufb) for the swapping. – chtz Nov 20 '20 at 15:45
  • If you really just have raw bytes, then there is no concept of endian. If you have some data structure like an array of dwords, that is different -- you would have to know the current endian in order to have swapping make sense. If you have a structure instead of an array, then each field could require its own endian adjustment (byte sized fields and arrays: nothing; word sized fields simply swapping of 2 bytes; dword: 4 byte swap, etc..) – Erik Eidt Nov 20 '20 at 16:04
  • @Erik Eidt "If you really just have raw bytes, then there is no concept of endian." Of course, you are right, I failed to describe the problem correctly. I'd like to copy those raw bytes **as if** they were big-endian dwords, changing them to little-endian in the destination location. I guess that means it was never "raw bytes" in the first place, my bad. – mdx Nov 20 '20 at 17:22
  • @chtz Thank you, I'll check it out. – mdx Nov 20 '20 at 17:22
  • 1
    You can use C with intrinsics for this; compilers generate decent asm. If you *want* to write in asm for some reason, that's fine. My answer on the linked question talks about what asm instructions you want, whether it's compiler-generated or hand-written. – Peter Cordes Nov 20 '20 at 19:53

0 Answers0