1 #ifndef __ASM_ARM_UNALIGNED_H
2 #define __ASM_ARM_UNALIGNED_H
6 extern int __bug_unaligned_x(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_4_le(__p) \
46 (__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
48 #define __get_unaligned_le(ptr) \
50 __typeof__(*(ptr)) __v; \
51 __u8 *__p = (__u8 *)(ptr); \
52 switch (sizeof(*(ptr))) { \
53 case 1: __v = *(ptr); break; \
54 case 2: __v = __get_unaligned_2_le(__p); break; \
55 case 4: __v = __get_unaligned_4_le(__p); break; \
57 unsigned int __v1, __v2; \
58 __v2 = __get_unaligned_4_le((__p+4)); \
59 __v1 = __get_unaligned_4_le(__p); \
60 __v = ((unsigned long long)__v2 << 32 | __v1); \
63 default: __v = __bug_unaligned_x(__p); break; \
68 static inline void __put_unaligned_2_le(__u32 __v
, register __u8
*__p
)
74 static inline void __put_unaligned_4_le(__u32 __v
, register __u8
*__p
)
76 __put_unaligned_2_le(__v
>> 16, __p
+ 2);
77 __put_unaligned_2_le(__v
, __p
);
80 static inline void __put_unaligned_8_le(const unsigned long long __v
, register __u8
*__p
)
83 * tradeoff: 8 bytes of stack for all unaligned puts (2
84 * instructions), or an extra register in the long long
85 * case - go for the extra register.
87 __put_unaligned_4_le(__v
>> 32, __p
+4);
88 __put_unaligned_4_le(__v
, __p
);
92 * Try to store an unaligned value as efficiently as possible.
94 #define __put_unaligned_le(val,ptr) \
96 switch (sizeof(*(ptr))) { \
100 case 2: __put_unaligned_2_le((val),(__u8 *)(ptr)); \
102 case 4: __put_unaligned_4_le((val),(__u8 *)(ptr)); \
104 case 8: __put_unaligned_8_le((val),(__u8 *)(ptr)); \
106 default: __bug_unaligned_x(ptr); \
115 #define get_unaligned __get_unaligned_le
116 #define put_unaligned __put_unaligned_le