1 #ifndef __ASM_ARM_UNALIGNED_H
2 #define __ASM_ARM_UNALIGNED_H
6 extern int __bug_unaligned_x(const void *ptr);
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
17 * Current stats with gcc 2.7.2.2 for these functions:
19 * ptrsize get: code regs put: code regs
25 * gcc 2.95.1 seems to code differently:
27 * ptrsize get: code regs put: code regs
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 (__p[0] | __p[1] << 8)
45 #define __get_unaligned_2_be(__p) \
46 (__p[0] << 8 | __p[1])
48 #define __get_unaligned_4_le(__p) \
49 (__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
51 #define __get_unaligned_4_be(__p) \
52 (__p[0] << 24 | __p[1] << 16 | __p[2] << 8 | __p[3])
54 #define __get_unaligned_8_le(__p) \
55 ((unsigned long long)__get_unaligned_4_le((__p+4)) << 32 | \
56 __get_unaligned_4_le(__p))
58 #define __get_unaligned_8_be(__p) \
59 ((unsigned long long)__get_unaligned_4_be(__p) << 32 | \
60 __get_unaligned_4_be((__p+4)))
62 #define __get_unaligned_le(ptr) \
63 ((__force typeof(*(ptr)))({ \
64 const __u8 *__p = (const __u8 *)(ptr); \
65 __builtin_choose_expr(sizeof(*(ptr)) == 1, *__p, \
66 __builtin_choose_expr(sizeof(*(ptr)) == 2, __get_unaligned_2_le(__p), \
67 __builtin_choose_expr(sizeof(*(ptr)) == 4, __get_unaligned_4_le(__p), \
68 __builtin_choose_expr(sizeof(*(ptr)) == 8, __get_unaligned_8_le(__p), \
69 (void)__bug_unaligned_x(__p))))); \
72 #define __get_unaligned_be(ptr) \
73 ((__force typeof(*(ptr)))({ \
74 const __u8 *__p = (const __u8 *)(ptr); \
75 __builtin_choose_expr(sizeof(*(ptr)) == 1, *__p, \
76 __builtin_choose_expr(sizeof(*(ptr)) == 2, __get_unaligned_2_be(__p), \
77 __builtin_choose_expr(sizeof(*(ptr)) == 4, __get_unaligned_4_be(__p), \
78 __builtin_choose_expr(sizeof(*(ptr)) == 8, __get_unaligned_8_be(__p), \
79 (void)__bug_unaligned_x(__p))))); \
83 static inline void __put_unaligned_2_le(__u32 __v, register __u8 *__p)
89 static inline void __put_unaligned_2_be(__u32 __v, register __u8 *__p)
95 static inline void __put_unaligned_4_le(__u32 __v, register __u8 *__p)
97 __put_unaligned_2_le(__v >> 16, __p + 2);
98 __put_unaligned_2_le(__v, __p);
101 static inline void __put_unaligned_4_be(__u32 __v, register __u8 *__p)
103 __put_unaligned_2_be(__v >> 16, __p);
104 __put_unaligned_2_be(__v, __p + 2);
107 static inline void __put_unaligned_8_le(const unsigned long long __v, register __u8 *__p)
110 * tradeoff: 8 bytes of stack for all unaligned puts (2
111 * instructions), or an extra register in the long long
112 * case - go for the extra register.
114 __put_unaligned_4_le(__v >> 32, __p+4);
115 __put_unaligned_4_le(__v, __p);
118 static inline void __put_unaligned_8_be(const unsigned long long __v, register __u8 *__p)
121 * tradeoff: 8 bytes of stack for all unaligned puts (2
122 * instructions), or an extra register in the long long
123 * case - go for the extra register.
125 __put_unaligned_4_be(__v >> 32, __p);
126 __put_unaligned_4_be(__v, __p+4);
130 * Try to store an unaligned value as efficiently as possible.
132 #define __put_unaligned_le(val,ptr) \
134 (void)sizeof(*(ptr) = (val)); \
135 switch (sizeof(*(ptr))) { \
139 case 2: __put_unaligned_2_le((__force u16)(val),(__u8 *)(ptr)); \
141 case 4: __put_unaligned_4_le((__force u32)(val),(__u8 *)(ptr)); \
143 case 8: __put_unaligned_8_le((__force u64)(val),(__u8 *)(ptr)); \
145 default: __bug_unaligned_x(ptr); \
151 #define __put_unaligned_be(val,ptr) \
153 (void)sizeof(*(ptr) = (val)); \
154 switch (sizeof(*(ptr))) { \
158 case 2: __put_unaligned_2_be((__force u16)(val),(__u8 *)(ptr)); \
160 case 4: __put_unaligned_4_be((__force u32)(val),(__u8 *)(ptr)); \
162 case 8: __put_unaligned_8_be((__force u64)(val),(__u8 *)(ptr)); \
164 default: __bug_unaligned_x(ptr); \
174 #define get_unaligned __get_unaligned_le
175 #define put_unaligned __put_unaligned_le
177 #define get_unaligned __get_unaligned_be
178 #define put_unaligned __put_unaligned_be