]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/architecture/byte_order.h
baf21ec93a141fc283a8a2523d1da5187492caac
[apple/xnu.git] / EXTERNAL_HEADERS / architecture / byte_order.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1992 NeXT Computer, Inc.
32 *
33 * Byte ordering conversion.
34 */
35
36 #ifndef _ARCHITECTURE_BYTE_ORDER_H_
37 #define _ARCHITECTURE_BYTE_ORDER_H_
38
39 typedef unsigned long NXSwappedFloat;
40 typedef unsigned long long NXSwappedDouble;
41
42 #if defined (__ppc__) || defined(__ppc64__)
43 #include "architecture/ppc/byte_order.h"
44 #elif defined (__i386__)
45 #include "architecture/i386/byte_order.h"
46 #else
47 #error architecture not supported
48 #endif
49
50 /*
51 * Identify the byte order
52 * of the current host.
53 */
54
55 enum NXByteOrder {
56 NX_UnknownByteOrder,
57 NX_LittleEndian,
58 NX_BigEndian
59 };
60
61 static __inline__
62 enum NXByteOrder
63 NXHostByteOrder(void)
64 {
65 unsigned int _x;
66
67 _x = (NX_BigEndian << 24) | NX_LittleEndian;
68
69 return ((enum NXByteOrder)*((unsigned char *)&_x));
70 }
71
72 /*
73 * The predicated versions
74 * are defined here in terms
75 * of the unpredicated ones.
76 */
77
78 #if __BIG_ENDIAN__
79
80 static __inline__
81 unsigned short
82 NXSwapBigShortToHost(
83 unsigned short x
84 )
85 {
86 return (x);
87 }
88
89 static __inline__
90 unsigned int
91 NXSwapBigIntToHost(
92 unsigned int x
93 )
94 {
95 return (x);
96 }
97
98 static __inline__
99 unsigned long
100 NXSwapBigLongToHost(
101 unsigned long x
102 )
103 {
104 return (x);
105 }
106
107 static __inline__
108 unsigned long long
109 NXSwapBigLongLongToHost(
110 unsigned long long x
111 )
112 {
113 return (x);
114 }
115
116 #ifndef KERNEL
117
118 static __inline__
119 double
120 NXSwapBigDoubleToHost(
121 NXSwappedDouble x
122 )
123 {
124 return NXConvertSwappedDoubleToHost(x);
125 }
126
127 static __inline__
128 float
129 NXSwapBigFloatToHost(
130 NXSwappedFloat x
131 )
132 {
133 return NXConvertSwappedFloatToHost(x);
134 }
135
136 #endif /* KERNEL */
137
138 static __inline__
139 unsigned short
140 NXSwapHostShortToBig(
141 unsigned short x
142 )
143 {
144 return (x);
145 }
146
147 static __inline__
148 unsigned int
149 NXSwapHostIntToBig(
150 unsigned int x
151 )
152 {
153 return (x);
154 }
155
156 static __inline__
157 unsigned long
158 NXSwapHostLongToBig(
159 unsigned long x
160 )
161 {
162 return (x);
163 }
164
165 static __inline__
166 unsigned long long
167 NXSwapHostLongLongToBig(
168 unsigned long long x
169 )
170 {
171 return (x);
172 }
173
174 #ifndef KERNEL
175
176 static __inline__
177 NXSwappedDouble
178 NXSwapHostDoubleToBig(
179 double x
180 )
181 {
182 return NXConvertHostDoubleToSwapped(x);
183 }
184
185 static __inline__
186 NXSwappedFloat
187 NXSwapHostFloatToBig(
188 float x
189 )
190 {
191 return NXConvertHostFloatToSwapped(x);
192 }
193
194 #endif /* KERNEL */
195
196 static __inline__
197 unsigned short
198 NXSwapLittleShortToHost(
199 unsigned short x
200 )
201 {
202 return (NXSwapShort(x));
203 }
204
205 static __inline__
206 unsigned int
207 NXSwapLittleIntToHost(
208 unsigned int x
209 )
210 {
211 return (NXSwapInt(x));
212 }
213
214 static __inline__
215 unsigned long
216 NXSwapLittleLongToHost(
217 unsigned long x
218 )
219 {
220 return (NXSwapLong(x));
221 }
222
223 static __inline__
224 unsigned long long
225 NXSwapLittleLongLongToHost(
226 unsigned long long x
227 )
228 {
229 return (NXSwapLongLong(x));
230 }
231
232 #ifndef KERNEL
233
234 static __inline__
235 double
236 NXSwapLittleDoubleToHost(
237 NXSwappedDouble x
238 )
239 {
240 return NXConvertSwappedDoubleToHost(NXSwapDouble(x));
241 }
242
243 static __inline__
244 float
245 NXSwapLittleFloatToHost(
246 NXSwappedFloat x
247 )
248 {
249 return NXConvertSwappedFloatToHost(NXSwapFloat(x));
250 }
251
252 #endif /* KERNEL */
253
254 static __inline__
255 unsigned short
256 NXSwapHostShortToLittle(
257 unsigned short x
258 )
259 {
260 return (NXSwapShort(x));
261 }
262
263 static __inline__
264 unsigned int
265 NXSwapHostIntToLittle(
266 unsigned int x
267 )
268 {
269 return (NXSwapInt(x));
270 }
271
272 static __inline__
273 unsigned long
274 NXSwapHostLongToLittle(
275 unsigned long x
276 )
277 {
278 return (NXSwapLong(x));
279 }
280
281 static __inline__
282 unsigned long long
283 NXSwapHostLongLongToLittle(
284 unsigned long long x
285 )
286 {
287 return (NXSwapLongLong(x));
288 }
289
290 #ifndef KERNEL
291
292 static __inline__
293 NXSwappedDouble
294 NXSwapHostDoubleToLittle(
295 double x
296 )
297 {
298 return NXSwapDouble(NXConvertHostDoubleToSwapped(x));
299 }
300
301 static __inline__
302 NXSwappedFloat
303 NXSwapHostFloatToLittle(
304 float x
305 )
306 {
307 return NXSwapFloat(NXConvertHostFloatToSwapped(x));
308 }
309
310 #endif /* KERNEL */
311 #endif /*__BIG_ENDIAN__ */
312
313 #if __LITTLE_ENDIAN__
314
315 static __inline__
316 unsigned short
317 NXSwapBigShortToHost(
318 unsigned short x
319 )
320 {
321 return (NXSwapShort(x));
322 }
323
324 static __inline__
325 unsigned int
326 NXSwapBigIntToHost(
327 unsigned int x
328 )
329 {
330 return (NXSwapInt(x));
331 }
332
333 static __inline__
334 unsigned long
335 NXSwapBigLongToHost(
336 unsigned long x
337 )
338 {
339 return (NXSwapLong(x));
340 }
341
342 static __inline__
343 unsigned long long
344 NXSwapBigLongLongToHost(
345 unsigned long long x
346 )
347 {
348 return (NXSwapLongLong(x));
349 }
350
351 static __inline__
352 double
353 NXSwapBigDoubleToHost(
354 NXSwappedDouble x
355 )
356 {
357 return NXConvertSwappedDoubleToHost(NXSwapDouble(x));
358 }
359
360 static __inline__
361 float
362 NXSwapBigFloatToHost(
363 NXSwappedFloat x
364 )
365 {
366 return NXConvertSwappedFloatToHost(NXSwapFloat(x));
367 }
368
369 static __inline__
370 unsigned short
371 NXSwapHostShortToBig(
372 unsigned short x
373 )
374 {
375 return (NXSwapShort(x));
376 }
377
378 static __inline__
379 unsigned int
380 NXSwapHostIntToBig(
381 unsigned int x
382 )
383 {
384 return (NXSwapInt(x));
385 }
386
387 static __inline__
388 unsigned long
389 NXSwapHostLongToBig(
390 unsigned long x
391 )
392 {
393 return (NXSwapLong(x));
394 }
395
396 static __inline__
397 unsigned long long
398 NXSwapHostLongLongToBig(
399 unsigned long long x
400 )
401 {
402 return (NXSwapLongLong(x));
403 }
404
405 static __inline__
406 NXSwappedDouble
407 NXSwapHostDoubleToBig(
408 double x
409 )
410 {
411 return (NXSwapDouble(NXConvertHostDoubleToSwapped(x)));
412 }
413
414 static __inline__
415 NXSwappedFloat
416 NXSwapHostFloatToBig(
417 float x
418 )
419 {
420 return (NXSwapFloat(NXConvertHostFloatToSwapped(x)));
421 }
422
423 static __inline__
424 unsigned short
425 NXSwapLittleShortToHost(
426 unsigned short x
427 )
428 {
429 return (x);
430 }
431
432 static __inline__
433 unsigned int
434 NXSwapLittleIntToHost(
435 unsigned int x
436 )
437 {
438 return (x);
439 }
440
441 static __inline__
442 unsigned long
443 NXSwapLittleLongToHost(
444 unsigned long x
445 )
446 {
447 return (x);
448 }
449
450 static __inline__
451 unsigned long long
452 NXSwapLittleLongLongToHost(
453 unsigned long long x
454 )
455 {
456 return (x);
457 }
458
459 static __inline__
460 double
461 NXSwapLittleDoubleToHost(
462 NXSwappedDouble x
463 )
464 {
465 return NXConvertSwappedDoubleToHost(x);
466 }
467
468 static __inline__
469 float
470 NXSwapLittleFloatToHost(
471 NXSwappedFloat x
472 )
473 {
474 return NXConvertSwappedFloatToHost(x);
475 }
476
477 static __inline__
478 unsigned short
479 NXSwapHostShortToLittle(
480 unsigned short x
481 )
482 {
483 return (x);
484 }
485
486 static __inline__
487 unsigned int
488 NXSwapHostIntToLittle(
489 unsigned int x
490 )
491 {
492 return (x);
493 }
494
495 static __inline__
496 unsigned long
497 NXSwapHostLongToLittle(
498 unsigned long x
499 )
500 {
501 return (x);
502 }
503
504 static __inline__
505 unsigned long long
506 NXSwapHostLongLongToLittle(
507 unsigned long long x
508 )
509 {
510 return (x);
511 }
512
513 static __inline__
514 NXSwappedDouble
515 NXSwapHostDoubleToLittle(
516 double x
517 )
518 {
519 return NXConvertHostDoubleToSwapped(x);
520 }
521
522 static __inline__
523 NXSwappedFloat
524 NXSwapHostFloatToLittle(
525 float x
526 )
527 {
528 return NXConvertHostFloatToSwapped(x);
529 }
530
531 #endif /* __LITTLE_ENDIAN__ */
532
533 #endif /* _ARCHITECTURE_BYTE_ORDER_H_ */