1 /* This file is part of the wvWare 2 project
2    Copyright (C) 2002-2003 Werner Trobin <trobin@kde.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License version 2 as published by the Free Software Foundation.
7 
8    This library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Library General Public License for more details.
12 
13    You should have received a copy of the GNU Library General Public License
14    along with this library; see the file COPYING.LIB.  If not, write to
15    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16    Boston, MA 02111-1307, USA.
17 */
18 
19 #include "global.h"
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 namespace wvWare {
26 
27 // Endianness fun
toLittleEndian(U16 data)28 U16 toLittleEndian( U16 data )
29 {
30 #if defined(WORDS_BIGENDIAN)
31     return ( ( data & 0x00ffU ) << 8 ) | ( ( data & 0xff00U ) >> 8 );
32 #else
33     return data;
34 #endif
35 }
36 
toBigEndian(U16 data)37 U16 toBigEndian( U16 data )
38 {
39 #if defined(WORDS_BIGENDIAN)
40     return data;
41 #else
42     return ( ( data & 0x00ffU ) << 8 ) | ( ( data & 0xff00U ) >> 8 );
43 #endif
44 }
45 
toLittleEndian(U32 data)46 U32 toLittleEndian( U32 data )
47 {
48 #if defined(WORDS_BIGENDIAN)
49     return ( ( data & 0x000000ffU ) << 24 ) | ( ( data & 0x0000ff00U ) <<  8 ) |
50         ( ( data & 0x00ff0000U ) >>  8 ) | ( ( data & 0xff000000U ) >> 24 );
51 #else
52     return data;
53 #endif
54 }
55 
toBigEndian(U32 data)56 U32 toBigEndian( U32 data )
57 {
58 #if defined(WORDS_BIGENDIAN)
59     return data;
60 #else
61     return ( ( data & 0x000000ffU ) << 24 ) | ( ( data & 0x0000ff00U ) <<  8 ) |
62         ( ( data & 0x00ff0000U ) >>  8 ) | ( ( data & 0xff000000U ) >> 24 );
63 #endif
64 }
65 
66 } // namespace wvWare
67