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