Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / include / asm-arm / unaligned.h
blob620f3729750926ead8d10abccb92c153543b9ac7
1 #ifndef __ASM_ARM_UNALIGNED_H
2 #define __ASM_ARM_UNALIGNED_H
4 #include <asm/types.h>
6 extern int __bug_unaligned_x(const void *ptr);
8 /*
9 * What is the most efficient way of loading/storing an unaligned value?
11 * That is the subject of this file. Efficiency here is defined as
12 * minimum code size with minimum register usage for the common cases.
13 * It is currently not believed that long longs are common, so we
14 * trade efficiency for the chars, shorts and longs against the long
15 * longs.
17 * Current stats with gcc 2.7.2.2 for these functions:
19 * ptrsize get: code regs put: code regs
20 * 1 1 1 1 2
21 * 2 3 2 3 2
22 * 4 7 3 7 3
23 * 8 20 6 16 6
25 * gcc 2.95.1 seems to code differently:
27 * ptrsize get: code regs put: code regs
28 * 1 1 1 1 2
29 * 2 3 2 3 2
30 * 4 7 4 7 4
31 * 8 19 8 15 6
33 * which may or may not be more efficient (depending upon whether
34 * you can afford the extra registers). Hopefully the gcc 2.95
35 * is inteligent enough to decide if it is better to use the
36 * extra register, but evidence so far seems to suggest otherwise.
38 * Unfortunately, gcc is not able to optimise the high word
39 * out of long long >> 32, or the low word from long long << 32
42 #define __get_unaligned_2_le(__p) \
43 <<<<<<< HEAD:include/asm-arm/unaligned.h
44 (__p[0] | __p[1] << 8)
45 =======
46 (unsigned int)(__p[0] | __p[1] << 8)
47 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:include/asm-arm/unaligned.h
49 #define __get_unaligned_2_be(__p) \
50 <<<<<<< HEAD:include/asm-arm/unaligned.h
51 (__p[0] << 8 | __p[1])
52 =======
53 (unsigned int)(__p[0] << 8 | __p[1])
54 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:include/asm-arm/unaligned.h
56 #define __get_unaligned_4_le(__p) \
57 <<<<<<< HEAD:include/asm-arm/unaligned.h
58 (__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
59 =======
60 (unsigned int)(__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
61 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:include/asm-arm/unaligned.h
63 #define __get_unaligned_4_be(__p) \
64 <<<<<<< HEAD:include/asm-arm/unaligned.h
65 (__p[0] << 24 | __p[1] << 16 | __p[2] << 8 | __p[3])
66 =======
67 (unsigned int)(__p[0] << 24 | __p[1] << 16 | __p[2] << 8 | __p[3])
68 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:include/asm-arm/unaligned.h
70 #define __get_unaligned_8_le(__p) \
71 ((unsigned long long)__get_unaligned_4_le((__p+4)) << 32 | \
72 __get_unaligned_4_le(__p))
74 #define __get_unaligned_8_be(__p) \
75 ((unsigned long long)__get_unaligned_4_be(__p) << 32 | \
76 __get_unaligned_4_be((__p+4)))
78 #define __get_unaligned_le(ptr) \
79 ((__force typeof(*(ptr)))({ \
80 const __u8 *__p = (const __u8 *)(ptr); \
81 __builtin_choose_expr(sizeof(*(ptr)) == 1, *__p, \
82 __builtin_choose_expr(sizeof(*(ptr)) == 2, __get_unaligned_2_le(__p), \
83 __builtin_choose_expr(sizeof(*(ptr)) == 4, __get_unaligned_4_le(__p), \
84 __builtin_choose_expr(sizeof(*(ptr)) == 8, __get_unaligned_8_le(__p), \
85 (void)__bug_unaligned_x(__p))))); \
86 }))
88 #define __get_unaligned_be(ptr) \
89 ((__force typeof(*(ptr)))({ \
90 const __u8 *__p = (const __u8 *)(ptr); \
91 __builtin_choose_expr(sizeof(*(ptr)) == 1, *__p, \
92 __builtin_choose_expr(sizeof(*(ptr)) == 2, __get_unaligned_2_be(__p), \
93 __builtin_choose_expr(sizeof(*(ptr)) == 4, __get_unaligned_4_be(__p), \
94 __builtin_choose_expr(sizeof(*(ptr)) == 8, __get_unaligned_8_be(__p), \
95 (void)__bug_unaligned_x(__p))))); \
96 }))
99 static inline void __put_unaligned_2_le(__u32 __v, register __u8 *__p)
101 *__p++ = __v;
102 *__p++ = __v >> 8;
105 static inline void __put_unaligned_2_be(__u32 __v, register __u8 *__p)
107 *__p++ = __v >> 8;
108 *__p++ = __v;
111 static inline void __put_unaligned_4_le(__u32 __v, register __u8 *__p)
113 __put_unaligned_2_le(__v >> 16, __p + 2);
114 __put_unaligned_2_le(__v, __p);
117 static inline void __put_unaligned_4_be(__u32 __v, register __u8 *__p)
119 __put_unaligned_2_be(__v >> 16, __p);
120 __put_unaligned_2_be(__v, __p + 2);
123 static inline void __put_unaligned_8_le(const unsigned long long __v, register __u8 *__p)
126 * tradeoff: 8 bytes of stack for all unaligned puts (2
127 * instructions), or an extra register in the long long
128 * case - go for the extra register.
130 __put_unaligned_4_le(__v >> 32, __p+4);
131 __put_unaligned_4_le(__v, __p);
134 static inline void __put_unaligned_8_be(const unsigned long long __v, register __u8 *__p)
137 * tradeoff: 8 bytes of stack for all unaligned puts (2
138 * instructions), or an extra register in the long long
139 * case - go for the extra register.
141 __put_unaligned_4_be(__v >> 32, __p);
142 __put_unaligned_4_be(__v, __p+4);
146 * Try to store an unaligned value as efficiently as possible.
148 #define __put_unaligned_le(val,ptr) \
149 ({ \
150 (void)sizeof(*(ptr) = (val)); \
151 switch (sizeof(*(ptr))) { \
152 case 1: \
153 *(ptr) = (val); \
154 break; \
155 case 2: __put_unaligned_2_le((__force u16)(val),(__u8 *)(ptr)); \
156 break; \
157 case 4: __put_unaligned_4_le((__force u32)(val),(__u8 *)(ptr)); \
158 break; \
159 case 8: __put_unaligned_8_le((__force u64)(val),(__u8 *)(ptr)); \
160 break; \
161 default: __bug_unaligned_x(ptr); \
162 break; \
164 (void) 0; \
167 #define __put_unaligned_be(val,ptr) \
168 ({ \
169 (void)sizeof(*(ptr) = (val)); \
170 switch (sizeof(*(ptr))) { \
171 case 1: \
172 *(ptr) = (val); \
173 break; \
174 case 2: __put_unaligned_2_be((__force u16)(val),(__u8 *)(ptr)); \
175 break; \
176 case 4: __put_unaligned_4_be((__force u32)(val),(__u8 *)(ptr)); \
177 break; \
178 case 8: __put_unaligned_8_be((__force u64)(val),(__u8 *)(ptr)); \
179 break; \
180 default: __bug_unaligned_x(ptr); \
181 break; \
183 (void) 0; \
187 * Select endianness
189 #ifndef __ARMEB__
190 #define get_unaligned __get_unaligned_le
191 #define put_unaligned __put_unaligned_le
192 #else
193 #define get_unaligned __get_unaligned_be
194 #define put_unaligned __put_unaligned_be
195 #endif
197 #endif