blob: 3d27ce694cc67f274f370493d6ec12c13234129b [file] [log] [blame]
Greg Hackmannc3bac8b2015-04-15 16:33:30 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __CUTILS_ENDIAN_H
18#define __CUTILS_ENDIAN_H
19
20#if defined(__linux__) && !defined(TEST_CUTILS_ENDIAN_H)
21#include <endian.h>
22#else
23
24#if !defined(__BYTE_ORDER__)
25/* gcc and clang predefine __BYTE_ORDER__, so this should never happen */
26#error Compiler does not define __BYTE_ORDER__
27#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
28#define htobe16(x) (x)
29#define htobe32(x) (x)
30#define htobe64(x) (x)
31#define htole16(x) __builtin_bswap16(x)
32#define htole32(x) __builtin_bswap32(x)
33#define htole64(x) __builtin_bswap64(x)
34#else
35#define htobe16(x) __builtin_bswap16(x)
36#define htobe32(x) __builtin_bswap32(x)
37#define htobe64(x) __builtin_bswap64(x)
38#define htole16(x) (x)
39#define htole32(x) (x)
40#define htole64(x) (x)
41#endif /* __BYTE_ORDER__ */
42
43#define be16toh(x) htobe16(x)
44#define le16toh(x) htole16(x)
45#define be32toh(x) htobe32(x)
46#define le32toh(x) htole32(x)
47#define be64toh(x) htobe64(x)
48#define le64toh(x) htole64(x)
49
50#endif /* defined(__linux__) */
51
52#endif /* __CUTILS_ENDIAN_H */