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