blob: 9ee1827e1e6d8ae8ea0dbf2a6d30cefb953dd4d3 [file] [log] [blame]
[email protected]9eb7b11b2012-03-28 20:19:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0e6f6192011-12-28 23:18:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eda78e62012-04-06 04:32:285// 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.
[email protected]0e6f6192011-12-28 23:18:2110
11#ifndef BASE_SYS_BYTEORDER_H_
12#define BASE_SYS_BYTEORDER_H_
13
avi9b6f42932015-12-26 22:15:1414#include <stdint.h>
15
palmera988c502016-12-14 09:40:3816#include "base/logging.h"
[email protected]0e6f6192011-12-28 23:18:2117#include "build/build_config.h"
18
robert.bradford1a09b1772016-06-17 23:26:1119#if defined(COMPILER_MSVC)
20#include <stdlib.h>
21#endif
22
[email protected]0e6f6192011-12-28 23:18:2123namespace base {
24
25// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
avi9b6f42932015-12-26 22:15:1426inline uint16_t ByteSwap(uint16_t x) {
robert.bradford1a09b1772016-06-17 23:26:1127#if defined(COMPILER_MSVC)
28 return _byteswap_ushort(x);
29#else
30 return __builtin_bswap16(x);
31#endif
[email protected]9eb7b11b2012-03-28 20:19:3132}
[email protected]2090c522013-12-23 04:02:4933
avi9b6f42932015-12-26 22:15:1434inline uint32_t ByteSwap(uint32_t x) {
robert.bradford1a09b1772016-06-17 23:26:1135#if defined(COMPILER_MSVC)
36 return _byteswap_ulong(x);
37#else
38 return __builtin_bswap32(x);
39#endif
[email protected]9eb7b11b2012-03-28 20:19:3140}
[email protected]2090c522013-12-23 04:02:4941
avi9b6f42932015-12-26 22:15:1442inline uint64_t ByteSwap(uint64_t x) {
robert.bradford1a09b1772016-06-17 23:26:1143#if defined(COMPILER_MSVC)
44 return _byteswap_uint64(x);
45#else
46 return __builtin_bswap64(x);
47#endif
[email protected]0e6f6192011-12-28 23:18:2148}
49
palmera988c502016-12-14 09:40:3850inline 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
[email protected]7f8ce3d92012-04-16 15:29:2765// Converts the bytes in |x| from host order (endianness) to little endian, and
66// returns the result.
avi9b6f42932015-12-26 22:15:1467inline uint16_t ByteSwapToLE16(uint16_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2768#if defined(ARCH_CPU_LITTLE_ENDIAN)
69 return x;
70#else
71 return ByteSwap(x);
72#endif
73}
avi9b6f42932015-12-26 22:15:1474inline uint32_t ByteSwapToLE32(uint32_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2775#if defined(ARCH_CPU_LITTLE_ENDIAN)
76 return x;
77#else
78 return ByteSwap(x);
79#endif
80}
avi9b6f42932015-12-26 22:15:1481inline uint64_t ByteSwapToLE64(uint64_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2782#if defined(ARCH_CPU_LITTLE_ENDIAN)
83 return x;
84#else
85 return ByteSwap(x);
86#endif
87}
88
[email protected]0e6f6192011-12-28 23:18:2189// Converts the bytes in |x| from network to host order (endianness), and
90// returns the result.
avi9b6f42932015-12-26 22:15:1491inline uint16_t NetToHost16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3192#if defined(ARCH_CPU_LITTLE_ENDIAN)
93 return ByteSwap(x);
94#else
95 return x;
96#endif
97}
avi9b6f42932015-12-26 22:15:1498inline uint32_t NetToHost32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3199#if defined(ARCH_CPU_LITTLE_ENDIAN)
100 return ByteSwap(x);
101#else
102 return x;
103#endif
104}
avi9b6f42932015-12-26 22:15:14105inline uint64_t NetToHost64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:21106#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.
avi9b6f42932015-12-26 22:15:14115inline uint16_t HostToNet16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:31116#if defined(ARCH_CPU_LITTLE_ENDIAN)
117 return ByteSwap(x);
118#else
119 return x;
120#endif
121}
avi9b6f42932015-12-26 22:15:14122inline uint32_t HostToNet32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:31123#if defined(ARCH_CPU_LITTLE_ENDIAN)
124 return ByteSwap(x);
125#else
126 return x;
127#endif
128}
avi9b6f42932015-12-26 22:15:14129inline uint64_t HostToNet64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:21130#if defined(ARCH_CPU_LITTLE_ENDIAN)
131 return ByteSwap(x);
132#else
133 return x;
134#endif
135}
136
137} // namespace base
138
[email protected]0e6f6192011-12-28 23:18:21139#endif // BASE_SYS_BYTEORDER_H_