]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
c64a3aa05c668bb532264631ea129488b62aa516
2 * Copyright (c) 2000-2003 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
34 #if defined(__GNUC__) && defined(__ppc__)
35 #include <libkern/ppc/OSByteOrder.h>
36 #elif defined(__GNUC__) && defined(__i386__)
37 #include <libkern/i386/OSByteOrder.h>
39 #include <libkern/machine/OSByteOrder.h>
50 OSHostByteOrder(void) {
51 #if defined(__LITTLE_ENDIAN__)
52 return OSLittleEndian
;
53 #elif defined(__BIG_ENDIAN__)
56 return OSUnknownByteOrder
;
60 /* Macros for swapping constant values in the preprocessing stage. */
61 #define OSSwapConstInt16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
62 (((uint16_t)(x) & 0x00ff) << 8))
64 #define OSSwapConstInt32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
65 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
66 (((uint32_t)(x) & 0x0000ff00) << 8) | \
67 (((uint32_t)(x) & 0x000000ff) << 24))
69 #define OSSwapConstInt64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
70 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
71 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
72 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
73 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
74 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
75 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
76 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))
78 #if !defined(__GNUC__)
79 #define __builtin_constant_p(x) (0)
82 #define OSSwapInt16(x) \
83 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
85 #define OSSwapInt32(x) \
86 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
88 #define OSSwapInt64(x) \
89 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
91 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
92 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
93 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
94 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
95 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
96 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
97 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
98 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
100 #if defined(__BIG_ENDIAN__)
102 /* Functions for loading big endian to host endianess. */
107 const volatile void * base
,
111 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
117 const volatile void * base
,
121 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
127 const volatile void * base
,
131 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
134 /* Functions for storing host endianess to big endian. */
139 volatile void * base
,
144 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
150 volatile void * base
,
155 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
161 volatile void * base
,
166 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
169 /* Functions for loading little endian to host endianess. */
174 volatile void * base
,
178 return OSReadSwapInt16(base
, offset
);
184 volatile void * base
,
188 return OSReadSwapInt32(base
, offset
);
194 volatile void * base
,
198 return OSReadSwapInt64(base
, offset
);
201 /* Functions for storing host endianess to little endian. */
206 volatile void * base
,
211 OSWriteSwapInt16(base
, offset
, data
);
217 volatile void * base
,
222 OSWriteSwapInt32(base
, offset
, data
);
228 volatile void * base
,
233 OSWriteSwapInt64(base
, offset
, data
);
236 /* Host endianess to big endian byte swapping macros for constants. */
238 #define OSSwapHostToBigConstInt16(x) (x)
239 #define OSSwapHostToBigConstInt32(x) (x)
240 #define OSSwapHostToBigConstInt64(x) (x)
242 /* Generic host endianess to big endian byte swapping functions. */
246 OSSwapHostToBigInt16(
255 OSSwapHostToBigInt32(
264 OSSwapHostToBigInt64(
271 /* Host endianess to little endian byte swapping macros for constants. */
273 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
274 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
275 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
277 /* Generic host endianess to little endian byte swapping functions. */
279 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
280 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
281 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
283 /* Big endian to host endianess byte swapping macros for constants. */
285 #define OSSwapBigToHostConstInt16(x) (x)
286 #define OSSwapBigToHostConstInt32(x) (x)
287 #define OSSwapBigToHostConstInt64(x) (x)
289 /* Generic big endian to host endianess byte swapping functions. */
293 OSSwapBigToHostInt16(
302 OSSwapBigToHostInt32(
311 OSSwapBigToHostInt64(
318 /* Little endian to host endianess byte swapping macros for constants. */
320 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
321 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
322 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
324 /* Generic little endian to host endianess byte swapping functions. */
326 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
327 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
328 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
330 #elif defined(__LITTLE_ENDIAN__)
332 /* Functions for loading big endian to host endianess. */
337 const volatile void * base
,
341 return OSReadSwapInt16(base
, offset
);
347 const volatile void * base
,
351 return OSReadSwapInt32(base
, offset
);
357 const volatile void * base
,
361 return OSReadSwapInt64(base
, offset
);
364 /* Functions for storing host endianess to big endian. */
369 volatile void * base
,
374 OSWriteSwapInt16(base
, offset
, data
);
380 volatile void * base
,
385 OSWriteSwapInt32(base
, offset
, data
);
391 volatile void * base
,
396 OSWriteSwapInt64(base
, offset
, data
);
399 /* Functions for loading little endian to host endianess. */
404 const volatile void * base
,
408 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
414 const volatile void * base
,
418 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
424 const volatile void * base
,
428 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
431 /* Functions for storing host endianess to little endian. */
436 volatile void * base
,
441 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
447 volatile void * base
,
452 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
458 volatile void * base
,
463 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
466 /* Host endianess to big endian byte swapping macros for constants. */
468 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
469 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
470 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
472 /* Generic host endianess to big endian byte swapping functions. */
474 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
475 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
476 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
478 /* Host endianess to little endian byte swapping macros for constants. */
480 #define OSSwapHostToLittleConstInt16(x) (x)
481 #define OSSwapHostToLittleConstInt32(x) (x)
482 #define OSSwapHostToLittleConstInt64(x) (x)
484 /* Generic host endianess to little endian byte swapping functions. */
488 OSSwapHostToLittleInt16(
497 OSSwapHostToLittleInt32(
506 OSSwapHostToLittleInt64(
513 /* Big endian to host endianess byte swapping macros for constants. */
515 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
516 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
517 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
519 /* Generic big endian to host endianess byte swapping functions. */
521 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
522 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
523 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
525 /* Little endian to host endianess byte swapping macros for constants. */
527 #define OSSwapLittleToHostConstInt16(x) (x)
528 #define OSSwapLittleToHostConstInt32(x) (x)
529 #define OSSwapLittleToHostConstInt64(x) (x)
531 /* Generic little endian to host endianess byte swapping functions. */
535 OSSwapLittleToHostInt16(
544 OSSwapLittleToHostInt32(
553 OSSwapLittleToHostInt64(
561 #error Unknown endianess.
564 #endif /* ! _OS_OSBYTEORDER_H */