GNUstep CoreBase Library 0.2
CFByteOrder.h
1/* CFByteOrder.h
2
3 Copyright (C) 2010 Free Software Foundation, Inc.
4
5 Written by: Eric Wasylishen
6 Date: June, 2010
7
8 Most of the code here was copied from NSByteOrder.h in GNUstep-base
9 written by Richard Frith-Macdonald.
10
11 This file is part of GNUstep CoreBase Library.
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2.1 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; see the file COPYING.LIB.
25 If not, see <http://www.gnu.org/licenses/> or write to the
26 Free Software Foundation, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA.
28*/
29
30#ifndef __COREFOUNDATION_CFBYTEORDER_H__
31#define __COREFOUNDATION_CFBYTEORDER_H__ 1
32
33#include <CoreFoundation/CFBase.h>
34
53typedef UInt32 CFSwappedFloat32; /* Same as GNUstep NSSwappedFloat */
54typedef UInt64 CFSwappedFloat64; /* Same as GNUstep NSSwappedDouble */
55
63CF_INLINE CFByteOrder
65{
66#if __BIG_ENDIAN__
68#else
70#endif
71}
74CF_INLINE UInt16
75CFSwapInt16(UInt16 in)
76{
77 union swap
78 {
79 UInt16 num;
80 UInt8 byt[2];
81 } dst;
82 union swap *src = (union swap*)&in;
83 dst.byt[0] = src->byt[1];
84 dst.byt[1] = src->byt[0];
85 return dst.num;
86}
87
88CF_INLINE UInt32
89CFSwapInt32(UInt32 in)
90{
91 union swap
92 {
93 UInt32 num;
94 UInt8 byt[4];
95 } dst;
96 union swap *src = (union swap*)&in;
97 dst.byt[0] = src->byt[3];
98 dst.byt[1] = src->byt[2];
99 dst.byt[2] = src->byt[1];
100 dst.byt[3] = src->byt[0];
101 return dst.num;
102}
103
104CF_INLINE UInt64
105CFSwapInt64(UInt64 in)
106{
107 union swap
108 {
109 UInt64 num;
110 UInt8 byt[8];
111 } dst;
112 union swap *src = (union swap*)&in;
113 dst.byt[0] = src->byt[7];
114 dst.byt[1] = src->byt[6];
115 dst.byt[2] = src->byt[5];
116 dst.byt[3] = src->byt[4];
117 dst.byt[4] = src->byt[3];
118 dst.byt[5] = src->byt[2];
119 dst.byt[6] = src->byt[1];
120 dst.byt[7] = src->byt[0];
121 return dst.num;
122}
123
124#if __BIG_ENDIAN__
125
126CF_INLINE UInt16
127CFSwapInt16BigToHost(UInt16 in)
128{
129 return in;
130}
131
132CF_INLINE UInt16
133CFSwapInt16HostToBig(UInt16 in)
134{
135 return in;
136}
137CF_INLINE UInt16
138CFSwapInt16HostToLittle(UInt16 in)
139{
140 return CFSwapInt16(in);
141}
142
143CF_INLINE UInt16
144CFSwapInt16LittleToHost(UInt16 in)
145{
146 return CFSwapInt16(in);
147}
148
149CF_INLINE UInt32
150CFSwapInt32BigToHost(UInt32 in)
151{
152 return in;
153}
154
155CF_INLINE UInt32
156CFSwapInt32HostToBig(UInt32 in)
157{
158 return in;
159}
160
161CF_INLINE UInt32
162CFSwapInt32HostToLittle(UInt32 in)
163{
164 return CFSwapInt32(in);
165}
166
167CF_INLINE UInt32
168CFSwapInt32LittleToHost(UInt32 in)
169{
170 return CFSwapInt32(in);
171}
172
173CF_INLINE UInt64
174CFSwapInt64BigToHost(UInt64 in)
175{
176 return in;
177}
178
179CF_INLINE UInt64
180CFSwapInt64HostToBig(UInt64 in)
181{
182 return in;
183}
184
185CF_INLINE UInt64
186CFSwapInt64HostToLittle(UInt64 in)
187{
188 return CFSwapInt64(in);
189}
190
191CF_INLINE UInt64
192CFSwapInt64LittleToHost(UInt64 in)
193{
194 return CFSwapInt64(in);
195}
196
197#else
198
199CF_INLINE UInt16
200CFSwapInt16BigToHost(UInt16 in)
201{
202 return CFSwapInt16(in);
203}
204
205CF_INLINE UInt16
206CFSwapInt16HostToBig(UInt16 in)
207{
208 return CFSwapInt16(in);
209}
210CF_INLINE UInt16
211CFSwapInt16HostToLittle(UInt16 in)
212{
213 return in;
214}
215
216CF_INLINE UInt16
217CFSwapInt16LittleToHost(UInt16 in)
218{
219 return in;
220}
221
222CF_INLINE UInt32
223CFSwapInt32BigToHost(UInt32 in)
224{
225 return CFSwapInt32(in);
226}
227
228CF_INLINE UInt32
229CFSwapInt32HostToBig(UInt32 in)
230{
231 return CFSwapInt32(in);
232}
233
234CF_INLINE UInt32
235CFSwapInt32HostToLittle(UInt32 in)
236{
237 return in;
238}
239
240CF_INLINE UInt32
241CFSwapInt32LittleToHost(UInt32 in)
242{
243 return in;
244}
245
246CF_INLINE UInt64
247CFSwapInt64BigToHost(UInt64 in)
248{
249 return CFSwapInt64(in);
250}
251
252CF_INLINE UInt64
253CFSwapInt64HostToBig(UInt64 in)
254{
255 return CFSwapInt64(in);
256}
257
258CF_INLINE UInt64
259CFSwapInt64HostToLittle(UInt64 in)
260{
261 return in;
262}
263
264CF_INLINE UInt64
265CFSwapInt64LittleToHost(UInt64 in)
266{
267 return in;
268}
269
270#endif
271
272union dconv
273{
274 double d;
275 Float64 num;
276 CFSwappedFloat64 sf;
277};
278
279union fconv
280{
281 float f;
282 Float32 num;
283 CFSwappedFloat32 sf;
284};
285
289CF_INLINE CFSwappedFloat64
290CFConvertFloat64HostToSwapped (Float64 in)
291{
292 union dconv conv;
293 conv.num = in;
294 return CFSwapInt64 (conv.sf);
295}
296
297CF_INLINE Float64
298CFConvertFloat64SwappedToHost (CFSwappedFloat64 in)
299{
300 union dconv conv;
301 conv.sf = CFSwapInt64 (in);
302 return conv.num;
303}
304
305CF_INLINE CFSwappedFloat64
306CFConvertDoubleHostToSwapped (double in)
307{
308 union dconv conv;
309 conv.d = in;
310 return CFSwapInt64 (conv.sf);
311}
312
313CF_INLINE double
314CFConvertDoubleSwappedToHost(CFSwappedFloat64 in)
315{
316 union dconv conv;
317 conv.sf = CFSwapInt64 (in);
318 return conv.d;
319}
320
321CF_INLINE CFSwappedFloat32
322CFConvertFloat32HostToSwapped(Float32 in)
323{
324 union fconv conv;
325 conv.num = in;
326 return CFSwapInt32 (conv.sf);
327}
328
329CF_INLINE Float32
330CFConvertFloat32SwappedToHost(CFSwappedFloat32 in)
331{
332 union fconv conv;
333 conv.sf = CFSwapInt32 (in);
334 return conv.num;
335}
336
337CF_INLINE CFSwappedFloat32
338CFConvertFloatHostToSwapped(float in)
339{
340 union fconv conv;
341 conv.f = in;
342 return CFSwapInt32 (conv.sf);
343}
344
345CF_INLINE float
346CFConvertFloatSwappedToHost(CFSwappedFloat32 in)
347{
348 union fconv conv;
349 conv.sf = CFSwapInt32 (in);
350 return conv.f;
351}
356#endif /* __COREFOUNDATION_CFBYTEORDER_H__ */
357
CFByteOrder
Definition CFByteOrder.h:44
CFByteOrder CFByteOrderGetCurrent()
Definition CFByteOrder.h:64
@ CFByteOrderUnknown
Definition CFByteOrder.h:45
@ CFByteOrderBigEndian
Definition CFByteOrder.h:48
@ CFByteOrderLittleEndian
Definition CFByteOrder.h:46
Definition CFByteOrder.h:273
Definition CFByteOrder.h:280