]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
29 #ifndef _OS_OSBYTEORDER_H
30 #define _OS_OSBYTEORDER_H
32 #include <libkern/OSTypes.h>
35 #include <libkern/ppc/OSByteOrder.h>
36 #elif defined(__i386__)
37 #include <libkern/i386/OSByteOrder.h>
39 #include <libkern/machine/OSByteOrder.h>
50 OSHostByteOrder(void) {
51 UInt32 x
= (OSBigEndian
<< 24) | OSLittleEndian
;
52 return (UInt32
)*((UInt8
*)&x
);
55 /* Macros for swapping constant values in the preprocessing stage. */
56 #define OSSwapConstInt16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
58 #define OSSwapConstInt32(x) ((OSSwapConstInt16(x) << 16) | \
59 (OSSwapConstInt16((x) >> 16)))
61 #define OSSwapConstInt64(x) ((OSSwapConstInt32(x) << 32) | \
62 (OSSwapConstInt32((x) >> 32)))
64 #if defined(__BIG_ENDIAN__)
66 /* Functions for loading big endian to host endianess. */
75 return *(volatile UInt
*)((UInt8
*)base
+ offset
);
85 return *(volatile UInt16
*)((UInt8
*)base
+ offset
);
95 return *(volatile UInt32
*)((UInt8
*)base
+ offset
);
101 volatile void * base
,
105 return *(volatile UInt64
*)((UInt8
*)base
+ offset
);
108 /* Functions for storing host endianess to big endian. */
113 volatile void * base
,
118 *(volatile UInt
*)((UInt8
*)base
+ offset
) = data
;
124 volatile void * base
,
129 *(volatile UInt16
*)((UInt8
*)base
+ offset
) = data
;
135 volatile void * base
,
140 *(volatile UInt32
*)((UInt8
*)base
+ offset
) = data
;
146 volatile void * base
,
151 *(volatile UInt64
*)((UInt8
*)base
+ offset
) = data
;
154 /* Functions for loading little endian to host endianess. */
159 volatile void * base
,
163 return OSReadSwapInt(base
, offset
);
169 volatile void * base
,
173 return OSReadSwapInt16(base
, offset
);
179 volatile void * base
,
183 return OSReadSwapInt32(base
, offset
);
189 volatile void * base
,
193 return OSReadSwapInt64(base
, offset
);
196 /* Functions for storing host endianess to little endian. */
201 volatile void * base
,
206 OSWriteSwapInt(base
, offset
, data
);
212 volatile void * base
,
217 OSWriteSwapInt16(base
, offset
, data
);
223 volatile void * base
,
228 OSWriteSwapInt32(base
, offset
, data
);
234 volatile void * base
,
239 OSWriteSwapInt64(base
, offset
, data
);
242 /* Host endianess to big endian byte swapping macros for constants. */
244 #define OSSwapHostToBigConstInt16(x) (x)
245 #define OSSwapHostToBigConstInt32(x) (x)
246 #define OSSwapHostToBigConstInt64(x) (x)
248 /* Generic host endianess to big endian byte swapping functions. */
261 OSSwapHostToBigInt16(
270 OSSwapHostToBigInt32(
279 OSSwapHostToBigInt64(
286 /* Host endianess to little endian byte swapping macros for constants. */
288 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
289 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
290 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
292 /* Generic host endianess to little endian byte swapping functions. */
296 OSSwapHostToLittleInt(
300 return OSSwapInt(data
);
305 OSSwapHostToLittleInt16(
309 return OSSwapInt16(data
);
314 OSSwapHostToLittleInt32(
318 return OSSwapInt32(data
);
323 OSSwapHostToLittleInt64(
327 return OSSwapInt64(data
);
330 /* Big endian to host endianess byte swapping macros for constants. */
332 #define OSSwapBigToHostConstInt16(x) (x)
333 #define OSSwapBigToHostConstInt32(x) (x)
334 #define OSSwapBigToHostConstInt64(x) (x)
336 /* Generic big endian to host endianess byte swapping functions. */
349 OSSwapBigToHostInt16(
358 OSSwapBigToHostInt32(
367 OSSwapBigToHostInt64(
374 /* Little endian to host endianess byte swapping macros for constants. */
376 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
377 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
378 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
380 /* Generic little endian to host endianess byte swapping functions. */
384 OSSwapLittleToHostInt(
388 return OSSwapInt(data
);
393 OSSwapLittleToHostInt16(
397 return OSSwapInt16(data
);
402 OSSwapLittleToHostInt32(
406 return OSSwapInt32(data
);
411 OSSwapLittleToHostInt64(
415 return OSSwapInt64(data
);
418 #elif defined(__LITTLE_ENDIAN__)
420 /* Functions for loading big endian to host endianess. */
425 volatile void * base
,
429 return OSReadSwapInt(base
, offset
);
435 volatile void * base
,
439 return OSReadSwapInt16(base
, offset
);
445 volatile void * base
,
449 return OSReadSwapInt32(base
, offset
);
455 volatile void * base
,
459 return OSReadSwapInt64(base
, offset
);
462 /* Functions for storing host endianess to big endian. */
467 volatile void * base
,
472 OSWriteSwapInt(base
, offset
, data
);
478 volatile void * base
,
483 OSWriteSwapInt16(base
, offset
, data
);
489 volatile void * base
,
494 OSWriteSwapInt32(base
, offset
, data
);
500 volatile void * base
,
505 OSWriteSwapInt64(base
, offset
, data
);
508 /* Functions for loading little endian to host endianess. */
513 volatile void * base
,
517 return *(volatile UInt
*)((UInt8
*)base
+ offset
);
523 volatile void * base
,
527 return *(volatile UInt16
*)((UInt8
*)base
+ offset
);
533 volatile void * base
,
537 return *(volatile UInt32
*)((UInt8
*)base
+ offset
);
543 volatile void * base
,
547 return *(volatile UInt64
*)((UInt8
*)base
+ offset
);
550 /* Functions for storing host endianess to little endian. */
555 volatile void * base
,
560 *(volatile UInt
*)((UInt8
*)base
+ offset
) = data
;
566 volatile void * base
,
571 *(volatile UInt16
*)((UInt8
*)base
+ offset
) = data
;
577 volatile void * base
,
582 *(volatile UInt32
*)((UInt8
*)base
+ offset
) = data
;
588 volatile void * base
,
593 *(volatile UInt64
*)((UInt8
*)base
+ offset
) = data
;
596 /* Host endianess to big endian byte swapping macros for constants. */
598 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
599 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
600 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
602 /* Generic host endianess to big endian byte swapping functions. */
610 return OSSwapInt(data
);
615 OSSwapHostToBigInt16(
619 return OSSwapInt16(data
);
624 OSSwapHostToBigInt32(
628 return OSSwapInt32(data
);
633 OSSwapHostToBigInt64(
637 return OSSwapInt64(data
);
640 /* Host endianess to little endian byte swapping macros for constants. */
642 #define OSSwapHostToLittleConstInt16(x) (x)
643 #define OSSwapHostToLittleConstInt32(x) (x)
644 #define OSSwapHostToLittleConstInt64(x) (x)
646 /* Generic host endianess to little endian byte swapping functions. */
650 OSSwapHostToLittleInt(
659 OSSwapHostToLittleInt16(
668 OSSwapHostToLittleInt32(
677 OSSwapHostToLittleInt64(
684 /* Big endian to host endianess byte swapping macros for constants. */
686 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
687 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
688 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
690 /* Generic big endian to host endianess byte swapping functions. */
698 return OSSwapInt(data
);
703 OSSwapBigToHostInt16(
707 return OSSwapInt16(data
);
712 OSSwapBigToHostInt32(
716 return OSSwapInt32(data
);
721 OSSwapBigToHostInt64(
725 return OSSwapInt64(data
);
728 /* Little endian to host endianess byte swapping macros for constants. */
730 #define OSSwapLittleToHostConstInt16(x) (x)
731 #define OSSwapLittleToHostConstInt32(x) (x)
732 #define OSSwapLittleToHostConstInt64(x) (x)
734 /* Generic little endian to host endianess byte swapping functions. */
738 OSSwapLittleToHostInt(
747 OSSwapLittleToHostInt16(
756 OSSwapLittleToHostInt32(
765 OSSwapLittleToHostInt64(
773 #error Unknown endianess.
776 #endif /* ! _OS_OSBYTEORDER_H */