1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2a07f7672SMatt Fleming #ifndef _TOOLS_BE_BYTESHIFT_H
3a07f7672SMatt Fleming #define _TOOLS_BE_BYTESHIFT_H
4a07f7672SMatt Fleming
5b3531062SYaakov Selkowitz #include <stdint.h>
6a07f7672SMatt Fleming
__get_unaligned_be16(const uint8_t * p)7b3531062SYaakov Selkowitz static inline uint16_t __get_unaligned_be16(const uint8_t *p)
8a07f7672SMatt Fleming {
9a07f7672SMatt Fleming return p[0] << 8 | p[1];
10a07f7672SMatt Fleming }
11a07f7672SMatt Fleming
__get_unaligned_be32(const uint8_t * p)12b3531062SYaakov Selkowitz static inline uint32_t __get_unaligned_be32(const uint8_t *p)
13a07f7672SMatt Fleming {
14a07f7672SMatt Fleming return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
15a07f7672SMatt Fleming }
16a07f7672SMatt Fleming
__get_unaligned_be64(const uint8_t * p)17b3531062SYaakov Selkowitz static inline uint64_t __get_unaligned_be64(const uint8_t *p)
18a07f7672SMatt Fleming {
19b3531062SYaakov Selkowitz return (uint64_t)__get_unaligned_be32(p) << 32 |
20a07f7672SMatt Fleming __get_unaligned_be32(p + 4);
21a07f7672SMatt Fleming }
22a07f7672SMatt Fleming
__put_unaligned_be16(uint16_t val,uint8_t * p)23b3531062SYaakov Selkowitz static inline void __put_unaligned_be16(uint16_t val, uint8_t *p)
24a07f7672SMatt Fleming {
25a07f7672SMatt Fleming *p++ = val >> 8;
26a07f7672SMatt Fleming *p++ = val;
27a07f7672SMatt Fleming }
28a07f7672SMatt Fleming
__put_unaligned_be32(uint32_t val,uint8_t * p)29b3531062SYaakov Selkowitz static inline void __put_unaligned_be32(uint32_t val, uint8_t *p)
30a07f7672SMatt Fleming {
31a07f7672SMatt Fleming __put_unaligned_be16(val >> 16, p);
32a07f7672SMatt Fleming __put_unaligned_be16(val, p + 2);
33a07f7672SMatt Fleming }
34a07f7672SMatt Fleming
__put_unaligned_be64(uint64_t val,uint8_t * p)35b3531062SYaakov Selkowitz static inline void __put_unaligned_be64(uint64_t val, uint8_t *p)
36a07f7672SMatt Fleming {
37a07f7672SMatt Fleming __put_unaligned_be32(val >> 32, p);
38a07f7672SMatt Fleming __put_unaligned_be32(val, p + 4);
39a07f7672SMatt Fleming }
40a07f7672SMatt Fleming
get_unaligned_be16(const void * p)41b3531062SYaakov Selkowitz static inline uint16_t get_unaligned_be16(const void *p)
42a07f7672SMatt Fleming {
43b3531062SYaakov Selkowitz return __get_unaligned_be16((const uint8_t *)p);
44a07f7672SMatt Fleming }
45a07f7672SMatt Fleming
get_unaligned_be32(const void * p)46b3531062SYaakov Selkowitz static inline uint32_t get_unaligned_be32(const void *p)
47a07f7672SMatt Fleming {
48b3531062SYaakov Selkowitz return __get_unaligned_be32((const uint8_t *)p);
49a07f7672SMatt Fleming }
50a07f7672SMatt Fleming
get_unaligned_be64(const void * p)51b3531062SYaakov Selkowitz static inline uint64_t get_unaligned_be64(const void *p)
52a07f7672SMatt Fleming {
53b3531062SYaakov Selkowitz return __get_unaligned_be64((const uint8_t *)p);
54a07f7672SMatt Fleming }
55a07f7672SMatt Fleming
put_unaligned_be16(uint16_t val,void * p)56b3531062SYaakov Selkowitz static inline void put_unaligned_be16(uint16_t val, void *p)
57a07f7672SMatt Fleming {
58a07f7672SMatt Fleming __put_unaligned_be16(val, p);
59a07f7672SMatt Fleming }
60a07f7672SMatt Fleming
put_unaligned_be32(uint32_t val,void * p)61b3531062SYaakov Selkowitz static inline void put_unaligned_be32(uint32_t val, void *p)
62a07f7672SMatt Fleming {
63a07f7672SMatt Fleming __put_unaligned_be32(val, p);
64a07f7672SMatt Fleming }
65a07f7672SMatt Fleming
put_unaligned_be64(uint64_t val,void * p)66b3531062SYaakov Selkowitz static inline void put_unaligned_be64(uint64_t val, void *p)
67a07f7672SMatt Fleming {
68a07f7672SMatt Fleming __put_unaligned_be64(val, p);
69a07f7672SMatt Fleming }
70a07f7672SMatt Fleming
71a07f7672SMatt Fleming #endif /* _TOOLS_BE_BYTESHIFT_H */
72