2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
24 // Copyright 2011 Apple Inc. All rights reserved.
28 #include "EndianPortable.h"
30 #define BSWAP16(x) (((x << 8) | ((x >> 8) & 0x00ff)))
31 #define BSWAP32(x) (((x << 24) | ((x << 8) & 0x00ff0000) | ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff)))
32 #define BSWAP64(x) ((((int64_t)x << 56) | (((int64_t)x << 40) & 0x00ff000000000000LL) | \
33 (((int64_t)x << 24) & 0x0000ff0000000000LL) | (((int64_t)x << 8) & 0x000000ff00000000LL) | \
34 (((int64_t)x >> 8) & 0x00000000ff000000LL) | (((int64_t)x >> 24) & 0x0000000000ff0000LL) | \
35 (((int64_t)x >> 40) & 0x000000000000ff00LL) | (((int64_t)x >> 56) & 0x00000000000000ffLL)))
38 #define TARGET_RT_LITTLE_ENDIAN 1
39 #elif defined(__x86_64__)
40 #define TARGET_RT_LITTLE_ENDIAN 1
41 #elif defined (TARGET_OS_WIN32)
42 #define TARGET_RT_LITTLE_ENDIAN 1
45 uint16_t Swap16NtoB(uint16_t inUInt16
)
47 #if TARGET_RT_LITTLE_ENDIAN
48 return BSWAP16(inUInt16
);
54 uint16_t Swap16BtoN(uint16_t inUInt16
)
56 #if TARGET_RT_LITTLE_ENDIAN
57 return BSWAP16(inUInt16
);
63 uint32_t Swap32NtoB(uint32_t inUInt32
)
65 #if TARGET_RT_LITTLE_ENDIAN
66 return BSWAP32(inUInt32
);
72 uint32_t Swap32BtoN(uint32_t inUInt32
)
74 #if TARGET_RT_LITTLE_ENDIAN
75 return BSWAP32(inUInt32
);
81 uint64_t Swap64BtoN(uint64_t inUInt64
)
83 #if TARGET_RT_LITTLE_ENDIAN
84 return BSWAP64(inUInt64
);
90 uint64_t Swap64NtoB(uint64_t inUInt64
)
92 #if TARGET_RT_LITTLE_ENDIAN
93 return BSWAP64(inUInt64
);
99 float SwapFloat32BtoN(float in
)
101 #if TARGET_RT_LITTLE_ENDIAN
114 float SwapFloat32NtoB(float in
)
116 #if TARGET_RT_LITTLE_ENDIAN
129 double SwapFloat64BtoN(double in
)
131 #if TARGET_RT_LITTLE_ENDIAN
144 double SwapFloat64NtoB(double in
)
146 #if TARGET_RT_LITTLE_ENDIAN
159 void Swap16(uint16_t * inUInt16
)
161 *inUInt16
= BSWAP16(*inUInt16
);
164 void Swap24(uint8_t * inUInt24
)
166 uint8_t tempVal
= inUInt24
[0];
167 inUInt24
[0] = inUInt24
[2];
168 inUInt24
[2] = tempVal
;
171 void Swap32(uint32_t * inUInt32
)
173 *inUInt32
= BSWAP32(*inUInt32
);