]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
09c85a1228d3b0336ccfec8c1b31b7c3b9707cb6
2 * Copyright (c) 2000-2005 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 #ifndef _OS_OSBYTEORDER_H
32 #define _OS_OSBYTEORDER_H
36 /* Macros for swapping constant values in the preprocessing stage. */
37 #define OSSwapConstInt16(x) \
38 ((uint16_t)((((uint16_t)(x) & 0xff00) >> 8) | \
39 (((uint16_t)(x) & 0x00ff) << 8)))
41 #define OSSwapConstInt32(x) \
42 ((uint32_t)((((uint32_t)(x) & 0xff000000) >> 24) | \
43 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
44 (((uint32_t)(x) & 0x0000ff00) << 8) | \
45 (((uint32_t)(x) & 0x000000ff) << 24)))
47 #define OSSwapConstInt64(x) \
48 ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
49 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
50 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
51 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
52 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
53 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
54 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
55 (((uint64_t)(x) & 0x00000000000000ffULL) << 56)))
59 #if (defined(__ppc__) || defined(__ppc64__))
60 #include <libkern/ppc/OSByteOrder.h>
61 #elif (defined(__i386__) || defined(__x86_64__))
62 #include <libkern/i386/OSByteOrder.h>
64 #include <libkern/machine/OSByteOrder.h>
67 #define OSSwapInt16(x) \
68 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
70 #define OSSwapInt32(x) \
71 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
73 #define OSSwapInt64(x) \
74 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
76 #else /* ! __GNUC__ */
78 #include <libkern/machine/OSByteOrder.h>
80 #define OSSwapInt16(x) OSSwapConstInt16(x)
82 #define OSSwapInt32(x) OSSwapConstInt32(x)
84 #define OSSwapInt64(x) OSSwapConstInt64(x)
96 OSHostByteOrder(void) {
97 #if defined(__LITTLE_ENDIAN__)
98 return OSLittleEndian
;
99 #elif defined(__BIG_ENDIAN__)
102 return OSUnknownByteOrder
;
106 #define OSReadBigInt(x, y) OSReadBigInt32(x, y)
107 #define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
108 #define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
109 #define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
110 #define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
111 #define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
112 #define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
113 #define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
115 /* Functions for loading native endian values. */
120 const volatile void * base
,
124 return *(volatile uint16_t *)((uintptr_t)base
+ byteOffset
);
130 const volatile void * base
,
134 return *(volatile uint32_t *)((uintptr_t)base
+ byteOffset
);
140 const volatile void * base
,
144 return *(volatile uint64_t *)((uintptr_t)base
+ byteOffset
);
147 /* Functions for storing native endian values. */
152 volatile void * base
,
153 uintptr_t byteOffset
,
157 *(volatile uint16_t *)((uintptr_t)base
+ byteOffset
) = data
;
163 volatile void * base
,
164 uintptr_t byteOffset
,
168 *(volatile uint32_t *)((uintptr_t)base
+ byteOffset
) = data
;
174 volatile void * base
,
175 uintptr_t byteOffset
,
179 *(volatile uint64_t *)((uintptr_t)base
+ byteOffset
) = data
;
182 #if defined(__BIG_ENDIAN__)
184 /* Functions for loading big endian to host endianess. */
186 #define OSReadBigInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
187 #define OSReadBigInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
188 #define OSReadBigInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
190 /* Functions for storing host endianess to big endian. */
192 #define OSWriteBigInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
193 #define OSWriteBigInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
194 #define OSWriteBigInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
196 /* Functions for loading little endian to host endianess. */
198 #define OSReadLittleInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
199 #define OSReadLittleInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
200 #define OSReadLittleInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
202 /* Functions for storing host endianess to little endian. */
204 #define OSWriteLittleInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
205 #define OSWriteLittleInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
206 #define OSWriteLittleInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
208 /* Host endianess to big endian byte swapping macros for constants. */
210 #define OSSwapHostToBigConstInt16(x) (x)
211 #define OSSwapHostToBigConstInt32(x) (x)
212 #define OSSwapHostToBigConstInt64(x) (x)
214 /* Generic host endianess to big endian byte swapping functions. */
216 #define OSSwapHostToBigInt16(x) ((uint16_t)(x))
217 #define OSSwapHostToBigInt32(x) ((uint32_t)(x))
218 #define OSSwapHostToBigInt64(x) ((uint64_t)(x))
220 /* Host endianess to little endian byte swapping macros for constants. */
222 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
223 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
224 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
226 /* Generic host endianess to little endian byte swapping functions. */
228 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
229 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
230 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
232 /* Big endian to host endianess byte swapping macros for constants. */
234 #define OSSwapBigToHostConstInt16(x) (x)
235 #define OSSwapBigToHostConstInt32(x) (x)
236 #define OSSwapBigToHostConstInt64(x) (x)
238 /* Generic big endian to host endianess byte swapping functions. */
240 #define OSSwapBigToHostInt16(x) ((uint16_t)(x))
241 #define OSSwapBigToHostInt32(x) ((uint32_t)(x))
242 #define OSSwapBigToHostInt64(x) ((uint64_t)(x))
244 /* Little endian to host endianess byte swapping macros for constants. */
246 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
247 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
248 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
250 /* Generic little endian to host endianess byte swapping functions. */
252 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
253 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
254 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
256 #elif defined(__LITTLE_ENDIAN__)
258 /* Functions for loading big endian to host endianess. */
260 #define OSReadBigInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
261 #define OSReadBigInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
262 #define OSReadBigInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
264 /* Functions for storing host endianess to big endian. */
266 #define OSWriteBigInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
267 #define OSWriteBigInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
268 #define OSWriteBigInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
270 /* Functions for loading little endian to host endianess. */
272 #define OSReadLittleInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
273 #define OSReadLittleInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
274 #define OSReadLittleInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
276 /* Functions for storing host endianess to little endian. */
278 #define OSWriteLittleInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
279 #define OSWriteLittleInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
280 #define OSWriteLittleInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
282 /* Host endianess to big endian byte swapping macros for constants. */
284 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
285 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
286 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
288 /* Generic host endianess to big endian byte swapping functions. */
290 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
291 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
292 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
294 /* Host endianess to little endian byte swapping macros for constants. */
296 #define OSSwapHostToLittleConstInt16(x) (x)
297 #define OSSwapHostToLittleConstInt32(x) (x)
298 #define OSSwapHostToLittleConstInt64(x) (x)
300 /* Generic host endianess to little endian byte swapping functions. */
302 #define OSSwapHostToLittleInt16(x) ((uint16_t)(x))
303 #define OSSwapHostToLittleInt32(x) ((uint32_t)(x))
304 #define OSSwapHostToLittleInt64(x) ((uint64_t)(x))
306 /* Big endian to host endianess byte swapping macros for constants. */
308 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
309 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
310 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
312 /* Generic big endian to host endianess byte swapping functions. */
314 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
315 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
316 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
318 /* Little endian to host endianess byte swapping macros for constants. */
320 #define OSSwapLittleToHostConstInt16(x) (x)
321 #define OSSwapLittleToHostConstInt32(x) (x)
322 #define OSSwapLittleToHostConstInt64(x) (x)
324 /* Generic little endian to host endianess byte swapping functions. */
326 #define OSSwapLittleToHostInt16(x) ((uint16_t)(x))
327 #define OSSwapLittleToHostInt32(x) ((uint32_t)(x))
328 #define OSSwapLittleToHostInt64(x) ((uint64_t)(x))
331 #error Unknown endianess.
334 #endif /* ! _OS_OSBYTEORDER_H */