Initial commit.
[libsalac.git] / src / lib / alac / codec / EndianPortable.c
blob5a7d5b89b30d1023e0d9209eb26be333adc4d204
1 /*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
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@
22 // EndianPortable.c
24 // Copyright 2011 Apple Inc. All rights reserved.
27 #include <stdio.h>
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)))
37 #if defined(__i386__)
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
43 #endif
45 uint16_t Swap16NtoB(uint16_t inUInt16)
47 #if TARGET_RT_LITTLE_ENDIAN
48 return BSWAP16(inUInt16);
49 #else
50 return inUInt16;
51 #endif
54 uint16_t Swap16BtoN(uint16_t inUInt16)
56 #if TARGET_RT_LITTLE_ENDIAN
57 return BSWAP16(inUInt16);
58 #else
59 return inUInt16;
60 #endif
63 uint32_t Swap32NtoB(uint32_t inUInt32)
65 #if TARGET_RT_LITTLE_ENDIAN
66 return BSWAP32(inUInt32);
67 #else
68 return inUInt32;
69 #endif
72 uint32_t Swap32BtoN(uint32_t inUInt32)
74 #if TARGET_RT_LITTLE_ENDIAN
75 return BSWAP32(inUInt32);
76 #else
77 return inUInt32;
78 #endif
81 uint64_t Swap64BtoN(uint64_t inUInt64)
83 #if TARGET_RT_LITTLE_ENDIAN
84 return BSWAP64(inUInt64);
85 #else
86 return inUInt64;
87 #endif
90 uint64_t Swap64NtoB(uint64_t inUInt64)
92 #if TARGET_RT_LITTLE_ENDIAN
93 return BSWAP64(inUInt64);
94 #else
95 return inUInt64;
96 #endif
99 float SwapFloat32BtoN(float in)
101 #if TARGET_RT_LITTLE_ENDIAN
102 union {
103 float f;
104 int32_t i;
105 } x;
106 x.f = in;
107 x.i = BSWAP32(x.i);
108 return x.f;
109 #else
110 return in;
111 #endif
114 float SwapFloat32NtoB(float in)
116 #if TARGET_RT_LITTLE_ENDIAN
117 union {
118 float f;
119 int32_t i;
120 } x;
121 x.f = in;
122 x.i = BSWAP32(x.i);
123 return x.f;
124 #else
125 return in;
126 #endif
129 double SwapFloat64BtoN(double in)
131 #if TARGET_RT_LITTLE_ENDIAN
132 union {
133 double f;
134 int64_t i;
135 } x;
136 x.f = in;
137 x.i = BSWAP64(x.i);
138 return x.f;
139 #else
140 return in;
141 #endif
144 double SwapFloat64NtoB(double in)
146 #if TARGET_RT_LITTLE_ENDIAN
147 union {
148 double f;
149 int64_t i;
150 } x;
151 x.f = in;
152 x.i = BSWAP64(x.i);
153 return x.f;
154 #else
155 return in;
156 #endif
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);