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 |
(...skipping 28 matching lines...) Expand all Loading... | |
39 } | 39 } |
40 | 40 |
41 inline uint64_t ByteSwap(uint64_t x) { | 41 inline uint64_t ByteSwap(uint64_t x) { |
42 #if defined(COMPILER_MSVC) | 42 #if defined(COMPILER_MSVC) |
43 return _byteswap_uint64(x); | 43 return _byteswap_uint64(x); |
44 #else | 44 #else |
45 return __builtin_bswap64(x); | 45 return __builtin_bswap64(x); |
46 #endif | 46 #endif |
47 } | 47 } |
48 | 48 |
49 inline uintptr_t ByteSwapUintPtrT(uintptr_t x) { | |
50 #if defined(ARCH_CPU_64_BITS) | |
51 static_assert(sizeof(uint64_t) == sizeof(uintptr_t), | |
52 "uintptr_t not the same as uint64_t"); | |
53 return ByteSwap(static_cast<uint64_t>(x)); | |
54 #elif defined(ARCH_CPU_32_BITS) | |
55 static_assert(sizeof(uint32_t) == sizeof(uintptr_t), | |
56 "uintptr_t not the same as uinti32_t"); | |
gab
2016/12/09 21:01:56
nit: typo s/uinti32_t/uint32_t/
| |
57 return ByteSwap(static_cast<uint32_t>(x)); | |
58 #else | |
59 #error architecture not supported | |
60 #endif | |
61 } | |
62 | |
49 // Converts the bytes in |x| from host order (endianness) to little endian, and | 63 // Converts the bytes in |x| from host order (endianness) to little endian, and |
50 // returns the result. | 64 // returns the result. |
51 inline uint16_t ByteSwapToLE16(uint16_t x) { | 65 inline uint16_t ByteSwapToLE16(uint16_t x) { |
52 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 66 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
53 return x; | 67 return x; |
54 #else | 68 #else |
55 return ByteSwap(x); | 69 return ByteSwap(x); |
56 #endif | 70 #endif |
57 } | 71 } |
58 inline uint32_t ByteSwapToLE32(uint32_t x) { | 72 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) | 128 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
115 return ByteSwap(x); | 129 return ByteSwap(x); |
116 #else | 130 #else |
117 return x; | 131 return x; |
118 #endif | 132 #endif |
119 } | 133 } |
120 | 134 |
121 } // namespace base | 135 } // namespace base |
122 | 136 |
123 #endif // BASE_SYS_BYTEORDER_H_ | 137 #endif // BASE_SYS_BYTEORDER_H_ |
OLD | NEW |