]> git.saurik.com Git - apple/cf.git/blob - Base.subproj/CFByteOrder.h
CF-368.25.tar.gz
[apple/cf.git] / Base.subproj / CFByteOrder.h
1 /*
2 * Copyright (c) 2005 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 /* CFByteOrder.h
24 Copyright (c) 1995-2005, Apple, Inc. All rights reserved.
25 */
26
27 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
28 #define __COREFOUNDATION_CFBYTEORDER__ 1
29
30 #if defined(__i386__) && !defined(__LITTLE_ENDIAN__)
31 #define __LITTLE_ENDIAN__ 1
32 #endif
33
34 #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
35 #error Do not know the endianess of this architecture
36 #endif
37
38 #include <CoreFoundation/CFBase.h>
39
40 #if defined(__cplusplus)
41 extern "C" {
42 #endif
43
44 typedef enum __CFByteOrder {
45 CFByteOrderUnknown,
46 CFByteOrderLittleEndian,
47 CFByteOrderBigEndian
48 } CFByteOrder;
49
50 CF_INLINE CFByteOrder CFByteOrderGetCurrent(void) {
51 uint32_t x = (CFByteOrderBigEndian << 24) | CFByteOrderLittleEndian;
52 return (CFByteOrder)*((uint8_t *)&x);
53 }
54
55 CF_INLINE uint16_t CFSwapInt16(uint16_t arg) {
56 #if defined(__i386__) && defined(__GNUC__)
57 __asm__("xchgb %b0, %h0" : "+q" (arg));
58 return arg;
59 #elif defined(__ppc__) && defined(__GNUC__)
60 uint16_t result;
61 __asm__("lhbrx %0,0,%1" : "=r" (result) : "r" (&arg), "m" (arg));
62 return result;
63 #else
64 uint16_t result;
65 result = ((arg << 8) & 0xFF00) | ((arg >> 8) & 0xFF);
66 return result;
67 #endif
68 }
69
70 CF_INLINE uint32_t CFSwapInt32(uint32_t arg) {
71 #if defined(__i386__) && defined(__GNUC__)
72 __asm__("bswap %0" : "+r" (arg));
73 return arg;
74 #elif defined(__ppc__) && defined(__GNUC__)
75 uint32_t result;
76 __asm__("lwbrx %0,0,%1" : "=r" (result) : "r" (&arg), "m" (arg));
77 return result;
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 union CFSwap {
87 uint64_t sv;
88 uint32_t ul[2];
89 } tmp, result;
90 tmp.sv = arg;
91 result.ul[0] = CFSwapInt32(tmp.ul[1]);
92 result.ul[1] = CFSwapInt32(tmp.ul[0]);
93 return result.sv;
94 }
95
96 CF_INLINE uint16_t CFSwapInt16BigToHost(uint16_t arg) {
97 #if defined(__BIG_ENDIAN__)
98 return arg;
99 #else
100 return CFSwapInt16(arg);
101 #endif
102 }
103
104 CF_INLINE uint32_t CFSwapInt32BigToHost(uint32_t arg) {
105 #if defined(__BIG_ENDIAN__)
106 return arg;
107 #else
108 return CFSwapInt32(arg);
109 #endif
110 }
111
112 CF_INLINE uint64_t CFSwapInt64BigToHost(uint64_t arg) {
113 #if defined(__BIG_ENDIAN__)
114 return arg;
115 #else
116 return CFSwapInt64(arg);
117 #endif
118 }
119
120 CF_INLINE uint16_t CFSwapInt16HostToBig(uint16_t arg) {
121 #if defined(__BIG_ENDIAN__)
122 return arg;
123 #else
124 return CFSwapInt16(arg);
125 #endif
126 }
127
128 CF_INLINE uint32_t CFSwapInt32HostToBig(uint32_t arg) {
129 #if defined(__BIG_ENDIAN__)
130 return arg;
131 #else
132 return CFSwapInt32(arg);
133 #endif
134 }
135
136 CF_INLINE uint64_t CFSwapInt64HostToBig(uint64_t arg) {
137 #if defined(__BIG_ENDIAN__)
138 return arg;
139 #else
140 return CFSwapInt64(arg);
141 #endif
142 }
143
144 CF_INLINE uint16_t CFSwapInt16LittleToHost(uint16_t arg) {
145 #if defined(__LITTLE_ENDIAN__)
146 return arg;
147 #else
148 return CFSwapInt16(arg);
149 #endif
150 }
151
152 CF_INLINE uint32_t CFSwapInt32LittleToHost(uint32_t arg) {
153 #if defined(__LITTLE_ENDIAN__)
154 return arg;
155 #else
156 return CFSwapInt32(arg);
157 #endif
158 }
159
160 CF_INLINE uint64_t CFSwapInt64LittleToHost(uint64_t arg) {
161 #if defined(__LITTLE_ENDIAN__)
162 return arg;
163 #else
164 return CFSwapInt64(arg);
165 #endif
166 }
167
168 CF_INLINE uint16_t CFSwapInt16HostToLittle(uint16_t arg) {
169 #if defined(__LITTLE_ENDIAN__)
170 return arg;
171 #else
172 return CFSwapInt16(arg);
173 #endif
174 }
175
176 CF_INLINE uint32_t CFSwapInt32HostToLittle(uint32_t arg) {
177 #if defined(__LITTLE_ENDIAN__)
178 return arg;
179 #else
180 return CFSwapInt32(arg);
181 #endif
182 }
183
184 CF_INLINE uint64_t CFSwapInt64HostToLittle(uint64_t arg) {
185 #if defined(__LITTLE_ENDIAN__)
186 return arg;
187 #else
188 return CFSwapInt64(arg);
189 #endif
190 }
191
192 typedef struct {uint32_t v;} CFSwappedFloat32;
193 typedef struct {uint64_t v;} CFSwappedFloat64;
194
195 CF_INLINE CFSwappedFloat32 CFConvertFloat32HostToSwapped(Float32 arg) {
196 union CFSwap {
197 Float32 v;
198 CFSwappedFloat32 sv;
199 } result;
200 result.v = arg;
201 #if defined(__LITTLE_ENDIAN__)
202 result.sv.v = CFSwapInt32(result.sv.v);
203 #endif
204 return result.sv;
205 }
206
207 CF_INLINE Float32 CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg) {
208 union CFSwap {
209 Float32 v;
210 CFSwappedFloat32 sv;
211 } result;
212 result.sv = arg;
213 #if defined(__LITTLE_ENDIAN__)
214 result.sv.v = CFSwapInt32(result.sv.v);
215 #endif
216 return result.v;
217 }
218
219 CF_INLINE CFSwappedFloat64 CFConvertFloat64HostToSwapped(Float64 arg) {
220 union CFSwap {
221 Float64 v;
222 CFSwappedFloat64 sv;
223 } result;
224 result.v = arg;
225 #if defined(__LITTLE_ENDIAN__)
226 result.sv.v = CFSwapInt64(result.sv.v);
227 #endif
228 return result.sv;
229 }
230
231 CF_INLINE Float64 CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg) {
232 union CFSwap {
233 Float64 v;
234 CFSwappedFloat64 sv;
235 } result;
236 result.sv = arg;
237 #if defined(__LITTLE_ENDIAN__)
238 result.sv.v = CFSwapInt64(result.sv.v);
239 #endif
240 return result.v;
241 }
242
243 CF_INLINE CFSwappedFloat32 CFConvertFloatHostToSwapped(float arg) {
244 union CFSwap {
245 float v;
246 CFSwappedFloat32 sv;
247 } result;
248 result.v = arg;
249 #if defined(__LITTLE_ENDIAN__)
250 result.sv.v = CFSwapInt32(result.sv.v);
251 #endif
252 return result.sv;
253 }
254
255 CF_INLINE float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg) {
256 union CFSwap {
257 float v;
258 CFSwappedFloat32 sv;
259 } result;
260 result.sv = arg;
261 #if defined(__LITTLE_ENDIAN__)
262 result.sv.v = CFSwapInt32(result.sv.v);
263 #endif
264 return result.v;
265 }
266
267 CF_INLINE CFSwappedFloat64 CFConvertDoubleHostToSwapped(double arg) {
268 union CFSwap {
269 double v;
270 CFSwappedFloat64 sv;
271 } result;
272 result.v = arg;
273 #if defined(__LITTLE_ENDIAN__)
274 result.sv.v = CFSwapInt64(result.sv.v);
275 #endif
276 return result.sv;
277 }
278
279 CF_INLINE double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg) {
280 union CFSwap {
281 double v;
282 CFSwappedFloat64 sv;
283 } result;
284 result.sv = arg;
285 #if defined(__LITTLE_ENDIAN__)
286 result.sv.v = CFSwapInt64(result.sv.v);
287 #endif
288 return result.v;
289 }
290
291 #if defined(__cplusplus)
292 }
293 #endif
294
295 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */
296