]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
85819b680007fe0939583baaebc8140daf7bbd12
[apple/xnu.git] / libkern / libkern / OSByteOrder.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _OS_OSBYTEORDER_H
30 #define _OS_OSBYTEORDER_H
31
32 #include <stdint.h>
33
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)))
38
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)))
44
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)))
54
55 #if defined(__GNUC__)
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>
61 #else
62 #include <libkern/machine/OSByteOrder.h>
63 #endif
64
65 #define OSSwapInt16(x) \
66 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
67
68 #define OSSwapInt32(x) \
69 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
70
71 #define OSSwapInt64(x) \
72 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
73
74 #else /* ! __GNUC__ */
75
76 #include <libkern/machine/OSByteOrder.h>
77
78 #define OSSwapInt16(x) OSSwapConstInt16(x)
79
80 #define OSSwapInt32(x) OSSwapConstInt32(x)
81
82 #define OSSwapInt64(x) OSSwapConstInt64(x)
83
84 #endif /* __GNUC__ */
85
86 enum {
87 OSUnknownByteOrder,
88 OSLittleEndian,
89 OSBigEndian
90 };
91
92 OS_INLINE
93 int32_t
94 OSHostByteOrder(void) {
95 #if defined(__LITTLE_ENDIAN__)
96 return OSLittleEndian;
97 #elif defined(__BIG_ENDIAN__)
98 return OSBigEndian;
99 #else
100 return OSUnknownByteOrder;
101 #endif
102 }
103
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)
112
113 /* Functions for loading native endian values. */
114
115 OS_INLINE
116 uint16_t
117 _OSReadInt16(
118 const volatile void * base,
119 uintptr_t byteOffset
120 )
121 {
122 return *(volatile uint16_t *)((uintptr_t)base + byteOffset);
123 }
124
125 OS_INLINE
126 uint32_t
127 _OSReadInt32(
128 const volatile void * base,
129 uintptr_t byteOffset
130 )
131 {
132 return *(volatile uint32_t *)((uintptr_t)base + byteOffset);
133 }
134
135 OS_INLINE
136 uint64_t
137 _OSReadInt64(
138 const volatile void * base,
139 uintptr_t byteOffset
140 )
141 {
142 return *(volatile uint64_t *)((uintptr_t)base + byteOffset);
143 }
144
145 /* Functions for storing native endian values. */
146
147 OS_INLINE
148 void
149 _OSWriteInt16(
150 volatile void * base,
151 uintptr_t byteOffset,
152 uint16_t data
153 )
154 {
155 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = data;
156 }
157
158 OS_INLINE
159 void
160 _OSWriteInt32(
161 volatile void * base,
162 uintptr_t byteOffset,
163 uint32_t data
164 )
165 {
166 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = data;
167 }
168
169 OS_INLINE
170 void
171 _OSWriteInt64(
172 volatile void * base,
173 uintptr_t byteOffset,
174 uint64_t data
175 )
176 {
177 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = data;
178 }
179
180 #if defined(__BIG_ENDIAN__)
181
182 /* Functions for loading big endian to host endianess. */
183
184 #define OSReadBigInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
185 #define OSReadBigInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
186 #define OSReadBigInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
187
188 /* Functions for storing host endianess to big endian. */
189
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)
193
194 /* Functions for loading little endian to host endianess. */
195
196 #define OSReadLittleInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
197 #define OSReadLittleInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
198 #define OSReadLittleInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
199
200 /* Functions for storing host endianess to little endian. */
201
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)
205
206 /* Host endianess to big endian byte swapping macros for constants. */
207
208 #define OSSwapHostToBigConstInt16(x) (x)
209 #define OSSwapHostToBigConstInt32(x) (x)
210 #define OSSwapHostToBigConstInt64(x) (x)
211
212 /* Generic host endianess to big endian byte swapping functions. */
213
214 #define OSSwapHostToBigInt16(x) ((uint16_t)(x))
215 #define OSSwapHostToBigInt32(x) ((uint32_t)(x))
216 #define OSSwapHostToBigInt64(x) ((uint64_t)(x))
217
218 /* Host endianess to little endian byte swapping macros for constants. */
219
220 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
221 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
222 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
223
224 /* Generic host endianess to little endian byte swapping functions. */
225
226 #define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
227 #define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
228 #define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
229
230 /* Big endian to host endianess byte swapping macros for constants. */
231
232 #define OSSwapBigToHostConstInt16(x) (x)
233 #define OSSwapBigToHostConstInt32(x) (x)
234 #define OSSwapBigToHostConstInt64(x) (x)
235
236 /* Generic big endian to host endianess byte swapping functions. */
237
238 #define OSSwapBigToHostInt16(x) ((uint16_t)(x))
239 #define OSSwapBigToHostInt32(x) ((uint32_t)(x))
240 #define OSSwapBigToHostInt64(x) ((uint64_t)(x))
241
242 /* Little endian to host endianess byte swapping macros for constants. */
243
244 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
245 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
246 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
247
248 /* Generic little endian to host endianess byte swapping functions. */
249
250 #define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
251 #define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
252 #define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
253
254 #elif defined(__LITTLE_ENDIAN__)
255
256 /* Functions for loading big endian to host endianess. */
257
258 #define OSReadBigInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset)
259 #define OSReadBigInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset)
260 #define OSReadBigInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset)
261
262 /* Functions for storing host endianess to big endian. */
263
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)
267
268 /* Functions for loading little endian to host endianess. */
269
270 #define OSReadLittleInt16(base, byteOffset) _OSReadInt16(base, byteOffset)
271 #define OSReadLittleInt32(base, byteOffset) _OSReadInt32(base, byteOffset)
272 #define OSReadLittleInt64(base, byteOffset) _OSReadInt64(base, byteOffset)
273
274 /* Functions for storing host endianess to little endian. */
275
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)
279
280 /* Host endianess to big endian byte swapping macros for constants. */
281
282 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
283 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
284 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
285
286 /* Generic host endianess to big endian byte swapping functions. */
287
288 #define OSSwapHostToBigInt16(x) OSSwapInt16(x)
289 #define OSSwapHostToBigInt32(x) OSSwapInt32(x)
290 #define OSSwapHostToBigInt64(x) OSSwapInt64(x)
291
292 /* Host endianess to little endian byte swapping macros for constants. */
293
294 #define OSSwapHostToLittleConstInt16(x) (x)
295 #define OSSwapHostToLittleConstInt32(x) (x)
296 #define OSSwapHostToLittleConstInt64(x) (x)
297
298 /* Generic host endianess to little endian byte swapping functions. */
299
300 #define OSSwapHostToLittleInt16(x) ((uint16_t)(x))
301 #define OSSwapHostToLittleInt32(x) ((uint32_t)(x))
302 #define OSSwapHostToLittleInt64(x) ((uint64_t)(x))
303
304 /* Big endian to host endianess byte swapping macros for constants. */
305
306 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
307 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
308 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
309
310 /* Generic big endian to host endianess byte swapping functions. */
311
312 #define OSSwapBigToHostInt16(x) OSSwapInt16(x)
313 #define OSSwapBigToHostInt32(x) OSSwapInt32(x)
314 #define OSSwapBigToHostInt64(x) OSSwapInt64(x)
315
316 /* Little endian to host endianess byte swapping macros for constants. */
317
318 #define OSSwapLittleToHostConstInt16(x) (x)
319 #define OSSwapLittleToHostConstInt32(x) (x)
320 #define OSSwapLittleToHostConstInt64(x) (x)
321
322 /* Generic little endian to host endianess byte swapping functions. */
323
324 #define OSSwapLittleToHostInt16(x) ((uint16_t)(x))
325 #define OSSwapLittleToHostInt32(x) ((uint32_t)(x))
326 #define OSSwapLittleToHostInt64(x) ((uint64_t)(x))
327
328 #else
329 #error Unknown endianess.
330 #endif
331
332 #endif /* ! _OS_OSBYTEORDER_H */
333
334