wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
wez@chromium.org | cd68c02 | 2012-04-06 13:32:28 +0900 | [diff] [blame] | 5 | // This header defines cross-platform ByteSwap() implementations for 16, 32 and |
| 6 | // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to |
| 7 | // the traditional ntohX() and htonX() functions. |
| 8 | // Use the functions defined here rather than using the platform-specific |
| 9 | // functions directly. |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 10 | |
| 11 | #ifndef BASE_SYS_BYTEORDER_H_ |
| 12 | #define BASE_SYS_BYTEORDER_H_ |
| 13 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
palmer | 841e377 | 2016-12-14 18:40:38 +0900 | [diff] [blame] | 16 | #include "base/logging.h" |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 17 | #include "build/build_config.h" |
| 18 | |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame] | 19 | #if defined(COMPILER_MSVC) |
| 20 | #include <stdlib.h> |
| 21 | #endif |
| 22 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 23 | namespace base { |
| 24 | |
| 25 | // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 26 | inline uint16_t ByteSwap(uint16_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame] | 27 | #if defined(COMPILER_MSVC) |
| 28 | return _byteswap_ushort(x); |
| 29 | #else |
| 30 | return __builtin_bswap16(x); |
| 31 | #endif |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 32 | } |
sergeyu@chromium.org | b9b2847 | 2013-12-23 13:02:49 +0900 | [diff] [blame] | 33 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 34 | inline uint32_t ByteSwap(uint32_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame] | 35 | #if defined(COMPILER_MSVC) |
| 36 | return _byteswap_ulong(x); |
| 37 | #else |
| 38 | return __builtin_bswap32(x); |
| 39 | #endif |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 40 | } |
sergeyu@chromium.org | b9b2847 | 2013-12-23 13:02:49 +0900 | [diff] [blame] | 41 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 42 | inline uint64_t ByteSwap(uint64_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame] | 43 | #if defined(COMPILER_MSVC) |
| 44 | return _byteswap_uint64(x); |
| 45 | #else |
| 46 | return __builtin_bswap64(x); |
| 47 | #endif |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 48 | } |
| 49 | |
palmer | 841e377 | 2016-12-14 18:40:38 +0900 | [diff] [blame] | 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 | |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 65 | // Converts the bytes in |x| from host order (endianness) to little endian, and |
| 66 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 67 | inline uint16_t ByteSwapToLE16(uint16_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 68 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 69 | return x; |
| 70 | #else |
| 71 | return ByteSwap(x); |
| 72 | #endif |
| 73 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 74 | inline uint32_t ByteSwapToLE32(uint32_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 75 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 76 | return x; |
| 77 | #else |
| 78 | return ByteSwap(x); |
| 79 | #endif |
| 80 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 81 | inline uint64_t ByteSwapToLE64(uint64_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 82 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 83 | return x; |
| 84 | #else |
| 85 | return ByteSwap(x); |
| 86 | #endif |
| 87 | } |
| 88 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 89 | // Converts the bytes in |x| from network to host order (endianness), and |
| 90 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 91 | inline uint16_t NetToHost16(uint16_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 92 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 93 | return ByteSwap(x); |
| 94 | #else |
| 95 | return x; |
| 96 | #endif |
| 97 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 98 | inline uint32_t NetToHost32(uint32_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 99 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 100 | return ByteSwap(x); |
| 101 | #else |
| 102 | return x; |
| 103 | #endif |
| 104 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 105 | inline uint64_t NetToHost64(uint64_t x) { |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 106 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 107 | return ByteSwap(x); |
| 108 | #else |
| 109 | return x; |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | // Converts the bytes in |x| from host to network order (endianness), and |
| 114 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 115 | inline uint16_t HostToNet16(uint16_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 116 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 117 | return ByteSwap(x); |
| 118 | #else |
| 119 | return x; |
| 120 | #endif |
| 121 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 122 | inline uint32_t HostToNet32(uint32_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 123 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 124 | return ByteSwap(x); |
| 125 | #else |
| 126 | return x; |
| 127 | #endif |
| 128 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 129 | inline uint64_t HostToNet64(uint64_t x) { |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 130 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 131 | return ByteSwap(x); |
| 132 | #else |
| 133 | return x; |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | } // namespace base |
| 138 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 139 | #endif // BASE_SYS_BYTEORDER_H_ |