Repository: SerenityOS/serenity
File: Userland/Libraries/LibC/endian.h (C/C++)

Code before:
#include <stdint.h>

static __inline uint16_t __bswap16(uint16_t x)
{
    return __builtin_bswap16(x);
}

static __inline uint32_t __bswap32(uint32_t x)
{
    return __builtin_bswap32(x);
}

static __inline uint64_t __bswap64(uint64_t x)
{
    return __builtin_bswap64(x);
}

#define LITTLE_ENDIAN __LITTLE_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#define PDP_ENDIAN __PDP_ENDIAN
#define BYTE_ORDER __BYTE_ORDER

#if __BYTE_ORDER == __LITTLE_ENDIAN
#    define htole16(x) ((uint16_t)(x))
#    define le16toh(x) ((uint16_t)(x))
#    define htole32(x) ((uint32_t)(x))
#    define le32toh(x) ((uint32_t)(x))
#    define htole64(x) ((uint64_t)(x))
#    define le64toh(x) ((uint64_t)(x))
#    define htobe16(x) (__builtin_bswap16(x))
#    define be16toh(x) (__builtin_bswap16(x))
#    define htobe32(x) (__builtin_bswap32(x))
#    define be32toh(x) (__builtin_bswap32(x))
#    define htobe64(x) (__builtin_bswap64(x))
#    define be64toh(x) (__builtin_bswap64(x))
#else
#    define htole16(x) (__builtin_bswap16(x))
#    define le16toh(x) (__builtin_bswap16(x))
#    define htole32(x) (__builtin_bswap32(x))
#    define le32toh(x) (__builtin_bswap32(x))
#    define htole64(x) (__builtin_bswap64(x))
#    define le64toh(x) (__builtin_bswap64(x))
#    define htobe16(x) ((uint16_t)(x))
#    define be16toh(x) ((uint16_t)(x))
#    define htobe32(x) ((uint32_t)(x))
#    define be32toh(x) ((uint32_t)(x))
#    define htobe64(x) ((uint64_t)(x))
#    define be64toh(x) ((uint64_t)(x))
#endif

__END_DECLS

Review comment (suggestion):
We have a bunch of things that aren't in posix. I suppose if nothing needs it, removing it is fine, but the reason should be "nothing needs this", not "isn't in posix".

Code after:
//      If BYTE_ORDER == LITTLE_ENDIAN, the host byte order is from least significant to most significant."
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__

// "BIG_ENDIAN
//      If BYTE_ORDER == BIG_ENDIAN, the host byte order is from most significant to least significant."
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__

// These definitions are not required by POSIX, but are assumed to be present by some ports.
#define __BYTE_ORDER BYTE_ORDER
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN

#if BYTE_ORDER == LITTLE_ENDIAN
#    define be16toh(x) (__builtin_bswap16(x))
#    define be32toh(x) (__builtin_bswap32(x))
#    define be64toh(x) (__builtin_bswap64(x))

#    define htobe16(x) (__builtin_bswap16(x))
#    define htobe32(x) (__builtin_bswap32(x))
#    define htobe64(x) (__builtin_bswap64(x))

#    define htole16(x) ((uint16_t)(x))
#    define htole32(x) ((uint32_t)(x))
#    define htole64(x) ((uint64_t)(x))

#    define le16toh(x) ((uint16_t)(x))
#    define le32toh(x) ((uint32_t)(x))
#    define le64toh(x) ((uint64_t)(x))
#else
#    define be16toh(x) ((uint16_t)(x))
#    define be32toh(x) ((uint32_t)(x))
#    define be64toh(x) ((uint64_t)(x))

#    define htobe16(x) ((uint16_t)(x))
#    define htobe32(x) ((uint32_t)(x))
#    define htobe64(x) ((uint64_t)(x))

#    define htole16(x) (__builtin_bswap16(x))
#    define htole32(x) (__builtin_bswap32(x))
#    define htole64(x) (__builtin_bswap64(x))

#    define le16toh(x) (__builtin_bswap16(x))
#    define le32toh(x) (__builtin_bswap32(x))
#    define le64toh(x) (__builtin_bswap64(x))
#endif

__END_DECLS