blob: c74caa371fe112779caeb40f8c5c85d57e9bb59c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
Copyright (c) 2008-2010 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _BYTE_ORDER_H
#define _BYTE_ORDER_H
#include <inttypes.h>
#define LS1 0x00ffU
#define MS1 0xff00U
#define LS2 0x0000ffffU
#define MS2 0xffff0000U
#define LS4 0x00000000ffffffffULL
#define MS4 0xffffffff00000000ULL
static uint16_t (*hton16) (uint16_t);
static uint32_t (*hton32) (uint32_t);
static uint64_t (*hton64) (uint64_t);
#define ntoh16 hton16
#define ntoh32 hton32
#define ntoh64 hton64
static uint16_t (*htole16) (uint16_t);
static uint32_t (*htole32) (uint32_t);
static uint64_t (*htole64) (uint64_t);
#define letoh16 htole16
#define letoh32 htole32
#define letoh64 htole64
static uint16_t (*htobe16) (uint16_t);
static uint32_t (*htobe32) (uint32_t);
static uint64_t (*htobe64) (uint64_t);
#define betoh16 htobe16
#define betoh32 htobe32
#define betoh64 htobe64
#define do_swap2(x) (((x&LS1) << 8)|(((x&MS1) >> 8)))
#define do_swap4(x) ((do_swap2(x&LS2) << 16)|(do_swap2((x&MS2) >> 16)))
#define do_swap8(x) ((do_swap4(x&LS4) << 32)|(do_swap4((x&MS4) >> 32)))
static inline uint16_t
__swap16 (uint16_t x)
{
return do_swap2(x);
}
static inline uint32_t
__swap32 (uint32_t x)
{
return do_swap4(x);
}
static inline uint64_t
__swap64 (uint64_t x)
{
return do_swap8(x);
}
static inline uint16_t
__noswap16 (uint16_t x)
{
return x;
}
static inline uint32_t
__noswap32 (uint32_t x)
{
return x;
}
static inline uint64_t
__noswap64 (uint64_t x)
{
return x;
}
static inline uint16_t
__byte_order_n16 (uint16_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
hton16 = __swap16;
hton32 = __swap32;
hton64 = __swap64;
} else {
/* cpu is be */
hton16 = __noswap16;
hton32 = __noswap32;
hton64 = __noswap64;
}
return hton16 (i);
}
static inline uint32_t
__byte_order_n32 (uint32_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
hton16 = __swap16;
hton32 = __swap32;
hton64 = __swap64;
} else {
/* cpu is be */
hton16 = __noswap16;
hton32 = __noswap32;
hton64 = __noswap64;
}
return hton32 (i);
}
static inline uint64_t
__byte_order_n64 (uint64_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
hton16 = __swap16;
hton32 = __swap32;
hton64 = __swap64;
} else {
/* cpu is be */
hton16 = __noswap16;
hton32 = __noswap32;
hton64 = __noswap64;
}
return hton64 (i);
}
static uint16_t (*hton16) (uint16_t) = __byte_order_n16;
static uint32_t (*hton32) (uint32_t) = __byte_order_n32;
static uint64_t (*hton64) (uint64_t) = __byte_order_n64;
static inline uint16_t
__byte_order_le16 (uint16_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htole16 = __noswap16;
htole32 = __noswap32;
htole64 = __noswap64;
} else {
/* cpu is be */
htole16 = __swap16;
htole32 = __swap32;
htole64 = __swap64;
}
return htole16 (i);
}
static inline uint32_t
__byte_order_le32 (uint32_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htole16 = __noswap16;
htole32 = __noswap32;
htole64 = __noswap64;
} else {
/* cpu is be */
htole16 = __swap16;
htole32 = __swap32;
htole64 = __swap64;
}
return htole32 (i);
}
static inline uint64_t
__byte_order_le64 (uint64_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htole16 = __noswap16;
htole32 = __noswap32;
htole64 = __noswap64;
} else {
/* cpu is be */
htole16 = __swap16;
htole32 = __swap32;
htole64 = __swap64;
}
return htole64 (i);
}
static uint16_t (*htole16) (uint16_t) = __byte_order_le16;
static uint32_t (*htole32) (uint32_t) = __byte_order_le32;
static uint64_t (*htole64) (uint64_t) = __byte_order_le64;
static inline uint16_t
__byte_order_be16 (uint16_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htobe16 = __swap16;
htobe32 = __swap32;
htobe64 = __swap64;
} else {
/* cpu is be */
htobe16 = __noswap16;
htobe32 = __noswap32;
htobe64 = __noswap64;
}
return htobe16 (i);
}
static inline uint32_t
__byte_order_be32 (uint32_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htobe16 = __swap16;
htobe32 = __swap32;
htobe64 = __swap64;
} else {
/* cpu is be */
htobe16 = __noswap16;
htobe32 = __noswap32;
htobe64 = __noswap64;
}
return htobe32 (i);
}
static inline uint64_t
__byte_order_be64 (uint64_t i)
{
uint32_t num = 1;
if (((char *)(&num))[0] == 1) {
/* cpu is le */
htobe16 = __swap16;
htobe32 = __swap32;
htobe64 = __swap64;
} else {
/* cpu is be */
htobe16 = __noswap16;
htobe32 = __noswap32;
htobe64 = __noswap64;
}
return htobe64 (i);
}
static uint16_t (*htobe16) (uint16_t) = __byte_order_be16;
static uint32_t (*htobe32) (uint32_t) = __byte_order_be32;
static uint64_t (*htobe64) (uint64_t) = __byte_order_be64;
#endif /* _BYTE_ORDER_H */
|