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) { | |
gab
2016/12/06 15:43:21
Add a test to sys_byteorder_unittest.cc
palmer
2016/12/06 21:47:41
Done.
| |
50 #if defined(ARCH_CPU_64_BITS) | |
51 return ByteSwap(static_cast<uint64_t>(x)); | |
52 #else | |
gab
2016/12/06 15:43:21
Make this a #elif and have a
#error Unsupported t
palmer
2016/12/06 21:47:41
Done.
| |
53 return ByteSwap(static_cast<uint32_t>(x)); | |
54 #endif | |
55 } | |
56 | |
49 // Converts the bytes in |x| from host order (endianness) to little endian, and | 57 // Converts the bytes in |x| from host order (endianness) to little endian, and |
50 // returns the result. | 58 // returns the result. |
51 inline uint16_t ByteSwapToLE16(uint16_t x) { | 59 inline uint16_t ByteSwapToLE16(uint16_t x) { |
52 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 60 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
53 return x; | 61 return x; |
54 #else | 62 #else |
55 return ByteSwap(x); | 63 return ByteSwap(x); |
56 #endif | 64 #endif |
57 } | 65 } |
58 inline uint32_t ByteSwapToLE32(uint32_t x) { | 66 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) | 122 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
115 return ByteSwap(x); | 123 return ByteSwap(x); |
116 #else | 124 #else |
117 return x; | 125 return x; |
118 #endif | 126 #endif |
119 } | 127 } |
120 | 128 |
121 } // namespace base | 129 } // namespace base |
122 | 130 |
123 #endif // BASE_SYS_BYTEORDER_H_ | 131 #endif // BASE_SYS_BYTEORDER_H_ |
OLD | NEW |