1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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
11 #ifndef BASE_SYS_BYTEORDER_H_
12 #define BASE_SYS_BYTEORDER_H_
16 #include "build/build_config.h"
18 #if defined(COMPILER_MSVC)
22 #if defined(COMPILER_MSVC) && !defined(__clang__)
23 // TODO(pkasting): See
24 // https://developercommunity.visualstudio.com/t/Mark-some-built-in-functions-as-constexp/362558
25 // https://developercommunity.visualstudio.com/t/constexpr-byte-swapping-optimization/983963
26 #define BASE_BYTESWAPS_CONSTEXPR
28 #define BASE_BYTESWAPS_CONSTEXPR constexpr
33 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
34 // TODO(pkasting): Once C++23 is available, replace with std::byteswap.
35 inline BASE_BYTESWAPS_CONSTEXPR uint16_t ByteSwap(uint16_t x) {
36 #if defined(COMPILER_MSVC) && !defined(__clang__)
37 return _byteswap_ushort(x);
39 return __builtin_bswap16(x);
43 inline BASE_BYTESWAPS_CONSTEXPR uint32_t ByteSwap(uint32_t x) {
44 #if defined(COMPILER_MSVC) && !defined(__clang__)
45 return _byteswap_ulong(x);
47 return __builtin_bswap32(x);
51 inline BASE_BYTESWAPS_CONSTEXPR uint64_t ByteSwap(uint64_t x) {
52 // Per build/build_config.h, clang masquerades as MSVC on Windows. If we are
53 // actually using clang, we can rely on the builtin.
55 // This matters in practice, because on x86(_64), this is a single "bswap"
56 // instruction. MSVC correctly replaces the call with an inlined bswap at /O2
57 // as of 2021, but clang as we use it in Chromium doesn't, keeping a function
58 // call for a single instruction.
59 #if defined(COMPILER_MSVC) && !defined(__clang__)
60 return _byteswap_uint64(x);
62 return __builtin_bswap64(x);
66 inline BASE_BYTESWAPS_CONSTEXPR uintptr_t ByteSwapUintPtrT(uintptr_t x) {
67 // We do it this way because some build configurations are ILP32 even when
68 // defined(ARCH_CPU_64_BITS). Unfortunately, we can't use sizeof in #ifs. But,
69 // because these conditionals are constexprs, the irrelevant branches will
70 // likely be optimized away, so this construction should not result in code
72 static_assert(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8,
73 "Unsupported uintptr_t size");
74 if (sizeof(uintptr_t) == 4)
75 return ByteSwap(static_cast<uint32_t>(x));
76 return ByteSwap(static_cast<uint64_t>(x));
79 // Converts the bytes in |x| from host order (endianness) to little endian, and
80 // returns the result.
81 inline BASE_BYTESWAPS_CONSTEXPR uint16_t ByteSwapToLE16(uint16_t x) {
82 #if defined(ARCH_CPU_LITTLE_ENDIAN)
88 inline BASE_BYTESWAPS_CONSTEXPR uint32_t ByteSwapToLE32(uint32_t x) {
89 #if defined(ARCH_CPU_LITTLE_ENDIAN)
95 inline BASE_BYTESWAPS_CONSTEXPR uint64_t ByteSwapToLE64(uint64_t x) {
96 #if defined(ARCH_CPU_LITTLE_ENDIAN)
103 // Converts the bytes in |x| from network to host order (endianness), and
104 // returns the result.
105 inline BASE_BYTESWAPS_CONSTEXPR uint16_t NetToHost16(uint16_t x) {
106 #if defined(ARCH_CPU_LITTLE_ENDIAN)
112 inline BASE_BYTESWAPS_CONSTEXPR uint32_t NetToHost32(uint32_t x) {
113 #if defined(ARCH_CPU_LITTLE_ENDIAN)
119 inline BASE_BYTESWAPS_CONSTEXPR uint64_t NetToHost64(uint64_t x) {
120 #if defined(ARCH_CPU_LITTLE_ENDIAN)
127 // Converts the bytes in |x| from host to network order (endianness), and
128 // returns the result.
129 inline BASE_BYTESWAPS_CONSTEXPR uint16_t HostToNet16(uint16_t x) {
130 #if defined(ARCH_CPU_LITTLE_ENDIAN)
136 inline BASE_BYTESWAPS_CONSTEXPR uint32_t HostToNet32(uint32_t x) {
137 #if defined(ARCH_CPU_LITTLE_ENDIAN)
143 inline BASE_BYTESWAPS_CONSTEXPR uint64_t HostToNet64(uint64_t x) {
144 #if defined(ARCH_CPU_LITTLE_ENDIAN)
153 #undef BASE_BYTESWAPS_CONSTEXPR
155 #endif // BASE_SYS_BYTEORDER_H_