#ifndef _SYS__ENDIAN_H_
#define _SYS__ENDIAN_H_
#include <sys/_types.h>
#define __FROM_SYS__ENDIAN
#include <machine/endian.h>
#undef __FROM_SYS__ENDIAN
#define _LITTLE_ENDIAN 1234
#define _BIG_ENDIAN 4321
#define _PDP_ENDIAN 3412
#define __swap16gen(x) \
(__uint16_t)(((__uint16_t)(x) & 0xffU) << 8 | ((__uint16_t)(x) & 0xff00U) >> 8)
#define __swap32gen(x) \
(__uint32_t)(((__uint32_t)(x) & 0xff) << 24 | \
((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
((__uint32_t)(x) & 0xff000000) >> 24)
#define __swap64gen(x) \
(__uint64_t)((((__uint64_t)(x) & 0xff) << 56) | \
((__uint64_t)(x) & 0xff00ULL) << 40 | \
((__uint64_t)(x) & 0xff0000ULL) << 24 | \
((__uint64_t)(x) & 0xff000000ULL) << 8 | \
((__uint64_t)(x) & 0xff00000000ULL) >> 8 | \
((__uint64_t)(x) & 0xff0000000000ULL) >> 24 | \
((__uint64_t)(x) & 0xff000000000000ULL) >> 40 | \
((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
#ifndef __HAVE_MD_SWAP
static __inline __uint16_t
__swap16md(__uint16_t x)
{
return (__swap16gen(x));
}
static __inline __uint32_t
__swap32md(__uint32_t x)
{
return (__swap32gen(x));
}
static __inline __uint64_t
__swap64md(__uint64_t x)
{
return (__swap64gen(x));
}
#endif
#define __swap16(x) \
(__uint16_t)(__builtin_constant_p(x) ? __swap16gen(x) : __swap16md(x))
#define __swap32(x) \
(__uint32_t)(__builtin_constant_p(x) ? __swap32gen(x) : __swap32md(x))
#define __swap64(x) \
(__uint64_t)(__builtin_constant_p(x) ? __swap64gen(x) : __swap64md(x))
#if _BYTE_ORDER == _LITTLE_ENDIAN
#define _QUAD_HIGHWORD 1
#define _QUAD_LOWWORD 0
#define __htobe16 __swap16
#define __htobe32 __swap32
#define __htobe64 __swap64
#define __htole16(x) ((__uint16_t)(x))
#define __htole32(x) ((__uint32_t)(x))
#define __htole64(x) ((__uint64_t)(x))
#ifdef _KERNEL
#ifdef __HAVE_MD_SWAPIO
#define __bemtoh16(_x) __mswap16(_x)
#define __bemtoh32(_x) __mswap32(_x)
#define __bemtoh64(_x) __mswap64(_x)
#define __htobem16(_x, _v) __swapm16((_x), (_v))
#define __htobem32(_x, _v) __swapm32((_x), (_v))
#define __htobem64(_x, _v) __swapm64((_x), (_v))
#endif
#endif
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
#define _QUAD_HIGHWORD 0
#define _QUAD_LOWWORD 1
#define __htobe16(x) ((__uint16_t)(x))
#define __htobe32(x) ((__uint32_t)(x))
#define __htobe64(x) ((__uint64_t)(x))
#define __htole16 __swap16
#define __htole32 __swap32
#define __htole64 __swap64
#ifdef _KERNEL
#ifdef __HAVE_MD_SWAPIO
#define __lemtoh16(_x) __mswap16(_x)
#define __lemtoh32(_x) __mswap32(_x)
#define __lemtoh64(_x) __mswap64(_x)
#define __htolem16(_x, _v) __swapm16((_x), (_v))
#define __htolem32(_x, _v) __swapm32((_x), (_v))
#define __htolem64(_x, _v) __swapm64((_x), (_v))
#endif
#endif
#endif
#ifdef _KERNEL
#ifndef __bemtoh16
#define __bemtoh16(_x) __htobe16(*(__uint16_t *)(_x))
#define __bemtoh32(_x) __htobe32(*(__uint32_t *)(_x))
#define __bemtoh64(_x) __htobe64(*(__uint64_t *)(_x))
#endif
#ifndef __htobem16
#define __htobem16(_x, _v) (*(__uint16_t *)(_x) = __htobe16(_v))
#define __htobem32(_x, _v) (*(__uint32_t *)(_x) = __htobe32(_v))
#define __htobem64(_x, _v) (*(__uint64_t *)(_x) = __htobe64(_v))
#endif
#ifndef __lemtoh16
#define __lemtoh16(_x) __htole16(*(__uint16_t *)(_x))
#define __lemtoh32(_x) __htole32(*(__uint32_t *)(_x))
#define __lemtoh64(_x) __htole64(*(__uint64_t *)(_x))
#endif
#ifndef __htolem16
#define __htolem16(_x, _v) (*(__uint16_t *)(_x) = __htole16(_v))
#define __htolem32(_x, _v) (*(__uint32_t *)(_x) = __htole32(_v))
#define __htolem64(_x, _v) (*(__uint64_t *)(_x) = __htole64(_v))
#endif
#endif
#endif