2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 Copyright (c) 1995-2007, Apple Inc. All rights reserved.
27 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
28 #define __COREFOUNDATION_CFBYTEORDER__ 1
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
40 CFByteOrderLittleEndian
,
43 typedef CFIndex CFByteOrder
;
45 CF_INLINE CFByteOrder
CFByteOrderGetCurrent(void) {
46 #if CF_USE_OSBYTEORDER_H
47 int32_t byteOrder
= OSHostByteOrder();
49 case OSLittleEndian
: return CFByteOrderLittleEndian
;
50 case OSBigEndian
: return CFByteOrderBigEndian
;
53 return CFByteOrderUnknown
;
56 return CFByteOrderLittleEndian
;
58 return CFByteOrderBigEndian
;
60 return CFByteOrderUnknown
;
65 CF_INLINE
uint16_t CFSwapInt16(uint16_t arg
) {
66 #if CF_USE_OSBYTEORDER_H
67 return OSSwapInt16(arg
);
70 result
= (uint16_t)(((arg
<< 8) & 0xFF00) | ((arg
>> 8) & 0xFF));
75 CF_INLINE
uint32_t CFSwapInt32(uint32_t arg
) {
76 #if CF_USE_OSBYTEORDER_H
77 return OSSwapInt32(arg
);
80 result
= ((arg
& 0xFF) << 24) | ((arg
& 0xFF00) << 8) | ((arg
>> 8) & 0xFF00) | ((arg
>> 24) & 0xFF);
85 CF_INLINE
uint64_t CFSwapInt64(uint64_t arg
) {
86 #if CF_USE_OSBYTEORDER_H
87 return OSSwapInt64(arg
);
94 result
.ul
[0] = CFSwapInt32(tmp
.ul
[1]);
95 result
.ul
[1] = CFSwapInt32(tmp
.ul
[0]);
100 CF_INLINE
uint16_t CFSwapInt16BigToHost(uint16_t arg
) {
101 #if CF_USE_OSBYTEORDER_H
102 return OSSwapBigToHostInt16(arg
);
106 return CFSwapInt16(arg
);
110 CF_INLINE
uint32_t CFSwapInt32BigToHost(uint32_t arg
) {
111 #if CF_USE_OSBYTEORDER_H
112 return OSSwapBigToHostInt32(arg
);
116 return CFSwapInt32(arg
);
120 CF_INLINE
uint64_t CFSwapInt64BigToHost(uint64_t arg
) {
121 #if CF_USE_OSBYTEORDER_H
122 return OSSwapBigToHostInt64(arg
);
126 return CFSwapInt64(arg
);
130 CF_INLINE
uint16_t CFSwapInt16HostToBig(uint16_t arg
) {
131 #if CF_USE_OSBYTEORDER_H
132 return OSSwapHostToBigInt16(arg
);
136 return CFSwapInt16(arg
);
140 CF_INLINE
uint32_t CFSwapInt32HostToBig(uint32_t arg
) {
141 #if CF_USE_OSBYTEORDER_H
142 return OSSwapHostToBigInt32(arg
);
146 return CFSwapInt32(arg
);
150 CF_INLINE
uint64_t CFSwapInt64HostToBig(uint64_t arg
) {
151 #if CF_USE_OSBYTEORDER_H
152 return OSSwapHostToBigInt64(arg
);
156 return CFSwapInt64(arg
);
160 CF_INLINE
uint16_t CFSwapInt16LittleToHost(uint16_t arg
) {
161 #if CF_USE_OSBYTEORDER_H
162 return OSSwapLittleToHostInt16(arg
);
163 #elif __LITTLE_ENDIAN__
166 return CFSwapInt16(arg
);
170 CF_INLINE
uint32_t CFSwapInt32LittleToHost(uint32_t arg
) {
171 #if CF_USE_OSBYTEORDER_H
172 return OSSwapLittleToHostInt32(arg
);
173 #elif __LITTLE_ENDIAN__
176 return CFSwapInt32(arg
);
180 CF_INLINE
uint64_t CFSwapInt64LittleToHost(uint64_t arg
) {
181 #if CF_USE_OSBYTEORDER_H
182 return OSSwapLittleToHostInt64(arg
);
183 #elif __LITTLE_ENDIAN__
186 return CFSwapInt64(arg
);
190 CF_INLINE
uint16_t CFSwapInt16HostToLittle(uint16_t arg
) {
191 #if CF_USE_OSBYTEORDER_H
192 return OSSwapHostToLittleInt16(arg
);
193 #elif __LITTLE_ENDIAN__
196 return CFSwapInt16(arg
);
200 CF_INLINE
uint32_t CFSwapInt32HostToLittle(uint32_t arg
) {
201 #if CF_USE_OSBYTEORDER_H
202 return OSSwapHostToLittleInt32(arg
);
203 #elif __LITTLE_ENDIAN__
206 return CFSwapInt32(arg
);
210 CF_INLINE
uint64_t CFSwapInt64HostToLittle(uint64_t arg
) {
211 #if CF_USE_OSBYTEORDER_H
212 return OSSwapHostToLittleInt64(arg
);
213 #elif __LITTLE_ENDIAN__
216 return CFSwapInt64(arg
);
220 typedef struct {uint32_t v
;} CFSwappedFloat32
;
221 typedef struct {uint64_t v
;} CFSwappedFloat64
;
223 CF_INLINE CFSwappedFloat32
CFConvertFloat32HostToSwapped(Float32 arg
) {
229 #if __LITTLE_ENDIAN__
230 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
235 CF_INLINE Float32
CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg
) {
241 #if __LITTLE_ENDIAN__
242 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
247 CF_INLINE CFSwappedFloat64
CFConvertFloat64HostToSwapped(Float64 arg
) {
253 #if __LITTLE_ENDIAN__
254 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
259 CF_INLINE Float64
CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg
) {
265 #if __LITTLE_ENDIAN__
266 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
271 CF_INLINE CFSwappedFloat32
CFConvertFloatHostToSwapped(float arg
) {
277 #if __LITTLE_ENDIAN__
278 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
283 CF_INLINE
float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg
) {
289 #if __LITTLE_ENDIAN__
290 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
295 CF_INLINE CFSwappedFloat64
CFConvertDoubleHostToSwapped(double arg
) {
301 #if __LITTLE_ENDIAN__
302 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
307 CF_INLINE
double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg
) {
313 #if __LITTLE_ENDIAN__
314 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
321 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */