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