2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1996, 1999, 2000, 2001, 2003 by Ralf Baechle
7 * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
9 #ifndef _ASM_UNALIGNED_H
10 #define _ASM_UNALIGNED_H
12 #include <linux/types.h>
15 * get_unaligned - get value from possibly mis-aligned location
16 * @ptr: pointer to value
18 * This macro should be used for accessing values larger in size than
19 * single bytes at locations that are expected to be improperly aligned,
20 * e.g. retrieving a u16 value from a location not u16-aligned.
22 * Note that unaligned accesses can be very expensive on some architectures.
24 #define get_unaligned(ptr) \
25 ((__typeof__(*(ptr)))__get_unaligned((ptr), sizeof(*(ptr))))
28 * put_unaligned - put value to a possibly mis-aligned location
29 * @val: value to place
30 * @ptr: pointer to location
32 * This macro should be used for placing values larger in size than
33 * single bytes at locations that are expected to be improperly aligned,
34 * e.g. writing a u16 value to a location not u16-aligned.
36 * Note that unaligned accesses can be very expensive on some architectures.
38 #define put_unaligned(x,ptr) \
39 __put_unaligned((__u64)(x), (ptr), sizeof(*(ptr)))
42 * This is a silly but good way to make sure that
43 * the get/put functions are indeed always optimized,
44 * and that we use the correct sizes.
46 extern void bad_unaligned_access_length(void);
49 * EGCS 1.1 knows about arbitrary unaligned loads. Define some
50 * packed structures to talk about such things with.
53 struct __una_u64 { __u64 x __attribute__((packed)); };
54 struct __una_u32 { __u32 x __attribute__((packed)); };
55 struct __una_u16 { __u16 x __attribute__((packed)); };
58 * Elemental unaligned loads
61 static inline __u64 __uldq(const __u64 * r11)
63 const struct __una_u64 *ptr = (const struct __una_u64 *) r11;
67 static inline __u32 __uldl(const __u32 * r11)
69 const struct __una_u32 *ptr = (const struct __una_u32 *) r11;
73 static inline __u16 __uldw(const __u16 * r11)
75 const struct __una_u16 *ptr = (const struct __una_u16 *) r11;
80 * Elemental unaligned stores
83 static inline void __ustq(__u64 r5, __u64 * r11)
85 struct __una_u64 *ptr = (struct __una_u64 *) r11;
89 static inline void __ustl(__u32 r5, __u32 * r11)
91 struct __una_u32 *ptr = (struct __una_u32 *) r11;
95 static inline void __ustw(__u16 r5, __u16 * r11)
97 struct __una_u16 *ptr = (struct __una_u16 *) r11;
101 static inline __u64 __get_unaligned(const void *ptr, size_t size)
107 val = *(const __u8 *)ptr;
110 val = __uldw((const __u16 *)ptr);
113 val = __uldl((const __u32 *)ptr);
116 val = __uldq((const __u64 *)ptr);
119 bad_unaligned_access_length();
124 static inline void __put_unaligned(__u64 val, void *ptr, size_t size)
128 *(__u8 *)ptr = (val);
131 __ustw(val, (__u16 *)ptr);
134 __ustl(val, (__u32 *)ptr);
137 __ustq(val, (__u64 *)ptr);
140 bad_unaligned_access_length();
144 #endif /* _ASM_UNALIGNED_H */