Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / asm-generic / unaligned.h
blobc856a43e3b455c30a654e0c2a16cc34c0ec1455c
1 #ifndef _ASM_GENERIC_UNALIGNED_H_
2 #define _ASM_GENERIC_UNALIGNED_H_
4 /*
5 * For the benefit of those who are trying to port Linux to another
6 * architecture, here are some C-language equivalents.
8 * This is based almost entirely upon Richard Henderson's
9 * asm-alpha/unaligned.h implementation. Some comments were
10 * taken from David Mosberger's asm-ia64/unaligned.h header.
13 #include <linux/types.h>
15 /*
16 * The main single-value unaligned transfer routines.
18 #define get_unaligned(ptr) \
19 ((__typeof__(*(ptr)))__get_unaligned((ptr), sizeof(*(ptr))))
20 #define put_unaligned(x,ptr) \
21 __put_unaligned((unsigned long)(x), (ptr), sizeof(*(ptr)))
24 * This function doesn't actually exist. The idea is that when
25 * someone uses the macros below with an unsupported size (datatype),
26 * the linker will alert us to the problem via an unresolved reference
27 * error.
29 extern void bad_unaligned_access_length(void) __attribute__((noreturn));
31 struct __una_u64 { __u64 x __attribute__((packed)); };
32 struct __una_u32 { __u32 x __attribute__((packed)); };
33 struct __una_u16 { __u16 x __attribute__((packed)); };
36 * Elemental unaligned loads
39 static inline unsigned long __uldq(const __u64 *addr)
41 const struct __una_u64 *ptr = (const struct __una_u64 *) addr;
42 return ptr->x;
45 static inline unsigned long __uldl(const __u32 *addr)
47 const struct __una_u32 *ptr = (const struct __una_u32 *) addr;
48 return ptr->x;
51 static inline unsigned long __uldw(const __u16 *addr)
53 const struct __una_u16 *ptr = (const struct __una_u16 *) addr;
54 return ptr->x;
58 * Elemental unaligned stores
61 static inline void __ustq(__u64 val, __u64 *addr)
63 struct __una_u64 *ptr = (struct __una_u64 *) addr;
64 ptr->x = val;
67 static inline void __ustl(__u32 val, __u32 *addr)
69 struct __una_u32 *ptr = (struct __una_u32 *) addr;
70 ptr->x = val;
73 static inline void __ustw(__u16 val, __u16 *addr)
75 struct __una_u16 *ptr = (struct __una_u16 *) addr;
76 ptr->x = val;
79 static inline unsigned long __get_unaligned(const void *ptr, size_t size)
81 unsigned long val;
82 switch (size) {
83 case 1:
84 val = *(const __u8 *)ptr;
85 break;
86 case 2:
87 val = __uldw((const __u16 *)ptr);
88 break;
89 case 4:
90 val = __uldl((const __u32 *)ptr);
91 break;
92 case 8:
93 val = __uldq((const __u64 *)ptr);
94 break;
95 default:
96 bad_unaligned_access_length();
98 return val;
101 static inline void __put_unaligned(unsigned long val, void *ptr, size_t size)
103 switch (size) {
104 case 1:
105 *(__u8 *)ptr = val;
106 break;
107 case 2:
108 __ustw(val, (__u16 *)ptr);
109 break;
110 case 4:
111 __ustl(val, (__u32 *)ptr);
112 break;
113 case 8:
114 __ustq(val, (__u64 *)ptr);
115 break;
116 default:
117 bad_unaligned_access_length();
121 #endif /* _ASM_GENERIC_UNALIGNED_H */