2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 Copyright (c) 1995-2003, Apple, Inc. All rights reserved.
29 #if !defined(__COREFOUNDATION_CFBYTEORDER__)
30 #define __COREFOUNDATION_CFBYTEORDER__ 1
32 #if defined(__i386) && !defined(__LITTLE_ENDIAN__)
33 #define __LITTLE_ENDIAN__ 1
36 #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
37 #error Do not know the endianess of this architecture
40 #include <CoreFoundation/CFBase.h>
42 #if defined(__cplusplus)
46 typedef enum __CFByteOrder
{
48 CFByteOrderLittleEndian
,
52 CF_INLINE CFByteOrder
CFByteOrderGetCurrent(void) {
53 uint32_t x
= (CFByteOrderBigEndian
<< 24) | CFByteOrderLittleEndian
;
54 return (CFByteOrder
)*((uint8_t *)&x
);
57 CF_INLINE
uint16_t CFSwapInt16(uint16_t arg
) {
58 #if defined(__i386__) && defined(__GNUC__)
59 __asm__("xchgb %b0, %h0" : "+q" (arg
));
61 #elif defined(__ppc__) && defined(__GNUC__)
63 __asm__("lhbrx %0,0,%1" : "=r" (result
) : "r" (&arg
), "m" (arg
));
67 result
= ((arg
<< 8) & 0xFF00) | ((arg
>> 8) & 0xFF);
72 CF_INLINE
uint32_t CFSwapInt32(uint32_t arg
) {
73 #if defined(__i386__) && defined(__GNUC__)
74 __asm__("bswap %0" : "+r" (arg
));
76 #elif defined(__ppc__) && defined(__GNUC__)
78 __asm__("lwbrx %0,0,%1" : "=r" (result
) : "r" (&arg
), "m" (arg
));
82 result
= ((arg
& 0xFF) << 24) | ((arg
& 0xFF00) << 8) | ((arg
>> 8) & 0xFF00) | ((arg
>> 24) & 0xFF);
87 CF_INLINE
uint64_t CFSwapInt64(uint64_t arg
) {
93 result
.ul
[0] = CFSwapInt32(tmp
.ul
[1]);
94 result
.ul
[1] = CFSwapInt32(tmp
.ul
[0]);
98 CF_INLINE
uint16_t CFSwapInt16BigToHost(uint16_t arg
) {
99 #if defined(__BIG_ENDIAN__)
102 return CFSwapInt16(arg
);
106 CF_INLINE
uint32_t CFSwapInt32BigToHost(uint32_t arg
) {
107 #if defined(__BIG_ENDIAN__)
110 return CFSwapInt32(arg
);
114 CF_INLINE
uint64_t CFSwapInt64BigToHost(uint64_t arg
) {
115 #if defined(__BIG_ENDIAN__)
118 return CFSwapInt64(arg
);
122 CF_INLINE
uint16_t CFSwapInt16HostToBig(uint16_t arg
) {
123 #if defined(__BIG_ENDIAN__)
126 return CFSwapInt16(arg
);
130 CF_INLINE
uint32_t CFSwapInt32HostToBig(uint32_t arg
) {
131 #if defined(__BIG_ENDIAN__)
134 return CFSwapInt32(arg
);
138 CF_INLINE
uint64_t CFSwapInt64HostToBig(uint64_t arg
) {
139 #if defined(__BIG_ENDIAN__)
142 return CFSwapInt64(arg
);
146 CF_INLINE
uint16_t CFSwapInt16LittleToHost(uint16_t arg
) {
147 #if defined(__LITTLE_ENDIAN__)
150 return CFSwapInt16(arg
);
154 CF_INLINE
uint32_t CFSwapInt32LittleToHost(uint32_t arg
) {
155 #if defined(__LITTLE_ENDIAN__)
158 return CFSwapInt32(arg
);
162 CF_INLINE
uint64_t CFSwapInt64LittleToHost(uint64_t arg
) {
163 #if defined(__LITTLE_ENDIAN__)
166 return CFSwapInt64(arg
);
170 CF_INLINE
uint16_t CFSwapInt16HostToLittle(uint16_t arg
) {
171 #if defined(__LITTLE_ENDIAN__)
174 return CFSwapInt16(arg
);
178 CF_INLINE
uint32_t CFSwapInt32HostToLittle(uint32_t arg
) {
179 #if defined(__LITTLE_ENDIAN__)
182 return CFSwapInt32(arg
);
186 CF_INLINE
uint64_t CFSwapInt64HostToLittle(uint64_t arg
) {
187 #if defined(__LITTLE_ENDIAN__)
190 return CFSwapInt64(arg
);
194 typedef struct {uint32_t v
;} CFSwappedFloat32
;
195 typedef struct {uint64_t v
;} CFSwappedFloat64
;
197 CF_INLINE CFSwappedFloat32
CFConvertFloat32HostToSwapped(Float32 arg
) {
203 #if defined(__LITTLE_ENDIAN__)
204 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
209 CF_INLINE Float32
CFConvertFloat32SwappedToHost(CFSwappedFloat32 arg
) {
215 #if defined(__LITTLE_ENDIAN__)
216 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
221 CF_INLINE CFSwappedFloat64
CFConvertFloat64HostToSwapped(Float64 arg
) {
227 #if defined(__LITTLE_ENDIAN__)
228 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
233 CF_INLINE Float64
CFConvertFloat64SwappedToHost(CFSwappedFloat64 arg
) {
239 #if defined(__LITTLE_ENDIAN__)
240 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
245 CF_INLINE CFSwappedFloat32
CFConvertFloatHostToSwapped(float arg
) {
251 #if defined(__LITTLE_ENDIAN__)
252 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
257 CF_INLINE
float CFConvertFloatSwappedToHost(CFSwappedFloat32 arg
) {
263 #if defined(__LITTLE_ENDIAN__)
264 result
.sv
.v
= CFSwapInt32(result
.sv
.v
);
269 CF_INLINE CFSwappedFloat64
CFConvertDoubleHostToSwapped(double arg
) {
275 #if defined(__LITTLE_ENDIAN__)
276 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
281 CF_INLINE
double CFConvertDoubleSwappedToHost(CFSwappedFloat64 arg
) {
287 #if defined(__LITTLE_ENDIAN__)
288 result
.sv
.v
= CFSwapInt64(result
.sv
.v
);
293 #if defined(__cplusplus)
297 #endif /* ! __COREFOUNDATION_CFBYTEORDER__ */