]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Copyright (c) 1992 NeXT Computer, Inc. | |
26 | * | |
27 | * Byte ordering conversion. | |
28 | * | |
29 | */ | |
30 | ||
31 | #ifndef _ARCHITECTURE_BYTE_ORDER_H_ | |
32 | #define _ARCHITECTURE_BYTE_ORDER_H_ | |
33 | ||
34 | #include <libkern/OSByteOrder.h> | |
35 | ||
36 | typedef unsigned long NXSwappedFloat; | |
37 | typedef unsigned long long NXSwappedDouble; | |
38 | ||
39 | static __inline__ | |
40 | unsigned short | |
41 | NXSwapShort( | |
42 | unsigned short inv | |
43 | ) | |
44 | { | |
45 | return (unsigned short)OSSwapInt16((uint16_t)inv); | |
46 | } | |
47 | ||
48 | static __inline__ | |
49 | unsigned int | |
50 | NXSwapInt( | |
51 | unsigned int inv | |
52 | ) | |
53 | { | |
54 | return (unsigned int)OSSwapInt32((uint32_t)inv); | |
55 | } | |
56 | ||
57 | static __inline__ | |
58 | unsigned long | |
59 | NXSwapLong( | |
60 | unsigned long inv | |
61 | ) | |
62 | { | |
63 | return (unsigned long)OSSwapInt32((uint32_t)inv); | |
64 | } | |
65 | ||
66 | static __inline__ | |
67 | unsigned long long | |
68 | NXSwapLongLong( | |
69 | unsigned long long inv | |
70 | ) | |
71 | { | |
72 | return (unsigned long long)OSSwapInt64((uint64_t)inv); | |
73 | } | |
74 | ||
75 | static __inline__ NXSwappedFloat | |
76 | NXConvertHostFloatToSwapped(float x) | |
77 | { | |
78 | union fconv { | |
79 | float number; | |
80 | NXSwappedFloat sf; | |
81 | } u; | |
82 | u.number = x; | |
83 | return u.sf; | |
84 | } | |
85 | ||
86 | static __inline__ float | |
87 | NXConvertSwappedFloatToHost(NXSwappedFloat x) | |
88 | { | |
89 | union fconv { | |
90 | float number; | |
91 | NXSwappedFloat sf; | |
92 | } u; | |
93 | u.sf = x; | |
94 | return u.number; | |
95 | } | |
96 | ||
97 | static __inline__ NXSwappedDouble | |
98 | NXConvertHostDoubleToSwapped(double x) | |
99 | { | |
100 | union dconv { | |
101 | double number; | |
102 | NXSwappedDouble sd; | |
103 | } u; | |
104 | u.number = x; | |
105 | return u.sd; | |
106 | } | |
107 | ||
108 | static __inline__ double | |
109 | NXConvertSwappedDoubleToHost(NXSwappedDouble x) | |
110 | { | |
111 | union dconv { | |
112 | double number; | |
113 | NXSwappedDouble sd; | |
114 | } u; | |
115 | u.sd = x; | |
116 | return u.number; | |
117 | } | |
118 | ||
119 | static __inline__ NXSwappedFloat | |
120 | NXSwapFloat(NXSwappedFloat x) | |
121 | { | |
122 | return (NXSwappedFloat)OSSwapInt32((uint32_t)x); | |
123 | } | |
124 | ||
125 | static __inline__ NXSwappedDouble | |
126 | NXSwapDouble(NXSwappedDouble x) | |
127 | { | |
128 | return (NXSwappedDouble)OSSwapInt64((uint64_t)x); | |
129 | } | |
130 | ||
131 | /* | |
132 | * Identify the byte order | |
133 | * of the current host. | |
134 | */ | |
135 | ||
136 | enum NXByteOrder { | |
137 | NX_UnknownByteOrder, | |
138 | NX_LittleEndian, | |
139 | NX_BigEndian | |
140 | }; | |
141 | ||
142 | static __inline__ | |
143 | enum NXByteOrder | |
144 | NXHostByteOrder(void) | |
145 | { | |
146 | #if defined(__LITTLE_ENDIAN__) | |
147 | return NX_LittleEndian; | |
148 | #elif defined(__BIG_ENDIAN__) | |
149 | return NX_BigEndian; | |
150 | #else | |
151 | return NX_UnknownByteOrder; | |
152 | #endif | |
153 | } | |
154 | ||
155 | static __inline__ | |
156 | unsigned short | |
157 | NXSwapBigShortToHost( | |
158 | unsigned short x | |
159 | ) | |
160 | { | |
161 | return (unsigned short)OSSwapBigToHostInt16((uint16_t)x); | |
162 | } | |
163 | ||
164 | static __inline__ | |
165 | unsigned int | |
166 | NXSwapBigIntToHost( | |
167 | unsigned int x | |
168 | ) | |
169 | { | |
170 | return (unsigned int)OSSwapBigToHostInt32((uint32_t)x); | |
171 | } | |
172 | ||
173 | static __inline__ | |
174 | unsigned long | |
175 | NXSwapBigLongToHost( | |
176 | unsigned long x | |
177 | ) | |
178 | { | |
179 | return (unsigned long)OSSwapBigToHostInt32((uint32_t)x); | |
180 | } | |
181 | ||
182 | static __inline__ | |
183 | unsigned long long | |
184 | NXSwapBigLongLongToHost( | |
185 | unsigned long long x | |
186 | ) | |
187 | { | |
188 | return (unsigned long long)OSSwapBigToHostInt64((uint64_t)x); | |
189 | } | |
190 | ||
191 | static __inline__ | |
192 | double | |
193 | NXSwapBigDoubleToHost( | |
194 | NXSwappedDouble x | |
195 | ) | |
196 | { | |
197 | return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapBigToHostInt64((uint64_t)x)); | |
198 | } | |
199 | ||
200 | static __inline__ | |
201 | float | |
202 | NXSwapBigFloatToHost( | |
203 | NXSwappedFloat x | |
204 | ) | |
205 | { | |
206 | return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapBigToHostInt32((uint32_t)x)); | |
207 | } | |
208 | ||
209 | static __inline__ | |
210 | unsigned short | |
211 | NXSwapHostShortToBig( | |
212 | unsigned short x | |
213 | ) | |
214 | { | |
215 | return (unsigned short)OSSwapHostToBigInt16((uint16_t)x); | |
216 | } | |
217 | ||
218 | static __inline__ | |
219 | unsigned int | |
220 | NXSwapHostIntToBig( | |
221 | unsigned int x | |
222 | ) | |
223 | { | |
224 | return (unsigned int)OSSwapHostToBigInt32((uint32_t)x); | |
225 | } | |
226 | ||
227 | static __inline__ | |
228 | unsigned long | |
229 | NXSwapHostLongToBig( | |
230 | unsigned long x | |
231 | ) | |
232 | { | |
233 | return (unsigned long)OSSwapHostToBigInt32((uint32_t)x); | |
234 | } | |
235 | ||
236 | static __inline__ | |
237 | unsigned long long | |
238 | NXSwapHostLongLongToBig( | |
239 | unsigned long long x | |
240 | ) | |
241 | { | |
242 | return (unsigned long long)OSSwapHostToBigInt64((uint64_t)x); | |
243 | } | |
244 | ||
245 | static __inline__ | |
246 | NXSwappedDouble | |
247 | NXSwapHostDoubleToBig( | |
248 | double x | |
249 | ) | |
250 | { | |
251 | return (NXSwappedDouble)OSSwapHostToBigInt64((uint64_t)NXConvertHostDoubleToSwapped(x)); | |
252 | } | |
253 | ||
254 | static __inline__ | |
255 | NXSwappedFloat | |
256 | NXSwapHostFloatToBig( | |
257 | float x | |
258 | ) | |
259 | { | |
260 | return (NXSwappedFloat)OSSwapHostToBigInt32((uint32_t)NXConvertHostFloatToSwapped(x)); | |
261 | } | |
262 | ||
263 | static __inline__ | |
264 | unsigned short | |
265 | NXSwapLittleShortToHost( | |
266 | unsigned short x | |
267 | ) | |
268 | { | |
269 | return (unsigned short)OSSwapLittleToHostInt16((uint16_t)x); | |
270 | } | |
271 | ||
272 | static __inline__ | |
273 | unsigned int | |
274 | NXSwapLittleIntToHost( | |
275 | unsigned int x | |
276 | ) | |
277 | { | |
278 | return (unsigned int)OSSwapLittleToHostInt32((uint32_t)x); | |
279 | } | |
280 | ||
281 | static __inline__ | |
282 | unsigned long | |
283 | NXSwapLittleLongToHost( | |
284 | unsigned long x | |
285 | ) | |
286 | { | |
287 | return (unsigned long)OSSwapLittleToHostInt32((uint32_t)x); | |
288 | } | |
289 | ||
290 | static __inline__ | |
291 | unsigned long long | |
292 | NXSwapLittleLongLongToHost( | |
293 | unsigned long long x | |
294 | ) | |
295 | { | |
296 | return (unsigned long long)OSSwapLittleToHostInt64((uint64_t)x); | |
297 | } | |
298 | ||
299 | static __inline__ | |
300 | double | |
301 | NXSwapLittleDoubleToHost( | |
302 | NXSwappedDouble x | |
303 | ) | |
304 | { | |
305 | return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapLittleToHostInt64((uint64_t)x)); | |
306 | } | |
307 | ||
308 | static __inline__ | |
309 | float | |
310 | NXSwapLittleFloatToHost( | |
311 | NXSwappedFloat x | |
312 | ) | |
313 | { | |
314 | return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapLittleToHostInt32((uint32_t)x)); | |
315 | } | |
316 | ||
317 | static __inline__ | |
318 | unsigned short | |
319 | NXSwapHostShortToLittle( | |
320 | unsigned short x | |
321 | ) | |
322 | { | |
323 | return (unsigned short)OSSwapHostToLittleInt16((uint16_t)x); | |
324 | } | |
325 | ||
326 | static __inline__ | |
327 | unsigned int | |
328 | NXSwapHostIntToLittle( | |
329 | unsigned int x | |
330 | ) | |
331 | { | |
332 | return (unsigned int)OSSwapHostToLittleInt32((uint32_t)x); | |
333 | } | |
334 | ||
335 | static __inline__ | |
336 | unsigned long | |
337 | NXSwapHostLongToLittle( | |
338 | unsigned long x | |
339 | ) | |
340 | { | |
341 | return (unsigned long)OSSwapHostToLittleInt32((uint32_t)x); | |
342 | } | |
343 | ||
344 | static __inline__ | |
345 | unsigned long long | |
346 | NXSwapHostLongLongToLittle( | |
347 | unsigned long long x | |
348 | ) | |
349 | { | |
350 | return (unsigned long long)OSSwapHostToLittleInt64((uint64_t)x); | |
351 | } | |
352 | ||
353 | static __inline__ | |
354 | NXSwappedDouble | |
355 | NXSwapHostDoubleToLittle( | |
356 | double x | |
357 | ) | |
358 | { | |
359 | return (NXSwappedDouble)OSSwapHostToLittleInt64((uint64_t)NXConvertHostDoubleToSwapped(x)); | |
360 | } | |
361 | ||
362 | static __inline__ | |
363 | NXSwappedFloat | |
364 | NXSwapHostFloatToLittle( | |
365 | float x | |
366 | ) | |
367 | { | |
368 | return (NXSwappedFloat)OSSwapHostToLittleInt32((uint32_t)NXConvertHostFloatToSwapped(x)); | |
369 | } | |
370 | ||
371 | #endif /* _ARCHITECTURE_BYTE_ORDER_H_ */ |