source: GPL/trunk/include/linux/byteorder/swab.h

Last change on this file was 32, checked in by vladest, 20 years ago

initial import

File size: 3.4 KB
Line 
1#ifndef _LINUX_BYTEORDER_SWAB_H
2#define _LINUX_BYTEORDER_SWAB_H
3
4/*
5 * linux/byteorder/swab.h
6 * Byte-swapping, independently from CPU endianness
7 * swabXX[ps]?(foo)
8 *
9 * Francois-Rene Rideau <fare@tunes.org> 19971205
10 * separated swab functions from cpu_to_XX,
11 * to clean up support for bizarre-endian architectures.
12 *
13 * See asm-i386/byteorder.h and suches for examples of how to provide
14 * architecture-dependent optimized versions
15 *
16 */
17
18/* casts are necessary for constants, because we never know how for sure
19 * how U/UL/ULL map to u16, u32, u64. At least not in a portable way.
20 */
21#define ___swab16(x) \
22 ((u16)( \
23 (((u16)(x) & (u16)0x00ffU) << 8) | \
24 (((u16)(x) & (u16)0xff00U) >> 8) ))
25#define ___swab32(x) \
26 ((u32)( \
27 (((u32)(x) & (u32)0x000000ffUL) << 24) | \
28 (((u32)(x) & (u32)0x0000ff00UL) << 8) | \
29 (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \
30 (((u32)(x) & (u32)0xff000000UL) >> 24) ))
31#define ___swab64(x) \
32 ((u64)( \
33 (u64)(((u64)(x) & (u64)0x00000000000000ff) << 56) | \
34 (u64)(((u64)(x) & (u64)0x000000000000ff00) << 40) | \
35 (u64)(((u64)(x) & (u64)0x0000000000ff0000) << 24) | \
36 (u64)(((u64)(x) & (u64)0x00000000ff000000) << 8) | \
37 (u64)(((u64)(x) & (u64)0x000000ff00000000) >> 8) | \
38 (u64)(((u64)(x) & (u64)0x0000ff0000000000) >> 24) | \
39 (u64)(((u64)(x) & (u64)0x00ff000000000000) >> 40) | \
40 (u64)(((u64)(x) & (u64)0xff00000000000000) >> 56) ))
41
42/*
43 * provide defaults when no architecture-specific optimization is detected
44 */
45#ifndef __arch__swab16
46# define __arch__swab16(x) ___swab16(x)
47#endif
48#ifndef __arch__swab32
49# define __arch__swab32(x) ___swab32(x)
50#endif
51#ifndef __arch__swab64
52# define __arch__swab64(x) ___swab64(x)
53#endif
54
55#ifndef __arch__swab16p
56# define __arch__swab16p(x) __swab16(*(x))
57#endif
58#ifndef __arch__swab32p
59# define __arch__swab32p(x) __swab32(*(x))
60#endif
61#ifndef __arch__swab64p
62# define __arch__swab64p(x) __swab64(*(x))
63#endif
64
65#ifndef __arch__swab16s
66# define __arch__swab16s(x) do { *(x) = __swab16p((x)); } while (0)
67#endif
68#ifndef __arch__swab32s
69# define __arch__swab32s(x) do { *(x) = __swab32p((x)); } while (0)
70#endif
71#ifndef __arch__swab64s
72# define __arch__swab64s(x) do { *(x) = __swab64p((x)); } while (0)
73#endif
74
75
76/*
77 * Allow constant folding
78 */
79#if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
80# define __swab16(x) \
81(__builtin_constant_p((u16)(x)) ? \
82 ___swab16((x)) : \
83 __fswab16((x)))
84# define __swab32(x) \
85(__builtin_constant_p((u32)(x)) ? \
86 ___swab32((x)) : \
87 __fswab32((x)))
88# define __swab64(x) \
89(__builtin_constant_p((u64)(x)) ? \
90 ___swab64((x)) : \
91 __fswab64((x)))
92#else
93# define __swab16(x) ___swab16(x)
94# define __swab32(x) ___swab32(x)
95# define __swab64(x) ___swab64(x)
96#endif /* OPTIMIZE */
97
98
99#ifdef __BYTEORDER_HAS_U64__
100extern __inline__ __const__ u64 __fswab64(u64 x)
101{
102# ifdef __SWAB_64_THRU_32__
103 u32 h = x >> 32;
104 u32 l = x & ((1ULL<<32)-1);
105 return (((u64)__swab32(l)) << 32) | ((u64)(__swab32(h)));
106# else
107 return __arch__swab64(x);
108# endif
109}
110#endif /* __BYTEORDER_HAS_U64__ */
111
112#if defined(__KERNEL__)
113#define swab16 __swab16
114#define swab32 __swab32
115#define swab64 __swab64
116#define swab16p __swab16p
117#define swab32p __swab32p
118#define swab64p __swab64p
119#define swab16s __swab16s
120#define swab32s __swab32s
121#define swab64s __swab64s
122#endif
123
124#endif /* _LINUX_BYTEORDER_SWAB_H */
Note: See TracBrowser for help on using the repository browser.