OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) | 53 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) |
54 #define PROTOBUF_LITTLE_ENDIAN 1 | 54 #define PROTOBUF_LITTLE_ENDIAN 1 |
55 #endif | 55 #endif |
56 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) | 56 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) |
57 // If MSVC has "/RTCc" set, it will complain about truncating casts at | 57 // If MSVC has "/RTCc" set, it will complain about truncating casts at |
58 // runtime. This file contains some intentional truncating casts. | 58 // runtime. This file contains some intentional truncating casts. |
59 #pragma runtime_checks("c", off) | 59 #pragma runtime_checks("c", off) |
60 #endif | 60 #endif |
61 #else | 61 #else |
62 #include <sys/param.h> // __BYTE_ORDER | 62 #include <sys/param.h> // __BYTE_ORDER |
| 63 #if defined(__OpenBSD__) |
| 64 #include <endian.h> |
| 65 #endif |
63 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ | 66 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ |
64 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ | 67 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ |
| 68 (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \ |
65 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) | 69 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) |
66 #define PROTOBUF_LITTLE_ENDIAN 1 | 70 #define PROTOBUF_LITTLE_ENDIAN 1 |
67 #endif | 71 #endif |
68 #endif | 72 #endif |
69 | 73 |
70 // The macros defined below are required in order to make protobuf_lite a | 74 // The macros defined below are required in order to make protobuf_lite a |
71 // component on all platforms. See http://crbug.com/172800. | 75 // component on all platforms. See http://crbug.com/172800. |
72 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) | 76 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS) |
73 #if defined(_MSC_VER) | 77 #if defined(_MSC_VER) |
74 #ifdef LIBPROTOBUF_EXPORTS | 78 #ifdef LIBPROTOBUF_EXPORTS |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 typedef signed __int8 int8; | 122 typedef signed __int8 int8; |
119 typedef __int16 int16; | 123 typedef __int16 int16; |
120 typedef __int32 int32; | 124 typedef __int32 int32; |
121 typedef __int64 int64; | 125 typedef __int64 int64; |
122 | 126 |
123 typedef unsigned __int8 uint8; | 127 typedef unsigned __int8 uint8; |
124 typedef unsigned __int16 uint16; | 128 typedef unsigned __int16 uint16; |
125 typedef unsigned __int32 uint32; | 129 typedef unsigned __int32 uint32; |
126 typedef unsigned __int64 uint64; | 130 typedef unsigned __int64 uint64; |
127 #else | 131 #else |
128 typedef int8_t int8; | 132 typedef int8_t int8; |
129 typedef int16_t int16; | 133 typedef int16_t int16; |
130 typedef int32_t int32; | 134 typedef int32_t int32; |
131 typedef int64_t int64; | 135 typedef int64_t int64; |
132 | 136 |
133 typedef uint8_t uint8; | 137 typedef uint8_t uint8; |
134 typedef uint16_t uint16; | 138 typedef uint16_t uint16; |
135 typedef uint32_t uint32; | 139 typedef uint32_t uint32; |
136 typedef uint64_t uint64; | 140 typedef uint64_t uint64; |
137 #endif | 141 #endif |
138 | 142 |
139 // long long macros to be used because gcc and vc++ use different suffixes, | 143 // long long macros to be used because gcc and vc++ use different suffixes, |
140 // and different size specifiers in format strings | 144 // and different size specifiers in format strings |
141 #undef GOOGLE_LONGLONG | 145 #undef GOOGLE_LONGLONG |
142 #undef GOOGLE_ULONGLONG | 146 #undef GOOGLE_ULONGLONG |
143 #undef GOOGLE_LL_FORMAT | 147 #undef GOOGLE_LL_FORMAT |
144 | 148 |
145 #ifdef _MSC_VER | 149 #ifdef _MSC_VER |
146 #define GOOGLE_LONGLONG(x) x##I64 | 150 #define GOOGLE_LONGLONG(x) x##I64 |
147 #define GOOGLE_ULONGLONG(x) x##UI64 | 151 #define GOOGLE_ULONGLONG(x) x##UI64 |
148 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) | 152 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...) |
149 #else | 153 #else |
| 154 // By long long, we actually mean int64. |
150 #define GOOGLE_LONGLONG(x) INT64_C(x) | 155 #define GOOGLE_LONGLONG(x) INT64_C(x) |
151 #define GOOGLE_ULONGLONG(x) UINT64_C(x) | 156 #define GOOGLE_ULONGLONG(x) UINT64_C(x) |
| 157 // Used to format real long long integers. |
152 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. | 158 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. |
153 #endif | 159 #endif |
154 | 160 |
155 static const int32 kint32max = 0x7FFFFFFF; | 161 static const int32 kint32max = 0x7FFFFFFF; |
156 static const int32 kint32min = -kint32max - 1; | 162 static const int32 kint32min = -kint32max - 1; |
157 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); | 163 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF); |
158 static const int64 kint64min = -kint64max - 1; | 164 static const int64 kint64min = -kint64max - 1; |
159 static const uint32 kuint32max = 0xFFFFFFFFu; | 165 static const uint32 kuint32max = 0xFFFFFFFFu; |
160 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); | 166 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); |
161 | 167 |
(...skipping 21 matching lines...) Expand all Loading... |
183 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) | 189 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline)) |
184 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) | 190 #elif defined(_MSC_VER) && (_MSC_VER >= 1400) |
185 // Seems to have been around since at least Visual Studio 2005 | 191 // Seems to have been around since at least Visual Studio 2005 |
186 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) | 192 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline) |
187 #else | 193 #else |
188 // Other compilers will have to figure it out for themselves. | 194 // Other compilers will have to figure it out for themselves. |
189 #define GOOGLE_ATTRIBUTE_NOINLINE | 195 #define GOOGLE_ATTRIBUTE_NOINLINE |
190 #endif | 196 #endif |
191 #endif | 197 #endif |
192 | 198 |
| 199 #ifndef GOOGLE_ATTRIBUTE_NORETURN |
| 200 #ifdef __GNUC__ |
| 201 // Tell the compiler that a given function never returns. |
| 202 #define GOOGLE_ATTRIBUTE_NORETURN __attribute__((noreturn)) |
| 203 #else |
| 204 #define GOOGLE_ATTRIBUTE_NORETURN |
| 205 #endif |
| 206 #endif |
| 207 |
193 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED | 208 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED |
194 #ifdef __GNUC__ | 209 #ifdef __GNUC__ |
195 // If the method/variable/type is used anywhere, produce a warning. | 210 // If the method/variable/type is used anywhere, produce a warning. |
196 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) | 211 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) |
197 #else | 212 #else |
198 #define GOOGLE_ATTRIBUTE_DEPRECATED | 213 #define GOOGLE_ATTRIBUTE_DEPRECATED |
199 #endif | 214 #endif |
200 #endif | 215 #endif |
201 | 216 |
202 #ifndef GOOGLE_PREDICT_TRUE | 217 #ifndef GOOGLE_PREDICT_TRUE |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 memcpy(p, &v, sizeof v); | 300 memcpy(p, &v, sizeof v); |
286 } | 301 } |
287 #endif | 302 #endif |
288 | 303 |
289 #if defined(_MSC_VER) | 304 #if defined(_MSC_VER) |
290 #define GOOGLE_THREAD_LOCAL __declspec(thread) | 305 #define GOOGLE_THREAD_LOCAL __declspec(thread) |
291 #else | 306 #else |
292 #define GOOGLE_THREAD_LOCAL __thread | 307 #define GOOGLE_THREAD_LOCAL __thread |
293 #endif | 308 #endif |
294 | 309 |
295 // The following guarantees declaration of the byte swap functions, and | 310 // The following guarantees declaration of the byte swap functions. |
296 // defines __BYTE_ORDER for MSVC | |
297 #ifdef _MSC_VER | 311 #ifdef _MSC_VER |
298 #define __BYTE_ORDER __LITTLE_ENDIAN | |
299 #define bswap_16(x) _byteswap_ushort(x) | 312 #define bswap_16(x) _byteswap_ushort(x) |
300 #define bswap_32(x) _byteswap_ulong(x) | 313 #define bswap_32(x) _byteswap_ulong(x) |
301 #define bswap_64(x) _byteswap_uint64(x) | 314 #define bswap_64(x) _byteswap_uint64(x) |
302 | 315 |
303 #elif defined(__APPLE__) | 316 #elif defined(__APPLE__) |
304 // Mac OS X / Darwin features | 317 // Mac OS X / Darwin features |
305 #define bswap_16(x) OSSwapInt16(x) | 318 #define bswap_16(x) OSSwapInt16(x) |
306 #define bswap_32(x) OSSwapInt32(x) | 319 #define bswap_32(x) OSSwapInt32(x) |
307 #define bswap_64(x) OSSwapInt64(x) | 320 #define bswap_64(x) OSSwapInt64(x) |
308 | 321 |
(...skipping 18 matching lines...) Expand all Loading... |
327 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | | 340 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) | |
328 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | | 341 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) | |
329 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | | 342 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) | |
330 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); | 343 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56)); |
331 } | 344 } |
332 #define bswap_64(x) bswap_64(x) | 345 #define bswap_64(x) bswap_64(x) |
333 | 346 |
334 #endif | 347 #endif |
335 | 348 |
336 // =================================================================== | 349 // =================================================================== |
| 350 // from google3/util/bits/bits.h |
| 351 |
| 352 class Bits { |
| 353 public: |
| 354 static uint32 Log2FloorNonZero(uint32 n) { |
| 355 #if defined(__GNUC__) |
| 356 return 31 ^ __builtin_clz(n); |
| 357 #elif defined(COMPILER_MSVC) && defined(_M_IX86) |
| 358 _asm { |
| 359 bsr ebx, n |
| 360 mov n, ebx |
| 361 } |
| 362 return n; |
| 363 #else |
| 364 return Log2FloorNonZero_Portable(n); |
| 365 #endif |
| 366 } |
| 367 |
| 368 static uint64 Log2FloorNonZero64(uint64 n) { |
| 369 #if defined(__GNUC__) |
| 370 return 63 ^ __builtin_clzll(n); |
| 371 #else |
| 372 return Log2FloorNonZero64_Portable(n); |
| 373 #endif |
| 374 } |
| 375 private: |
| 376 static int Log2FloorNonZero_Portable(uint32 n) { |
| 377 if (n == 0) |
| 378 return -1; |
| 379 int log = 0; |
| 380 uint32 value = n; |
| 381 for (int i = 4; i >= 0; --i) { |
| 382 int shift = (1 << i); |
| 383 uint32 x = value >> shift; |
| 384 if (x != 0) { |
| 385 value = x; |
| 386 log += shift; |
| 387 } |
| 388 } |
| 389 assert(value == 1); |
| 390 return log; |
| 391 } |
| 392 |
| 393 static int Log2FloorNonZero64_Portable(uint64 n) { |
| 394 const uint32 topbits = static_cast<uint32>(n >> 32); |
| 395 if (topbits == 0) { |
| 396 // Top bits are zero, so scan in bottom bits |
| 397 return Log2FloorNonZero(static_cast<uint32>(n)); |
| 398 } else { |
| 399 return 32 + Log2FloorNonZero(topbits); |
| 400 } |
| 401 } |
| 402 }; |
| 403 |
| 404 // =================================================================== |
337 // from google3/util/endian/endian.h | 405 // from google3/util/endian/endian.h |
338 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); | 406 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x); |
339 | 407 |
340 class BigEndian { | 408 class BigEndian { |
341 public: | 409 public: |
342 #ifdef PROTOBUF_LITTLE_ENDIAN | 410 #ifdef PROTOBUF_LITTLE_ENDIAN |
343 | 411 |
344 static uint16 FromHost16(uint16 x) { return bswap_16(x); } | 412 static uint16 FromHost16(uint16 x) { return bswap_16(x); } |
345 static uint16 ToHost16(uint16 x) { return bswap_16(x); } | 413 static uint16 ToHost16(uint16 x) { return bswap_16(x); } |
346 | 414 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 static void Store64(void *p, uint64 v) { | 459 static void Store64(void *p, uint64 v) { |
392 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); | 460 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); |
393 } | 461 } |
394 }; | 462 }; |
395 | 463 |
396 | 464 |
397 } // namespace protobuf | 465 } // namespace protobuf |
398 } // namespace google | 466 } // namespace google |
399 | 467 |
400 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ | 468 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ |
OLD | NEW |