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