]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
14acbcc8835a5376f41dec1e4023c5409ec1d92e
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 #ifndef _OS_OSBYTEORDER_H
31 #define _OS_OSBYTEORDER_H
35 #if defined(__GNUC__) && defined(__ppc__)
36 #include <libkern/ppc/OSByteOrder.h>
37 #elif defined(__GNUC__) && defined(__i386__)
38 #include <libkern/i386/OSByteOrder.h>
40 #include <libkern/machine/OSByteOrder.h>
51 OSHostByteOrder(void) {
52 #if defined(__LITTLE_ENDIAN__)
53 return OSLittleEndian
;
54 #elif defined(__BIG_ENDIAN__)
57 return OSUnknownByteOrder
;
61 /* Macros for swapping constant values in the preprocessing stage. */
62 #define OSSwapConstInt16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
63 (((uint16_t)(x) & 0x00ff) << 8))
65 #define OSSwapConstInt32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
66 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
67 (((uint32_t)(x) & 0x0000ff00) << 8) | \
68 (((uint32_t)(x) & 0x000000ff) << 24))
70 #define OSSwapConstInt64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
71 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
72 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
73 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
74 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
75 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
76 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
77 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))
79 #if !defined(__GNUC__)
80 #define __builtin_constant_p(x) (0)
83 #define OSSwapInt16(x) \
84 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
86 #define OSSwapInt32(x) \
87 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
89 #define OSSwapInt64(x) \
90 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
92 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
93 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
94 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
95 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
96 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
97 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
98 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
99 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
101 #if defined(__BIG_ENDIAN__)
103 /* Functions for loading big endian to host endianess. */
108 const volatile void * base
,
112 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
118 const volatile void * base
,
122 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
128 const volatile void * base
,
132 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
135 /* Functions for storing host endianess to big endian. */
140 volatile void * base
,
145 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
151 volatile void * base
,
156 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
162 volatile void * base
,
167 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
170 /* Functions for loading little endian to host endianess. */
175 volatile void * base
,
179 return OSReadSwapInt16(base
, offset
);
185 volatile void * base
,
189 return OSReadSwapInt32(base
, offset
);
195 volatile void * base
,
199 return OSReadSwapInt64(base
, offset
);
202 /* Functions for storing host endianess to little endian. */
207 volatile void * base
,
212 OSWriteSwapInt16(base
, offset
, data
);
218 volatile void * base
,
223 OSWriteSwapInt32(base
, offset
, data
);
229 volatile void * base
,
234 OSWriteSwapInt64(base
, offset
, data
);
237 /* Host endianess to big endian byte swapping macros for constants. */
239 #define OSSwapHostToBigConstInt16(x) (x)
240 #define OSSwapHostToBigConstInt32(x) (x)
241 #define OSSwapHostToBigConstInt64(x) (x)
243 /* Generic host endianess to big endian byte swapping functions. */
247 OSSwapHostToBigInt16(
256 OSSwapHostToBigInt32(
265 OSSwapHostToBigInt64(
272 /* Host endianess to little endian byte swapping macros for constants. */
274 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
275 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
276 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
278 /* Generic host endianess to little endian byte swapping functions. */
280 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
281 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
282 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
284 /* Big endian to host endianess byte swapping macros for constants. */
286 #define OSSwapBigToHostConstInt16(x) (x)
287 #define OSSwapBigToHostConstInt32(x) (x)
288 #define OSSwapBigToHostConstInt64(x) (x)
290 /* Generic big endian to host endianess byte swapping functions. */
294 OSSwapBigToHostInt16(
303 OSSwapBigToHostInt32(
312 OSSwapBigToHostInt64(
319 /* Little endian to host endianess byte swapping macros for constants. */
321 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
322 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
323 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
325 /* Generic little endian to host endianess byte swapping functions. */
327 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
328 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
329 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
331 #elif defined(__LITTLE_ENDIAN__)
333 /* Functions for loading big endian to host endianess. */
338 const volatile void * base
,
342 return OSReadSwapInt16(base
, offset
);
348 const volatile void * base
,
352 return OSReadSwapInt32(base
, offset
);
358 const volatile void * base
,
362 return OSReadSwapInt64(base
, offset
);
365 /* Functions for storing host endianess to big endian. */
370 volatile void * base
,
375 OSWriteSwapInt16(base
, offset
, data
);
381 volatile void * base
,
386 OSWriteSwapInt32(base
, offset
, data
);
392 volatile void * base
,
397 OSWriteSwapInt64(base
, offset
, data
);
400 /* Functions for loading little endian to host endianess. */
405 const volatile void * base
,
409 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
415 const volatile void * base
,
419 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
425 const volatile void * base
,
429 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
432 /* Functions for storing host endianess to little endian. */
437 volatile void * base
,
442 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
448 volatile void * base
,
453 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
459 volatile void * base
,
464 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
467 /* Host endianess to big endian byte swapping macros for constants. */
469 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
470 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
471 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
473 /* Generic host endianess to big endian byte swapping functions. */
475 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
476 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
477 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
479 /* Host endianess to little endian byte swapping macros for constants. */
481 #define OSSwapHostToLittleConstInt16(x) (x)
482 #define OSSwapHostToLittleConstInt32(x) (x)
483 #define OSSwapHostToLittleConstInt64(x) (x)
485 /* Generic host endianess to little endian byte swapping functions. */
489 OSSwapHostToLittleInt16(
498 OSSwapHostToLittleInt32(
507 OSSwapHostToLittleInt64(
514 /* Big endian to host endianess byte swapping macros for constants. */
516 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
517 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
518 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
520 /* Generic big endian to host endianess byte swapping functions. */
522 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
523 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
524 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
526 /* Little endian to host endianess byte swapping macros for constants. */
528 #define OSSwapLittleToHostConstInt16(x) (x)
529 #define OSSwapLittleToHostConstInt32(x) (x)
530 #define OSSwapLittleToHostConstInt64(x) (x)
532 /* Generic little endian to host endianess byte swapping functions. */
536 OSSwapLittleToHostInt16(
545 OSSwapLittleToHostInt32(
554 OSSwapLittleToHostInt64(
562 #error Unknown endianess.
565 #endif /* ! _OS_OSBYTEORDER_H */