#ifndef PG_BSWAP_H
#define PG_BSWAP_H
#if defined(HAVE__BUILTIN_BSWAP16)
#define pg_bswap16(x) __builtin_bswap16(x)
#elif defined(_MSC_VER)
#define pg_bswap16(x) _byteswap_ushort(x)
#else
static inline uint16
pg_bswap16(uint16 x)
{
return
((x << 8) & 0xff00) |
((x >> 8) & 0x00ff);
}
#endif
#if defined(HAVE__BUILTIN_BSWAP32)
#define pg_bswap32(x) __builtin_bswap32(x)
#elif defined(_MSC_VER)
#define pg_bswap32(x) _byteswap_ulong(x)
#else
static inline uint32
pg_bswap32(uint32 x)
{
return
((x << 24) & 0xff000000) |
((x << 8) & 0x00ff0000) |
((x >> 8) & 0x0000ff00) |
((x >> 24) & 0x000000ff);
}
#endif
#if defined(HAVE__BUILTIN_BSWAP64)
#define pg_bswap64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER)
#define pg_bswap64(x) _byteswap_uint64(x)
#else
static inline uint64
pg_bswap64(uint64 x)
{
return
((x << 56) & UINT64CONST(0xff00000000000000)) |
((x << 40) & UINT64CONST(0x00ff000000000000)) |
((x << 24) & UINT64CONST(0x0000ff0000000000)) |
((x << 8) & UINT64CONST(0x000000ff00000000)) |
((x >> 8) & UINT64CONST(0x00000000ff000000)) |
((x >> 24) & UINT64CONST(0x0000000000ff0000)) |
((x >> 40) & UINT64CONST(0x000000000000ff00)) |
((x >> 56) & UINT64CONST(0x00000000000000ff));
}
#endif
#ifdef WORDS_BIGENDIAN
#define pg_hton16(x) (x)
#define pg_hton32(x) (x)
#define pg_hton64(x) (x)
#define pg_ntoh16(x) (x)
#define pg_ntoh32(x) (x)
#define pg_ntoh64(x) (x)
#else
#define pg_hton16(x) pg_bswap16(x)
#define pg_hton32(x) pg_bswap32(x)
#define pg_hton64(x) pg_bswap64(x)
#define pg_ntoh16(x) pg_bswap16(x)
#define pg_ntoh32(x) pg_bswap32(x)
#define pg_ntoh64(x) pg_bswap64(x)
#endif
#ifdef SIZEOF_DATUM
#ifdef WORDS_BIGENDIAN
#define DatumBigEndianToNative(x) (x)
#else
#if SIZEOF_DATUM == 8
#define DatumBigEndianToNative(x) pg_bswap64(x)
#else
#define DatumBigEndianToNative(x) pg_bswap32(x)
#endif
#endif
#endif
#endif