1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _TOOLS_LE_BYTESHIFT_H
3 #define _TOOLS_LE_BYTESHIFT_H
4
5 #include <stdint.h>
6
__get_unaligned_le16(const uint8_t * p)7 static inline uint16_t __get_unaligned_le16(const uint8_t *p)
8 {
9 return p[0] | p[1] << 8;
10 }
11
__get_unaligned_le32(const uint8_t * p)12 static inline uint32_t __get_unaligned_le32(const uint8_t *p)
13 {
14 return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
15 }
16
__get_unaligned_le64(const uint8_t * p)17 static inline uint64_t __get_unaligned_le64(const uint8_t *p)
18 {
19 return (uint64_t)__get_unaligned_le32(p + 4) << 32 |
20 __get_unaligned_le32(p);
21 }
22
__put_unaligned_le16(uint16_t val,uint8_t * p)23 static inline void __put_unaligned_le16(uint16_t val, uint8_t *p)
24 {
25 *p++ = val;
26 *p++ = val >> 8;
27 }
28
__put_unaligned_le32(uint32_t val,uint8_t * p)29 static inline void __put_unaligned_le32(uint32_t val, uint8_t *p)
30 {
31 __put_unaligned_le16(val >> 16, p + 2);
32 __put_unaligned_le16(val, p);
33 }
34
__put_unaligned_le64(uint64_t val,uint8_t * p)35 static inline void __put_unaligned_le64(uint64_t val, uint8_t *p)
36 {
37 __put_unaligned_le32(val >> 32, p + 4);
38 __put_unaligned_le32(val, p);
39 }
40
get_unaligned_le16(const void * p)41 static inline uint16_t get_unaligned_le16(const void *p)
42 {
43 return __get_unaligned_le16((const uint8_t *)p);
44 }
45
get_unaligned_le32(const void * p)46 static inline uint32_t get_unaligned_le32(const void *p)
47 {
48 return __get_unaligned_le32((const uint8_t *)p);
49 }
50
get_unaligned_le64(const void * p)51 static inline uint64_t get_unaligned_le64(const void *p)
52 {
53 return __get_unaligned_le64((const uint8_t *)p);
54 }
55
put_unaligned_le16(uint16_t val,void * p)56 static inline void put_unaligned_le16(uint16_t val, void *p)
57 {
58 __put_unaligned_le16(val, p);
59 }
60
put_unaligned_le32(uint32_t val,void * p)61 static inline void put_unaligned_le32(uint32_t val, void *p)
62 {
63 __put_unaligned_le32(val, p);
64 }
65
put_unaligned_le64(uint64_t val,void * p)66 static inline void put_unaligned_le64(uint64_t val, void *p)
67 {
68 __put_unaligned_le64(val, p);
69 }
70
71 #endif /* _TOOLS_LE_BYTESHIFT_H */
72