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

Merge pull request #14621 from benpicco/core/byteorder-be

core/byteorder: add Big Endian implementations
This commit is contained in:
benpicco 2020-07-28 10:30:27 +02:00 committed by GitHub
commit 6ef4276c35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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