]> git.saurik.com Git - apple/cf.git/blame - CFByteOrder.h
CF-476.18.tar.gz
[apple/cf.git] / CFByteOrder.h
CommitLineData
9ce05555 1/*
bd5b749c 2 * Copyright (c) 2008 Apple Inc. All rights reserved.
9ce05555
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9ce05555
A
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/* CFByteOrder.h
bd5b749c 24 Copyright (c) 1995-2007, Apple Inc. All rights reserved.
9ce05555
A
25*/
26
27#if !defined(__COREFOUNDATION_CFBYTEORDER__)
28#define __COREFOUNDATION_CFBYTEORDER__ 1
29
9ce05555 30#include <CoreFoundation/CFBase.h>
bd5b749c
A
31#if defined(__MACH__) && !defined(CF_USE_OSBYTEORDER_H)
32#include <libkern/OSByteOrder.h>
33#define CF_USE_OSBYTEORDER_H 1
9ce05555
A
34#endif
35
bd5b749c
A
36CF_EXTERN_C_BEGIN
37
38enum __CFByteOrder {
9ce05555
A
39 CFByteOrderUnknown,
40 CFByteOrderLittleEndian,
41 CFByteOrderBigEndian
bd5b749c
A
42};
43typedef CFIndex CFByteOrder;
9ce05555
A
44
45CF_INLINE CFByteOrder CFByteOrderGetCurrent(void) {
bd5b749c
A
46#if CF_USE_OSBYTEORDER_H
47 int32_t byteOrder = OSHostByteOrder();
48 switch (byteOrder) {
49 case OSLittleEndian: return CFByteOrderLittleEndian;
50 case OSBigEndian: return CFByteOrderBigEndian;
51 default: break;
52 }
53 return CFByteOrderUnknown;
54#else
55#if __LITTLE_ENDIAN__
56 return CFByteOrderLittleEndian;
57#elif __BIG_ENDIAN__
58 return CFByteOrderBigEndian;
59#else
60 return CFByteOrderUnknown;
61#endif
62#endif
9ce05555
A
63}
64
65CF_INLINE uint16_t CFSwapInt16(uint16_t arg) {
bd5b749c
A
66#if CF_USE_OSBYTEORDER_H
67 return OSSwapInt16(arg);
9ce05555
A
68#else
69 uint16_t result;
bd5b749c 70 result = (uint16_t)(((arg << 8) & 0xFF00) | ((arg >> 8) & 0xFF));
9ce05555
A
71 return result;
72#endif
73}
74
75CF_INLINE uint32_t CFSwapInt32(uint32_t arg) {
bd5b749c
A
76#if CF_USE_OSBYTEORDER_H
77 return OSSwapInt32(arg);
9ce05555
A
78#else
79 uint32_t result;
80 result = ((arg & 0xFF) << 24) | ((arg & 0xFF00) << 8) | ((arg >> 8) & 0xFF00) | ((arg >> 24) & 0xFF);
81 return result;
82#endif
83}
84
85CF_INLINE uint64_t CFSwapInt64(uint64_t arg) {
bd5b749c
A
86#if CF_USE_OSBYTEORDER_H
87 return OSSwapInt64(arg);
88#else
9ce05555
A
89 union CFSwap {
90 uint64_t sv;
91 uint32_t ul[2];
92 } tmp, result;
93 tmp.sv = arg;
94 result.ul[0] = CFSwapInt32(tmp.ul[1]);
95 result.ul[1] = CFSwapInt32(tmp.ul[0]);
96 return result.sv;
bd5b749c 97#endif
9ce05555
A
98}
99
100CF_INLINE uint16_t CFSwapInt16BigToHost(uint16_t arg) {
bd5b749c
A
101#if CF_USE_OSBYTEORDER_H
102 return OSSwapBigToHostInt16(arg);
103#elif __BIG_ENDIAN__
9ce05555
A
104 return arg;
105#else
106 return CFSwapInt16(arg);
107#endif
108}
109
110CF_INLINE uint32_t CFSwapInt32BigToHost(uint32_t arg) {
bd5b749c
A
111#if CF_USE_OSBYTEORDER_H
112 return OSSwapBigToHostInt32(arg);
113#elif __BIG_ENDIAN__
9ce05555
A
114 return arg;
115#else
116 return CFSwapInt32(arg);
117#endif
118}
119
120CF_INLINE uint64_t CFSwapInt64BigToHost(uint64_t arg) {
bd5b749c
A
121#if CF_USE_OSBYTEORDER_H
122 return OSSwapBigToHostInt64(arg);
123#elif __BIG_ENDIAN__
9ce05555
A
124 return arg;
125#else
126 return CFSwapInt64(arg);
127#endif
128}
129
130CF_INLINE uint16_t CFSwapInt16HostToBig(uint16_t arg) {
bd5b749c
A
131#if CF_USE_OSBYTEORDER_H
132 return OSSwapHostToBigInt16(arg);
133#elif __BIG_ENDIAN__
9ce05555
A
134 return arg;
135#else
136 return CFSwapInt16(arg);
137#endif
138}
139
140CF_INLINE uint32_t CFSwapInt32HostToBig(uint32_t arg) {
bd5b749c
A
141#if CF_USE_OSBYTEORDER_H
142 return OSSwapHostToBigInt32(arg);
143#elif __BIG_ENDIAN__
9ce05555
A
144 return arg;
145#else
146 return CFSwapInt32(arg);
147#endif
148}
149
150CF_INLINE uint64_t CFSwapInt64HostToBig(uint64_t arg) {
bd5b749c
A
151#if CF_USE_OSBYTEORDER_H
152 return OSSwapHostToBigInt64(arg);
153#elif __BIG_ENDIAN__
9ce05555
A
154 return arg;
155#else
156 return CFSwapInt64(arg);
157#endif
158}
159
160CF_INLINE uint16_t CFSwapInt16LittleToHost(uint16_t arg) {
bd5b749c
A
161#if CF_USE_OSBYTEORDER_H
162 return OSSwapLittleToHostInt16(arg);
163#elif __LITTLE_ENDIAN__
9ce05555
A
164 return arg;
165#else
166 return CFSwapInt16(arg);
167#endif
168}
169
170CF_INLINE uint32_t CFSwapInt32LittleToHost(uint32_t arg) {
bd5b749c
A
171#if CF_USE_OSBYTEORDER_H
172 return OSSwapLittleToHostInt32(arg);
173#elif __LITTLE_ENDIAN__
9ce05555
A
174 return arg;
175#else
176 return CFSwapInt32(arg);
177#endif
178}
179
180CF_INLINE uint64_t CFSwapInt64LittleToHost(uint64_t arg) {
bd5b749c
A
181#if CF_USE_OSBYTEORDER_H
182 return OSSwapLittleToHostInt64(arg);
183#elif __LITTLE_ENDIAN__
9ce05555
A
184 return arg;
185#else
186 return CFSwapInt64(arg);
187#endif
188}
189
190CF_INLINE uint16_t CFSwapInt16HostToLittle(uint16_t arg) {
bd5b749c
A
191#if CF_USE_OSBYTEORDER_H
192 return OSSwapHostToLittleInt16(arg);
193#elif __LITTLE_ENDIAN__
9ce05555
A
194 return arg;
195#else
196 return CFSwapInt16(arg);
197#endif
198}
199
200CF_INLINE uint32_t CFSwapInt32HostToLittle(uint32_t arg) {
bd5b749c
A
201#if CF_USE_OSBYTEORDER_H
202 return OSSwapHostToLittleInt32(arg);
203#elif __LITTLE_ENDIAN__
9ce05555
A
204 return arg;
205#else
206 return CFSwapInt32(arg);
207#endif
208}
209
210CF_INLINE uint64_t CFSwapInt64HostToLittle(uint64_t arg) {
bd5b749c
A
211#if CF_USE_OSBYTEORDER_H
212 return OSSwapHostToLittleInt64(arg);
213#elif __LITTLE_ENDIAN__
9ce05555
A
214 return arg;
215#else
216 return CFSwapInt64(arg);
217#endif
218}
219
220typedef struct {uint32_t v;} CFSwappedFloat32;
221typedef struct {uint64_t v;} CFSwappedFloat64;
222
223CF_INLINE CFSwappedFloat32 CFConvertFloat32HostToSwapped(Float32 arg) {
224 union CFSwap {
225 Float32 v;
226 CFSwappedFloat32 sv;
227 } result;
228 result.v = arg;
bd5b749c 229#if __LITTLE_ENDIAN__
9ce05555
A
230 result.sv.v = CFSwapInt32(result.sv.v);
231#endif
232 return result.sv;
233}
234
235CF_INLINE Float32 CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg) {
236 union CFSwap {
237 Float32 v;
238 CFSwappedFloat32 sv;
239 } result;
240 result.sv = arg;
bd5b749c 241#if __LITTLE_ENDIAN__
9ce05555
A
242 result.sv.v = CFSwapInt32(result.sv.v);
243#endif
244 return result.v;
245}
246
247CF_INLINE CFSwappedFloat64 CFConvertFloat64HostToSwapped(Float64 arg) {
248 union CFSwap {
249 Float64 v;
250 CFSwappedFloat64 sv;
251 } result;
252 result.v = arg;
bd5b749c 253#if __LITTLE_ENDIAN__
9ce05555
A
254 result.sv.v = CFSwapInt64(result.sv.v);
255#endif
256 return result.sv;
257}
258
259CF_INLINE Float64 CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg) {
260 union CFSwap {
261 Float64 v;
262 CFSwappedFloat64 sv;
263 } result;
264 result.sv = arg;
bd5b749c 265#if __LITTLE_ENDIAN__
9ce05555
A
266 result.sv.v = CFSwapInt64(result.sv.v);
267#endif
268 return result.v;
269}
270
271CF_INLINE CFSwappedFloat32 CFConvertFloatHostToSwapped(float arg) {
272 union CFSwap {
273 float v;
274 CFSwappedFloat32 sv;
275 } result;
276 result.v = arg;
bd5b749c 277#if __LITTLE_ENDIAN__
9ce05555
A
278 result.sv.v = CFSwapInt32(result.sv.v);
279#endif
280 return result.sv;
281}
282
283CF_INLINE float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg) {
284 union CFSwap {
285 float v;
286 CFSwappedFloat32 sv;
287 } result;
288 result.sv = arg;
bd5b749c 289#if __LITTLE_ENDIAN__
9ce05555
A
290 result.sv.v = CFSwapInt32(result.sv.v);
291#endif
292 return result.v;
293}
294
295CF_INLINE CFSwappedFloat64 CFConvertDoubleHostToSwapped(double arg) {
296 union CFSwap {
297 double v;
298 CFSwappedFloat64 sv;
299 } result;
300 result.v = arg;
bd5b749c 301#if __LITTLE_ENDIAN__
9ce05555
A
302 result.sv.v = CFSwapInt64(result.sv.v);
303#endif
304 return result.sv;
305}
306
307CF_INLINE double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg) {
308 union CFSwap {
309 double v;
310 CFSwappedFloat64 sv;
311 } result;
312 result.sv = arg;
bd5b749c 313#if __LITTLE_ENDIAN__
9ce05555
A
314 result.sv.v = CFSwapInt64(result.sv.v);
315#endif
316 return result.v;
317}
318
bd5b749c 319CF_EXTERN_C_END
9ce05555
A
320
321#endif /* ! __COREFOUNDATION_CFBYTEORDER__ */
322