Elaborating on Peter's comment, there is most certainly an implicit endianness inherent to the SIMD implementation, owing to the fact that there are instructions that can:
- Read and write 128-bit SIMD registers from memory. Since memory must always be accessed by byte offset (regardless of the instruction or how many bytes it stores or fetches), and can subsequently be examined by other non-SIMD means, the
movaps
, movdqa
, movdqu
, etc. instructions inherently imply an endianness.
- Index vector elements with instructions like
pshufd
and even runtime-variable indexing with pshufb
that use an integer index to select elements. This means that elements have something like addresses, and wide elements contain multiple independently-addressable narrow elements. (Not part of memory address space, of course, but unlike scalar registers we have a 2nd way to talk about position other than left/right shift within a wide element. This is the same thing that makes endianness an issue for memory.)
The indexing of elements within a register is chosen to match the order in memory (little-endian), but it could have been different.
- Shift bits across byte boundaries of a SIMD register with
pslld
, psrld
, or whole-vector byte-shifts like pslldq
etc. Note that crossing "byte boundaries" includes within the individual word
, dword
, or qword
components, because (for the same reason noted in the previous point), the register can be subsequently be imaged to memory. A byte-shift of a whole vector groups the low byte of one word with the high byte of the adjacent word, in a way that depends on endianness.
- Re-interpret the component size (
byte
, word
, dword
, or qword
) of an existing SIMD register's contents. This is the analogue of reading the bytes of a dword
in memory: they have an order. Shuffling around qwords using pshufd
requires you to consider the endianness when choosing the shuffle control, to keep the right high:low pairs of dwords grouped in the right order.
So while it's true that if you never do any of these things, meaning you exclusively use SIMD memory images with SIMD registers, with matching component sizes and never examine that memory otherwise, and also maintain consistent component sizes in operations on those SIMD registers, then you don't have to worry about SIMD endianness. Otherwise, read on...
Knowing now that the SIMD operations listed above expose an endianness, what then is it? Well, we already know that Intel architecture is little-endian, meaning word
, dword
, and qword
(respectively, 16-, 32-, and 64-bit memory accesses) are recursively swapped. For example, storing a single qword
swaps the stores of its two dwords
, each of which swaps its two words
, each of which swaps its two bytes
. This results in the memory image of a CPU register having a reversed byte
order overall.
For compatibility with non-SIMD instructions operating on the same size, the memory image of each individual component of a SIMD register should be bit-identical, for every component size, with existing (little-endian) format. The prior existence of non-SIMD instructions for word
, dword
, and qword
accesses thus represent hard constraints, and those SIMD components must manifest little-endian images.
But there are no prior non-SIMD instructions for 128-bit memory access, so there isn't a prior constraint on the (qword, qword)
layout of the dqword
SIMD register itself. That leaves really just the one possible question we could be asking here: does the recursive little-endian swapping pattern (word
, dword
, qword
, ...?
) continue, applying to dqword
values as well? In other words, in the 16-byte memory image of a SIMD register, which of its two qword
components—the numerically least-significant, or the more-significant—is stored in the lower-addressed 8-bytes?
ANSWER: The least-significant qword
is stored at the l̲o̲w̲e̲r̲-address 8-bytes.
This preserves the symmetry of the "little-endian" recursive swapping, by extending the pattern to include dqword
values as well. To summarize, a 128-bit SIMD register is little-endian, because its memory image at [esi]
has:
- the less-significant
qword
(SIMD index 0) at the lower address qword ptr [esi]
,
- the more-significant
qword
(SIMD index 1) at the higher address qword ptr [esi + 8]
.