]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
b0fd3dabc52f37f91ffe4d3d66a2d00b3a19b8e8
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
32 #ifndef _OS_OSBYTEORDER_H
33 #define _OS_OSBYTEORDER_H
35 #include <libkern/OSTypes.h>
38 #include <libkern/ppc/OSByteOrder.h>
39 #elif defined(__i386__)
40 #include <libkern/i386/OSByteOrder.h>
42 #include <libkern/machine/OSByteOrder.h>
53 OSHostByteOrder(void) {
54 UInt32 x
= (OSBigEndian
<< 24) | OSLittleEndian
;
55 return (UInt32
)*((UInt8
*)&x
);
58 /* Macros for swapping constant values in the preprocessing stage. */
59 #define OSSwapConstInt16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
61 #define OSSwapConstInt32(x) ((OSSwapConstInt16(x) << 16) | \
62 (OSSwapConstInt16((x) >> 16)))
64 #define OSSwapConstInt64(x) ((OSSwapConstInt32(x) << 32) | \
65 (OSSwapConstInt32((x) >> 32)))
67 #if defined(__BIG_ENDIAN__)
69 /* Functions for loading big endian to host endianess. */
78 return *(volatile UInt
*)((UInt8
*)base
+ offset
);
88 return *(volatile UInt16
*)((UInt8
*)base
+ offset
);
98 return *(volatile UInt32
*)((UInt8
*)base
+ offset
);
104 volatile void * base
,
108 return *(volatile UInt64
*)((UInt8
*)base
+ offset
);
111 /* Functions for storing host endianess to big endian. */
116 volatile void * base
,
121 *(volatile UInt
*)((UInt8
*)base
+ offset
) = data
;
127 volatile void * base
,
132 *(volatile UInt16
*)((UInt8
*)base
+ offset
) = data
;
138 volatile void * base
,
143 *(volatile UInt32
*)((UInt8
*)base
+ offset
) = data
;
149 volatile void * base
,
154 *(volatile UInt64
*)((UInt8
*)base
+ offset
) = data
;
157 /* Functions for loading little endian to host endianess. */
162 volatile void * base
,
166 return OSReadSwapInt(base
, offset
);
172 volatile void * base
,
176 return OSReadSwapInt16(base
, offset
);
182 volatile void * base
,
186 return OSReadSwapInt32(base
, offset
);
192 volatile void * base
,
196 return OSReadSwapInt64(base
, offset
);
199 /* Functions for storing host endianess to little endian. */
204 volatile void * base
,
209 OSWriteSwapInt(base
, offset
, data
);
215 volatile void * base
,
220 OSWriteSwapInt16(base
, offset
, data
);
226 volatile void * base
,
231 OSWriteSwapInt32(base
, offset
, data
);
237 volatile void * base
,
242 OSWriteSwapInt64(base
, offset
, data
);
245 /* Host endianess to big endian byte swapping macros for constants. */
247 #define OSSwapHostToBigConstInt16(x) (x)
248 #define OSSwapHostToBigConstInt32(x) (x)
249 #define OSSwapHostToBigConstInt64(x) (x)
251 /* Generic host endianess to big endian byte swapping functions. */
264 OSSwapHostToBigInt16(
273 OSSwapHostToBigInt32(
282 OSSwapHostToBigInt64(
289 /* Host endianess to little endian byte swapping macros for constants. */
291 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
292 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
293 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
295 /* Generic host endianess to little endian byte swapping functions. */
299 OSSwapHostToLittleInt(
303 return OSSwapInt(data
);
308 OSSwapHostToLittleInt16(
312 return OSSwapInt16(data
);
317 OSSwapHostToLittleInt32(
321 return OSSwapInt32(data
);
326 OSSwapHostToLittleInt64(
330 return OSSwapInt64(data
);
333 /* Big endian to host endianess byte swapping macros for constants. */
335 #define OSSwapBigToHostConstInt16(x) (x)
336 #define OSSwapBigToHostConstInt32(x) (x)
337 #define OSSwapBigToHostConstInt64(x) (x)
339 /* Generic big endian to host endianess byte swapping functions. */
352 OSSwapBigToHostInt16(
361 OSSwapBigToHostInt32(
370 OSSwapBigToHostInt64(
377 /* Little endian to host endianess byte swapping macros for constants. */
379 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
380 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
381 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
383 /* Generic little endian to host endianess byte swapping functions. */
387 OSSwapLittleToHostInt(
391 return OSSwapInt(data
);
396 OSSwapLittleToHostInt16(
400 return OSSwapInt16(data
);
405 OSSwapLittleToHostInt32(
409 return OSSwapInt32(data
);
414 OSSwapLittleToHostInt64(
418 return OSSwapInt64(data
);
421 #elif defined(__LITTLE_ENDIAN__)
423 /* Functions for loading big endian to host endianess. */
428 volatile void * base
,
432 return OSReadSwapInt(base
, offset
);
438 volatile void * base
,
442 return OSReadSwapInt16(base
, offset
);
448 volatile void * base
,
452 return OSReadSwapInt32(base
, offset
);
458 volatile void * base
,
462 return OSReadSwapInt64(base
, offset
);
465 /* Functions for storing host endianess to big endian. */
470 volatile void * base
,
475 OSWriteSwapInt(base
, offset
, data
);
481 volatile void * base
,
486 OSWriteSwapInt16(base
, offset
, data
);
492 volatile void * base
,
497 OSWriteSwapInt32(base
, offset
, data
);
503 volatile void * base
,
508 OSWriteSwapInt64(base
, offset
, data
);
511 /* Functions for loading little endian to host endianess. */
516 volatile void * base
,
520 return *(volatile UInt
*)((UInt8
*)base
+ offset
);
526 volatile void * base
,
530 return *(volatile UInt16
*)((UInt8
*)base
+ offset
);
536 volatile void * base
,
540 return *(volatile UInt32
*)((UInt8
*)base
+ offset
);
546 volatile void * base
,
550 return *(volatile UInt64
*)((UInt8
*)base
+ offset
);
553 /* Functions for storing host endianess to little endian. */
558 volatile void * base
,
563 *(volatile UInt
*)((UInt8
*)base
+ offset
) = data
;
569 volatile void * base
,
574 *(volatile UInt16
*)((UInt8
*)base
+ offset
) = data
;
580 volatile void * base
,
585 *(volatile UInt32
*)((UInt8
*)base
+ offset
) = data
;
591 volatile void * base
,
596 *(volatile UInt64
*)((UInt8
*)base
+ offset
) = data
;
599 /* Host endianess to big endian byte swapping macros for constants. */
601 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
602 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
603 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
605 /* Generic host endianess to big endian byte swapping functions. */
613 return OSSwapInt(data
);
618 OSSwapHostToBigInt16(
622 return OSSwapInt16(data
);
627 OSSwapHostToBigInt32(
631 return OSSwapInt32(data
);
636 OSSwapHostToBigInt64(
640 return OSSwapInt64(data
);
643 /* Host endianess to little endian byte swapping macros for constants. */
645 #define OSSwapHostToLittleConstInt16(x) (x)
646 #define OSSwapHostToLittleConstInt32(x) (x)
647 #define OSSwapHostToLittleConstInt64(x) (x)
649 /* Generic host endianess to little endian byte swapping functions. */
653 OSSwapHostToLittleInt(
662 OSSwapHostToLittleInt16(
671 OSSwapHostToLittleInt32(
680 OSSwapHostToLittleInt64(
687 /* Big endian to host endianess byte swapping macros for constants. */
689 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
690 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
691 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
693 /* Generic big endian to host endianess byte swapping functions. */
701 return OSSwapInt(data
);
706 OSSwapBigToHostInt16(
710 return OSSwapInt16(data
);
715 OSSwapBigToHostInt32(
719 return OSSwapInt32(data
);
724 OSSwapBigToHostInt64(
728 return OSSwapInt64(data
);
731 /* Little endian to host endianess byte swapping macros for constants. */
733 #define OSSwapLittleToHostConstInt16(x) (x)
734 #define OSSwapLittleToHostConstInt32(x) (x)
735 #define OSSwapLittleToHostConstInt64(x) (x)
737 /* Generic little endian to host endianess byte swapping functions. */
741 OSSwapLittleToHostInt(
750 OSSwapLittleToHostInt16(
759 OSSwapLittleToHostInt32(
768 OSSwapLittleToHostInt64(
776 #error Unknown endianess.
779 #endif /* ! _OS_OSBYTEORDER_H */