• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 /* A cross-platform equivalent of bionic's <sys/endian.h>. */
20 
21 #if defined(__BIONIC__)
22 
23 #include <sys/endian.h>
24 
25 #elif defined(__GLIBC__)
26 
27 /* glibc's <endian.h> is like bionic's <sys/endian.h>. */
28 #include <endian.h>
29 
30 /* glibc keeps htons and htonl in <netinet/in.h>. */
31 #include <netinet/in.h>
32 
33 /* glibc doesn't have the 64-bit variants. */
34 #define htonq(x) htobe64(x)
35 #define ntohq(x) be64toh(x)
36 
37 /* glibc has different names to BSD for these. */
38 #define betoh16(x) be16toh(x)
39 #define betoh32(x) be32toh(x)
40 #define betoh64(x) be64toh(x)
41 
42 #else
43 
44 /* Mac OS and Windows have nothing. */
45 
46 #define __LITTLE_ENDIAN 1234
47 #define LITTLE_ENDIAN __LITTLE_ENDIAN
48 
49 #define __BIG_ENDIAN 4321
50 #define BIG_ENDIAN __BIG_ENDIAN
51 
52 #define __BYTE_ORDER __LITTLE_ENDIAN
53 #define BYTE_ORDER __BYTE_ORDER
54 
55 #define htons(x) __builtin_bswap16(x)
56 #define htonl(x) __builtin_bswap32(x)
57 #define htonq(x) __builtin_bswap64(x)
58 
59 #define ntohs(x) __builtin_bswap16(x)
60 #define ntohl(x) __builtin_bswap32(x)
61 #define ntohq(x) __builtin_bswap64(x)
62 
63 #define htobe16(x) __builtin_bswap16(x)
64 #define htobe32(x) __builtin_bswap32(x)
65 #define htobe64(x) __builtin_bswap64(x)
66 
67 #define betoh16(x) __builtin_bswap16(x)
68 #define betoh32(x) __builtin_bswap32(x)
69 #define betoh64(x) __builtin_bswap64(x)
70 
71 #define htole16(x) (x)
72 #define htole32(x) (x)
73 #define htole64(x) (x)
74 
75 #define letoh16(x) (x)
76 #define letoh32(x) (x)
77 #define letoh64(x) (x)
78 
79 #define be16toh(x) __builtin_bswap16(x)
80 #define be32toh(x) __builtin_bswap32(x)
81 #define be64toh(x) __builtin_bswap64(x)
82 
83 #define le16toh(x) (x)
84 #define le32toh(x) (x)
85 #define le64toh(x) (x)
86 
87 #endif
88