udftools

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bswap.h
Go to the documentation of this file.
1 /*
2 * bswap.h
3 *
4 * Copyright (c) 2001-2002 Ben Fennema <bfennema@falcon.csc.calpoly.edu>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 #ifndef __BSWAP_H
24 #define __BSWAP_H
25
26 #include "config.h"
27
28 #include <inttypes.h>
29 #include <sys/types.h>
30
31 #ifdef HAVE_SYS_ISA_DEFS_H
32 #define __LITTLE_ENDIAN 1234
33 #define __BIG_ENDIAN 4321
34 #define __PDP_ENDIAN 3412
35
36 #ifdef _LITTLE_ENDIAN
37 #define __BYTE_ORDER __LITTLE_ENDIAN
38 #endif
39
40 #ifdef _BIG_ENDIAN
41 #define __BYTE_ORDER __BIG_ENDIAN
42 #endif
43 #endif
44
45 #define constant_swab16(x) \
46 ((uint16_t)((((uint16_t)(x) & 0x00FFU) << 8) | \
47 (((uint16_t)(x) & 0xFF00U) >> 8)))
48
49 #define constant_swab32(x) \
50 ((uint32_t)((((uint32_t)(x) & 0x000000FFU) << 24) | \
51 (((uint32_t)(x) & 0x0000FF00U) << 8) | \
52 (((uint32_t)(x) & 0x00FF0000U) >> 8) | \
53 (((uint32_t)(x) & 0xFF000000U) >> 24)))
54
55 #define constant_swab64(x) \
56 ((uint64_t)((((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
57 (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
58 (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
59 (((uint64_t)(x) & 0x00000000FF000000ULL) << 8) | \
60 (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8) | \
61 (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
62 (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
63 (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56)))
64
65 static inline uint16_t swab16(uint16_t x)
66 {
67 return ((uint16_t)((((uint16_t)(x) & 0x00FFU) << 8) | \
68 (((uint16_t)(x) & 0xFF00U) >> 8)));
69 }
70
71 static inline uint32_t swab32(uint32_t x)
72 {
73 return ((uint32_t)((((uint32_t)(x) & 0x000000FFU) << 24) | \
74 (((uint32_t)(x) & 0x0000FF00U) << 8) | \
75 (((uint32_t)(x) & 0x00FF0000U) >> 8) | \
76 (((uint32_t)(x) & 0xFF000000U) >> 24)));
77 }
78
79 static inline uint64_t swab64(uint64_t x)
80 {
81 return ((uint64_t)((((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
82 (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
83 (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
84 (((uint64_t)(x) & 0x00000000FF000000ULL) << 8) | \
85 (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8) | \
86 (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
87 (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
88 (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56)));
89 }
90
91 #define constant_swab16p(x) \
92 ((uint16_t)(((*(uint16_t *)(x) & 0x00FFU) << 8) | \
93 ((*(uint16_t *)(x) & 0xFF00U) >> 8)))
94
95 #define constant_swab32p(x) \
96 ((uint32_t)(((*(uint32_t *)(x) & 0x000000FFU) << 24) | \
97 ((*(uint32_t *)(x) & 0x0000FF00U) << 8) | \
98 ((*(uint32_t *)(x) & 0x00FF0000U) >> 8) | \
99 ((*(uint32_t *)(x) & 0xFF000000U) >> 24)))
100
101 #define constant_swab64p(x) \
102 ((uint64_t)(((*(uint64_t *)(x) & 0x00000000000000FFULL) << 56) | \
103 ((*(uint64_t *)(x) & 0x000000000000FF00ULL) << 40) | \
104 ((*(uint64_t *)(x) & 0x0000000000FF0000ULL) << 24) | \
105 ((*(uint64_t *)(x) & 0x00000000FF000000ULL) << 8) | \
106 ((*(uint64_t *)(x) & 0x000000FF00000000ULL) >> 8) | \
107 ((*(uint64_t *)(x) & 0x0000FF0000000000ULL) >> 24) | \
108 ((*(uint64_t *)(x) & 0x00FF000000000000ULL) >> 40) | \
109 ((*(uint64_t *)(x) & 0xFF00000000000000ULL) >> 56)))
110
111
112 static inline uint16_t swab16p(uint16_t *x)
113 {
114 return ((uint16_t)(((*(uint16_t *)(x) & 0x00FFU) << 8) | \
115 ((*(uint16_t *)(x) & 0xFF00U) >> 8)));
116 }
117
118 static inline uint32_t swab32p(uint32_t *x)
119 {
120 return ((uint32_t)(((*(uint32_t *)(x) & 0x000000FFU) << 24) | \
121 ((*(uint32_t *)(x) & 0x0000FF00U) << 8) | \
122 ((*(uint32_t *)(x) & 0x00FF0000U) >> 8) | \
123 ((*(uint32_t *)(x) & 0xFF000000U) >> 24)));
124 }
125
126 static inline uint64_t swab64p(uint64_t *x)
127 {
128 return ((uint64_t)(((*(uint64_t *)(x) & 0x00000000000000FFULL) << 56) | \
129 ((*(uint64_t *)(x) & 0x000000000000FF00ULL) << 40) | \
130 ((*(uint64_t *)(x) & 0x0000000000FF0000ULL) << 24) | \
131 ((*(uint64_t *)(x) & 0x00000000FF000000ULL) << 8) | \
132 ((*(uint64_t *)(x) & 0x000000FF00000000ULL) >> 8) | \
133 ((*(uint64_t *)(x) & 0x0000FF0000000000ULL) >> 24) | \
134 ((*(uint64_t *)(x) & 0x00FF000000000000ULL) >> 40) | \
135 ((*(uint64_t *)(x) & 0xFF00000000000000ULL) >> 56)));
136 }
137
138 #if __BYTE_ORDER == 0
139
140 #error "__BYTE_ORDER must be defined"
141
142 #elif __BYTE_ORDER == __BIG_ENDIAN
143
144 #define le16_to_cpu(x) (__builtin_constant_p(x) ? \
145 constant_swab16(x) : \
146 swab16(x))
147
148 #define le32_to_cpu(x) (__builtin_constant_p(x) ? \
149 constant_swab32(x) : \
150 swab32(x))
151
152 #define le64_to_cpu(x) (__builtin_constant_p(x) ? \
153 constant_swab64(x) : \
154 swab64(x))
155
156
157 #define constant_le16_to_cpu(x) constant_swab16((x))
158 #define constant_le32_to_cpu(x) constant_swab32((x))
159 #define constant_le64_to_cpu(x) constant_swab64((x))
160
161 #define le16_to_cpup(x) (__builtin_constant_p(x) ? \
162 constant_swab16p(x) : \
163 swab16p(x))
164
165 #define le32_to_cpup(x) (__builtin_constant_p(x) ? \
166 constant_swab32p(x) : \
167 swab32p(x))
168
169 #define le64_to_cpup(x) (__builtin_constant_p(x) ? \
170 constant_swab64p(x) : \
171 swab64p(x))
172
173 #define constant_le16_to_cpup(x) constant_swab16p((x))
174 #define constant_le32_to_cpup(x) constant_swab32p((x))
175 #define constant_le64_to_cpup(x) constant_swab64p((x))
176
177 #define be16_to_cpu(x) ((uint16_t)(x))
178 #define be32_to_cpu(x) ((uint32_t)(x))
179 #define be64_to_cpu(x) ((uint64_t)(x))
180
181 #define constant_be16_to_cpu(x) ((uint16_t)(x))
182 #define constant_be32_to_cpu(x) ((uint32_t)(x))
183 #define constant_be64_to_cpu(x) ((uint64_t)(x))
184
185 #define be16_to_cpup(x) (*(uint16_t *)(x))
186 #define be32_to_cpup(x) (*(uint32_t *)(x))
187 #define be64_to_cpup(x) (*(uint64_t *)(x))
188
189 #define constant_be16_to_cpup(x) (*(uint16_t *)(x))
190 #define constant_be32_to_cpup(x) (*(uint32_t *)(x))
191 #define constant_be64_to_cpup(x) (*(uint64_t *)(x))
192
193 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
194
195 #define le16_to_cpu(x) ((uint16_t)(x))
196 #define le32_to_cpu(x) ((uint32_t)(x))
197 #define le64_to_cpu(x) ((uint64_t)(x))
198
199 #define constant_le16_to_cpu(x) ((uint16_t)(x))
200 #define constant_le32_to_cpu(x) ((uint32_t)(x))
201 #define constant_le64_to_cpu(x) ((uint64_t)(x))
202
203 #define le16_to_cpup(x) (*(uint16_t *)(x))
204 #define le32_to_cpup(x) (*(uint32_t *)(x))
205 #define le64_to_cpup(x) (*(uint64_t *)(x))
206
207 #define constant_le16_to_cpup(x) (*(uint16_t *)(x))
208 #define constant_le32_to_cpup(x) (*(uint32_t *)(x))
209 #define constant_le64_to_cpup(x) (*(uint64_t *)(x))
210
211 #define be16_to_cpu(x) (__builtin_constant_p(x) ? \
212 constant_swab16(x) : \
213 swab16(x))
214
215 #define be32_to_cpu(x) (__builtin_constant_p(x) ? \
216 constant_swab32(x) : \
217 swab32(x))
218
219 #define be64_to_cpu(x) (__builtin_constant_p(x) ? \
220 constant_swab64(x) : \
221 swab64(x))
222
223
224 #define constant_be16_to_cpu(x) constant_swab16((x))
225 #define constant_be32_to_cpu(x) constant_swab32((x))
226 #define constant_be64_to_cpu(x) constant_swab64((x))
227
228 #define be16_to_cpup(x) (__builtin_constant_p(x) ? \
229 constant_swab16p(x) : \
230 swab16p(x))
231
232 #define be32_to_cpup(x) (__builtin_constant_p(x) ? \
233 constant_swab32p(x) : \
234 swab32p(x))
235
236 #define be64_to_cpup(x) (__builtin_constant_p(x) ? \
237 constant_swab64p(x) : \
238 swab64p(x))
239
240 #define constant_be16_to_cpup(x) constant_swab16p((x))
241 #define constant_be32_to_cpup(x) constant_swab32p((x))
242 #define constant_be64_to_cpup(x) constant_swab64p((x))
243
244 #endif /* __BYTE_ORDER == 0 */
245
246 #define cpu_to_le16(x) le16_to_cpu((x))
247 #define cpu_to_le32(x) le32_to_cpu((x))
248 #define cpu_to_le64(x) le64_to_cpu((x))
249
250 #define constant_cpu_to_le16(x) constant_le16_to_cpu((x))
251 #define constant_cpu_to_le32(x) constant_le32_to_cpu((x))
252 #define constant_cpu_to_le64(x) constant_le64_to_cpu((x))
253
254 #define cpu_to_le16p(x) le16_to_cpup((x))
255 #define cpu_to_le32p(x) le32_to_cpup((x))
256 #define cpu_to_le64p(x) le64_to_cpup((x))
257
258 #define constant_cpu_to_le16p(x) constant_le16_to_cpup((x))
259 #define constant_cpu_to_le32p(x) constant_le32_to_cpup((x))
260 #define constant_cpu_to_le64p(x) constant_le64_to_cpup((x))
261
262 #define cpu_to_be16(x) be16_to_cpu((x))
263 #define cpu_to_be32(x) be32_to_cpu((x))
264 #define cpu_to_be64(x) be64_to_cpu((x))
265
266 #define constant_cpu_to_be16(x) constant_be16_to_cpu((x))
267 #define constant_cpu_to_be32(x) constant_be32_to_cpu((x))
268 #define constant_cpu_to_be64(x) constant_be64_to_cpu((x))
269
270 #define cpu_to_be16p(x) be16_to_cpup((x))
271 #define cpu_to_be32p(x) be32_to_cpup((x))
272 #define cpu_to_be64p(x) be64_to_cpup((x))
273
274 #define constant_cpu_to_be16p(x) constant_be16_to_cpup((x))
275 #define constant_cpu_to_be32p(x) constant_be32_to_cpup((x))
276 #define constant_cpu_to_be64p(x) constant_be64_to_cpup((x))
277
278 #endif /* __BSWAP_H */
swab32p
static uint32_t swab32p(uint32_t *x)
Definition: bswap.h:118
swab16p
static uint16_t swab16p(uint16_t *x)
Definition: bswap.h:112
swab16
static uint16_t swab16(uint16_t x)
Definition: bswap.h:65
swab32
static uint32_t swab32(uint32_t x)
Definition: bswap.h:71
swab64p
static uint64_t swab64p(uint64_t *x)
Definition: bswap.h:126
swab64
static uint64_t swab64(uint64_t x)
Definition: bswap.h:79