
/* Emscripten does not have endian.h */
# if defined(EMSCRIPTEN)

#  include <endian.h>

/* MinGW implements some MSVC idioms, so always test for MinGW first. */
# elif defined(__MINGW32__) || defined(__riscos__)

# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

#  define htobe16(x) htons(x)
#  define htole16(x) (x)
#  define be16toh(x) ntohs(x)
#  define le16toh(x) (x)

#  define htobe32(x) htonl(x)
#  define htole32(x) (x)
#  define be32toh(x) ntohl(x)
#  define le32toh(x) (x)

#  define htobe64(x) htonll(x)
#  define htole64(x) (x)
#  define be64toh(x) ntohll(x)
#  define le64toh(x) (x)

# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__

#  define htobe16(x) (x)
#  define htole16(x) __builtin_bswap16(x)
#  define be16toh(x) (x)
#  define le16toh(x) __builtin_bswap16(x)

#  define htobe32(x) (x)
#  define htole32(x) __builtin_bswap32(x)
#  define be32toh(x) (x)
#  define le32toh(x) __builtin_bswap32(x)

#  define htobe64(x) (x)
#  define htole64(x) __builtin_bswap64(x)
#  define be64toh(x) (x)
#  define le64toh(x) __builtin_bswap64(x)

# else
#  error Unexpected __BYTE_ORDER__

# endif /* __MINGW__ __BYTE_ORDER__ */

#elif defined(_MSC_VER)

# if BYTE_ORDER == LITTLE_ENDIAN

# define htobe16(x) htons(x)
# define htole16(x) (x)
# define be16toh(x) ntohs(x)
# define le16toh(x) (x)

# define htobe32(x) htonl(x)
# define htole32(x) (x)
# define be32toh(x) ntohl(x)
# define le32toh(x) (x)

# define htobe64(x) htonll(x)
# define htole64(x) (x)
# define be64toh(x) ntohll(x)
# define le64toh(x) (x)

# elif BYTE_ORDER == BIG_ENDIAN

# define htobe16(x) (x)
# define htole16(x) __builtin_bswap16(x)
# define be16toh(x) (x)
# define le16toh(x) __builtin_bswap16(x)

# define htobe32(x) (x)
# define htole32(x) __builtin_bswap32(x)
# define be32toh(x) (x)
# define le32toh(x) __builtin_bswap32(x)

# define htobe64(x) (x)
# define htole64(x) __builtin_bswap64(x)
# define be64toh(x) (x)
# define le64toh(x) __builtin_bswap64(x)

# else
# error Unexpected BYTE_ORDER.

# endif /* _MSC_VER BYTE_ORDER */

#elif defined(__APPLE__)
 /* This is a simple compatibility shim to convert
 * BSD/Linux endian macros to the Mac OS X equivalents. */
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)

#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)

#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)

/* QNX 8.0 aarch64 (little-endian) */
/* QNX 8.0 aarch64 (little-endian) */
#elif defined(__QNXNTO__)
#  include <stdint.h>
#  include <gulliver.h>

  /* Undefine QNX's conflicting macros from net/netbyte.h */
#  ifdef htobe16
#    undef htobe16
#  endif
#  ifdef htobe32
#    undef htobe32
#  endif
#  ifdef htobe64
#    undef htobe64
#  endif
#  ifdef htole16
#    undef htole16
#  endif
#  ifdef htole32
#    undef htole32
#  endif
#  ifdef htole64
#    undef htole64
#  endif
#  ifdef be16toh
#    undef be16toh
#  endif
#  ifdef be32toh
#    undef be32toh
#  endif
#  ifdef be64toh
#    undef be64toh
#  endif
#  ifdef le16toh
#    undef le16toh
#  endif
#  ifdef le32toh
#    undef le32toh
#  endif
#  ifdef le64toh
#    undef le64toh
#  endif

