1 /* MN10300 Unaligned memory access handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #ifndef _ASM_UNALIGNED_H
12 #define _ASM_UNALIGNED_H
14 #include <asm/types.h>
17 extern int __bug_unaligned_x(void *ptr);
20 * What is the most efficient way of loading/storing an unaligned value?
22 * That is the subject of this file. Efficiency here is defined as
23 * minimum code size with minimum register usage for the common cases.
24 * It is currently not believed that long longs are common, so we
25 * trade efficiency for the chars, shorts and longs against the long
28 * Current stats with gcc 2.7.2.2 for these functions:
30 * ptrsize get: code regs put: code regs
36 * gcc 2.95.1 seems to code differently:
38 * ptrsize get: code regs put: code regs
44 * which may or may not be more efficient (depending upon whether
45 * you can afford the extra registers). Hopefully the gcc 2.95
46 * is inteligent enough to decide if it is better to use the
47 * extra register, but evidence so far seems to suggest otherwise.
49 * Unfortunately, gcc is not able to optimise the high word
50 * out of long long >> 32, or the low word from long long << 32
53 #define __get_unaligned_2(__p) \
54 (__p[0] | __p[1] << 8)
56 #define __get_unaligned_4(__p) \
57 (__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
59 #define get_unaligned(ptr) \
61 unsigned int __v1, __v2; \
62 __typeof__(*(ptr)) __v; \
63 __u8 *__p = (__u8 *)(ptr); \
65 switch (sizeof(*(ptr))) { \
66 case 1: __v = *(ptr); break; \
67 case 2: __v = __get_unaligned_2(__p); break; \
68 case 4: __v = __get_unaligned_4(__p); break; \
70 __v2 = __get_unaligned_4((__p+4)); \
71 __v1 = __get_unaligned_4(__p); \
72 __v = ((unsigned long long)__v2 << 32 | __v1); \
74 default: __v = __bug_unaligned_x(__p); break; \
80 static inline void __put_unaligned_2(__u32 __v, register __u8 *__p)
86 static inline void __put_unaligned_4(__u32 __v, register __u8 *__p)
88 __put_unaligned_2(__v >> 16, __p + 2);
89 __put_unaligned_2(__v, __p);
92 static inline void __put_unaligned_8(const unsigned long long __v, __u8 *__p)
95 * tradeoff: 8 bytes of stack for all unaligned puts (2
96 * instructions), or an extra register in the long long
97 * case - go for the extra register.
99 __put_unaligned_4(__v >> 32, __p + 4);
100 __put_unaligned_4(__v, __p);
104 * Try to store an unaligned value as efficiently as possible.
106 #define put_unaligned(val, ptr) \
108 switch (sizeof(*(ptr))) { \
113 __put_unaligned_2((val), (__u8 *)(ptr)); \
116 __put_unaligned_4((val), (__u8 *)(ptr)); \
119 __put_unaligned_8((val), (__u8 *)(ptr)); \
122 __bug_unaligned_x(ptr); \
131 #define get_unaligned(ptr) (*(ptr))
132 #define put_unaligned(val, ptr) ({ *(ptr) = (val); (void) 0; })