[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / sys_byteorder.h
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.
4
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
9 // functions directly.
10
11 #ifndef BASE_SYS_BYTEORDER_H_
12 #define BASE_SYS_BYTEORDER_H_
13
14 #include <stdint.h>
15
16 #include "build/build_config.h"
17
18 #if defined(COMPILER_MSVC)
19 #include <stdlib.h>
20 #endif
21
22 namespace base {
23
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);
28 #else
29   return __builtin_bswap16(x);
30 #endif
31 }
32
33 inline uint32_t ByteSwap(uint32_t x) {
34 #if defined(COMPILER_MSVC) && !defined(__clang__)
35   return _byteswap_ulong(x);
36 #else
37   return __builtin_bswap32(x);
38 #endif
39 }
40
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.
44   //
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);
51 #else
52   return __builtin_bswap64(x);
53 #endif
54 }
55
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
61   // bloat.
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));
67 }
68
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)
73   return x;
74 #else
75   return ByteSwap(x);
76 #endif
77 }
78 inline uint32_t ByteSwapToLE32(uint32_t x) {
79 #if defined(ARCH_CPU_LITTLE_ENDIAN)
80   return x;
81 #else
82   return ByteSwap(x);
83 #endif
84 }
85 inline uint64_t ByteSwapToLE64(uint64_t x) {
86 #if defined(ARCH_CPU_LITTLE_ENDIAN)
87   return x;
88 #else
89   return ByteSwap(x);
90 #endif
91 }
92
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)
97   return ByteSwap(x);
98 #else
99   return x;
100 #endif
101 }
102 inline uint32_t NetToHost32(uint32_t x) {
103 #if defined(ARCH_CPU_LITTLE_ENDIAN)
104   return ByteSwap(x);
105 #else
106   return x;
107 #endif
108 }
109 inline uint64_t NetToHost64(uint64_t x) {
110 #if defined(ARCH_CPU_LITTLE_ENDIAN)
111   return ByteSwap(x);
112 #else
113   return x;
114 #endif
115 }
116
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)
121   return ByteSwap(x);
122 #else
123   return x;
124 #endif
125 }
126 inline uint32_t HostToNet32(uint32_t x) {
127 #if defined(ARCH_CPU_LITTLE_ENDIAN)
128   return ByteSwap(x);
129 #else
130   return x;
131 #endif
132 }
133 inline uint64_t HostToNet64(uint64_t x) {
134 #if defined(ARCH_CPU_LITTLE_ENDIAN)
135   return ByteSwap(x);
136 #else
137   return x;
138 #endif
139 }
140
141 }  // namespace base
142
143 #endif  // BASE_SYS_BYTEORDER_H_