Add support for endianness convertion function in endian.h Those macros implement interfaces to convert values between host and big-/little-endian byte order as defined in standard endian.h header.
Also make exisiting hton* and ntoh* macros use same helpers as above funtions. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/c78ed8a3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/c78ed8a3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/c78ed8a3 Branch: refs/heads/develop Commit: c78ed8a3f43119eff3d488ed44af544d8735c1ec Parents: 31f894b Author: Szymon Janc <[email protected]> Authored: Fri Jan 20 19:36:55 2017 +0100 Committer: Christopher Collins <[email protected]> Committed: Tue Jan 24 16:03:30 2017 -0800 ---------------------------------------------------------------------- kernel/os/include/os/endian.h | 143 ++++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c78ed8a3/kernel/os/include/os/endian.h ---------------------------------------------------------------------- diff --git a/kernel/os/include/os/endian.h b/kernel/os/include/os/endian.h index 959d74c..8786f40 100644 --- a/kernel/os/include/os/endian.h +++ b/kernel/os/include/os/endian.h @@ -26,6 +26,33 @@ extern "C" { #endif +/* Internal helpers */ +#ifndef __bswap_64 +#define __bswap_64(x) ((uint64_t) \ + ((((x) & 0xff00000000000000ull) >> 56) | \ + (((x) & 0x00ff000000000000ull) >> 40) | \ + (((x) & 0x0000ff0000000000ull) >> 24) | \ + (((x) & 0x000000ff00000000ull) >> 8) | \ + (((x) & 0x00000000ff000000ull) << 8) | \ + (((x) & 0x0000000000ff0000ull) << 24) | \ + (((x) & 0x000000000000ff00ull) << 40) | \ + (((x) & 0x00000000000000ffull) << 56))) +#endif + +#ifndef __bswap_32 +#define __bswap_32(x) ((uint32_t) \ + ((((x) & 0xff000000) >> 24) | \ + (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | \ + (((x) & 0x000000ff) << 24))) +#endif + +#ifndef __bswap_16 +#define __bswap_16(x) ((uint16_t) \ + ((((x) & 0xff00) >> 8) | \ + (((x) & 0x00ff) << 8))) +#endif + #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #ifndef ntohll @@ -52,18 +79,58 @@ extern "C" { #define htons(x) ((uint16_t)(x)) #endif +#ifndef htobe16 +#define htobe16(x) ((uint16_t)(x)) +#endif + +#ifndef htole16 +#define htole16(x) __bswap_16 (x) +#endif + +#ifndef be16toh +#define be16toh(x) ((uint16_t)(x)) +#endif + +#ifndef le16toh +#define le16toh(x) __bswap_16 (x) +#endif + +#ifndef htobe32 +#define htobe32(x) ((uint32_t)(x)) +#endif + +#ifndef htole32 +#define htole32(x) __bswap_32 (x) +#endif + +#ifndef be32toh +#define be32toh(x) ((uint32_t)(x)) +#endif + +#ifndef le32toh +#define le32toh(x) __bswap_32 (x) +#endif + +#ifndef htobe64 +#define htobe64(x) ((uint64_t)(x)) +#endif + +#ifndef htole64 +#define htole64(x) __bswap_64 (x) +#endif + +#ifndef be64toh +#define be64toh(x) ((uint64_t)(x)) +#endif + +#ifndef le64toh +#define le64toh(x) __bswap_64 (x) +#endif + #else #ifndef ntohll -#define ntohll(x) ((uint64_t) \ - ((((x) & 0xff00000000000000ull) >> 56) | \ - (((x) & 0x00ff000000000000ull) >> 40) | \ - (((x) & 0x0000ff0000000000ull) >> 24) | \ - (((x) & 0x000000ff00000000ull) >> 8) | \ - (((x) & 0x00000000ff000000ull) << 8) | \ - (((x) & 0x0000000000ff0000ull) << 24) | \ - (((x) & 0x000000000000ff00ull) << 40) | \ - (((x) & 0x00000000000000ffull) << 56))) +#define ntohll(x) __bswap_64(x) #endif #ifndef htonll @@ -71,11 +138,7 @@ extern "C" { #endif #ifndef ntohl -#define ntohl(x) ((uint32_t) \ - ((((x) & 0xff000000) >> 24) | \ - (((x) & 0x00ff0000) >> 8) | \ - (((x) & 0x0000ff00) << 8) | \ - (((x) & 0x000000ff) << 24))) +#define ntohl(x) __bswap_32(x) #endif #ifndef htonl @@ -83,15 +146,61 @@ extern "C" { #endif #ifndef htons -#define htons(x) ((uint16_t) \ - ((((x) & 0xff00) >> 8) | \ - (((x) & 0x00ff) << 8))) +#define htons(x) __bswap_16(x) #endif #ifndef ntohs #define ntohs htons #endif +#ifndef htobe16 +#define htobe16(x) __bswap_16(x) +#endif + +#ifndef htole16 +#define htole16(x) ((uint16_t)(x)) +#endif + +#ifndef be16toh +#define be16toh(x) __bswap_16(x) +#endif + +#ifndef le16toh +#define le16toh(x) ((uint16_t)(x)) +#endif + +#ifndef htobe32 +#define htobe32(x) __bswap_32(x) +#endif + +#ifndef htole32 +#define htole32(x) ((uint32_t)(x)) +#endif + +#ifndef be32toh +#define be32toh(x) __bswap_32(x) +#endif + +#ifndef le32toh +#define le32toh(x) ((uint32_t)(x)) +#endif + +#ifndef htobe64 +#define htobe64(x) __builtin_bswap64(x) +#endif + +#ifndef htole64 +#define htole64(x) ((uint64_t)(x)) +#endif + +#ifndef be64toh +#define be64toh(x) __builtin_bswap64(x) +#endif + +#ifndef le64toh +#define le64toh(x) ((uint64_t)(x)) +#endif + #endif #ifdef __cplusplus }
