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
36typedef unsigned long NXSwappedFloat;
37typedef unsigned long long NXSwappedDouble;
38
39static __inline__
40unsigned short
41NXSwapShort(
42    unsigned short inv
43)
44{
45    return (unsigned short)OSSwapInt16((uint16_t)inv);
46}
47
48static __inline__
49unsigned int
50NXSwapInt(
51    unsigned int inv
52)
53{
54    return (unsigned int)OSSwapInt32((uint32_t)inv);
55}
56
57static __inline__
58unsigned long
59NXSwapLong(
60    unsigned long inv
61)
62{
63    return (unsigned long)OSSwapInt32((uint32_t)inv);
64}
65
66static __inline__
67unsigned long long
68NXSwapLongLong(
69    unsigned long long inv
70)
71{
72    return (unsigned long long)OSSwapInt64((uint64_t)inv);
73}
74
75static __inline__ NXSwappedFloat
76NXConvertHostFloatToSwapped(float x)
77{
78    union fconv {
79        float number;
80        NXSwappedFloat sf;
81    } u;
82    u.number = x;
83    return u.sf;
84}
85
86static __inline__ float
87NXConvertSwappedFloatToHost(NXSwappedFloat x)
88{
89    union fconv {
90        float number;
91        NXSwappedFloat sf;
92    } u;
93    u.sf = x;
94    return u.number;
95}
96
97static __inline__ NXSwappedDouble
98NXConvertHostDoubleToSwapped(double x)
99{
100    union dconv {
101        double number;
102        NXSwappedDouble sd;
103    } u;
104    u.number = x;
105    return u.sd;
106}
107
108static __inline__ double
109NXConvertSwappedDoubleToHost(NXSwappedDouble x)
110{
111    union dconv {
112        double number;
113        NXSwappedDouble sd;
114    } u;
115    u.sd = x;
116    return u.number;
117}
118
119static __inline__ NXSwappedFloat
120NXSwapFloat(NXSwappedFloat x)
121{
122    return (NXSwappedFloat)OSSwapInt32((uint32_t)x);
123}
124
125static __inline__ NXSwappedDouble
126NXSwapDouble(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
136enum NXByteOrder {
137    NX_UnknownByteOrder,
138    NX_LittleEndian,
139    NX_BigEndian
140};
141
142static __inline__
143enum NXByteOrder
144NXHostByteOrder(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
155static __inline__
156unsigned short
157NXSwapBigShortToHost(
158    unsigned short	x
159)
160{
161    return (unsigned short)OSSwapBigToHostInt16((uint16_t)x);
162}
163
164static __inline__
165unsigned int
166NXSwapBigIntToHost(
167    unsigned int	x
168)
169{
170    return (unsigned int)OSSwapBigToHostInt32((uint32_t)x);
171}
172
173static __inline__
174unsigned long
175NXSwapBigLongToHost(
176    unsigned long	x
177)
178{
179    return (unsigned long)OSSwapBigToHostInt32((uint32_t)x);
180}
181
182static __inline__
183unsigned long long
184NXSwapBigLongLongToHost(
185    unsigned long long	x
186)
187{
188    return (unsigned long long)OSSwapBigToHostInt64((uint64_t)x);
189}
190
191static __inline__
192double
193NXSwapBigDoubleToHost(
194    NXSwappedDouble	x
195)
196{
197    return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapBigToHostInt64((uint64_t)x));
198}
199
200static __inline__
201float
202NXSwapBigFloatToHost(
203    NXSwappedFloat	x
204)
205{
206    return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapBigToHostInt32((uint32_t)x));
207}
208
209static __inline__
210unsigned short
211NXSwapHostShortToBig(
212    unsigned short	x
213)
214{
215    return (unsigned short)OSSwapHostToBigInt16((uint16_t)x);
216}
217
218static __inline__
219unsigned int
220NXSwapHostIntToBig(
221    unsigned int	x
222)
223{
224    return (unsigned int)OSSwapHostToBigInt32((uint32_t)x);
225}
226
227static __inline__
228unsigned long
229NXSwapHostLongToBig(
230    unsigned long	x
231)
232{
233    return (unsigned long)OSSwapHostToBigInt32((uint32_t)x);
234}
235
236static __inline__
237unsigned long long
238NXSwapHostLongLongToBig(
239    unsigned long long	x
240)
241{
242    return (unsigned long long)OSSwapHostToBigInt64((uint64_t)x);
243}
244
245static __inline__
246NXSwappedDouble
247NXSwapHostDoubleToBig(
248    double	x
249)
250{
251    return (NXSwappedDouble)OSSwapHostToBigInt64((uint64_t)NXConvertHostDoubleToSwapped(x));
252}
253
254static __inline__
255NXSwappedFloat
256NXSwapHostFloatToBig(
257    float	x
258)
259{
260    return (NXSwappedFloat)OSSwapHostToBigInt32((uint32_t)NXConvertHostFloatToSwapped(x));
261}
262
263static __inline__
264unsigned short
265NXSwapLittleShortToHost(
266    unsigned short	x
267)
268{
269    return (unsigned short)OSSwapLittleToHostInt16((uint16_t)x);
270}
271
272static __inline__
273unsigned int
274NXSwapLittleIntToHost(
275    unsigned int	x
276)
277{
278    return (unsigned int)OSSwapLittleToHostInt32((uint32_t)x);
279}
280
281static __inline__
282unsigned long
283NXSwapLittleLongToHost(
284    unsigned long	x
285)
286{
287    return (unsigned long)OSSwapLittleToHostInt32((uint32_t)x);
288}
289
290static __inline__
291unsigned long long
292NXSwapLittleLongLongToHost(
293    unsigned long long	x
294)
295{
296    return (unsigned long long)OSSwapLittleToHostInt64((uint64_t)x);
297}
298
299static __inline__
300double
301NXSwapLittleDoubleToHost(
302    NXSwappedDouble	x
303)
304{
305    return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapLittleToHostInt64((uint64_t)x));
306}
307
308static __inline__
309float
310NXSwapLittleFloatToHost(
311    NXSwappedFloat	x
312)
313{
314    return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapLittleToHostInt32((uint32_t)x));
315}
316
317static __inline__
318unsigned short
319NXSwapHostShortToLittle(
320    unsigned short	x
321)
322{
323    return (unsigned short)OSSwapHostToLittleInt16((uint16_t)x);
324}
325
326static __inline__
327unsigned int
328NXSwapHostIntToLittle(
329    unsigned int	x
330)
331{
332    return (unsigned int)OSSwapHostToLittleInt32((uint32_t)x);
333}
334
335static __inline__
336unsigned long
337NXSwapHostLongToLittle(
338    unsigned long	x
339)
340{
341    return (unsigned long)OSSwapHostToLittleInt32((uint32_t)x);
342}
343
344static __inline__
345unsigned long long
346NXSwapHostLongLongToLittle(
347    unsigned long long	x
348)
349{
350    return (unsigned long long)OSSwapHostToLittleInt64((uint64_t)x);
351}
352
353static __inline__
354NXSwappedDouble
355NXSwapHostDoubleToLittle(
356    double	x
357)
358{
359    return (NXSwappedDouble)OSSwapHostToLittleInt64((uint64_t)NXConvertHostDoubleToSwapped(x));
360}
361
362static __inline__
363NXSwappedFloat
364NXSwapHostFloatToLittle(
365    float	x
366)
367{
368    return (NXSwappedFloat)OSSwapHostToLittleInt32((uint32_t)NXConvertHostFloatToSwapped(x));
369}
370
371#endif	/* _ARCHITECTURE_BYTE_ORDER_H_ */
372