1 // Copyright (C) 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Utility functions that depend on bytesex. We define versions of htonll and
16 // ntohll (HostToNetworkLL and NetworkToHostLL in our naming), as well as
17 // "Google" versions of all the standards: ghtonl, ghtons, and so on
18 // (GHostToNetworkL, GHostToNetworkS, etc in our naming). These functions do
19 // exactly the same as their standard variants, but don't require including the
20 // dangerous netinet/in.h.
21
22 #ifndef ICING_PORTABLE_ENDIAN_H_
23 #define ICING_PORTABLE_ENDIAN_H_
24
25 #include <cstdint>
26
27 // IS_LITTLE_ENDIAN, IS_BIG_ENDIAN
28 #if defined OS_LINUX || defined OS_ANDROID || defined(__ANDROID__)
29 // _BIG_ENDIAN
30 #include <endian.h>
31
32 #elif defined(__APPLE__)
33
34 // BIG_ENDIAN
35 #include <machine/endian.h> // NOLINT(build/include)
36
37 /* Let's try and follow the Linux convention */
38 #define __BYTE_ORDER BYTE_ORDER
39 #define __LITTLE_ENDIAN LITTLE_ENDIAN
40 #define __BIG_ENDIAN BIG_ENDIAN
41
42 #endif // operating system
43
44 // defines __BYTE_ORDER for MSVC
45 #ifdef COMPILER_MSVC
46 #define __BYTE_ORDER __LITTLE_ENDIAN
47 #define IS_LITTLE_ENDIAN
48 #else // COMPILER_MSVC
49
50 // define the macros IS_LITTLE_ENDIAN or IS_BIG_ENDIAN
51 // using the above endian definitions from endian.h if
52 // endian.h was included
53 #ifdef __BYTE_ORDER
54 #if __BYTE_ORDER == __LITTLE_ENDIAN
55 #define IS_LITTLE_ENDIAN
56 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
57
58 #if __BYTE_ORDER == __BIG_ENDIAN
59 #define IS_BIG_ENDIAN
60 #endif // __BYTE_ORDER == __BIG_ENDIAN
61
62 #else // __BYTE_ORDER
63
64 #if defined(__LITTLE_ENDIAN__)
65 #define IS_LITTLE_ENDIAN
66 #elif defined(__BIG_ENDIAN__)
67 #define IS_BIG_ENDIAN
68 #endif // __LITTLE_ENDIAN__ or __BIG_ENDIAN__
69
70 #endif // __BYTE_ORDER
71 #endif // COMPILER_MSVC
72
73 // byte swap functions (bswap_16, bswap_32, bswap_64).
74 // byte swap functions reverse the order of bytes, e.g.
75 // byteswap of 102030405060708 = 807060504030201
76 // byteswap of 1020304 = 4030201
77
78 // The following guarantees declaration of the byte swap functions
79 #ifdef COMPILER_MSVC
80 #include <cstdlib> // NOLINT(build/include)
81
82 #define bswap_16(x) _byteswap_ushort(x)
83 #define bswap_32(x) _byteswap_ulong(x)
84 #define bswap_64(x) _byteswap_uint64(x)
85
86 #elif defined(__APPLE__)
87 // Mac OS X / Darwin features
88 #include <libkern/OSByteOrder.h>
89
90 #define bswap_16(x) OSSwapInt16(x)
91 #define bswap_32(x) OSSwapInt32(x)
92 #define bswap_64(x) OSSwapInt64(x)
93
94 #elif defined(__GLIBC__) || defined(__BIONIC__) || defined(__ASYLO__)
95 #include <byteswap.h> // IWYU pragma: export
96
97 #else // built-in byteswap functions
98
bswap_16(uint16 x)99 static inline uint16 bswap_16(uint16 x) {
100 #ifdef __cplusplus
101 return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
102 #else // __cplusplus
103 return (uint16)(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8)); // NOLINT
104 #endif // __cplusplus
105 }
106 #define bswap_16(x) bswap_16(x)
bswap_32(uint32 x)107 static inline uint32 bswap_32(uint32 x) {
108 return (((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) |
109 ((x & 0xFF000000) >> 24));
110 }
111 #define bswap_32(x) bswap_32(x)
bswap_64(uint64 x)112 static inline uint64 bswap_64(uint64 x) {
113 return (((x & (uint64_t)0xFF) << 56) | ((x & (uint64_t)0xFF00) << 40) |
114 ((x & (uint64_t)0xFF0000) << 24) | ((x & (uint64_t)0xFF000000) << 8) |
115 ((x & (uint64_t)0xFF00000000) >> 8) |
116 ((x & (uint64_t)0xFF0000000000) >> 24) |
117 ((x & (uint64_t)0xFF000000000000) >> 40) |
118 ((x & (uint64_t)0xFF00000000000000) >> 56));
119 }
120 #define bswap_64(x) bswap_64(x)
121
122 #endif // end byteswap functions
123
124 // Use compiler byte-swapping intrinsics if they are available. 32-bit
125 // and 64-bit versions are available in Clang and GCC as of GCC 4.3.0.
126 // The 16-bit version is available in Clang and GCC only as of GCC 4.8.0.
127 // For simplicity, we enable them all only for GCC 4.8.0 or later.
128 #if defined(__clang__) || \
129 (defined(__GNUC__) && \
130 ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
131
gbswap_64(uint64_t host_int)132 inline uint64_t gbswap_64(uint64_t host_int) {
133 return __builtin_bswap64(host_int);
134 }
gbswap_32(uint32_t host_int)135 inline uint32_t gbswap_32(uint32_t host_int) {
136 return __builtin_bswap32(host_int);
137 }
gbswap_16(uint16_t host_int)138 inline uint16_t gbswap_16(uint16_t host_int) {
139 return __builtin_bswap16(host_int);
140 }
141
142 #else // intrinsics available
143
gbswap_64(uint64 host_int)144 inline uint64 gbswap_64(uint64 host_int) {
145 #if defined(__GNUC__) && defined(__x86_64__) && \
146 !(defined(__APPLE__) && defined(__MACH__))
147 // Adapted from /usr/include/byteswap.h. Not available on Mac.
148 if (__builtin_constant_p(host_int)) {
149 return __bswap_constant_64(host_int);
150 } else {
151 uint64 result;
152 __asm__("bswap %0" : "=r"(result) : "0"(host_int));
153 return result;
154 }
155 #elif defined(bswap_64)
156 return bswap_64(host_int);
157 #else // bswap_64
158 return static_cast<uint64>(bswap_32(static_cast<uint32>(host_int >> 32))) |
159 (static_cast<uint64>(bswap_32(static_cast<uint32>(host_int))) << 32);
160 #endif // bswap_64
161 }
gbswap_32(uint32 host_int)162 inline uint32 gbswap_32(uint32 host_int) { return bswap_32(host_int); }
gbswap_16(uint16 host_int)163 inline uint16 gbswap_16(uint16 host_int) { return bswap_16(host_int); }
164
165 #endif // intrinsics available
166
167 #ifdef IS_LITTLE_ENDIAN
168
169 // Definitions for ntohl etc. that don't require us to include
170 // netinet/in.h. We wrap gbswap_32 and gbswap_16 in functions rather
171 // than just #defining them because in debug mode, gcc doesn't
172 // correctly handle the (rather involved) definitions of bswap_32.
173 // gcc guarantees that inline functions are as fast as macros, so
174 // this isn't a performance hit.
GHostToNetworkS(uint16_t x)175 inline uint16_t GHostToNetworkS(uint16_t x) { return gbswap_16(x); }
GHostToNetworkL(uint32_t x)176 inline uint32_t GHostToNetworkL(uint32_t x) { return gbswap_32(x); }
GHostToNetworkLL(uint64_t x)177 inline uint64_t GHostToNetworkLL(uint64_t x) { return gbswap_64(x); }
178
179 #elif defined IS_BIG_ENDIAN
180
181 // These definitions are simpler on big-endian machines
182 // These are functions instead of macros to avoid self-assignment warnings
183 // on calls such as "i = ghtnol(i);". This also provides type checking.
GHostToNetworkS(uint16 x)184 inline uint16 GHostToNetworkS(uint16 x) { return x; }
GHostToNetworkL(uint32 x)185 inline uint32 GHostToNetworkL(uint32 x) { return x; }
GHostToNetworkLL(uint64 x)186 inline uint64 GHostToNetworkLL(uint64 x) { return x; }
187
188 #else // bytesex
189 #error \
190 "Unsupported bytesex: Either IS_BIG_ENDIAN or IS_LITTLE_ENDIAN must be defined" // NOLINT
191 #endif // bytesex
192
193 #ifndef HostToNetworkLL
194 // With the rise of 64-bit, some systems are beginning to define this.
195 #define HostToNetworkLL(x) GHostToNetworkLL(x)
196 #endif // HostToNetworkLL
197
198 // ntoh* and hton* are the same thing for any size and bytesex,
199 // since the function is an involution, i.e., its own inverse.
GNetworkToHostS(uint16_t x)200 inline uint16_t GNetworkToHostS(uint16_t x) { return GHostToNetworkS(x); }
GNetworkToHostL(uint32_t x)201 inline uint32_t GNetworkToHostL(uint32_t x) { return GHostToNetworkL(x); }
GNetworkToHostLL(uint64_t x)202 inline uint64_t GNetworkToHostLL(uint64_t x) { return GHostToNetworkLL(x); }
203
204 #ifndef NetworkToHostLL
205 #define NetworkToHostLL(x) GHostToNetworkLL(x)
206 #endif // NetworkToHostLL
207
208 #endif // ICING_PORTABLE_ENDIAN_H_
209