File indexing completed on 2024-11-15 09:00:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
0017 #define ABSL_BASE_INTERNAL_ENDIAN_H_
0018
0019 #include <cstdint>
0020 #include <cstdlib>
0021
0022 #include "absl/base/casts.h"
0023 #include "absl/base/config.h"
0024 #include "absl/base/internal/unaligned_access.h"
0025 #include "absl/base/nullability.h"
0026 #include "absl/base/port.h"
0027
0028 namespace absl {
0029 ABSL_NAMESPACE_BEGIN
0030
0031 inline uint64_t gbswap_64(uint64_t host_int) {
0032 #if ABSL_HAVE_BUILTIN(__builtin_bswap64) || defined(__GNUC__)
0033 return __builtin_bswap64(host_int);
0034 #elif defined(_MSC_VER)
0035 return _byteswap_uint64(host_int);
0036 #else
0037 return (((host_int & uint64_t{0xFF}) << 56) |
0038 ((host_int & uint64_t{0xFF00}) << 40) |
0039 ((host_int & uint64_t{0xFF0000}) << 24) |
0040 ((host_int & uint64_t{0xFF000000}) << 8) |
0041 ((host_int & uint64_t{0xFF00000000}) >> 8) |
0042 ((host_int & uint64_t{0xFF0000000000}) >> 24) |
0043 ((host_int & uint64_t{0xFF000000000000}) >> 40) |
0044 ((host_int & uint64_t{0xFF00000000000000}) >> 56));
0045 #endif
0046 }
0047
0048 inline uint32_t gbswap_32(uint32_t host_int) {
0049 #if ABSL_HAVE_BUILTIN(__builtin_bswap32) || defined(__GNUC__)
0050 return __builtin_bswap32(host_int);
0051 #elif defined(_MSC_VER)
0052 return _byteswap_ulong(host_int);
0053 #else
0054 return (((host_int & uint32_t{0xFF}) << 24) |
0055 ((host_int & uint32_t{0xFF00}) << 8) |
0056 ((host_int & uint32_t{0xFF0000}) >> 8) |
0057 ((host_int & uint32_t{0xFF000000}) >> 24));
0058 #endif
0059 }
0060
0061 inline uint16_t gbswap_16(uint16_t host_int) {
0062 #if ABSL_HAVE_BUILTIN(__builtin_bswap16) || defined(__GNUC__)
0063 return __builtin_bswap16(host_int);
0064 #elif defined(_MSC_VER)
0065 return _byteswap_ushort(host_int);
0066 #else
0067 return (((host_int & uint16_t{0xFF}) << 8) |
0068 ((host_int & uint16_t{0xFF00}) >> 8));
0069 #endif
0070 }
0071
0072 #ifdef ABSL_IS_LITTLE_ENDIAN
0073
0074
0075
0076 inline uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
0077 inline uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
0078 inline uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
0079
0080 #elif defined ABSL_IS_BIG_ENDIAN
0081
0082
0083
0084
0085 inline uint16_t ghtons(uint16_t x) { return x; }
0086 inline uint32_t ghtonl(uint32_t x) { return x; }
0087 inline uint64_t ghtonll(uint64_t x) { return x; }
0088
0089 #else
0090 #error \
0091 "Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
0092 "ABSL_IS_LITTLE_ENDIAN must be defined"
0093 #endif
0094
0095 inline uint16_t gntohs(uint16_t x) { return ghtons(x); }
0096 inline uint32_t gntohl(uint32_t x) { return ghtonl(x); }
0097 inline uint64_t gntohll(uint64_t x) { return ghtonll(x); }
0098
0099
0100
0101
0102
0103 namespace little_endian {
0104
0105 #ifdef ABSL_IS_LITTLE_ENDIAN
0106
0107 inline uint16_t FromHost16(uint16_t x) { return x; }
0108 inline uint16_t ToHost16(uint16_t x) { return x; }
0109
0110 inline uint32_t FromHost32(uint32_t x) { return x; }
0111 inline uint32_t ToHost32(uint32_t x) { return x; }
0112
0113 inline uint64_t FromHost64(uint64_t x) { return x; }
0114 inline uint64_t ToHost64(uint64_t x) { return x; }
0115
0116 inline constexpr bool IsLittleEndian() { return true; }
0117
0118 #elif defined ABSL_IS_BIG_ENDIAN
0119
0120 inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
0121 inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
0122
0123 inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
0124 inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
0125
0126 inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
0127 inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
0128
0129 inline constexpr bool IsLittleEndian() { return false; }
0130
0131 #endif
0132
0133 inline uint8_t FromHost(uint8_t x) { return x; }
0134 inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
0135 inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
0136 inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
0137 inline uint8_t ToHost(uint8_t x) { return x; }
0138 inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
0139 inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
0140 inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
0141
0142 inline int8_t FromHost(int8_t x) { return x; }
0143 inline int16_t FromHost(int16_t x) {
0144 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
0145 }
0146 inline int32_t FromHost(int32_t x) {
0147 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
0148 }
0149 inline int64_t FromHost(int64_t x) {
0150 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
0151 }
0152 inline int8_t ToHost(int8_t x) { return x; }
0153 inline int16_t ToHost(int16_t x) {
0154 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
0155 }
0156 inline int32_t ToHost(int32_t x) {
0157 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
0158 }
0159 inline int64_t ToHost(int64_t x) {
0160 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
0161 }
0162
0163
0164 inline uint16_t Load16(absl::Nonnull<const void *> p) {
0165 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
0166 }
0167
0168 inline void Store16(absl::Nonnull<void *> p, uint16_t v) {
0169 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
0170 }
0171
0172 inline uint32_t Load32(absl::Nonnull<const void *> p) {
0173 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
0174 }
0175
0176 inline void Store32(absl::Nonnull<void *> p, uint32_t v) {
0177 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
0178 }
0179
0180 inline uint64_t Load64(absl::Nonnull<const void *> p) {
0181 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
0182 }
0183
0184 inline void Store64(absl::Nonnull<void *> p, uint64_t v) {
0185 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
0186 }
0187
0188 }
0189
0190
0191
0192
0193
0194 namespace big_endian {
0195 #ifdef ABSL_IS_LITTLE_ENDIAN
0196
0197 inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
0198 inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
0199
0200 inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
0201 inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
0202
0203 inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
0204 inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
0205
0206 inline constexpr bool IsLittleEndian() { return true; }
0207
0208 #elif defined ABSL_IS_BIG_ENDIAN
0209
0210 inline uint16_t FromHost16(uint16_t x) { return x; }
0211 inline uint16_t ToHost16(uint16_t x) { return x; }
0212
0213 inline uint32_t FromHost32(uint32_t x) { return x; }
0214 inline uint32_t ToHost32(uint32_t x) { return x; }
0215
0216 inline uint64_t FromHost64(uint64_t x) { return x; }
0217 inline uint64_t ToHost64(uint64_t x) { return x; }
0218
0219 inline constexpr bool IsLittleEndian() { return false; }
0220
0221 #endif
0222
0223 inline uint8_t FromHost(uint8_t x) { return x; }
0224 inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
0225 inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
0226 inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
0227 inline uint8_t ToHost(uint8_t x) { return x; }
0228 inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
0229 inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
0230 inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
0231
0232 inline int8_t FromHost(int8_t x) { return x; }
0233 inline int16_t FromHost(int16_t x) {
0234 return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
0235 }
0236 inline int32_t FromHost(int32_t x) {
0237 return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
0238 }
0239 inline int64_t FromHost(int64_t x) {
0240 return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
0241 }
0242 inline int8_t ToHost(int8_t x) { return x; }
0243 inline int16_t ToHost(int16_t x) {
0244 return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
0245 }
0246 inline int32_t ToHost(int32_t x) {
0247 return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
0248 }
0249 inline int64_t ToHost(int64_t x) {
0250 return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
0251 }
0252
0253
0254 inline uint16_t Load16(absl::Nonnull<const void *> p) {
0255 return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
0256 }
0257
0258 inline void Store16(absl::Nonnull<void *> p, uint16_t v) {
0259 ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
0260 }
0261
0262 inline uint32_t Load32(absl::Nonnull<const void *> p) {
0263 return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
0264 }
0265
0266 inline void Store32(absl::Nonnull<void *>p, uint32_t v) {
0267 ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
0268 }
0269
0270 inline uint64_t Load64(absl::Nonnull<const void *> p) {
0271 return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
0272 }
0273
0274 inline void Store64(absl::Nonnull<void *> p, uint64_t v) {
0275 ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
0276 }
0277
0278 }
0279
0280 ABSL_NAMESPACE_END
0281 }
0282
0283 #endif