Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


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