blob: ae5e5f914bf4a082b1f12e3956f4e868011710db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_BYTEORDER_SWABB_H
2#define _LINUX_BYTEORDER_SWABB_H
3
4/*
5 * linux/byteorder/swabb.h
6 * SWAp Bytes Bizarrely
7 * swaHHXX[ps]?(foo)
8 *
9 * Support for obNUXIous pdp-endian and other bizarre architectures.
10 * Will Linux ever run on such ancient beasts? if not, this file
11 * will be but a programming pearl. Still, it's a reminder that we
12 * shouldn't be making too many assumptions when trying to be portable.
13 *
14 */
15
16/*
17 * Meaning of the names I chose (vaxlinux people feel free to correct them):
18 * swahw32 swap 16-bit half-words in a 32-bit word
19 * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word
20 *
21 * No 64-bit support yet. I don't know NUXI conventions for long longs.
22 * I guarantee it will be a mess when it's there, though :->
23 * It will be even worse if there are conflicting 64-bit conventions.
24 * Hopefully, no one ever used 64-bit objects on NUXI machines.
25 *
26 */
27
28#define ___swahw32(x) \
29({ \
30 __u32 __x = (x); \
31 ((__u32)( \
32 (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
33 (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
34})
35#define ___swahb32(x) \
36({ \
37 __u32 __x = (x); \
38 ((__u32)( \
39 (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
40 (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \
41})
42
43#define ___constant_swahw32(x) \
44 ((__u32)( \
45 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
46 (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
47#define ___constant_swahb32(x) \
48 ((__u32)( \
49 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
50 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
51
52/*
53 * provide defaults when no architecture-specific optimization is detected
54 */
55#ifndef __arch__swahw32
56# define __arch__swahw32(x) ___swahw32(x)
57#endif
58#ifndef __arch__swahb32
59# define __arch__swahb32(x) ___swahb32(x)
60#endif
61
62#ifndef __arch__swahw32p
63# define __arch__swahw32p(x) __swahw32(*(x))
64#endif
65#ifndef __arch__swahb32p
66# define __arch__swahb32p(x) __swahb32(*(x))
67#endif
68
69#ifndef __arch__swahw32s
70# define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
71#endif
72#ifndef __arch__swahb32s
73# define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
74#endif
75
76
77/*
78 * Allow constant folding
79 */
Andrew Mortona1365642006-01-08 01:04:09 -080080#if defined(__GNUC__) && defined(__OPTIMIZE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081# define __swahw32(x) \
82(__builtin_constant_p((__u32)(x)) ? \
83 ___swahw32((x)) : \
84 __fswahw32((x)))
85# define __swahb32(x) \
86(__builtin_constant_p((__u32)(x)) ? \
87 ___swahb32((x)) : \
88 __fswahb32((x)))
89#else
90# define __swahw32(x) __fswahw32(x)
91# define __swahb32(x) __fswahb32(x)
92#endif /* OPTIMIZE */
93
94
Andrew Morton05133fc2005-06-28 20:44:54 -070095static inline __u32 __fswahw32(__u32 x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 return __arch__swahw32(x);
98}
Andrew Morton05133fc2005-06-28 20:44:54 -070099
100static inline __u32 __swahw32p(__u32 *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 return __arch__swahw32p(x);
103}
Andrew Morton05133fc2005-06-28 20:44:54 -0700104
105static inline void __swahw32s(__u32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 __arch__swahw32s(addr);
108}
109
Andrew Morton05133fc2005-06-28 20:44:54 -0700110static inline __u32 __fswahb32(__u32 x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 return __arch__swahb32(x);
113}
Andrew Morton05133fc2005-06-28 20:44:54 -0700114
115static inline __u32 __swahb32p(__u32 *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 return __arch__swahb32p(x);
118}
Andrew Morton05133fc2005-06-28 20:44:54 -0700119
120static inline void __swahb32s(__u32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 __arch__swahb32s(addr);
123}
124
125#ifdef __BYTEORDER_HAS_U64__
126/*
127 * Not supported yet
128 */
129#endif /* __BYTEORDER_HAS_U64__ */
130
131#if defined(__KERNEL__)
132#define swahw32 __swahw32
133#define swahb32 __swahb32
134#define swahw32p __swahw32p
135#define swahb32p __swahb32p
136#define swahw32s __swahw32s
137#define swahb32s __swahb32s
138#endif
139
140#endif /* _LINUX_BYTEORDER_SWABB_H */