Skip to content

Commit

Permalink
endian: Make all endian related functions use the expicit type
Browse files Browse the repository at this point in the history
so the printf can give the unambiguous format specifier

Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 authored and acassis committed Oct 19, 2023
1 parent ee44351 commit bbfe945
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
19 changes: 6 additions & 13 deletions include/netinet/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,12 @@
* for uIP:
*/

#ifdef CONFIG_ENDIAN_BIG
# define HTONS(ns) (ns)
# define HTONL(nl) (nl)
# define HTONQ(nq) (nq)
#else
# define HTONS __swap_uint16
# define HTONL __swap_uint32
# define HTONQ __swap_uint64
#endif

#define NTOHS(hs) HTONS(hs)
#define NTOHL(hl) HTONL(hl)
#define NTOHQ(hq) HTONQ(hq)
#define HTONS htobe16
#define HTONL htobe32
#define HTONQ htobe64
#define NTOHS be16toh
#define NTOHL be32toh
#define NTOHQ be64toh

/****************************************************************************
* Public Type Definitions
Expand Down
56 changes: 28 additions & 28 deletions include/sys/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
/* Common byte swapping macros */

#ifdef CONFIG_HAVE_BUILTIN_BSWAP16
# define __swap_uint16 __builtin_bswap16
# define __swap_uint16(n) ((uint16_t)__builtin_bswap16(n))
#else
# define __swap_uint16(n) \
(uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \
((((uint16_t)(n)) >> 8) & 0x00ff))
#endif

#ifdef CONFIG_HAVE_BUILTIN_BSWAP32
# define __swap_uint32 __builtin_bswap32
# define __swap_uint32(n) ((uint32_t)__builtin_bswap32(n))
#else
# define __swap_uint32(n) \
(uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \
Expand All @@ -71,7 +71,7 @@

#ifdef CONFIG_HAVE_LONG_LONG
# ifdef CONFIG_HAVE_BUILTIN_BSWAP64
# define __swap_uint64 __builtin_bswap64
# define __swap_uint64(n) ((uint64_t)__builtin_bswap64(n))
# else
# define __swap_uint64(n) \
(uint64_t)(((((uint64_t)(n)) & 0x00000000000000ffULL) << 56) | \
Expand All @@ -95,21 +95,21 @@

/* Big-endian byte order macros */

# define htobe16(n) (n)
# define htole16(n) __swap_uint16((uint16_t)n)
# define be16toh(n) (n)
# define le16toh(n) __swap_uint16((uint16_t)n)
# define htobe16(n) ((uint16_t)(n))
# define htole16(n) __swap_uint16(n)
# define be16toh(n) ((uint16_t)(n))
# define le16toh(n) __swap_uint16(n)

# define htobe32(n) (n)
# define htole32(n) __swap_uint32((uint32_t)n)
# define be32toh(n) (n)
# define htobe32(n) ((uint32_t)(n))
# define htole32(n) __swap_uint32(n)
# define be32toh(n) ((uint32_t)(n))
# define le32toh(n) __swap_uint32(n)

# ifdef CONFIG_HAVE_LONG_LONG
# define htobe64(n) (n)
# define htole64(n) __swap_uint64((uint64_t)n)
# define be64toh(n) (n)
# define le64toh(n) __swap_uint64((uint64_t)n)
# define htobe64(n) ((uint64_t)(n))
# define htole64(n) __swap_uint64(n)
# define be64toh(n) ((uint64_t)(n))
# define le64toh(n) __swap_uint64(n)
# endif

#else
Expand All @@ -120,21 +120,21 @@

/* Little-endian byte order macros */

# define htobe16(n) __swap_uint16((uint16_t)n)
# define htole16(n) (n)
# define be16toh(n) __swap_uint16((uint16_t)n)
# define le16toh(n) (n)
# define htobe16(n) __swap_uint16(n)
# define htole16(n) ((uint16_t)(n))
# define be16toh(n) __swap_uint16(n)
# define le16toh(n) ((uint16_t)(n))

# define htobe32(n) __swap_uint32((uint32_t)n)
# define htole32(n) (n)
# define be32toh(n) __swap_uint32((uint32_t)n)
# define le32toh(n) (n)
# define htobe32(n) __swap_uint32(n)
# define htole32(n) ((uint32_t)(n))
# define be32toh(n) __swap_uint32(n)
# define le32toh(n) ((uint32_t)(n))

# ifdef CONFIG_HAVE_LONG_LONG
# define htobe64(n) __swap_uint64((uint64_t)n)
# define htole64(n) (n)
# define be64toh(n) __swap_uint64((uint64_t)n)
# define le64toh(n) (n)
# define htobe64(n) __swap_uint64(n)
# define htole64(n) ((uint64_t)(n))
# define be64toh(n) __swap_uint64(n)
# define le64toh(n) ((uint64_t)(n))
# endif
#endif

Expand All @@ -152,15 +152,15 @@
#define htolem16(x, v) (*(FAR uint16_t *)(x) = htole16(v))
#define betoh32 be32toh
#define letoh32 le32toh
#define bemtoh32(x) htobe32(*(FAR uint32_t *)(x))
#define bemtoh32(x) betoh32(*(FAR uint32_t *)(x))
#define htobem32(x, v) (*(FAR uint32_t *)(x) = htobe32(v))
#define lemtoh32(x) letoh32(*(FAR uint32_t *)(x))
#define htolem32(x, v) (*(FAR uint32_t *)(x) = htole32(v))

#ifdef CONFIG_HAVE_LONG_LONG
# define betoh64 be64toh
# define letoh64 le64toh
# define bemtoh64(x) htobe64(*(FAR uint64_t *)(x))
# define bemtoh64(x) betoh64(*(FAR uint64_t *)(x))
# define htobem64(x, v) (*(FAR uint64_t *)(x) = htobe64(v))
# define lemtoh64(x) letoh64(*(FAR uint64_t *)(x))
# define htolem64(x, v) (*(FAR uint64_t *)(x) = htole64(v))
Expand Down

0 comments on commit bbfe945

Please sign in to comment.