~ubuntu-branches/ubuntu/wily/glusterfs/wily

« back to all changes in this revision

Viewing changes to libglusterfs/src/byte-order.h

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2011-08-26 21:50:55 UTC
  • mfrom: (1.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110826215055-6y9q7rlttfft0szr
Tags: 3.2.3-1
* New upstream release.
  - Refresh patch 02-spelling-error.diff.
* Fix lintian warning: brace-expansion-in-debhelper-config-file.

Show diffs side-by-side

added added

removed removed

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