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