2 * Copyright (c) 2015 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@
25 Copyright (c) 1995-2014, Apple Inc. All rights reserved.
28 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
29 #define __COREFOUNDATION_CFBYTEORDER__ 1
31 #include <CoreFoundation/CFBase.h>
32 #if ((TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) && !defined(CF_USE_OSBYTEORDER_H)
33 #include <libkern/OSByteOrder.h>
34 #define CF_USE_OSBYTEORDER_H 1
41 CFByteOrderLittleEndian
,
44 typedef CFIndex CFByteOrder
;
46 CF_INLINE CFByteOrder
CFByteOrderGetCurrent(void) {
47 #if CF_USE_OSBYTEORDER_H
48 int32_t byteOrder
= OSHostByteOrder();
50 case OSLittleEndian
: return CFByteOrderLittleEndian
;
51 case OSBigEndian
: return CFByteOrderBigEndian
;
54 return CFByteOrderUnknown
;
57 return CFByteOrderLittleEndian
;
59 return CFByteOrderBigEndian
;
61 return CFByteOrderUnknown
;
66 CF_INLINE
uint16_t CFSwapInt16(uint16_t arg
) {
67 #if CF_USE_OSBYTEORDER_H
68 return OSSwapInt16(arg
);
71 result
= (uint16_t)(((arg
<< 8) & 0xFF00) | ((arg
>> 8) & 0xFF));
76 CF_INLINE
uint32_t CFSwapInt32(uint32_t arg
) {
77 #if CF_USE_OSBYTEORDER_H
78 return OSSwapInt32(arg
);
81 result
= ((arg
& 0xFF) << 24) | ((arg
& 0xFF00) << 8) | ((arg
>> 8) & 0xFF00) | ((arg
>> 24) & 0xFF);
86 CF_INLINE
uint64_t CFSwapInt64(uint64_t arg
) {
87 #if CF_USE_OSBYTEORDER_H
88 return OSSwapInt64(arg
);
95 result
.ul
[0] = CFSwapInt32(tmp
.ul
[1]);
96 result
.ul
[1] = CFSwapInt32(tmp
.ul
[0]);
101 CF_INLINE
uint16_t CFSwapInt16BigToHost(uint16_t arg
) {
102 #if CF_USE_OSBYTEORDER_H
103 return OSSwapBigToHostInt16(arg
);
107 return CFSwapInt16(arg
);
111 CF_INLINE
uint32_t CFSwapInt32BigToHost(uint32_t arg
) {
112 #if CF_USE_OSBYTEORDER_H
113 return OSSwapBigToHostInt32(arg
);
117 return CFSwapInt32(arg
);
121 CF_INLINE
uint64_t CFSwapInt64BigToHost(uint64_t arg
) {
122 #if CF_USE_OSBYTEORDER_H
123 return OSSwapBigToHostInt64(arg
);
127 return CFSwapInt64(arg
);
131 CF_INLINE
uint16_t CFSwapInt16HostToBig(uint16_t arg
) {
132 #if CF_USE_OSBYTEORDER_H
133 return OSSwapHostToBigInt16(arg
);
137 return CFSwapInt16(arg
);
141 CF_INLINE
uint32_t CFSwapInt32HostToBig(uint32_t arg
) {
142 #if CF_USE_OSBYTEORDER_H
143 return OSSwapHostToBigInt32(arg
);
147 return CFSwapInt32(arg
);
151 CF_INLINE
uint64_t CFSwapInt64HostToBig(uint64_t arg
) {
152 #if CF_USE_OSBYTEORDER_H
153 return OSSwapHostToBigInt64(arg
);
157 return CFSwapInt64(arg
);
161 CF_INLINE
uint16_t CFSwapInt16LittleToHost(uint16_t arg
) {
162 #if CF_USE_OSBYTEORDER_H
163 return OSSwapLittleToHostInt16(arg
);
164 #elif __LITTLE_ENDIAN__
167 return CFSwapInt16(arg
);
171 CF_INLINE
uint32_t CFSwapInt32LittleToHost(uint32_t arg
) {
172 #if CF_USE_OSBYTEORDER_H
173 return OSSwapLittleToHostInt32(arg
);
174 #elif __LITTLE_ENDIAN__
177 return CFSwapInt32(arg
);
181 CF_INLINE
uint64_t CFSwapInt64LittleToHost(uint64_t arg
) {
182 #if CF_USE_OSBYTEORDER_H
183 return OSSwapLittleToHostInt64(arg
);
184 #elif __LITTLE_ENDIAN__
187 return CFSwapInt64(arg
);
191 CF_INLINE
uint16_t CFSwapInt16HostToLittle(uint16_t arg
) {
192 #if CF_USE_OSBYTEORDER_H
193 return OSSwapHostToLittleInt16(arg
);
194 #elif __LITTLE_ENDIAN__
197 return CFSwapInt16(arg
);
201 CF_INLINE
uint32_t CFSwapInt32HostToLittle(uint32_t arg
) {
202 #if CF_USE_OSBYTEORDER_H
203 return OSSwapHostToLittleInt32(arg
);
204 #elif __LITTLE_ENDIAN__
207 return CFSwapInt32(arg
);
211 CF_INLINE
uint64_t CFSwapInt64HostToLittle(uint64_t arg
) {
212 #if CF_USE_OSBYTEORDER_H
213 return OSSwapHostToLittleInt64(arg
);
214 #elif __LITTLE_ENDIAN__
217 return CFSwapInt64(arg
);
221 typedef struct {uint32_t v
;} CFSwappedFloat32
;
222 typedef struct {uint64_t v
;} CFSwappedFloat64
;
224 CF_INLINE CFSwappedFloat32
CFConvertFloat32HostToSwapped(Float32 arg
) {
230 #if __LITTLE_ENDIAN__
231 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
236 CF_INLINE Float32
CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg
) {
242 #if __LITTLE_ENDIAN__
243 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
248 CF_INLINE CFSwappedFloat64
CFConvertFloat64HostToSwapped(Float64 arg
) {
254 #if __LITTLE_ENDIAN__
255 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
260 CF_INLINE Float64
CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg
) {
266 #if __LITTLE_ENDIAN__
267 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
272 CF_INLINE CFSwappedFloat32
CFConvertFloatHostToSwapped(float arg
) {
278 #if __LITTLE_ENDIAN__
279 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
284 CF_INLINE
float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg
) {
290 #if __LITTLE_ENDIAN__
291 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
296 CF_INLINE CFSwappedFloat64
CFConvertDoubleHostToSwapped(double arg
) {
302 #if __LITTLE_ENDIAN__
303 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
308 CF_INLINE
double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg
) {
314 #if __LITTLE_ENDIAN__
315 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
322 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */