blob: 8d9066c70228bfd7c36ec0cb3a2afe30dc006a2d [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
[email protected]0e6f6192011-12-28 23:18:2116#include "build/build_config.h"
17
robert.bradford1a09b1772016-06-17 23:26:1118#if defined(COMPILER_MSVC)
19#include <stdlib.h>
20#endif
21
[email protected]0e6f6192011-12-28 23:18:2122namespace base {
23
24// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
avi9b6f42932015-12-26 22:15:1425inline uint16_t ByteSwap(uint16_t x) {
robert.bradford1a09b1772016-06-17 23:26:1126#if defined(COMPILER_MSVC)
27 return _byteswap_ushort(x);
28#else
29 return __builtin_bswap16(x);
30#endif
[email protected]9eb7b11b2012-03-28 20:19:3131}
[email protected]2090c522013-12-23 04:02:4932
avi9b6f42932015-12-26 22:15:1433inline uint32_t ByteSwap(uint32_t x) {
robert.bradford1a09b1772016-06-17 23:26:1134#if defined(COMPILER_MSVC)
35 return _byteswap_ulong(x);
36#else
37 return __builtin_bswap32(x);
38#endif
[email protected]9eb7b11b2012-03-28 20:19:3139}
[email protected]2090c522013-12-23 04:02:4940
avi9b6f42932015-12-26 22:15:1441inline uint64_t ByteSwap(uint64_t x) {
robert.bradford1a09b1772016-06-17 23:26:1142#if defined(COMPILER_MSVC)
43 return _byteswap_uint64(x);
44#else
45 return __builtin_bswap64(x);
46#endif
[email protected]0e6f6192011-12-28 23:18:2147}
48
[email protected]7f8ce3d92012-04-16 15:29:2749// Converts the bytes in |x| from host order (endianness) to little endian, and
50// returns the result.
avi9b6f42932015-12-26 22:15:1451inline uint16_t ByteSwapToLE16(uint16_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2752#if defined(ARCH_CPU_LITTLE_ENDIAN)
53 return x;
54#else
55 return ByteSwap(x);
56#endif
57}
avi9b6f42932015-12-26 22:15:1458inline uint32_t ByteSwapToLE32(uint32_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2759#if defined(ARCH_CPU_LITTLE_ENDIAN)
60 return x;
61#else
62 return ByteSwap(x);
63#endif
64}
avi9b6f42932015-12-26 22:15:1465inline uint64_t ByteSwapToLE64(uint64_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2766#if defined(ARCH_CPU_LITTLE_ENDIAN)
67 return x;
68#else
69 return ByteSwap(x);
70#endif
71}
72
[email protected]0e6f6192011-12-28 23:18:2173// Converts the bytes in |x| from network to host order (endianness), and
74// returns the result.
avi9b6f42932015-12-26 22:15:1475inline uint16_t NetToHost16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3176#if defined(ARCH_CPU_LITTLE_ENDIAN)
77 return ByteSwap(x);
78#else
79 return x;
80#endif
81}
avi9b6f42932015-12-26 22:15:1482inline uint32_t NetToHost32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3183#if defined(ARCH_CPU_LITTLE_ENDIAN)
84 return ByteSwap(x);
85#else
86 return x;
87#endif
88}
avi9b6f42932015-12-26 22:15:1489inline uint64_t NetToHost64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:2190#if defined(ARCH_CPU_LITTLE_ENDIAN)
91 return ByteSwap(x);
92#else
93 return x;
94#endif
95}
96
97// Converts the bytes in |x| from host to network order (endianness), and
98// returns the result.
avi9b6f42932015-12-26 22:15:1499inline uint16_t HostToNet16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:31100#if defined(ARCH_CPU_LITTLE_ENDIAN)
101 return ByteSwap(x);
102#else
103 return x;
104#endif
105}
avi9b6f42932015-12-26 22:15:14106inline uint32_t HostToNet32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:31107#if defined(ARCH_CPU_LITTLE_ENDIAN)
108 return ByteSwap(x);
109#else
110 return x;
111#endif
112}
avi9b6f42932015-12-26 22:15:14113inline uint64_t HostToNet64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:21114#if defined(ARCH_CPU_LITTLE_ENDIAN)
115 return ByteSwap(x);
116#else
117 return x;
118#endif
119}
120
121} // namespace base
122
[email protected]0e6f6192011-12-28 23:18:21123#endif // BASE_SYS_BYTEORDER_H_