]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
85819b680007fe0939583baaebc8140daf7bbd12
2 * Copyright (c) 2000-2005 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 #ifndef _OS_OSBYTEORDER_H
30 #define _OS_OSBYTEORDER_H
34 /* Macros for swapping constant values in the preprocessing stage. */
35 #define OSSwapConstInt16(x) \
36 ((uint16_t)((((uint16_t)(x) & 0xff00) >> 8) | \
37 (((uint16_t)(x) & 0x00ff) << 8)))
39 #define OSSwapConstInt32(x) \
40 ((uint32_t)((((uint32_t)(x) & 0xff000000) >> 24) | \
41 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
42 (((uint32_t)(x) & 0x0000ff00) << 8) | \
43 (((uint32_t)(x) & 0x000000ff) << 24)))
45 #define OSSwapConstInt64(x) \
46 ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
47 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
48 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
49 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
50 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
51 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
52 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
53 (((uint64_t)(x) & 0x00000000000000ffULL) << 56)))
57 #if (defined(__ppc__) || defined(__ppc64__))
58 #include <libkern/ppc/OSByteOrder.h>
59 #elif (defined(__i386__) || defined(__x86_64__))
60 #include <libkern/i386/OSByteOrder.h>
62 #include <libkern/machine/OSByteOrder.h>
65 #define OSSwapInt16(x) \
66 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
68 #define OSSwapInt32(x) \
69 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
71 #define OSSwapInt64(x) \
72 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
74 #else /* ! __GNUC__ */
76 #include <libkern/machine/OSByteOrder.h>
78 #define OSSwapInt16(x) OSSwapConstInt16(x)
80 #define OSSwapInt32(x) OSSwapConstInt32(x)
82 #define OSSwapInt64(x) OSSwapConstInt64(x)
94 OSHostByteOrder(void) {
95 #if defined(__LITTLE_ENDIAN__)
96 return OSLittleEndian
;
97 #elif defined(__BIG_ENDIAN__)
100 return OSUnknownByteOrder
;
104 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
105 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
106 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
107 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
108 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
109 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
110 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
111 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
113 /* Functions for loading native endian values. */
118 const volatile void * base
,
122 return *(volatile uint16_t *)((uintptr_t)base
+ byteOffset
);
128 const volatile void * base
,
132 return *(volatile uint32_t *)((uintptr_t)base
+ byteOffset
);
138 const volatile void * base
,
142 return *(volatile uint64_t *)((uintptr_t)base
+ byteOffset
);
145 /* Functions for storing native endian values. */
150 volatile void * base
,
151 uintptr_t byteOffset
,
155 *(volatile uint16_t *)((uintptr_t)base
+ byteOffset
) = data
;
161 volatile void * base
,
162 uintptr_t byteOffset
,
166 *(volatile uint32_t *)((uintptr_t)base
+ byteOffset
) = data
;
172 volatile void * base
,
173 uintptr_t byteOffset
,
177 *(volatile uint64_t *)((uintptr_t)base
+ byteOffset
) = data
;
180 #if defined(__BIG_ENDIAN__)
182 /* Functions for loading big endian to host endianess. */
184 #define OSReadBigInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
185 #define OSReadBigInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
186 #define OSReadBigInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
188 /* Functions for storing host endianess to big endian. */
190 #define OSWriteBigInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
191 #define OSWriteBigInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
192 #define OSWriteBigInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
194 /* Functions for loading little endian to host endianess. */
196 #define OSReadLittleInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
197 #define OSReadLittleInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
198 #define OSReadLittleInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
200 /* Functions for storing host endianess to little endian. */
202 #define OSWriteLittleInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
203 #define OSWriteLittleInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
204 #define OSWriteLittleInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
206 /* Host endianess to big endian byte swapping macros for constants. */
208 #define OSSwapHostToBigConstInt16(x) (x)
209 #define OSSwapHostToBigConstInt32(x) (x)
210 #define OSSwapHostToBigConstInt64(x) (x)
212 /* Generic host endianess to big endian byte swapping functions. */
214 #define OSSwapHostToBigInt16(x) ((uint16_t)(x))
215 #define OSSwapHostToBigInt32(x) ((uint32_t)(x))
216 #define OSSwapHostToBigInt64(x) ((uint64_t)(x))
218 /* Host endianess to little endian byte swapping macros for constants. */
220 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
221 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
222 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
224 /* Generic host endianess to little endian byte swapping functions. */
226 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
227 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
228 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
230 /* Big endian to host endianess byte swapping macros for constants. */
232 #define OSSwapBigToHostConstInt16(x) (x)
233 #define OSSwapBigToHostConstInt32(x) (x)
234 #define OSSwapBigToHostConstInt64(x) (x)
236 /* Generic big endian to host endianess byte swapping functions. */
238 #define OSSwapBigToHostInt16(x) ((uint16_t)(x))
239 #define OSSwapBigToHostInt32(x) ((uint32_t)(x))
240 #define OSSwapBigToHostInt64(x) ((uint64_t)(x))
242 /* Little endian to host endianess byte swapping macros for constants. */
244 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
245 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
246 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
248 /* Generic little endian to host endianess byte swapping functions. */
250 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
251 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
252 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
254 #elif defined(__LITTLE_ENDIAN__)
256 /* Functions for loading big endian to host endianess. */
258 #define OSReadBigInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
259 #define OSReadBigInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
260 #define OSReadBigInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
262 /* Functions for storing host endianess to big endian. */
264 #define OSWriteBigInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
265 #define OSWriteBigInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
266 #define OSWriteBigInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
268 /* Functions for loading little endian to host endianess. */
270 #define OSReadLittleInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
271 #define OSReadLittleInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
272 #define OSReadLittleInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
274 /* Functions for storing host endianess to little endian. */
276 #define OSWriteLittleInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
277 #define OSWriteLittleInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
278 #define OSWriteLittleInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
280 /* Host endianess to big endian byte swapping macros for constants. */
282 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
283 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
284 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
286 /* Generic host endianess to big endian byte swapping functions. */
288 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
289 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
290 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
292 /* Host endianess to little endian byte swapping macros for constants. */
294 #define OSSwapHostToLittleConstInt16(x) (x)
295 #define OSSwapHostToLittleConstInt32(x) (x)
296 #define OSSwapHostToLittleConstInt64(x) (x)
298 /* Generic host endianess to little endian byte swapping functions. */
300 #define OSSwapHostToLittleInt16(x) ((uint16_t)(x))
301 #define OSSwapHostToLittleInt32(x) ((uint32_t)(x))
302 #define OSSwapHostToLittleInt64(x) ((uint64_t)(x))
304 /* Big endian to host endianess byte swapping macros for constants. */
306 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
307 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
308 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
310 /* Generic big endian to host endianess byte swapping functions. */
312 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
313 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
314 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
316 /* Little endian to host endianess byte swapping macros for constants. */
318 #define OSSwapLittleToHostConstInt16(x) (x)
319 #define OSSwapLittleToHostConstInt32(x) (x)
320 #define OSSwapLittleToHostConstInt64(x) (x)
322 /* Generic little endian to host endianess byte swapping functions. */
324 #define OSSwapLittleToHostInt16(x) ((uint16_t)(x))
325 #define OSSwapLittleToHostInt32(x) ((uint32_t)(x))
326 #define OSSwapLittleToHostInt64(x) ((uint64_t)(x))
329 #error Unknown endianess.
332 #endif /* ! _OS_OSBYTEORDER_H */