blob: 8d9066c70228bfd7c36ec0cb3a2afe30dc006a2d [file] [log] [blame]
wez@chromium.org31745282012-03-29 05:19:31 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
isherman@chromium.org333d7fd2011-12-29 08:18:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
wez@chromium.orgcd68c022012-04-06 13:32:28 +09005// 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.org333d7fd2011-12-29 08:18:21 +090010
11#ifndef BASE_SYS_BYTEORDER_H_
12#define BASE_SYS_BYTEORDER_H_
13
avia6a6a682015-12-27 07:15:14 +090014#include <stdint.h>
15
isherman@chromium.org333d7fd2011-12-29 08:18:21 +090016#include "build/build_config.h"
17
robert.bradford5a044462016-06-18 08:26:11 +090018#if defined(COMPILER_MSVC)
19#include <stdlib.h>
20#endif
21
isherman@chromium.org333d7fd2011-12-29 08:18:21 +090022namespace base {
23
24// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
avia6a6a682015-12-27 07:15:14 +090025inline uint16_t ByteSwap(uint16_t x) {
robert.bradford5a044462016-06-18 08:26:11 +090026#if defined(COMPILER_MSVC)
27 return _byteswap_ushort(x);
28#else
29 return __builtin_bswap16(x);
30#endif
wez@chromium.org31745282012-03-29 05:19:31 +090031}
sergeyu@chromium.orgb9b28472013-12-23 13:02:49 +090032
avia6a6a682015-12-27 07:15:14 +090033inline uint32_t ByteSwap(uint32_t x) {
robert.bradford5a044462016-06-18 08:26:11 +090034#if defined(COMPILER_MSVC)
35 return _byteswap_ulong(x);
36#else
37 return __builtin_bswap32(x);
38#endif
wez@chromium.org31745282012-03-29 05:19:31 +090039}
sergeyu@chromium.orgb9b28472013-12-23 13:02:49 +090040
avia6a6a682015-12-27 07:15:14 +090041inline uint64_t ByteSwap(uint64_t x) {
robert.bradford5a044462016-06-18 08:26:11 +090042#if defined(COMPILER_MSVC)
43 return _byteswap_uint64(x);
44#else
45 return __builtin_bswap64(x);
46#endif
isherman@chromium.org333d7fd2011-12-29 08:18:21 +090047}
48
jwd@chromium.orgd0dab472012-04-17 00:29:27 +090049// Converts the bytes in |x| from host order (endianness) to little endian, and
50// returns the result.
avia6a6a682015-12-27 07:15:14 +090051inline uint16_t ByteSwapToLE16(uint16_t x) {
jwd@chromium.orgd0dab472012-04-17 00:29:27 +090052#if defined(ARCH_CPU_LITTLE_ENDIAN)
53 return x;
54#else
55 return ByteSwap(x);
56#endif
57}
avia6a6a682015-12-27 07:15:14 +090058inline uint32_t ByteSwapToLE32(uint32_t x) {
jwd@chromium.orgd0dab472012-04-17 00:29:27 +090059#if defined(ARCH_CPU_LITTLE_ENDIAN)
60 return x;
61#else
62 return ByteSwap(x);
63#endif
64}
avia6a6a682015-12-27 07:15:14 +090065inline uint64_t ByteSwapToLE64(uint64_t x) {
jwd@chromium.orgd0dab472012-04-17 00:29:27 +090066#if defined(ARCH_CPU_LITTLE_ENDIAN)
67 return x;
68#else
69 return ByteSwap(x);
70#endif
71}
72
isherman@chromium.org333d7fd2011-12-29 08:18:21 +090073// Converts the bytes in |x| from network to host order (endianness), and
74// returns the result.
avia6a6a682015-12-27 07:15:14 +090075inline uint16_t NetToHost16(uint16_t x) {
wez@chromium.org31745282012-03-29 05:19:31 +090076#if defined(ARCH_CPU_LITTLE_ENDIAN)
77 return ByteSwap(x);
78#else
79 return x;
80#endif
81}
avia6a6a682015-12-27 07:15:14 +090082inline uint32_t NetToHost32(uint32_t x) {
wez@chromium.org31745282012-03-29 05:19:31 +090083#if defined(ARCH_CPU_LITTLE_ENDIAN)
84 return ByteSwap(x);
85#else
86 return x;
87#endif
88}
avia6a6a682015-12-27 07:15:14 +090089inline uint64_t NetToHost64(uint64_t x) {
isherman@chromium.org333d7fd2011-12-29 08:18:21 +090090#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.
avia6a6a682015-12-27 07:15:14 +090099inline uint16_t HostToNet16(uint16_t x) {
wez@chromium.org31745282012-03-29 05:19:31 +0900100#if defined(ARCH_CPU_LITTLE_ENDIAN)
101 return ByteSwap(x);
102#else
103 return x;
104#endif
105}
avia6a6a682015-12-27 07:15:14 +0900106inline uint32_t HostToNet32(uint32_t x) {
wez@chromium.org31745282012-03-29 05:19:31 +0900107#if defined(ARCH_CPU_LITTLE_ENDIAN)
108 return ByteSwap(x);
109#else
110 return x;
111#endif
112}
avia6a6a682015-12-27 07:15:14 +0900113inline uint64_t HostToNet64(uint64_t x) {
isherman@chromium.org333d7fd2011-12-29 08:18:21 +0900114#if defined(ARCH_CPU_LITTLE_ENDIAN)
115 return ByteSwap(x);
116#else
117 return x;
118#endif
119}
120
121} // namespace base
122
isherman@chromium.org333d7fd2011-12-29 08:18:21 +0900123#endif // BASE_SYS_BYTEORDER_H_