xref: /freebsd/contrib/llvm-project/libc/include/llvm-libc-macros/endian-macros.h (revision bb722a7d0f1642bff6487f943ad0427799a6e5bf)
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 LLVM_LIBC_MACROS_ENDIAN_MACROS_H
10 #define LLVM_LIBC_MACROS_ENDIAN_MACROS_H
11 
12 #include "stdint-macros.h"
13 
14 #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
15 #define BIG_ENDIAN __ORDER_BIG_ENDIAN__
16 #define BYTE_ORDER __BYTE_ORDER__
17 
18 #if BYTE_ORDER == LITTLE_ENDIAN
19 
20 #define htobe16(x) __builtin_bswap16((x))
21 #define htobe32(x) __builtin_bswap32((x))
22 #define htobe64(x) __builtin_bswap64((x))
23 #define htole16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
24 #define htole32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
25 #define htole64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
26 #define be16toh(x) __builtin_bswap16((x))
27 #define be32toh(x) __builtin_bswap32((x))
28 #define be64toh(x) __builtin_bswap64((x))
29 #define le16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
30 #define le32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
31 #define le64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
32 
33 #else
34 
35 #define htobe16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
36 #define htobe32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
37 #define htobe64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
38 #define htole16(x) __builtin_bswap16((x))
39 #define htole32(x) __builtin_bswap32((x))
40 #define htole64(x) __builtin_bswap64((x))
41 #define be16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
42 #define be32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
43 #define be64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
44 #define le16toh(x) __builtin_bswap16((x))
45 #define le32toh(x) __builtin_bswap32((x))
46 #define le64toh(x) __builtin_bswap64((x))
47 
48 #endif
49 
50 #endif // LLVM_LIBC_MACROS_ENDIAN_MACROS_H
51