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