OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This header defines cross-platform ByteSwap() implementations for 16, 32 and | 5 // This header defines cross-platform ByteSwap() implementations for 16, 32 and |
6 // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to | 6 // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to |
7 // the traditional ntohX() and htonX() functions. | 7 // the traditional ntohX() and htonX() functions. |
8 // Use the functions defined here rather than using the platform-specific | 8 // Use the functions defined here rather than using the platform-specific |
9 // functions directly. | 9 // functions directly. |
10 | 10 |
11 #ifndef BASE_SYS_BYTEORDER_H_ | 11 #ifndef BASE_SYS_BYTEORDER_H_ |
12 #define BASE_SYS_BYTEORDER_H_ | 12 #define BASE_SYS_BYTEORDER_H_ |
13 | 13 |
14 #include <stdint.h> | 14 #include <stdint.h> |
15 | 15 |
| 16 #include "base/logging.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 | 18 |
18 #if defined(COMPILER_MSVC) | 19 #if defined(COMPILER_MSVC) |
19 #include <stdlib.h> | 20 #include <stdlib.h> |
20 #endif | 21 #endif |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | 25 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
25 inline uint16_t ByteSwap(uint16_t x) { | 26 inline uint16_t ByteSwap(uint16_t x) { |
(...skipping 13 matching lines...) Expand all Loading... |
39 } | 40 } |
40 | 41 |
41 inline uint64_t ByteSwap(uint64_t x) { | 42 inline uint64_t ByteSwap(uint64_t x) { |
42 #if defined(COMPILER_MSVC) | 43 #if defined(COMPILER_MSVC) |
43 return _byteswap_uint64(x); | 44 return _byteswap_uint64(x); |
44 #else | 45 #else |
45 return __builtin_bswap64(x); | 46 return __builtin_bswap64(x); |
46 #endif | 47 #endif |
47 } | 48 } |
48 | 49 |
| 50 inline uintptr_t ByteSwapUintPtrT(uintptr_t x) { |
| 51 // We do it this way because some build configurations are ILP32 even when |
| 52 // defined(ARCH_CPU_64_BITS). Unfortunately, we can't use sizeof in #ifs. But, |
| 53 // because these conditionals are constexprs, the irrelevant branches will |
| 54 // likely be optimized away, so this construction should not result in code |
| 55 // bloat. |
| 56 if (sizeof(uintptr_t) == 4) { |
| 57 return ByteSwap(static_cast<uint32_t>(x)); |
| 58 } else if (sizeof(uintptr_t) == 8) { |
| 59 return ByteSwap(static_cast<uint64_t>(x)); |
| 60 } else { |
| 61 NOTREACHED(); |
| 62 } |
| 63 } |
| 64 |
49 // Converts the bytes in |x| from host order (endianness) to little endian, and | 65 // Converts the bytes in |x| from host order (endianness) to little endian, and |
50 // returns the result. | 66 // returns the result. |
51 inline uint16_t ByteSwapToLE16(uint16_t x) { | 67 inline uint16_t ByteSwapToLE16(uint16_t x) { |
52 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 68 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
53 return x; | 69 return x; |
54 #else | 70 #else |
55 return ByteSwap(x); | 71 return ByteSwap(x); |
56 #endif | 72 #endif |
57 } | 73 } |
58 inline uint32_t ByteSwapToLE32(uint32_t x) { | 74 inline uint32_t ByteSwapToLE32(uint32_t x) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 130 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
115 return ByteSwap(x); | 131 return ByteSwap(x); |
116 #else | 132 #else |
117 return x; | 133 return x; |
118 #endif | 134 #endif |
119 } | 135 } |
120 | 136 |
121 } // namespace base | 137 } // namespace base |
122 | 138 |
123 #endif // BASE_SYS_BYTEORDER_H_ | 139 #endif // BASE_SYS_BYTEORDER_H_ |
OLD | NEW |