Keith Thompson <
ks...@mib.org> writes:
>sc...@slp53.sl.home (Scott Lurndal) writes:
>> David Brown <
david...@hesbynett.no> writes:
>[...]
>>>The /compiler/ can handle it at compile time - it can figure out the
>>>result of this function at compile time and use that in optimisation.
>>
>> Can it? How does that work when you're cross-compiling to a different
>> architecture (like a microcontroller, for example)?
>
>Presumably the compiler knows the endianness of the target architecture
>it's generating code for. There might be an issue with the compiler not
>having that information soon enough, but it could certainly get the
>information if it's worthwhile.
Each ring in ARM64 can be set to run big- or little-endian. In ARMv7,
the application itself can change the endianness dynamically (SETEND
instruction). Linux supports reading the endianness from the ELF
header on Arm64 and will configure the process state accordingly.
So, absent some indication to the compiler on the command line
which endianness is desired, there doesn't seem to be a way for the
compiler to figure it out itself.
There is absolutely nothing wrong with using the pre-processor and
implementation defined (or specified by the programmer with -D)
macros to determine which endianness is being used.
We write a lot of code that is required to run in both big- and
little-endian environments, and one of the larger issues with
portability is related to bitfields. All of our structures with
bitfields have declarations similar to:
struct INTCTLR_CMD_CLEAR_s {
#if __BYTE_ORDER == __BIG_ENDIAN
uint64_t dev_id : 32; /**< [ 63: 32] Interrupt device ID. */
uint64_t reserved_8_31 : 24;
uint64_t cmd_type : 8; /**< [ 7: 0] Command type. Indicates GITS_CMD_TYPE_E::CMD_CLEAR. */
#else
uint64_t cmd_type : 8;
uint64_t reserved_8_31 : 24;
uint64_t dev_id : 32;
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
uint64_t reserved_96_127 : 32;
uint64_t int_id : 32; /**< [ 95: 64] Interrupt ID to be translated. */
#else
uint64_t int_id : 32;
uint64_t reserved_96_127 : 32;
#endif
uint64_t reserved_128_191 : 64;
uint64_t reserved_192_255 : 64;
} s;