#ifndef CCAN_ENDIAN_H
#define CCAN_ENDIAN_H
#include <stdint.h>
#include "config.h"
#include "cursor.h"
#define BSWAP_16(val) \
((((uint16_t)(val) & 0x00ff) << 8) \
| (((uint16_t)(val) & 0xff00) >> 8))
#define BSWAP_32(val) \
((((uint32_t)(val) & 0x000000ff) << 24) \
| (((uint32_t)(val) & 0x0000ff00) << 8) \
| (((uint32_t)(val) & 0x00ff0000) >> 8) \
| (((uint32_t)(val) & 0xff000000) >> 24))
#define BSWAP_64(val) \
((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
| (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
| (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
| (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
| (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
| (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
| (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
| (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
#if HAVE_BYTESWAP_H
#include <byteswap.h>
#else
static inline uint16_t bswap_16(uint16_t val)
{
return BSWAP_16(val);
}
static inline uint32_t bswap_32(uint32_t val)
{
return BSWAP_32(val);
}
#endif
#if !HAVE_BSWAP_64
static inline uint64_t bswap_64(uint64_t val)
{
return BSWAP_64(val);
}
#endif
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
#error "Unknown endian"
#elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
#error "Can't compile for both big and little endian."
#elif HAVE_LITTLE_ENDIAN
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __LITTLE_ENDIAN
#elif __BYTE_ORDER != __LITTLE_ENDIAN
#error "__BYTE_ORDER already defined, but not equal to __LITTLE_ENDIAN"
#endif
#elif HAVE_BIG_ENDIAN
#ifndef __BYTE_ORDER
#define __BYTE_ORDER __BIG_ENDIAN
#elif __BYTE_ORDER != __BIG_ENDIAN
#error "__BYTE_ORDER already defined, but not equal to __BIG_ENDIAN"
#endif
#endif
#ifdef __CHECKER__
#define ENDIAN_CAST __attribute__((force))
#define ENDIAN_TYPE __attribute__((bitwise))
#else
#define ENDIAN_CAST
#define ENDIAN_TYPE
#endif
typedef uint64_t ENDIAN_TYPE leint64_t;
typedef uint64_t ENDIAN_TYPE beint64_t;
typedef uint32_t ENDIAN_TYPE leint32_t;
typedef uint32_t ENDIAN_TYPE beint32_t;
typedef uint16_t ENDIAN_TYPE leint16_t;
typedef uint16_t ENDIAN_TYPE beint16_t;
#if HAVE_LITTLE_ENDIAN
#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)(native))
#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)(native))
#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)(native))
#define LE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))
#define LE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))
#define LE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))
#else
#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)BSWAP_64(native))
#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)BSWAP_32(native))
#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)BSWAP_16(native))
#define LE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
#define LE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
#define LE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
#endif
#if HAVE_BIG_ENDIAN
#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)(native))
#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)(native))
#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)(native))
#define BE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))
#define BE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))
#define BE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))
#else
#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)BSWAP_64(native))
#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)BSWAP_32(native))
#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)BSWAP_16(native))
#define BE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
#define BE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
#define BE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
#endif
static inline leint64_t cpu_to_le64(uint64_t native)
{
return CPU_TO_LE64(native);
}
static inline leint32_t cpu_to_le32(uint32_t native)
{
return CPU_TO_LE32(native);
}
static inline leint16_t cpu_to_le16(uint16_t native)
{
return CPU_TO_LE16(native);
}
static inline uint64_t le64_to_cpu(leint64_t le_val)
{
return LE64_TO_CPU(le_val);
}
static inline uint32_t le32_to_cpu(leint32_t le_val)
{
return LE32_TO_CPU(le_val);
}
static inline uint16_t le16_to_cpu(leint16_t le_val)
{
return LE16_TO_CPU(le_val);
}
static inline beint64_t cpu_to_be64(uint64_t native)
{
return CPU_TO_BE64(native);
}
static inline beint32_t cpu_to_be32(uint32_t native)
{
return CPU_TO_BE32(native);
}
static inline beint16_t cpu_to_be16(uint16_t native)
{
return CPU_TO_BE16(native);
}
static inline uint64_t be64_to_cpu(beint64_t be_val)
{
return BE64_TO_CPU(be_val);
}
static inline uint32_t be32_to_cpu(beint32_t be_val)
{
return BE32_TO_CPU(be_val);
}
static inline uint16_t be16_to_cpu(beint16_t be_val)
{
return BE16_TO_CPU(be_val);
}
typedef beint64_t be64;
typedef beint32_t be32;
typedef beint16_t be16;
typedef leint64_t le64;
typedef leint32_t le32;
typedef leint16_t le16;
#endif