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