1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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)
24 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
25 inline uint16_t ByteSwap(uint16_t x) {
26 #if defined(COMPILER_MSVC) && !defined(__clang__)
27 return _byteswap_ushort(x);
29 return __builtin_bswap16(x);
33 inline uint32_t ByteSwap(uint32_t x) {
34 #if defined(COMPILER_MSVC) && !defined(__clang__)
35 return _byteswap_ulong(x);
37 return __builtin_bswap32(x);
41 inline uint64_t ByteSwap(uint64_t x) {
42 // Per build/build_config.h, clang masquerades as MSVC on Windows. If we are
43 // actually using clang, we can rely on the builtin.
45 // This matters in practice, because on x86(_64), this is a single "bswap"
46 // instruction. MSVC correctly replaces the call with an inlined bswap at /O2
47 // as of 2021, but clang as we use it in Chromium doesn't, keeping a function
48 // call for a single instruction.
49 #if defined(COMPILER_MSVC) && !defined(__clang__)
50 return _byteswap_uint64(x);
52 return __builtin_bswap64(x);
56 inline uintptr_t ByteSwapUintPtrT(uintptr_t x) {
57 // We do it this way because some build configurations are ILP32 even when
58 // defined(ARCH_CPU_64_BITS). Unfortunately, we can't use sizeof in #ifs. But,
59 // because these conditionals are constexprs, the irrelevant branches will
60 // likely be optimized away, so this construction should not result in code
62 static_assert(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8,
63 "Unsupported uintptr_t size");
64 if (sizeof(uintptr_t) == 4)
65 return ByteSwap(static_cast<uint32_t>(x));
66 return ByteSwap(static_cast<uint64_t>(x));
69 // Converts the bytes in |x| from host order (endianness) to little endian, and
70 // returns the result.
71 inline uint16_t ByteSwapToLE16(uint16_t x) {
72 #if defined(ARCH_CPU_LITTLE_ENDIAN)
78 inline uint32_t ByteSwapToLE32(uint32_t x) {
79 #if defined(ARCH_CPU_LITTLE_ENDIAN)
85 inline uint64_t ByteSwapToLE64(uint64_t x) {
86 #if defined(ARCH_CPU_LITTLE_ENDIAN)
93 // Converts the bytes in |x| from network to host order (endianness), and
94 // returns the result.
95 inline uint16_t NetToHost16(uint16_t x) {
96 #if defined(ARCH_CPU_LITTLE_ENDIAN)
102 inline uint32_t NetToHost32(uint32_t x) {
103 #if defined(ARCH_CPU_LITTLE_ENDIAN)
109 inline uint64_t NetToHost64(uint64_t x) {
110 #if defined(ARCH_CPU_LITTLE_ENDIAN)
117 // Converts the bytes in |x| from host to network order (endianness), and
118 // returns the result.
119 inline uint16_t HostToNet16(uint16_t x) {
120 #if defined(ARCH_CPU_LITTLE_ENDIAN)
126 inline uint32_t HostToNet32(uint32_t x) {
127 #if defined(ARCH_CPU_LITTLE_ENDIAN)
133 inline uint64_t HostToNet64(uint64_t x) {
134 #if defined(ARCH_CPU_LITTLE_ENDIAN)
143 #endif // BASE_SYS_BYTEORDER_H_