This question came up in a Power8 in-core crypto patch. The patch provides AES using Power8 built-ins. When loading a VSX register we need to perform a 128-bit endian reversal when running on a little-endian machine to ensure the VSX register loads the proper value.
At compile time we can check macros like __BYTE_ORDER__
. However, I believe we are supposed to check the machine status register at runtime. If msr.le=1
, then we perform the endian swap. Also see the AltiVec Programming Environment Manual, Section 3.1.4, p. 3-5.
How do we check the machine status register at runtime using built-ins?
You don't need to - it's known at compile time. Your instructions will be encoded completely incorrectly if you're running in the opposite endianness of your compiled code. So, your OS will ensure that your program is running in the correct
MSR[LE]
setting for the endianness of the executable.In essence: the
MSR[LE]
bit controls instructions as well as data loads/stores.There are some tricks we can use to detect endianness if we really have no idea, but unless you're writing super early boot code, you won't need that.