#  if !defined(__LITTLE_ENDIAN)
#    define __LITTLE_ENDIAN 1234
#  endif
#  if !defined(__BIG_ENDIAN)
#    define __BIG_ENDIAN 4321
#  endif

#  ifndef DOSBOX_BYTEORDER_QNX_DEFINED
#    define DOSBOX_BYTEORDER_QNX_DEFINED

#    if defined(__LITTLEENDIAN__)
#      define __BYTE_ORDER __LITTLE_ENDIAN
#      define BYTE_ORDER     LITTLE_ENDIAN
#      define LITTLE_ENDIAN  __LITTLE_ENDIAN
#      define BIG_ENDIAN     __BIG_ENDIAN

      extern "C++" inline uint16_t htobe16(uint16_t x) { return ENDIAN_RET16(x); }
      extern "C++" inline uint32_t htobe32(uint32_t x) { return ENDIAN_RET32(x); }
      extern "C++" inline uint64_t htobe64(uint64_t x) { return ENDIAN_RET64(x); }

      extern "C++" inline uint16_t htole16(uint16_t x) { return x; }
      extern "C++" inline uint32_t htole32(uint32_t x) { return x; }
      extern "C++" inline uint64_t htole64(uint64_t x) { return x; }

      extern "C++" inline uint16_t be16toh(uint16_t x) { return ENDIAN_RET16(x); }
      extern "C++" inline uint32_t be32toh(uint32_t x) { return ENDIAN_RET32(x); }
      extern "C++" inline uint64_t be64toh(uint64_t x) { return ENDIAN_RET64(x); }

      extern "C++" inline uint16_t le16toh(uint16_t x) { return x; }
      extern "C++" inline uint32_t le32toh(uint32_t x) { return x; }
      extern "C++" inline uint64_t le64toh(uint64_t x) { return x; }

#    elif defined(__BIGENDIAN__)
#      define __BYTE_ORDER __BIG_ENDIAN
#      define BYTE_ORDER     BIG_ENDIAN
#      define LITTLE_ENDIAN  __LITTLE_ENDIAN
#      define BIG_ENDIAN     __BIG_ENDIAN

      extern "C++" inline uint16_t htobe16(uint16_t x) { return x; }
      extern "C++" inline uint32_t htobe32(uint32_t x) { return x; }
      extern "C++" inline uint64_t htobe64(uint64_t x) { return x; }

      extern "C++" inline uint16_t htole16(uint16_t x) { return ENDIAN_RET16(x); }
      extern "C++" inline uint32_t htole32(uint32_t x) { return ENDIAN_RET32(x); }
      extern "C++" inline uint64_t htole64(uint64_t x) { return ENDIAN_RET64(x); }

      extern "C++" inline uint16_t be16toh(uint16_t x) { return x; }
      extern "C++" inline uint32_t be32toh(uint32_t x) { return x; }
      extern "C++" inline uint64_t be64toh(uint64_t x) { return x; }

      extern "C++" inline uint16_t le16toh(uint16_t x) { return ENDIAN_RET16(x); }
      extern "C++" inline uint32_t le32toh(uint32_t x) { return ENDIAN_RET32(x); }
      extern "C++" inline uint64_t le64toh(uint64_t x) { return ENDIAN_RET64(x); }

#    else
#      error "QNX: Unknown endianness"
#    endif
#  endif


#elif defined(__linux__) || defined(__CYGWIN__)

#include <endian.h>

#elif defined(__HAIKU__)

#define _BSD_SOURCE
#include <endian.h>

#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)

#include <sys/endian.h>

#elif defined(__DragonFly__)

#include <sys/endian.h>

#define be16toh(x) betoh16(x)
#define le16toh(x) letoh16(x)

#define be32toh(x) betoh32(x)
#define le32toh(x) letoh32(x)

#define be64toh(x) betoh64(x)
#define le64toh(x) letoh64(x)

#elif defined(__HAIKU__) || defined(OS2)

#include <endian.h>

#endif
