chiark / gitweb /
unaligned: let gcc generate optimal code
[elogind.git] / src / basic / unaligned.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2014 Tom Gundersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <endian.h>
24 #include <stdint.h>
25
26 /* BE */
27
28 static inline uint16_t unaligned_read_be16(const void *_u) {
29         const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
30
31         return be16toh(u->x);
32 }
33
34 static inline uint32_t unaligned_read_be32(const void *_u) {
35         const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
36
37         return be32toh(u->x);
38 }
39
40 static inline uint64_t unaligned_read_be64(const void *_u) {
41         const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
42
43         return be64toh(u->x);
44 }
45
46 static inline void unaligned_write_be16(void *_u, uint16_t a) {
47         struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
48
49         u->x = be16toh(a);
50 }
51
52 static inline void unaligned_write_be32(void *_u, uint32_t a) {
53         struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
54
55         u->x = be32toh(a);
56 }
57
58 static inline void unaligned_write_be64(void *_u, uint64_t a) {
59         struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
60
61         u->x = be64toh(a);
62 }
63
64 /* LE */
65
66 static inline uint16_t unaligned_read_le16(const void *_u) {
67         const struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
68
69         return le16toh(u->x);
70 }
71
72 static inline uint32_t unaligned_read_le32(const void *_u) {
73         const struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
74
75         return le32toh(u->x);
76 }
77
78 static inline uint64_t unaligned_read_le64(const void *_u) {
79         const struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
80
81         return le64toh(u->x);
82 }
83
84 static inline void unaligned_write_le16(void *_u, uint16_t a) {
85         struct __attribute__((packed, may_alias)) { uint16_t x; } *u = _u;
86
87         u->x = le16toh(a);
88 }
89
90 static inline void unaligned_write_le32(void *_u, uint32_t a) {
91         struct __attribute__((packed, may_alias)) { uint32_t x; } *u = _u;
92
93         u->x = le32toh(a);
94 }
95
96 static inline void unaligned_write_le64(void *_u, uint64_t a) {
97         struct __attribute__((packed, may_alias)) { uint64_t x; } *u = _u;
98
99         u->x = le64toh(a);
100 }
101
102 #if __BYTE_ORDER == __BIG_ENDIAN
103 #define unaligned_read_ne16 unaligned_read_be16
104 #define unaligned_read_ne32 unaligned_read_be32
105 #define unaligned_read_ne64 unaligned_read_be64
106
107 #define unaligned_write_ne16 unaligned_write_be16
108 #define unaligned_write_ne32 unaligned_write_be32
109 #define unaligned_write_ne64 unaligned_write_be64
110 #else
111 #define unaligned_read_ne16 unaligned_read_le16
112 #define unaligned_read_ne32 unaligned_read_le32
113 #define unaligned_read_ne64 unaligned_read_le64
114
115 #define unaligned_write_ne16 unaligned_write_le16
116 #define unaligned_write_ne32 unaligned_write_le32
117 #define unaligned_write_ne64 unaligned_write_le64
118 #endif