1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_UNALIGNED_H
3 #define __LINUX_UNALIGNED_H
6 * This is the most generic implementation of unaligned accesses
7 * and should work almost anywhere.
9 #include <linux/unaligned/packed_struct.h>
10 #include <asm/byteorder.h>
11 #include <vdso/unaligned.h>
13 #define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
14 #define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))
16 static inline u16
get_unaligned_le16(const void *p
)
18 return le16_to_cpu(__get_unaligned_t(__le16
, p
));
21 static inline u32
get_unaligned_le32(const void *p
)
23 return le32_to_cpu(__get_unaligned_t(__le32
, p
));
26 static inline u64
get_unaligned_le64(const void *p
)
28 return le64_to_cpu(__get_unaligned_t(__le64
, p
));
31 static inline void put_unaligned_le16(u16 val
, void *p
)
33 __put_unaligned_t(__le16
, cpu_to_le16(val
), p
);
36 static inline void put_unaligned_le32(u32 val
, void *p
)
38 __put_unaligned_t(__le32
, cpu_to_le32(val
), p
);
41 static inline void put_unaligned_le64(u64 val
, void *p
)
43 __put_unaligned_t(__le64
, cpu_to_le64(val
), p
);
46 static inline u16
get_unaligned_be16(const void *p
)
48 return be16_to_cpu(__get_unaligned_t(__be16
, p
));
51 static inline u32
get_unaligned_be32(const void *p
)
53 return be32_to_cpu(__get_unaligned_t(__be32
, p
));
56 static inline u64
get_unaligned_be64(const void *p
)
58 return be64_to_cpu(__get_unaligned_t(__be64
, p
));
61 static inline void put_unaligned_be16(u16 val
, void *p
)
63 __put_unaligned_t(__be16
, cpu_to_be16(val
), p
);
66 static inline void put_unaligned_be32(u32 val
, void *p
)
68 __put_unaligned_t(__be32
, cpu_to_be32(val
), p
);
71 static inline void put_unaligned_be64(u64 val
, void *p
)
73 __put_unaligned_t(__be64
, cpu_to_be64(val
), p
);
76 static inline u32
__get_unaligned_be24(const u8
*p
)
78 return p
[0] << 16 | p
[1] << 8 | p
[2];
81 static inline u32
get_unaligned_be24(const void *p
)
83 return __get_unaligned_be24(p
);
86 static inline u32
__get_unaligned_le24(const u8
*p
)
88 return p
[0] | p
[1] << 8 | p
[2] << 16;
91 static inline u32
get_unaligned_le24(const void *p
)
93 return __get_unaligned_le24(p
);
96 static inline void __put_unaligned_be24(const u32 val
, u8
*p
)
98 *p
++ = (val
>> 16) & 0xff;
99 *p
++ = (val
>> 8) & 0xff;
103 static inline void put_unaligned_be24(const u32 val
, void *p
)
105 __put_unaligned_be24(val
, p
);
108 static inline void __put_unaligned_le24(const u32 val
, u8
*p
)
111 *p
++ = (val
>> 8) & 0xff;
112 *p
++ = (val
>> 16) & 0xff;
115 static inline void put_unaligned_le24(const u32 val
, void *p
)
117 __put_unaligned_le24(val
, p
);
120 static inline void __put_unaligned_be48(const u64 val
, u8
*p
)
122 *p
++ = (val
>> 40) & 0xff;
123 *p
++ = (val
>> 32) & 0xff;
124 *p
++ = (val
>> 24) & 0xff;
125 *p
++ = (val
>> 16) & 0xff;
126 *p
++ = (val
>> 8) & 0xff;
130 static inline void put_unaligned_be48(const u64 val
, void *p
)
132 __put_unaligned_be48(val
, p
);
135 static inline u64
__get_unaligned_be48(const u8
*p
)
137 return (u64
)p
[0] << 40 | (u64
)p
[1] << 32 | (u64
)p
[2] << 24 |
138 p
[3] << 16 | p
[4] << 8 | p
[5];
141 static inline u64
get_unaligned_be48(const void *p
)
143 return __get_unaligned_be48(p
);
146 #endif /* __LINUX_UNALIGNED_H */