1) Are there any compiler builtins, or assembly instructions for x86, ARM or another architecture that will take a big endian byte array (2 bytes -> uint16_t, 4 bytes -> uint32_t, 8 bytes -> uint64_t) and convert it to an unsigned integer of native endianness (big, little, mixed) .
2) Are there also any builtins or instructions to perform the inverse conversion (integer to big endian byte array).
Naive native C functions would be:
static inline void put_be16(uint8_t a[static sizeof(uint16_t)], uint16_t val)
{
a[0] = (val >> 8) & 0xff;
a[1] = val & 0xff;
}
static inline uint16_t get_be16(uint8_t const a[static sizeof(uint16_t)])
{
return (a[0] << 8) | a[1];
}
These are for reading unsigned integers from inbound network packets, and encoding unsigned integers for use in outbound network packets.
Solutions must prevent, or mitigate unaligned memory accesses.
Edit: And looking to be efficient, so something that operates directly on the input/output buffer is what I'm really looking for.