Go to the documentation of this file.
9#ifndef __CLANG_ENDIAN_H
10#define __CLANG_ENDIAN_H
13#if __has_include_next(<endian.h>)
14#include_next <endian.h>
21#define __CLANG_ENDIAN_CAST(cast, type, value) (cast<type>(value))
23#define __CLANG_ENDIAN_CAST(cast, type, value) ((type)(value))
26#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
27#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
28#define PDP_ENDIAN __ORDER_PDP_ENDIAN__
29#define BYTE_ORDER __BYTE_ORDER__
33#define __BYTE_ORDER BYTE_ORDER
35#ifndef __LITTLE_ENDIAN
36#define __LITTLE_ENDIAN LITTLE_ENDIAN
39#define __BIG_ENDIAN BIG_ENDIAN
42#define __PDP_ENDIAN PDP_ENDIAN
45#if BYTE_ORDER == LITTLE_ENDIAN
48 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
50 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
52 __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
53#define htole16(x) __CLANG_ENDIAN_CAST(static_cast, uint16_t, x)
54#define htole32(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
55#define htole64(x) __CLANG_ENDIAN_CAST(static_cast, uint64_t, x)
57 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
59 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
61 __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
62#define le16toh(x) __CLANG_ENDIAN_CAST(static_cast, uint16_t, x)
63#define le32toh(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
64#define le64toh(x) __CLANG_ENDIAN_CAST(static_cast, uint64_t, x)
66#elif BYTE_ORDER == BIG_ENDIAN
68#define htobe16(x) __CLANG_ENDIAN_CAST(static_cast, uint16_t, x)
69#define htobe32(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
70#define htobe64(x) __CLANG_ENDIAN_CAST(static_cast, uint64_t, x)
72 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
74 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
76 __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
77#define be16toh(x) __CLANG_ENDIAN_CAST(static_cast, uint16_t, x)
78#define be32toh(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
79#define be64toh(x) __CLANG_ENDIAN_CAST(static_cast, uint64_t, x)
81 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
83 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
85 __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
88#error "Unsupported endianness"