Debian Codegraph @

include/linux/swab.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_SWAB_H
3 #define _UAPI_LINUX_SWAB_H
4
5 /*
6 * casts are necessary for constants, because we never know how for sure
7 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
8 */
9 #define ___constant_swab16(x) ((__u16)( \
10 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
11 (((__u16)(x) & (__u16)0xff00U) >> 8)))
12
13 #define ___constant_swab32(x) ((__u32)( \
14 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
15 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
16 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
17 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
18
19 #define ___constant_swab64(x) ((__u64)( \
20 (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
21 (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
22 (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
23 (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
24 (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
25 (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
26 (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
27 (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
28
29 #define ___constant_swahw32(x) ((__u32)( \
30 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
31 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
32
33 #define ___constant_swahb32(x) ((__u32)( \
34 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
35 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
36
37 /*
38 * Implement the following as inlines, but define the interface using
39 * macros to allow constant folding when possible:
40 * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
41 */
42
43 static inline __attribute_const__ __u16 __fswab16(__u16 val)
44 {
45 #if defined (__arch_swab16)
46 return __arch_swab16(val);
47 #else
48 return ___constant_swab16(val);
49 #endif
50 }
51
52 static inline __attribute_const__ __u32 __fswab32(__u32 val)
53 {
54 #if defined(__arch_swab32)
55 return __arch_swab32(val);
56 #else
57 return ___constant_swab32(val);
58 #endif
59 }
60
61 static inline __attribute_const__ __u64 __fswab64(__u64 val)
62 {
63 #if defined (__arch_swab64)
64 return __arch_swab64(val);
65 #elif defined(__SWAB_64_THRU_32__)
66 __u32 h = val >> 32;
67 __u32 l = val & ((1ULL << 32) - 1);
68 return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
69 #else
70 return ___constant_swab64(val);
71 #endif
72 }
73
74 static inline __attribute_const__ __u32 __fswahw32(__u32 val)
75 {
76 #ifdef __arch_swahw32
77 return __arch_swahw32(val);
78 #else
79 return ___constant_swahw32(val);
80 #endif
81 }
82
83 static inline __attribute_const__ __u32 __fswahb32(__u32 val)
84 {
85 #ifdef __arch_swahb32
86 return __arch_swahb32(val);
87 #else
88 return ___constant_swahb32(val);
89 #endif
90 }
91
92 /**
93 * __swab16 - return a byteswapped 16-bit value
94 * @x: value to byteswap
95 */
96 #ifdef __HAVE_BUILTIN_BSWAP16__
97 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
98 #else
99 #define __swab16(x) \
100 (__u16)(__builtin_constant_p(x) ? \
101 ___constant_swab16(x) : \
102 __fswab16(x))
103 #endif
104
105 /**
106 * __swab32 - return a byteswapped 32-bit value
107 * @x: value to byteswap
108 */
109 #ifdef __HAVE_BUILTIN_BSWAP32__
110 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
111 #else
112 #define __swab32(x) \
113 (__u32)(__builtin_constant_p(x) ? \
114 ___constant_swab32(x) : \
115 __fswab32(x))
116 #endif
117
118 /**
119 * __swab64 - return a byteswapped 64-bit value
120 * @x: value to byteswap
121 */
122 #ifdef __HAVE_BUILTIN_BSWAP64__
123 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
124 #else
125 #define __swab64(x) \
126 (__u64)(__builtin_constant_p(x) ? \
127 ___constant_swab64(x) : \
128 __fswab64(x))
129 #endif
130
131 static inline __always_inline unsigned long __swab(const unsigned long y)
132 {
133 #if __BITS_PER_LONG == 64
134 return __swab64(y);
135 #else /* __BITS_PER_LONG == 32 */
136 return __swab32(y);
137 #endif
138 }
139
140 /**
141 * __swahw32 - return a word-swapped 32-bit value
142 * @x: value to wordswap
143 *
144 * __swahw32(0x12340000) is 0x00001234
145 */
146 #define __swahw32(x) \
147 (__builtin_constant_p((__u32)(x)) ? \
148 ___constant_swahw32(x) : \
149 __fswahw32(x))
150
151 /**
152 * __swahb32 - return a high and low byte-swapped 32-bit value
153 * @x: value to byteswap
154 *
155 * __swahb32(0x12345678) is 0x34127856
156 */
157 #define __swahb32(x) \
158 (__builtin_constant_p((__u32)(x)) ? \
159 ___constant_swahb32(x) : \
160 __fswahb32(x))
161
162 /**
163 * __swab16p - return a byteswapped 16-bit value from a pointer
164 * @p: pointer to a naturally-aligned 16-bit value
165 */
166 static inline __always_inline __u16 __swab16p(const __u16 *p)
167 {
168 #ifdef __arch_swab16p
169 return __arch_swab16p(p);
170 #else
171 return __swab16(*p);
172 #endif
173 }
174
175 /**
176 * __swab32p - return a byteswapped 32-bit value from a pointer
177 * @p: pointer to a naturally-aligned 32-bit value
178 */
179 static inline __always_inline __u32 __swab32p(const __u32 *p)
180 {
181 #ifdef __arch_swab32p
182 return __arch_swab32p(p);
183 #else
184 return __swab32(*p);
185 #endif
186 }
187
188 /**
189 * __swab64p - return a byteswapped 64-bit value from a pointer
190 * @p: pointer to a naturally-aligned 64-bit value
191 */
192 static inline __always_inline __u64 __swab64p(const __u64 *p)
193 {
194 #ifdef __arch_swab64p
195 return __arch_swab64p(p);
196 #else
197 return __swab64(*p);
198 #endif
199 }
200
201 /**
202 * __swahw32p - return a wordswapped 32-bit value from a pointer
203 * @p: pointer to a naturally-aligned 32-bit value
204 *
205 * See __swahw32() for details of wordswapping.
206 */
207 static inline __u32 __swahw32p(const __u32 *p)
208 {
209 #ifdef __arch_swahw32p
210 return __arch_swahw32p(p);
211 #else
212 return __swahw32(*p);
213 #endif
214 }
215
216 /**
217 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
218 * @p: pointer to a naturally-aligned 32-bit value
219 *
220 * See __swahb32() for details of high/low byteswapping.
221 */
222 static inline __u32 __swahb32p(const __u32 *p)
223 {
224 #ifdef __arch_swahb32p
225 return __arch_swahb32p(p);
226 #else
227 return __swahb32(*p);
228 #endif
229 }
230
231 /**
232 * __swab16s - byteswap a 16-bit value in-place
233 * @p: pointer to a naturally-aligned 16-bit value
234 */
235 static inline void __swab16s(__u16 *p)
236 {
237 #ifdef __arch_swab16s
238 __arch_swab16s(p);
239 #else
240 *p = __swab16p(p);
241 #endif
242 }
243 /**
244 * __swab32s - byteswap a 32-bit value in-place
245 * @p: pointer to a naturally-aligned 32-bit value
246 */
247 static inline __always_inline void __swab32s(__u32 *p)
248 {
249 #ifdef __arch_swab32s
250 __arch_swab32s(p);
251 #else
252 *p = __swab32p(p);
253 #endif
254 }
255
256 /**
257 * __swab64s - byteswap a 64-bit value in-place
258 * @p: pointer to a naturally-aligned 64-bit value
259 */
260 static inline __always_inline void __swab64s(__u64 *p)
261 {
262 #ifdef __arch_swab64s
263 __arch_swab64s(p);
264 #else
265 *p = __swab64p(p);
266 #endif
267 }
268
269 /**
270 * __swahw32s - wordswap a 32-bit value in-place
271 * @p: pointer to a naturally-aligned 32-bit value
272 *
273 * See __swahw32() for details of wordswapping
274 */
275 static inline void __swahw32s(__u32 *p)
276 {
277 #ifdef __arch_swahw32s
278 __arch_swahw32s(p);
279 #else
280 *p = __swahw32p(p);
281 #endif
282 }
283
284 /**
285 * __swahb32s - high and low byteswap a 32-bit value in-place
286 * @p: pointer to a naturally-aligned 32-bit value
287 *
288 * See __swahb32() for details of high and low byte swapping
289 */
290 static inline void __swahb32s(__u32 *p)
291 {
292 #ifdef __arch_swahb32s
293 __arch_swahb32s(p);
294 #else
295 *p = __swahb32p(p);
296 #endif
297 }
298
299
300 #endif /* _UAPI_LINUX_SWAB_H */