]> git.saurik.com Git - apple/cf.git/blob - Base.subproj/CFByteOrder.h
CF-299.tar.gz
[apple/cf.git] / Base.subproj / CFByteOrder.h
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* CFByteOrder.h
26 Copyright (c) 1995-2003, Apple, Inc. All rights reserved.
27 */
28
29 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
30 #define __COREFOUNDATION_CFBYTEORDER__ 1
31
32 #if defined(__i386) && !defined(__LITTLE_ENDIAN__)
33 #define __LITTLE_ENDIAN__ 1
34 #endif
35
36 #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
37 #error Do not know the endianess of this architecture
38 #endif
39
40 #include <CoreFoundation/CFBase.h>
41
42 #if defined(__cplusplus)
43 extern "C" {
44 #endif
45
46 typedef enum __CFByteOrder {
47 CFByteOrderUnknown,
48 CFByteOrderLittleEndian,
49 CFByteOrderBigEndian
50 } CFByteOrder;
51
52 CF_INLINE CFByteOrder CFByteOrderGetCurrent(void) {
53 uint32_t x = (CFByteOrderBigEndian << 24) | CFByteOrderLittleEndian;
54 return (CFByteOrder)*((uint8_t *)&x);
55 }
56
57 CF_INLINE uint16_t CFSwapInt16(uint16_t arg) {
58 #if defined(__i386__) && defined(__GNUC__)
59 __asm__("xchgb %b0, %h0" : "+q" (arg));
60 return arg;
61 #elif defined(__ppc__) && defined(__GNUC__)
62 uint16_t result;
63 __asm__("lhbrx %0,0,%1" : "=r" (result) : "r" (&arg), "m" (arg));
64 return result;
65 #else
66 uint16_t result;
67 result = ((arg << 8) & 0xFF00) | ((arg >> 8) & 0xFF);
68 return result;
69 #endif
70 }
71
72 CF_INLINE uint32_t CFSwapInt32(uint32_t arg) {
73 #if defined(__i386__) && defined(__GNUC__)
74 __asm__("bswap %0" : "+r" (arg));
75 return arg;
76 #elif defined(__ppc__) && defined(__GNUC__)
77 uint32_t result;
78 __asm__("lwbrx %0,0,%1" : "=r" (result) : "r" (&arg), "m" (arg));
79 return result;
80 #else
81 uint32_t result;
82 result = ((arg & 0xFF) << 24) | ((arg & 0xFF00) << 8) | ((arg >> 8) & 0xFF00) | ((arg >> 24) & 0xFF);
83 return result;
84 #endif
85 }
86
87 CF_INLINE uint64_t CFSwapInt64(uint64_t arg) {
88 union CFSwap {
89 uint64_t sv;
90 uint32_t ul[2];
91 } tmp, result;
92 tmp.sv = arg;
93 result.ul[0] = CFSwapInt32(tmp.ul[1]);
94 result.ul[1] = CFSwapInt32(tmp.ul[0]);
95 return result.sv;
96 }
97
98 CF_INLINE uint16_t CFSwapInt16BigToHost(uint16_t arg) {
99 #if defined(__BIG_ENDIAN__)
100 return arg;
101 #else
102 return CFSwapInt16(arg);
103 #endif
104 }
105
106 CF_INLINE uint32_t CFSwapInt32BigToHost(uint32_t arg) {
107 #if defined(__BIG_ENDIAN__)
108 return arg;
109 #else
110 return CFSwapInt32(arg);
111 #endif
112 }
113
114 CF_INLINE uint64_t CFSwapInt64BigToHost(uint64_t arg) {
115 #if defined(__BIG_ENDIAN__)
116 return arg;
117 #else
118 return CFSwapInt64(arg);
119 #endif
120 }
121
122 CF_INLINE uint16_t CFSwapInt16HostToBig(uint16_t arg) {
123 #if defined(__BIG_ENDIAN__)
124 return arg;
125 #else
126 return CFSwapInt16(arg);
127 #endif
128 }
129
130 CF_INLINE uint32_t CFSwapInt32HostToBig(uint32_t arg) {
131 #if defined(__BIG_ENDIAN__)
132 return arg;
133 #else
134 return CFSwapInt32(arg);
135 #endif
136 }
137
138 CF_INLINE uint64_t CFSwapInt64HostToBig(uint64_t arg) {
139 #if defined(__BIG_ENDIAN__)
140 return arg;
141 #else
142 return CFSwapInt64(arg);
143 #endif
144 }
145
146 CF_INLINE uint16_t CFSwapInt16LittleToHost(uint16_t arg) {
147 #if defined(__LITTLE_ENDIAN__)
148 return arg;
149 #else
150 return CFSwapInt16(arg);
151 #endif
152 }
153
154 CF_INLINE uint32_t CFSwapInt32LittleToHost(uint32_t arg) {
155 #if defined(__LITTLE_ENDIAN__)
156 return arg;
157 #else
158 return CFSwapInt32(arg);
159 #endif
160 }
161
162 CF_INLINE uint64_t CFSwapInt64LittleToHost(uint64_t arg) {
163 #if defined(__LITTLE_ENDIAN__)
164 return arg;
165 #else
166 return CFSwapInt64(arg);
167 #endif
168 }
169
170 CF_INLINE uint16_t CFSwapInt16HostToLittle(uint16_t arg) {
171 #if defined(__LITTLE_ENDIAN__)
172 return arg;
173 #else
174 return CFSwapInt16(arg);
175 #endif
176 }
177
178 CF_INLINE uint32_t CFSwapInt32HostToLittle(uint32_t arg) {
179 #if defined(__LITTLE_ENDIAN__)
180 return arg;
181 #else
182 return CFSwapInt32(arg);
183 #endif
184 }
185
186 CF_INLINE uint64_t CFSwapInt64HostToLittle(uint64_t arg) {
187 #if defined(__LITTLE_ENDIAN__)
188 return arg;
189 #else
190 return CFSwapInt64(arg);
191 #endif
192 }
193
194 typedef struct {uint32_t v;} CFSwappedFloat32;
195 typedef struct {uint64_t v;} CFSwappedFloat64;
196
197 CF_INLINE CFSwappedFloat32 CFConvertFloat32HostToSwapped(Float32 arg) {
198 union CFSwap {
199 Float32 v;
200 CFSwappedFloat32 sv;
201 } result;
202 result.v = arg;
203 #if defined(__LITTLE_ENDIAN__)
204 result.sv.v = CFSwapInt32(result.sv.v);
205 #endif
206 return result.sv;
207 }
208
209 CF_INLINE Float32 CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg) {
210 union CFSwap {
211 Float32 v;
212 CFSwappedFloat32 sv;
213 } result;
214 result.sv = arg;
215 #if defined(__LITTLE_ENDIAN__)
216 result.sv.v = CFSwapInt32(result.sv.v);
217 #endif
218 return result.v;
219 }
220
221 CF_INLINE CFSwappedFloat64 CFConvertFloat64HostToSwapped(Float64 arg) {
222 union CFSwap {
223 Float64 v;
224 CFSwappedFloat64 sv;
225 } result;
226 result.v = arg;
227 #if defined(__LITTLE_ENDIAN__)
228 result.sv.v = CFSwapInt64(result.sv.v);
229 #endif
230 return result.sv;
231 }
232
233 CF_INLINE Float64 CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg) {
234 union CFSwap {
235 Float64 v;
236 CFSwappedFloat64 sv;
237 } result;
238 result.sv = arg;
239 #if defined(__LITTLE_ENDIAN__)
240 result.sv.v = CFSwapInt64(result.sv.v);
241 #endif
242 return result.v;
243 }
244
245 CF_INLINE CFSwappedFloat32 CFConvertFloatHostToSwapped(float arg) {
246 union CFSwap {
247 float v;
248 CFSwappedFloat32 sv;
249 } result;
250 result.v = arg;
251 #if defined(__LITTLE_ENDIAN__)
252 result.sv.v = CFSwapInt32(result.sv.v);
253 #endif
254 return result.sv;
255 }
256
257 CF_INLINE float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg) {
258 union CFSwap {
259 float v;
260 CFSwappedFloat32 sv;
261 } result;
262 result.sv = arg;
263 #if defined(__LITTLE_ENDIAN__)
264 result.sv.v = CFSwapInt32(result.sv.v);
265 #endif
266 return result.v;
267 }
268
269 CF_INLINE CFSwappedFloat64 CFConvertDoubleHostToSwapped(double arg) {
270 union CFSwap {
271 double v;
272 CFSwappedFloat64 sv;
273 } result;
274 result.v = arg;
275 #if defined(__LITTLE_ENDIAN__)
276 result.sv.v = CFSwapInt64(result.sv.v);
277 #endif
278 return result.sv;
279 }
280
281 CF_INLINE double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg) {
282 union CFSwap {
283 double v;
284 CFSwappedFloat64 sv;
285 } result;
286 result.sv = arg;
287 #if defined(__LITTLE_ENDIAN__)
288 result.sv.v = CFSwapInt64(result.sv.v);
289 #endif
290 return result.v;
291 }
292
293 #if defined(__cplusplus)
294 }
295 #endif
296
297 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */
298