2

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?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

2

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.

Jeremy Kerr
  • 1,895
  • 12
  • 24
  • Thanks @Jeremy. So I am clear: we only need to check at compile time. We do not need to check at runtime. Is that correct? – jww Sep 12 '17 at 03:11
  • Yes, exactly. Just use `__BYTE_ORDER__`, as you've already found. If the LE bit is set for the opposite endianness, you can't run any code to check endianness anyway :) – Jeremy Kerr Sep 12 '17 at 03:13
  • Ack, thanks. I was not aware `msr[le]` applied to instructions as well. If I knew that, then I probably would not have asked the question in the first place :o – jww Sep 12 '17 at 03:14
  • All good! Nice to have the question asked regardless, so it's there for future users... – Jeremy Kerr Sep 12 '17 at 03:28