2 * Copyright (c) 2005 Apple Computer, 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-2005, Apple, Inc. All rights reserved.
27 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
28 #define __COREFOUNDATION_CFBYTEORDER__ 1
30 #if defined(__i386__) && !defined(__LITTLE_ENDIAN__)
31 #define __LITTLE_ENDIAN__ 1
34 #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
35 #error Do not know the endianess of this architecture
38 #include <CoreFoundation/CFBase.h>
40 #if defined(__cplusplus)
44 typedef enum __CFByteOrder
{
46 CFByteOrderLittleEndian
,
50 CF_INLINE CFByteOrder
CFByteOrderGetCurrent(void) {
51 uint32_t x
= (CFByteOrderBigEndian
<< 24) | CFByteOrderLittleEndian
;
52 return (CFByteOrder
)*((uint8_t *)&x
);
55 CF_INLINE
uint16_t CFSwapInt16(uint16_t arg
) {
56 #if defined(__i386__) && defined(__GNUC__)
57 __asm__("xchgb %b0, %h0" : "+q" (arg
));
59 #elif defined(__ppc__) && defined(__GNUC__)
61 __asm__("lhbrx %0,0,%1" : "=r" (result
) : "r" (&arg
), "m" (arg
));
65 result
= ((arg
<< 8) & 0xFF00) | ((arg
>> 8) & 0xFF);
70 CF_INLINE
uint32_t CFSwapInt32(uint32_t arg
) {
71 #if defined(__i386__) && defined(__GNUC__)
72 __asm__("bswap %0" : "+r" (arg
));
74 #elif defined(__ppc__) && defined(__GNUC__)
76 __asm__("lwbrx %0,0,%1" : "=r" (result
) : "r" (&arg
), "m" (arg
));
80 result
= ((arg
& 0xFF) << 24) | ((arg
& 0xFF00) << 8) | ((arg
>> 8) & 0xFF00) | ((arg
>> 24) & 0xFF);
85 CF_INLINE
uint64_t CFSwapInt64(uint64_t arg
) {
91 result
.ul
[0] = CFSwapInt32(tmp
.ul
[1]);
92 result
.ul
[1] = CFSwapInt32(tmp
.ul
[0]);
96 CF_INLINE
uint16_t CFSwapInt16BigToHost(uint16_t arg
) {
97 #if defined(__BIG_ENDIAN__)
100 return CFSwapInt16(arg
);
104 CF_INLINE
uint32_t CFSwapInt32BigToHost(uint32_t arg
) {
105 #if defined(__BIG_ENDIAN__)
108 return CFSwapInt32(arg
);
112 CF_INLINE
uint64_t CFSwapInt64BigToHost(uint64_t arg
) {
113 #if defined(__BIG_ENDIAN__)
116 return CFSwapInt64(arg
);
120 CF_INLINE
uint16_t CFSwapInt16HostToBig(uint16_t arg
) {
121 #if defined(__BIG_ENDIAN__)
124 return CFSwapInt16(arg
);
128 CF_INLINE
uint32_t CFSwapInt32HostToBig(uint32_t arg
) {
129 #if defined(__BIG_ENDIAN__)
132 return CFSwapInt32(arg
);
136 CF_INLINE
uint64_t CFSwapInt64HostToBig(uint64_t arg
) {
137 #if defined(__BIG_ENDIAN__)
140 return CFSwapInt64(arg
);
144 CF_INLINE
uint16_t CFSwapInt16LittleToHost(uint16_t arg
) {
145 #if defined(__LITTLE_ENDIAN__)
148 return CFSwapInt16(arg
);
152 CF_INLINE
uint32_t CFSwapInt32LittleToHost(uint32_t arg
) {
153 #if defined(__LITTLE_ENDIAN__)
156 return CFSwapInt32(arg
);
160 CF_INLINE
uint64_t CFSwapInt64LittleToHost(uint64_t arg
) {
161 #if defined(__LITTLE_ENDIAN__)
164 return CFSwapInt64(arg
);
168 CF_INLINE
uint16_t CFSwapInt16HostToLittle(uint16_t arg
) {
169 #if defined(__LITTLE_ENDIAN__)
172 return CFSwapInt16(arg
);
176 CF_INLINE
uint32_t CFSwapInt32HostToLittle(uint32_t arg
) {
177 #if defined(__LITTLE_ENDIAN__)
180 return CFSwapInt32(arg
);
184 CF_INLINE
uint64_t CFSwapInt64HostToLittle(uint64_t arg
) {
185 #if defined(__LITTLE_ENDIAN__)
188 return CFSwapInt64(arg
);
192 typedef struct {uint32_t v
;} CFSwappedFloat32
;
193 typedef struct {uint64_t v
;} CFSwappedFloat64
;
195 CF_INLINE CFSwappedFloat32
CFConvertFloat32HostToSwapped(Float32 arg
) {
201 #if defined(__LITTLE_ENDIAN__)
202 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
207 CF_INLINE Float32
CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg
) {
213 #if defined(__LITTLE_ENDIAN__)
214 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
219 CF_INLINE CFSwappedFloat64
CFConvertFloat64HostToSwapped(Float64 arg
) {
225 #if defined(__LITTLE_ENDIAN__)
226 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
231 CF_INLINE Float64
CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg
) {
237 #if defined(__LITTLE_ENDIAN__)
238 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
243 CF_INLINE CFSwappedFloat32
CFConvertFloatHostToSwapped(float arg
) {
249 #if defined(__LITTLE_ENDIAN__)
250 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
255 CF_INLINE
float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg
) {
261 #if defined(__LITTLE_ENDIAN__)
262 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
267 CF_INLINE CFSwappedFloat64
CFConvertDoubleHostToSwapped(double arg
) {
273 #if defined(__LITTLE_ENDIAN__)
274 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
279 CF_INLINE
double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg
) {
285 #if defined(__LITTLE_ENDIAN__)
286 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
291 #if defined(__cplusplus)
295 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */