]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
1ca0e49f20d414805a3c120e4c2cf2344a8e66f3
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
37 #ifndef _OS_OSBYTEORDER_H
38 #define _OS_OSBYTEORDER_H
42 #if defined(__GNUC__) && defined(__ppc__)
43 #include <libkern/ppc/OSByteOrder.h>
44 #elif defined(__GNUC__) && defined(__i386__)
45 #include <libkern/i386/OSByteOrder.h>
47 #include <libkern/machine/OSByteOrder.h>
58 OSHostByteOrder(void) {
59 #if defined(__LITTLE_ENDIAN__)
60 return OSLittleEndian
;
61 #elif defined(__BIG_ENDIAN__)
64 return OSUnknownByteOrder
;
68 /* Macros for swapping constant values in the preprocessing stage. */
69 #define OSSwapConstInt16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
70 (((uint16_t)(x) & 0x00ff) << 8))
72 #define OSSwapConstInt32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
73 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
74 (((uint32_t)(x) & 0x0000ff00) << 8) | \
75 (((uint32_t)(x) & 0x000000ff) << 24))
77 #define OSSwapConstInt64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
78 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
79 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
80 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
81 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
82 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
83 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
84 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))
86 #if !defined(__GNUC__)
87 #define __builtin_constant_p(x) (0)
90 #define OSSwapInt16(x) \
91 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
93 #define OSSwapInt32(x) \
94 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
96 #define OSSwapInt64(x) \
97 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
99 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
100 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
101 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
102 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
103 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
104 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
105 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
106 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
108 #if defined(__BIG_ENDIAN__)
110 /* Functions for loading big endian to host endianess. */
115 const volatile void * base
,
119 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
125 const volatile void * base
,
129 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
135 const volatile void * base
,
139 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
142 /* Functions for storing host endianess to big endian. */
147 volatile void * base
,
152 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
158 volatile void * base
,
163 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
169 volatile void * base
,
174 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
177 /* Functions for loading little endian to host endianess. */
182 volatile void * base
,
186 return OSReadSwapInt16(base
, offset
);
192 volatile void * base
,
196 return OSReadSwapInt32(base
, offset
);
202 volatile void * base
,
206 return OSReadSwapInt64(base
, offset
);
209 /* Functions for storing host endianess to little endian. */
214 volatile void * base
,
219 OSWriteSwapInt16(base
, offset
, data
);
225 volatile void * base
,
230 OSWriteSwapInt32(base
, offset
, data
);
236 volatile void * base
,
241 OSWriteSwapInt64(base
, offset
, data
);
244 /* Host endianess to big endian byte swapping macros for constants. */
246 #define OSSwapHostToBigConstInt16(x) (x)
247 #define OSSwapHostToBigConstInt32(x) (x)
248 #define OSSwapHostToBigConstInt64(x) (x)
250 /* Generic host endianess to big endian byte swapping functions. */
254 OSSwapHostToBigInt16(
263 OSSwapHostToBigInt32(
272 OSSwapHostToBigInt64(
279 /* Host endianess to little endian byte swapping macros for constants. */
281 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
282 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
283 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
285 /* Generic host endianess to little endian byte swapping functions. */
287 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
288 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
289 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
291 /* Big endian to host endianess byte swapping macros for constants. */
293 #define OSSwapBigToHostConstInt16(x) (x)
294 #define OSSwapBigToHostConstInt32(x) (x)
295 #define OSSwapBigToHostConstInt64(x) (x)
297 /* Generic big endian to host endianess byte swapping functions. */
301 OSSwapBigToHostInt16(
310 OSSwapBigToHostInt32(
319 OSSwapBigToHostInt64(
326 /* Little endian to host endianess byte swapping macros for constants. */
328 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
329 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
330 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
332 /* Generic little endian to host endianess byte swapping functions. */
334 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
335 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
336 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
338 #elif defined(__LITTLE_ENDIAN__)
340 /* Functions for loading big endian to host endianess. */
345 const volatile void * base
,
349 return OSReadSwapInt16(base
, offset
);
355 const volatile void * base
,
359 return OSReadSwapInt32(base
, offset
);
365 const volatile void * base
,
369 return OSReadSwapInt64(base
, offset
);
372 /* Functions for storing host endianess to big endian. */
377 volatile void * base
,
382 OSWriteSwapInt16(base
, offset
, data
);
388 volatile void * base
,
393 OSWriteSwapInt32(base
, offset
, data
);
399 volatile void * base
,
404 OSWriteSwapInt64(base
, offset
, data
);
407 /* Functions for loading little endian to host endianess. */
412 const volatile void * base
,
416 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
422 const volatile void * base
,
426 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
432 const volatile void * base
,
436 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
439 /* Functions for storing host endianess to little endian. */
444 volatile void * base
,
449 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
455 volatile void * base
,
460 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
466 volatile void * base
,
471 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
474 /* Host endianess to big endian byte swapping macros for constants. */
476 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
477 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
478 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
480 /* Generic host endianess to big endian byte swapping functions. */
482 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
483 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
484 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
486 /* Host endianess to little endian byte swapping macros for constants. */
488 #define OSSwapHostToLittleConstInt16(x) (x)
489 #define OSSwapHostToLittleConstInt32(x) (x)
490 #define OSSwapHostToLittleConstInt64(x) (x)
492 /* Generic host endianess to little endian byte swapping functions. */
496 OSSwapHostToLittleInt16(
505 OSSwapHostToLittleInt32(
514 OSSwapHostToLittleInt64(
521 /* Big endian to host endianess byte swapping macros for constants. */
523 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
524 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
525 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
527 /* Generic big endian to host endianess byte swapping functions. */
529 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
530 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
531 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
533 /* Little endian to host endianess byte swapping macros for constants. */
535 #define OSSwapLittleToHostConstInt16(x) (x)
536 #define OSSwapLittleToHostConstInt32(x) (x)
537 #define OSSwapLittleToHostConstInt64(x) (x)
539 /* Generic little endian to host endianess byte swapping functions. */
543 OSSwapLittleToHostInt16(
552 OSSwapLittleToHostInt32(
561 OSSwapLittleToHostInt64(
569 #error Unknown endianess.
572 #endif /* ! _OS_OSBYTEORDER_H */