xref: /aosp_15_r20/system/libbase/include/android-base/endian.h (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker /* A cross-platform equivalent of bionic's <sys/endian.h>. */
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker /* For __BIONIC__ and __GLIBC__ */
22*8f0ba417SAndroid Build Coastguard Worker #include <sys/cdefs.h>
23*8f0ba417SAndroid Build Coastguard Worker 
24*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
25*8f0ba417SAndroid Build Coastguard Worker 
26*8f0ba417SAndroid Build Coastguard Worker #include <sys/endian.h>
27*8f0ba417SAndroid Build Coastguard Worker 
28*8f0ba417SAndroid Build Coastguard Worker #elif defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
29*8f0ba417SAndroid Build Coastguard Worker 
30*8f0ba417SAndroid Build Coastguard Worker /* glibc and musl's <endian.h> are like bionic's <sys/endian.h>. */
31*8f0ba417SAndroid Build Coastguard Worker #include <endian.h>
32*8f0ba417SAndroid Build Coastguard Worker 
33*8f0ba417SAndroid Build Coastguard Worker /* glibc and musl keep htons and htonl in <netinet/in.h>. */
34*8f0ba417SAndroid Build Coastguard Worker #include <netinet/in.h>
35*8f0ba417SAndroid Build Coastguard Worker 
36*8f0ba417SAndroid Build Coastguard Worker /* glibc and musl don't have the 64-bit variants. */
37*8f0ba417SAndroid Build Coastguard Worker #define htonq(x) htobe64(x)
38*8f0ba417SAndroid Build Coastguard Worker #define ntohq(x) be64toh(x)
39*8f0ba417SAndroid Build Coastguard Worker 
40*8f0ba417SAndroid Build Coastguard Worker #if defined(__GLIBC__)
41*8f0ba417SAndroid Build Coastguard Worker /* glibc has different names to BSD for these. */
42*8f0ba417SAndroid Build Coastguard Worker #define betoh16(x) be16toh(x)
43*8f0ba417SAndroid Build Coastguard Worker #define betoh32(x) be32toh(x)
44*8f0ba417SAndroid Build Coastguard Worker #define betoh64(x) be64toh(x)
45*8f0ba417SAndroid Build Coastguard Worker #define letoh16(x) le16toh(x)
46*8f0ba417SAndroid Build Coastguard Worker #define letoh32(x) le32toh(x)
47*8f0ba417SAndroid Build Coastguard Worker #define letoh64(x) le64toh(x)
48*8f0ba417SAndroid Build Coastguard Worker #endif
49*8f0ba417SAndroid Build Coastguard Worker 
50*8f0ba417SAndroid Build Coastguard Worker #else
51*8f0ba417SAndroid Build Coastguard Worker 
52*8f0ba417SAndroid Build Coastguard Worker #if defined(__APPLE__)
53*8f0ba417SAndroid Build Coastguard Worker /* macOS has some of the basics. */
54*8f0ba417SAndroid Build Coastguard Worker #include <sys/_endian.h>
55*8f0ba417SAndroid Build Coastguard Worker #else
56*8f0ba417SAndroid Build Coastguard Worker /* Windows has some of the basics as well. */
57*8f0ba417SAndroid Build Coastguard Worker #include <sys/param.h>
58*8f0ba417SAndroid Build Coastguard Worker #include <winsock2.h>
59*8f0ba417SAndroid Build Coastguard Worker /* winsock2.h *must* be included before the following four macros. */
60*8f0ba417SAndroid Build Coastguard Worker #define htons(x) __builtin_bswap16(x)
61*8f0ba417SAndroid Build Coastguard Worker #define htonl(x) __builtin_bswap32(x)
62*8f0ba417SAndroid Build Coastguard Worker #define ntohs(x) __builtin_bswap16(x)
63*8f0ba417SAndroid Build Coastguard Worker #define ntohl(x) __builtin_bswap32(x)
64*8f0ba417SAndroid Build Coastguard Worker #endif
65*8f0ba417SAndroid Build Coastguard Worker 
66*8f0ba417SAndroid Build Coastguard Worker /* Neither macOS nor Windows have the rest. */
67*8f0ba417SAndroid Build Coastguard Worker 
68*8f0ba417SAndroid Build Coastguard Worker #define __LITTLE_ENDIAN 1234
69*8f0ba417SAndroid Build Coastguard Worker #define __BIG_ENDIAN 4321
70*8f0ba417SAndroid Build Coastguard Worker #define __BYTE_ORDER __LITTLE_ENDIAN
71*8f0ba417SAndroid Build Coastguard Worker 
72*8f0ba417SAndroid Build Coastguard Worker #define htonq(x) __builtin_bswap64(x)
73*8f0ba417SAndroid Build Coastguard Worker 
74*8f0ba417SAndroid Build Coastguard Worker #define ntohq(x) __builtin_bswap64(x)
75*8f0ba417SAndroid Build Coastguard Worker 
76*8f0ba417SAndroid Build Coastguard Worker #define htobe16(x) __builtin_bswap16(x)
77*8f0ba417SAndroid Build Coastguard Worker #define htobe32(x) __builtin_bswap32(x)
78*8f0ba417SAndroid Build Coastguard Worker #define htobe64(x) __builtin_bswap64(x)
79*8f0ba417SAndroid Build Coastguard Worker 
80*8f0ba417SAndroid Build Coastguard Worker #define betoh16(x) __builtin_bswap16(x)
81*8f0ba417SAndroid Build Coastguard Worker #define betoh32(x) __builtin_bswap32(x)
82*8f0ba417SAndroid Build Coastguard Worker #define betoh64(x) __builtin_bswap64(x)
83*8f0ba417SAndroid Build Coastguard Worker 
84*8f0ba417SAndroid Build Coastguard Worker #define htole16(x) (x)
85*8f0ba417SAndroid Build Coastguard Worker #define htole32(x) (x)
86*8f0ba417SAndroid Build Coastguard Worker #define htole64(x) (x)
87*8f0ba417SAndroid Build Coastguard Worker 
88*8f0ba417SAndroid Build Coastguard Worker #define letoh16(x) (x)
89*8f0ba417SAndroid Build Coastguard Worker #define letoh32(x) (x)
90*8f0ba417SAndroid Build Coastguard Worker #define letoh64(x) (x)
91*8f0ba417SAndroid Build Coastguard Worker 
92*8f0ba417SAndroid Build Coastguard Worker #define be16toh(x) __builtin_bswap16(x)
93*8f0ba417SAndroid Build Coastguard Worker #define be32toh(x) __builtin_bswap32(x)
94*8f0ba417SAndroid Build Coastguard Worker #define be64toh(x) __builtin_bswap64(x)
95*8f0ba417SAndroid Build Coastguard Worker 
96*8f0ba417SAndroid Build Coastguard Worker #define le16toh(x) (x)
97*8f0ba417SAndroid Build Coastguard Worker #define le32toh(x) (x)
98*8f0ba417SAndroid Build Coastguard Worker #define le64toh(x) (x)
99*8f0ba417SAndroid Build Coastguard Worker 
100*8f0ba417SAndroid Build Coastguard Worker #endif
101