]> git.saurik.com Git - apple/xnu.git/blame - libkern/libkern/OSByteOrder.h
xnu-1504.15.3.tar.gz
[apple/xnu.git] / libkern / libkern / OSByteOrder.h
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
6601e61a 27 */
1c79356b
A
28
29#ifndef _OS_OSBYTEORDER_H
30#define _OS_OSBYTEORDER_H
31
55e303ae 32#include <stdint.h>
2d21ac55 33#include <libkern/_OSByteOrder.h>
1c79356b 34
0c530ab8 35/* Macros for swapping constant values in the preprocessing stage. */
2d21ac55
A
36#define OSSwapConstInt16(x) __DARWIN_OSSwapConstInt16(x)
37#define OSSwapConstInt32(x) __DARWIN_OSSwapConstInt32(x)
38#define OSSwapConstInt64(x) __DARWIN_OSSwapConstInt64(x)
0c530ab8
A
39
40#if defined(__GNUC__)
41
42#if (defined(__ppc__) || defined(__ppc64__))
1c79356b 43#include <libkern/ppc/OSByteOrder.h>
0c530ab8 44#elif (defined(__i386__) || defined(__x86_64__))
1c79356b
A
45#include <libkern/i386/OSByteOrder.h>
46#else
47#include <libkern/machine/OSByteOrder.h>
48#endif
49
0c530ab8
A
50#else /* ! __GNUC__ */
51
52#include <libkern/machine/OSByteOrder.h>
53
0c530ab8
A
54#endif /* __GNUC__ */
55
2d21ac55
A
56#define OSSwapInt16(x) __DARWIN_OSSwapInt16(x)
57#define OSSwapInt32(x) __DARWIN_OSSwapInt32(x)
58#define OSSwapInt64(x) __DARWIN_OSSwapInt64(x)
59
1c79356b
A
60enum {
61 OSUnknownByteOrder,
62 OSLittleEndian,
63 OSBigEndian
64};
65
66OS_INLINE
55e303ae 67int32_t
1c79356b 68OSHostByteOrder(void) {
55e303ae
A
69#if defined(__LITTLE_ENDIAN__)
70 return OSLittleEndian;
71#elif defined(__BIG_ENDIAN__)
72 return OSBigEndian;
73#else
74 return OSUnknownByteOrder;
75#endif
1c79356b
A
76}
77
55e303ae
A
78#define OSReadBigInt(x, y) OSReadBigInt32(x, y)
79#define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
80#define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
81#define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
82#define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
83#define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
84#define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
85#define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
1c79356b 86
0c530ab8 87/* Functions for loading native endian values. */
1c79356b
A
88
89OS_INLINE
55e303ae 90uint16_t
0c530ab8 91_OSReadInt16(
91447636 92 const volatile void * base,
0c530ab8 93 uintptr_t byteOffset
1c79356b
A
94)
95{
0c530ab8 96 return *(volatile uint16_t *)((uintptr_t)base + byteOffset);
1c79356b
A
97}
98
99OS_INLINE
55e303ae 100uint32_t
0c530ab8 101_OSReadInt32(
91447636 102 const volatile void * base,
0c530ab8 103 uintptr_t byteOffset
1c79356b
A
104)
105{
0c530ab8 106 return *(volatile uint32_t *)((uintptr_t)base + byteOffset);
1c79356b
A
107}
108
109OS_INLINE
55e303ae 110uint64_t
0c530ab8 111_OSReadInt64(
91447636 112 const volatile void * base,
0c530ab8 113 uintptr_t byteOffset
1c79356b
A
114)
115{
0c530ab8 116 return *(volatile uint64_t *)((uintptr_t)base + byteOffset);
1c79356b
A
117}
118
0c530ab8 119/* Functions for storing native endian values. */
1c79356b 120
1c79356b
A
121OS_INLINE
122void
0c530ab8 123_OSWriteInt16(
1c79356b 124 volatile void * base,
0c530ab8 125 uintptr_t byteOffset,
55e303ae 126 uint16_t data
1c79356b
A
127)
128{
0c530ab8 129 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = data;
1c79356b
A
130}
131
132OS_INLINE
133void
0c530ab8 134_OSWriteInt32(
1c79356b 135 volatile void * base,
0c530ab8 136 uintptr_t byteOffset,
55e303ae 137 uint32_t data
1c79356b
A
138)
139{
0c530ab8 140 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = data;
1c79356b
A
141}
142
143OS_INLINE
144void
0c530ab8 145_OSWriteInt64(
1c79356b 146 volatile void * base,
0c530ab8 147 uintptr_t byteOffset,
55e303ae 148 uint64_t data
1c79356b
A
149)
150{
0c530ab8 151 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = data;
1c79356b
A
152}
153
0c530ab8 154#if defined(__BIG_ENDIAN__)
1c79356b 155
0c530ab8 156/* Functions for loading big endian to host endianess. */
1c79356b 157
0c530ab8
A
158#define OSReadBigInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
159#define OSReadBigInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
160#define OSReadBigInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
1c79356b 161
0c530ab8 162/* Functions for storing host endianess to big endian. */
c0fea474 163
0c530ab8
A
164#define OSWriteBigInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
165#define OSWriteBigInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
166#define OSWriteBigInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
5d5c5d0d 167
0c530ab8 168/* Functions for loading little endian to host endianess. */
89b3af67 169
0c530ab8
A
170#define OSReadLittleInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
171#define OSReadLittleInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
172#define OSReadLittleInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
4452a7af 173
0c530ab8
A
174/* Functions for storing host endianess to little endian. */
175
176#define OSWriteLittleInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
177#define OSWriteLittleInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
178#define OSWriteLittleInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
1c79356b
A
179
180/* Host endianess to big endian byte swapping macros for constants. */
181
b0d623f7
A
182#define OSSwapHostToBigConstInt16(x) ((uint16_t)(x))
183#define OSSwapHostToBigConstInt32(x) ((uint32_t)(x))
184#define OSSwapHostToBigConstInt64(x) ((uint64_t)(x))
1c79356b
A
185
186/* Generic host endianess to big endian byte swapping functions. */
187
0c530ab8
A
188#define OSSwapHostToBigInt16(x) ((uint16_t)(x))
189#define OSSwapHostToBigInt32(x) ((uint32_t)(x))
190#define OSSwapHostToBigInt64(x) ((uint64_t)(x))
1c79356b
A
191
192/* Host endianess to little endian byte swapping macros for constants. */
193
194#define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
195#define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
196#define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
197
198/* Generic host endianess to little endian byte swapping functions. */
199
55e303ae
A
200#define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
201#define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
202#define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
1c79356b
A
203
204/* Big endian to host endianess byte swapping macros for constants. */
205
b0d623f7
A
206#define OSSwapBigToHostConstInt16(x) ((uint16_t)(x))
207#define OSSwapBigToHostConstInt32(x) ((uint32_t)(x))
208#define OSSwapBigToHostConstInt64(x) ((uint64_t)(x))
1c79356b
A
209
210/* Generic big endian to host endianess byte swapping functions. */
211
0c530ab8
A
212#define OSSwapBigToHostInt16(x) ((uint16_t)(x))
213#define OSSwapBigToHostInt32(x) ((uint32_t)(x))
214#define OSSwapBigToHostInt64(x) ((uint64_t)(x))
1c79356b
A
215
216/* Little endian to host endianess byte swapping macros for constants. */
217
218#define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
219#define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
220#define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
221
222/* Generic little endian to host endianess byte swapping functions. */
223
55e303ae
A
224#define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
225#define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
226#define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
1c79356b
A
227
228#elif defined(__LITTLE_ENDIAN__)
229
230/* Functions for loading big endian to host endianess. */
231
0c530ab8
A
232#define OSReadBigInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
233#define OSReadBigInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
234#define OSReadBigInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
1c79356b
A
235
236/* Functions for storing host endianess to big endian. */
237
0c530ab8
A
238#define OSWriteBigInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data)
239#define OSWriteBigInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data)
240#define OSWriteBigInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data)
1c79356b
A
241
242/* Functions for loading little endian to host endianess. */
243
0c530ab8
A
244#define OSReadLittleInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
245#define OSReadLittleInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
246#define OSReadLittleInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
1c79356b
A
247
248/* Functions for storing host endianess to little endian. */
249
0c530ab8
A
250#define OSWriteLittleInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data)
251#define OSWriteLittleInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data)
252#define OSWriteLittleInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data)
1c79356b
A
253
254/* Host endianess to big endian byte swapping macros for constants. */
255
256#define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
257#define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
258#define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
259
260/* Generic host endianess to big endian byte swapping functions. */
261
55e303ae
A
262#define OSSwapHostToBigInt16(x) OSSwapInt16(x)
263#define OSSwapHostToBigInt32(x) OSSwapInt32(x)
264#define OSSwapHostToBigInt64(x) OSSwapInt64(x)
1c79356b
A
265
266/* Host endianess to little endian byte swapping macros for constants. */
267
b0d623f7
A
268#define OSSwapHostToLittleConstInt16(x) ((uint16_t)(x))
269#define OSSwapHostToLittleConstInt32(x) ((uint32_t)(x))
270#define OSSwapHostToLittleConstInt64(x) ((uint64_t)(x))
1c79356b
A
271
272/* Generic host endianess to little endian byte swapping functions. */
273
0c530ab8
A
274#define OSSwapHostToLittleInt16(x) ((uint16_t)(x))
275#define OSSwapHostToLittleInt32(x) ((uint32_t)(x))
276#define OSSwapHostToLittleInt64(x) ((uint64_t)(x))
1c79356b
A
277
278/* Big endian to host endianess byte swapping macros for constants. */
279
280#define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
281#define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
282#define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
283
284/* Generic big endian to host endianess byte swapping functions. */
285
55e303ae
A
286#define OSSwapBigToHostInt16(x) OSSwapInt16(x)
287#define OSSwapBigToHostInt32(x) OSSwapInt32(x)
288#define OSSwapBigToHostInt64(x) OSSwapInt64(x)
1c79356b
A
289
290/* Little endian to host endianess byte swapping macros for constants. */
291
b0d623f7
A
292#define OSSwapLittleToHostConstInt16(x) ((uint16_t)(x))
293#define OSSwapLittleToHostConstInt32(x) ((uint32_t)(x))
294#define OSSwapLittleToHostConstInt64(x) ((uint64_t)(x))
1c79356b
A
295
296/* Generic little endian to host endianess byte swapping functions. */
297
0c530ab8
A
298#define OSSwapLittleToHostInt16(x) ((uint16_t)(x))
299#define OSSwapLittleToHostInt32(x) ((uint32_t)(x))
300#define OSSwapLittleToHostInt64(x) ((uint64_t)(x))
1c79356b
A
301
302#else
303#error Unknown endianess.
304#endif
305
306#endif /* ! _OS_OSBYTEORDER_H */
307
308