clang 23.0.0git
endian.h
Go to the documentation of this file.
1//===-- Definition of macros from endian.h --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __CLANG_ENDIAN_H
10#define __CLANG_ENDIAN_H
11
12// If the system has an endian.h, let's use that instead.
13#if __has_include_next(<endian.h>)
14#include_next <endian.h>
15#else
16
17#include <stdint.h>
18
19// Implementation taken from llvm libc endian-macros.h.
20#ifdef __cplusplus
21#define __CLANG_ENDIAN_CAST(cast, type, value) (cast<type>(value))
22#else
23#define __CLANG_ENDIAN_CAST(cast, type, value) ((type)(value))
24#endif
25
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__
30
31// Define some compatibility macros if they are not defined.
32#ifndef __BYTE_ORDER
33#define __BYTE_ORDER BYTE_ORDER
34#endif
35#ifndef __LITTLE_ENDIAN
36#define __LITTLE_ENDIAN LITTLE_ENDIAN
37#endif
38#ifndef __BIG_ENDIAN
39#define __BIG_ENDIAN BIG_ENDIAN
40#endif
41#ifndef __PDP_ENDIAN
42#define __PDP_ENDIAN PDP_ENDIAN
43#endif
44
45#if BYTE_ORDER == LITTLE_ENDIAN
46
47#define htobe16(x) \
48 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
49#define htobe32(x) \
50 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
51#define htobe64(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)
56#define be16toh(x) \
57 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
58#define be32toh(x) \
59 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
60#define be64toh(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)
65
66#elif BYTE_ORDER == BIG_ENDIAN
67
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)
71#define htole16(x) \
72 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
73#define htole32(x) \
74 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
75#define htole64(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)
80#define le16toh(x) \
81 __builtin_bswap16(__CLANG_ENDIAN_CAST(static_cast, uint16_t, x))
82#define le32toh(x) \
83 __builtin_bswap32(__CLANG_ENDIAN_CAST(static_cast, uint32_t, x))
84#define le64toh(x) \
85 __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
86
87#else
88#error "Unsupported endianness"
89#endif
90#endif // __has_include_next
91#endif // __CLANG_ENDIAN_H