2016-08-09 14:30:29 +00:00
|
|
|
#ifndef __mpw_endian_h__
|
|
|
|
#define __mpw_endian_h__
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-08-09 15:56:25 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-08-09 14:30:29 +00:00
|
|
|
#if defined(HAVE_ENDIAN_H)
|
|
|
|
#include <endian.h>
|
|
|
|
#elif defined(HAVE_SYS_ENDIAN_H)
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#elif defined(HAVE_MACHINE_ENDIAN_H)
|
|
|
|
#include <machine/endian.h>
|
|
|
|
#else
|
2016-08-10 23:37:08 +00:00
|
|
|
/* aix doesn't have an endian, but gcc #defines _BIG_ENDIAN=1 and __BIG_ENDIAN__=1 */
|
2016-08-09 14:30:29 +00:00
|
|
|
#error missing endian.h
|
|
|
|
#endif
|
|
|
|
|
2016-08-09 15:56:25 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
#define bswap16 __builtin_bswap16
|
|
|
|
#define bswap32 __builtin_bswap32
|
|
|
|
#define bswap64 __builtin_bswap64
|
|
|
|
|
2016-08-09 17:29:07 +00:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
2016-08-09 15:56:25 +00:00
|
|
|
#define htobe16(x) bswap16((x))
|
|
|
|
#define htobe32(x) bswap32((x))
|
|
|
|
#define htobe64(x) bswap64((x))
|
|
|
|
#define htole16(x) ((uint16_t)(x))
|
|
|
|
#define htole32(x) ((uint32_t)(x))
|
|
|
|
#define htole64(x) ((uint64_t)(x))
|
|
|
|
|
|
|
|
#define be16toh(x) bswap16((x))
|
|
|
|
#define be32toh(x) bswap32((x))
|
|
|
|
#define be64toh(x) bswap64((x))
|
|
|
|
#define le16toh(x) ((uint16_t)(x))
|
|
|
|
#define le32toh(x) ((uint32_t)(x))
|
|
|
|
#define le64toh(x) ((uint64_t)(x))
|
2016-08-09 17:29:07 +00:00
|
|
|
#else /* BYTE_ORDER != LITTLE_ENDIAN */
|
2016-08-09 15:56:25 +00:00
|
|
|
#define htobe16(x) ((uint16_t)(x))
|
|
|
|
#define htobe32(x) ((uint32_t)(x))
|
|
|
|
#define htobe64(x) ((uint64_t)(x))
|
|
|
|
#define htole16(x) bswap16((x))
|
|
|
|
#define htole32(x) bswap32((x))
|
|
|
|
#define htole64(x) bswap64((x))
|
|
|
|
|
|
|
|
#define be16toh(x) ((uint16_t)(x))
|
|
|
|
#define be32toh(x) ((uint32_t)(x))
|
|
|
|
#define be64toh(x) ((uint64_t)(x))
|
|
|
|
#define le16toh(x) bswap16((x))
|
|
|
|
#define le32toh(x) bswap32((x))
|
|
|
|
#define le64toh(x) bswap64((x))
|
2016-08-09 17:29:07 +00:00
|
|
|
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
// https://github.com/HowardHinnant/hash_append/blob/master/endian.h
|
|
|
|
enum class endian
|
|
|
|
{
|
|
|
|
native = LITTLE_ENDIAN,
|
|
|
|
little = LITTLE_ENDIAN,
|
|
|
|
big = BIG_ENDIAN
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(endian::native == endian::little ||
|
|
|
|
endian::native == endian::big,
|
|
|
|
"endian::native shall be one of endian::little or endian::big");
|
|
|
|
|
|
|
|
static_assert(endian::big != endian::little,
|
|
|
|
"endian::big and endian::little shall have different values");
|
|
|
|
|
2016-08-09 15:56:25 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-08-09 14:30:29 +00:00
|
|
|
#endif
|
|
|
|
|