chiark / gitweb /
8a1d06445e5a74bdfa995e089fbc130aa79f76cf
[elogind.git] / src / basic / unaligned.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4
5 #include <endian.h>
6 #include <stdint.h>
7
8 /* BE */
9
10 static inline uint16_t unaligned_read_be16(const void *_u) {
11         const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
12
13         return be16toh(u->x);
14 }
15
16 static inline uint32_t unaligned_read_be32(const void *_u) {
17         const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
18
19         return be32toh(u->x);
20 }
21
22 static inline uint64_t unaligned_read_be64(const void *_u) {
23         const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
24
25         return be64toh(u->x);
26 }
27
28 static inline void unaligned_write_be16(void *_u, uint16_t a) {
29         struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
30
31         u->x = be16toh(a);
32 }
33
34 static inline void unaligned_write_be32(void *_u, uint32_t a) {
35         struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
36
37         u->x = be32toh(a);
38 }
39
40 static inline void unaligned_write_be64(void *_u, uint64_t a) {
41         struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
42
43         u->x = be64toh(a);
44 }
45
46 /* LE */
47
48 static inline uint16_t unaligned_read_le16(const void *_u) {
49         const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
50
51         return le16toh(u->x);
52 }
53
54 static inline uint32_t unaligned_read_le32(const void *_u) {
55         const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
56
57         return le32toh(u->x);
58 }
59
60 static inline uint64_t unaligned_read_le64(const void *_u) {
61         const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
62
63         return le64toh(u->x);
64 }
65
66 static inline void unaligned_write_le16(void *_u, uint16_t a) {
67         struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
68
69         u->x = le16toh(a);
70 }
71
72 static inline void unaligned_write_le32(void *_u, uint32_t a) {
73         struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
74
75         u->x = le32toh(a);
76 }
77
78 static inline void unaligned_write_le64(void *_u, uint64_t a) {
79         struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
80
81         u->x = le64toh(a);
82 }
83
84 #if __BYTE_ORDER == __BIG_ENDIAN
85 #define unaligned_read_ne16 unaligned_read_be16
86 #define unaligned_read_ne32 unaligned_read_be32
87 #define unaligned_read_ne64 unaligned_read_be64
88
89 #define unaligned_write_ne16 unaligned_write_be16
90 #define unaligned_write_ne32 unaligned_write_be32
91 #define unaligned_write_ne64 unaligned_write_be64
92 #else
93 #define unaligned_read_ne16 unaligned_read_le16
94 #define unaligned_read_ne32 unaligned_read_le32
95 #define unaligned_read_ne64 unaligned_read_le64
96
97 #define unaligned_write_ne16 unaligned_write_le16
98 #define unaligned_write_ne32 unaligned_write_le32
99 #define unaligned_write_ne64 unaligned_write_le64
100 #endif