1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

core/byteorder: add Big Endian implementations

This commit is contained in:
Benjamin Valentin 2020-07-27 18:36:07 +02:00
parent 42eb044ec6
commit df18bfb459

View File

@ -482,29 +482,52 @@ static inline uint64_t ntohll(uint64_t v)
static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (uint16_t)((buf[0] << 8) | (buf[1] << 0));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return (uint16_t)((buf[1] << 8) | (buf[0] << 0));
#endif
}
static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return (((uint32_t) buf[0] << 24)
| ((uint32_t) buf[1] << 16)
| ((uint32_t) buf[2] << 8)
| ((uint32_t) buf[3] << 0));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return (((uint32_t) buf[3] << 24)
| ((uint32_t) buf[2] << 16)
| ((uint32_t) buf[1] << 8)
| ((uint32_t) buf[0] << 0));
#endif
}
static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
buf[0] = (uint8_t)(val >> 8);
buf[1] = (uint8_t)(val >> 0);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
buf[1] = (uint8_t)(val >> 8);
buf[0] = (uint8_t)(val >> 0);
#endif
}
static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
buf[0] = (uint8_t)(val >> 24);
buf[1] = (uint8_t)(val >> 16);
buf[2] = (uint8_t)(val >> 8);
buf[3] = (uint8_t)(val >> 0);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
buf[3] = (uint8_t)(val >> 24);
buf[2] = (uint8_t)(val >> 16);
buf[1] = (uint8_t)(val >> 8);
buf[0] = (uint8_t)(val >> 0);
#endif
}
#ifdef __cplusplus