]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
efde3cba82a4073d747cf2d712f3828c3b5f0f4d
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
35 #ifndef _OS_OSBYTEORDER_H
36 #define _OS_OSBYTEORDER_H
40 #if defined(__GNUC__) && defined(__ppc__)
41 #include <libkern/ppc/OSByteOrder.h>
42 #elif defined(__GNUC__) && defined(__i386__)
43 #include <libkern/i386/OSByteOrder.h>
45 #include <libkern/machine/OSByteOrder.h>
56 OSHostByteOrder(void) {
57 #if defined(__LITTLE_ENDIAN__)
58 return OSLittleEndian
;
59 #elif defined(__BIG_ENDIAN__)
62 return OSUnknownByteOrder
;
66 /* Macros for swapping constant values in the preprocessing stage. */
67 #define OSSwapConstInt16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
68 (((uint16_t)(x) & 0x00ff) << 8))
70 #define OSSwapConstInt32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
71 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
72 (((uint32_t)(x) & 0x0000ff00) << 8) | \
73 (((uint32_t)(x) & 0x000000ff) << 24))
75 #define OSSwapConstInt64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
76 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
77 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
78 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
79 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
80 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
81 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
82 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))
84 #if !defined(__GNUC__)
85 #define __builtin_constant_p(x) (0)
88 #define OSSwapInt16(x) \
89 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
91 #define OSSwapInt32(x) \
92 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
94 #define OSSwapInt64(x) \
95 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
97 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
98 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
99 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
100 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
101 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
102 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
103 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
104 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
106 #if defined(__BIG_ENDIAN__)
108 /* Functions for loading big endian to host endianess. */
113 const volatile void * base
,
117 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
123 const volatile void * base
,
127 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
133 const volatile void * base
,
137 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
140 /* Functions for storing host endianess to big endian. */
145 volatile void * base
,
150 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
156 volatile void * base
,
161 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
167 volatile void * base
,
172 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
175 /* Functions for loading little endian to host endianess. */
180 volatile void * base
,
184 return OSReadSwapInt16(base
, offset
);
190 volatile void * base
,
194 return OSReadSwapInt32(base
, offset
);
200 volatile void * base
,
204 return OSReadSwapInt64(base
, offset
);
207 /* Functions for storing host endianess to little endian. */
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. */
252 OSSwapHostToBigInt16(
261 OSSwapHostToBigInt32(
270 OSSwapHostToBigInt64(
277 /* Host endianess to little endian byte swapping macros for constants. */
279 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
280 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
281 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
283 /* Generic host endianess to little endian byte swapping functions. */
285 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
286 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
287 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
289 /* Big endian to host endianess byte swapping macros for constants. */
291 #define OSSwapBigToHostConstInt16(x) (x)
292 #define OSSwapBigToHostConstInt32(x) (x)
293 #define OSSwapBigToHostConstInt64(x) (x)
295 /* Generic big endian to host endianess byte swapping functions. */
299 OSSwapBigToHostInt16(
308 OSSwapBigToHostInt32(
317 OSSwapBigToHostInt64(
324 /* Little endian to host endianess byte swapping macros for constants. */
326 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
327 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
328 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
330 /* Generic little endian to host endianess byte swapping functions. */
332 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
333 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
334 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
336 #elif defined(__LITTLE_ENDIAN__)
338 /* Functions for loading big endian to host endianess. */
343 const volatile void * base
,
347 return OSReadSwapInt16(base
, offset
);
353 const volatile void * base
,
357 return OSReadSwapInt32(base
, offset
);
363 const volatile void * base
,
367 return OSReadSwapInt64(base
, offset
);
370 /* Functions for storing host endianess to big endian. */
375 volatile void * base
,
380 OSWriteSwapInt16(base
, offset
, data
);
386 volatile void * base
,
391 OSWriteSwapInt32(base
, offset
, data
);
397 volatile void * base
,
402 OSWriteSwapInt64(base
, offset
, data
);
405 /* Functions for loading little endian to host endianess. */
410 const volatile void * base
,
414 return *(volatile uint16_t *)((uintptr_t)base
+ offset
);
420 const volatile void * base
,
424 return *(volatile uint32_t *)((uintptr_t)base
+ offset
);
430 const volatile void * base
,
434 return *(volatile uint64_t *)((uintptr_t)base
+ offset
);
437 /* Functions for storing host endianess to little endian. */
442 volatile void * base
,
447 *(volatile uint16_t *)((uintptr_t)base
+ offset
) = data
;
453 volatile void * base
,
458 *(volatile uint32_t *)((uintptr_t)base
+ offset
) = data
;
464 volatile void * base
,
469 *(volatile uint64_t *)((uintptr_t)base
+ offset
) = data
;
472 /* Host endianess to big endian byte swapping macros for constants. */
474 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
475 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
476 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
478 /* Generic host endianess to big endian byte swapping functions. */
480 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
481 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
482 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
484 /* Host endianess to little endian byte swapping macros for constants. */
486 #define OSSwapHostToLittleConstInt16(x) (x)
487 #define OSSwapHostToLittleConstInt32(x) (x)
488 #define OSSwapHostToLittleConstInt64(x) (x)
490 /* Generic host endianess to little endian byte swapping functions. */
494 OSSwapHostToLittleInt16(
503 OSSwapHostToLittleInt32(
512 OSSwapHostToLittleInt64(
519 /* Big endian to host endianess byte swapping macros for constants. */
521 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
522 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
523 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
525 /* Generic big endian to host endianess byte swapping functions. */
527 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
528 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
529 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
531 /* Little endian to host endianess byte swapping macros for constants. */
533 #define OSSwapLittleToHostConstInt16(x) (x)
534 #define OSSwapLittleToHostConstInt32(x) (x)
535 #define OSSwapLittleToHostConstInt64(x) (x)
537 /* Generic little endian to host endianess byte swapping functions. */
541 OSSwapLittleToHostInt16(
550 OSSwapLittleToHostInt32(
559 OSSwapLittleToHostInt64(
567 #error Unknown endianess.
570 #endif /* ! _OS_OSBYTEORDER_H */