1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 ******************************************************************************/
20 #ifndef _LINUX_BYTEORDER_SWABB_H
21 #define _LINUX_BYTEORDER_SWABB_H
24 * linux/byteorder/swabb.h
25 * SWAp Bytes Bizarrely
28 * Support for obNUXIous pdp-endian and other bizarre architectures.
29 * Will Linux ever run on such ancient beasts? if not, this file
30 * will be but a programming pearl. Still, it's a reminder that we
31 * shouldn't be making too many assumptions when trying to be portable.
36 * Meaning of the names I chose (vaxlinux people feel free to correct them):
37 * swahw32 swap 16-bit half-words in a 32-bit word
38 * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word
40 * No 64-bit support yet. I don't know NUXI conventions for long longs.
41 * I guarantee it will be a mess when it's there, though :->
42 * It will be even worse if there are conflicting 64-bit conventions.
43 * Hopefully, no one ever used 64-bit objects on NUXI machines.
47 #define ___swahw32(x) \
51 (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
52 (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
54 #define ___swahb32(x) \
58 (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
59 (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \
62 #define ___constant_swahw32(x) \
64 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
65 (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
66 #define ___constant_swahb32(x) \
68 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
69 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
72 * provide defaults when no architecture-specific optimization is detected
74 #ifndef __arch__swahw32
75 # define __arch__swahw32(x) ___swahw32(x)
77 #ifndef __arch__swahb32
78 # define __arch__swahb32(x) ___swahb32(x)
81 #ifndef __arch__swahw32p
82 # define __arch__swahw32p(x) __swahw32(*(x))
84 #ifndef __arch__swahb32p
85 # define __arch__swahb32p(x) __swahb32(*(x))
88 #ifndef __arch__swahw32s
89 # define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
91 #ifndef __arch__swahb32s
92 # define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
97 * Allow constant folding
99 #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
100 # define __swahw32(x) \
101 (__builtin_constant_p((__u32)(x)) ? \
104 # define __swahb32(x) \
105 (__builtin_constant_p((__u32)(x)) ? \
109 # define __swahw32(x) __fswahw32(x)
110 # define __swahb32(x) __fswahb32(x)
111 #endif /* OPTIMIZE */
114 __inline static__ __const__ __u32 __fswahw32(__u32 x)
116 return __arch__swahw32(x);
118 __inline static__ __u32 __swahw32p(__u32 *x)
120 return __arch__swahw32p(x);
122 __inline static__ void __swahw32s(__u32 *addr)
124 __arch__swahw32s(addr);
128 __inline static__ __const__ __u32 __fswahb32(__u32 x)
130 return __arch__swahb32(x);
132 __inline static__ __u32 __swahb32p(__u32 *x)
134 return __arch__swahb32p(x);
136 __inline static__ void __swahb32s(__u32 *addr)
138 __arch__swahb32s(addr);
141 #ifdef __BYTEORDER_HAS_U64__
145 #endif /* __BYTEORDER_HAS_U64__ */
147 #if defined(PLATFORM_LINUX)
148 #define swahw32 __swahw32
149 #define swahb32 __swahb32
150 #define swahw32p __swahw32p
151 #define swahb32p __swahb32p
152 #define swahw32s __swahw32s
153 #define swahb32s __swahb32s
156 #endif /* _LINUX_BYTEORDER_SWABB_H */