mirror of
https://github.com/ksherlock/mpw.git
synced 2024-11-26 11:49:19 +00:00
80 lines
1.9 KiB
C
80 lines
1.9 KiB
C
#ifndef __mpw_endian_h__
|
|
#define __mpw_endian_h__
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#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
|
|
/* aix doesn't have an endian, but gcc #defines _BIG_ENDIAN=1 and __BIG_ENDIAN__=1 */
|
|
#error missing endian.h
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define bswap16 __builtin_bswap16
|
|
#define bswap32 __builtin_bswap32
|
|
#define bswap64 __builtin_bswap64
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
#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))
|
|
#else /* BYTE_ORDER != LITTLE_ENDIAN */
|
|
#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))
|
|
#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");
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|