1 #ifndef __XEN_BYTEORDER_SWAB_H__
2 #define __XEN_BYTEORDER_SWAB_H__
3 
4 /*
5  * Byte-swapping, independently from CPU endianness
6  *     swabXX[ps]?(foo)
7  *
8  * Francois-Rene Rideau <fare@tunes.org> 19971205
9  *    separated swab functions from cpu_to_XX,
10  *    to clean up support for bizarre-endian architectures.
11  */
12 
13 /* casts are necessary for constants, because we never know how for sure
14  * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
15  */
16 #define ___swab16(x)                                    \
17 ({                                                      \
18     __u16 __x = (x);                                    \
19     ((__u16)(                                           \
20         (((__u16)(__x) & (__u16)0x00ffU) << 8) |        \
21         (((__u16)(__x) & (__u16)0xff00U) >> 8) ));      \
22 })
23 
24 #define ___swab32(x)                                            \
25 ({                                                              \
26     __u32 __x = (x);                                            \
27     ((__u32)(                                                   \
28         (((__u32)(__x) & (__u32)0x000000ffUL) << 24) |          \
29         (((__u32)(__x) & (__u32)0x0000ff00UL) <<  8) |          \
30         (((__u32)(__x) & (__u32)0x00ff0000UL) >>  8) |          \
31         (((__u32)(__x) & (__u32)0xff000000UL) >> 24) ));        \
32 })
33 
34 #define ___swab64(x)                                                       \
35 ({                                                                         \
36     __u64 __x = (x);                                                       \
37     ((__u64)(                                                              \
38         (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) |     \
39         (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) |     \
40         (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) |     \
41         (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) |     \
42             (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
43         (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) |     \
44         (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) |     \
45         (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) ));   \
46 })
47 
48 #define ___constant_swab16(x)                   \
49     ((__u16)(                                   \
50         (((__u16)(x) & (__u16)0x00ffU) << 8) |  \
51         (((__u16)(x) & (__u16)0xff00U) >> 8) ))
52 #define ___constant_swab32(x)                           \
53     ((__u32)(                                           \
54         (((__u32)(x) & (__u32)0x000000ffUL) << 24) |    \
55         (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |    \
56         (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |    \
57         (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
58 #define ___constant_swab64(x)                                            \
59     ((__u64)(                                                            \
60         (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |     \
61         (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |     \
62         (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |     \
63         (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |     \
64             (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) | \
65         (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |     \
66         (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |     \
67         (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
68 
69 /*
70  * provide defaults when no architecture-specific optimization is detected
71  */
72 #ifndef __arch__swab16
73 #  define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); })
74 #endif
75 #ifndef __arch__swab32
76 #  define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
77 #endif
78 #ifndef __arch__swab64
79 #  define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
80 #endif
81 
82 #ifndef __arch__swab16p
83 #  define __arch__swab16p(x) __arch__swab16(*(x))
84 #endif
85 #ifndef __arch__swab32p
86 #  define __arch__swab32p(x) __arch__swab32(*(x))
87 #endif
88 #ifndef __arch__swab64p
89 #  define __arch__swab64p(x) __arch__swab64(*(x))
90 #endif
91 
92 #ifndef __arch__swab16s
93 #  define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
94 #endif
95 #ifndef __arch__swab32s
96 #  define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
97 #endif
98 #ifndef __arch__swab64s
99 #  define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
100 #endif
101 
102 
103 /*
104  * Allow constant folding
105  */
106 #if defined(__GNUC__) && defined(__OPTIMIZE__)
107 #  define __swab16(x) \
108 (__builtin_constant_p((__u16)(x)) ? \
109  ___swab16((x)) : \
110  __fswab16((x)))
111 #  define __swab32(x) \
112 (__builtin_constant_p((__u32)(x)) ? \
113  ___swab32((x)) : \
114  __fswab32((x)))
115 #  define __swab64(x) \
116 (__builtin_constant_p((__u64)(x)) ? \
117  ___swab64((x)) : \
118  __fswab64((x)))
119 #else
120 #  define __swab16(x) __fswab16(x)
121 #  define __swab32(x) __fswab32(x)
122 #  define __swab64(x) __fswab64(x)
123 #endif /* OPTIMIZE */
124 
125 
__fswab16(__u16 x)126 static inline __attribute_const__ __u16 __fswab16(__u16 x)
127 {
128     return __arch__swab16(x);
129 }
__swab16p(const __u16 * x)130 static inline __u16 __swab16p(const __u16 *x)
131 {
132     return __arch__swab16p(x);
133 }
__swab16s(__u16 * addr)134 static inline void __swab16s(__u16 *addr)
135 {
136     __arch__swab16s(addr);
137 }
138 
__fswab32(__u32 x)139 static inline __attribute_const__ __u32 __fswab32(__u32 x)
140 {
141     return __arch__swab32(x);
142 }
__swab32p(const __u32 * x)143 static inline __u32 __swab32p(const __u32 *x)
144 {
145     return __arch__swab32p(x);
146 }
__swab32s(__u32 * addr)147 static inline void __swab32s(__u32 *addr)
148 {
149     __arch__swab32s(addr);
150 }
151 
152 #ifdef __BYTEORDER_HAS_U64__
__fswab64(__u64 x)153 static inline __attribute_const__ __u64 __fswab64(__u64 x)
154 {
155 #  ifdef __SWAB_64_THRU_32__
156     __u32 h = x >> 32;
157         __u32 l = x & ((1ULL<<32)-1);
158         return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
159 #  else
160     return __arch__swab64(x);
161 #  endif
162 }
__swab64p(const __u64 * x)163 static inline __u64 __swab64p(const __u64 *x)
164 {
165     return __arch__swab64p(x);
166 }
__swab64s(__u64 * addr)167 static inline void __swab64s(__u64 *addr)
168 {
169     __arch__swab64s(addr);
170 }
171 #endif /* __BYTEORDER_HAS_U64__ */
172 
173 #define swab16 __swab16
174 #define swab32 __swab32
175 #define swab64 __swab64
176 #define swab16p __swab16p
177 #define swab32p __swab32p
178 #define swab64p __swab64p
179 #define swab16s __swab16s
180 #define swab32s __swab32s
181 #define swab64s __swab64s
182 
183 #endif /* __XEN_BYTEORDER_SWAB_H__ */
184