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