| 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 "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 | 17 |
| 18 #if defined(COMPILER_MSVC) |
| 19 #include <stdlib.h> |
| 20 #endif |
| 21 |
| 18 namespace base { | 22 namespace base { |
| 19 | 23 |
| 20 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | 24 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
| 21 inline uint16_t ByteSwap(uint16_t x) { | 25 inline uint16_t ByteSwap(uint16_t x) { |
| 22 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); | 26 #if defined(COMPILER_MSVC) |
| 27 return _byteswap_ushort(x); |
| 28 #else |
| 29 return __builtin_bswap16(x); |
| 30 #endif |
| 23 } | 31 } |
| 24 | 32 |
| 25 inline uint32_t ByteSwap(uint32_t x) { | 33 inline uint32_t ByteSwap(uint32_t x) { |
| 26 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | | 34 #if defined(COMPILER_MSVC) |
| 27 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); | 35 return _byteswap_ulong(x); |
| 36 #else |
| 37 return __builtin_bswap32(x); |
| 38 #endif |
| 28 } | 39 } |
| 29 | 40 |
| 30 inline uint64_t ByteSwap(uint64_t x) { | 41 inline uint64_t ByteSwap(uint64_t x) { |
| 31 return ((x & 0x00000000000000ffull) << 56) | | 42 #if defined(COMPILER_MSVC) |
| 32 ((x & 0x000000000000ff00ull) << 40) | | 43 return _byteswap_uint64(x); |
| 33 ((x & 0x0000000000ff0000ull) << 24) | | 44 #else |
| 34 ((x & 0x00000000ff000000ull) << 8) | | 45 return __builtin_bswap64(x); |
| 35 ((x & 0x000000ff00000000ull) >> 8) | | 46 #endif |
| 36 ((x & 0x0000ff0000000000ull) >> 24) | | |
| 37 ((x & 0x00ff000000000000ull) >> 40) | | |
| 38 ((x & 0xff00000000000000ull) >> 56); | |
| 39 } | 47 } |
| 40 | 48 |
| 41 // Converts the bytes in |x| from host order (endianness) to little endian, and | 49 // Converts the bytes in |x| from host order (endianness) to little endian, and |
| 42 // returns the result. | 50 // returns the result. |
| 43 inline uint16_t ByteSwapToLE16(uint16_t x) { | 51 inline uint16_t ByteSwapToLE16(uint16_t x) { |
| 44 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 52 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 45 return x; | 53 return x; |
| 46 #else | 54 #else |
| 47 return ByteSwap(x); | 55 return ByteSwap(x); |
| 48 #endif | 56 #endif |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 114 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 107 return ByteSwap(x); | 115 return ByteSwap(x); |
| 108 #else | 116 #else |
| 109 return x; | 117 return x; |
| 110 #endif | 118 #endif |
| 111 } | 119 } |
| 112 | 120 |
| 113 } // namespace base | 121 } // namespace base |
| 114 | 122 |
| 115 #endif // BASE_SYS_BYTEORDER_H_ | 123 #endif // BASE_SYS_BYTEORDER_H_ |
| OLD | NEW |