1
#ifndef _KVI_BYTEORDER_H_
2
#define _KVI_BYTEORDER_H_
4
//=============================================================================
6
// File : KviByteOrder.h
7
// Creation date : Mon Dec 25 2006 19:56:16 CEST by Szymon Stefanek
8
// Based on file : kvi_bswap.h
9
// Creation date : Fri Mar 19 1999 03:15:21 CEST by Szymon Stefanek
11
// This file is part of the KVIrc irc client distribution
12
// Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
14
// This program is FREE software. You can redistribute it and/or
15
// modify it under the terms of the GNU General Public License
16
// as published by the Free Software Foundation; either version 2
17
// of the License, or (at your opinion) any later version.
19
// This program is distributed in the HOPE that it will be USEFUL,
20
// but WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
// See the GNU General Public License for more details.
24
// You should have received a copy of the GNU General Public License
25
// along with this program. If not, write to the Free Software Foundation,
26
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28
//=============================================================================
30
#include "kvi_settings.h"
31
#include "kvi_inttypes.h"
33
namespace KviByteOrder
36
* \brief Swaps the endianess of a kvi_u64_t
38
* \param i the original value
41
inline kvi_u64_t swap64(kvi_u64_t i)
43
// abcdefgh to hgfedcba
44
return ((i << 56) | /* h to a */
45
((i & 0xff00) << 40) | /* g to b */
46
((i & 0xff0000) << 24) | /* f to c */
47
((i & 0xff000000) << 8) | /* e to d */
48
((i >> 8) & 0xff000000) | /* d to e */
49
((i >> 24) & 0xff0000) | /* c to f */
50
((i >> 40) & 0xff00) | /* b to g */
51
(i >> 56)); /* a to h */
55
* \brief Swaps the endianess of a kvi_u32_t
57
* \param i the original value
60
inline kvi_u32_t swap32(kvi_u32_t i)
63
return ((i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24));
67
* \brief Swaps the endianess of a kvi_u16_t
69
* \param i the original value
72
inline kvi_u16_t swap16(kvi_u16_t i)
75
return ((i << 8) | (i >> 8));
80
// Byte Orders Reminder
82
// Little Endian Stores 0xdd 0xcc 0xbb 0xaa
83
// Big Endian Stores 0xaa 0xbb 0xcc 0xdd
84
// Perverse Middle Endian 0xbb 0xaa 0xdd 0xcc or another braindamaged combination (unsupported)
85
// Network Byte Order is Big Endian
86
// Intel Stuff uses Little Endian
88
// Universal binaries on MacOSX use both Big and Little Endian
91
#ifdef BIG_ENDIAN_MACHINE_BYTE_ORDER
93
inline kvi_u16_t localCpuToLittleEndian16(kvi_u16_t u)
98
inline kvi_u32_t localCpuToLittleEndian32(kvi_u32_t u)
103
inline kvi_u64_t localCpuToLittleEndian64(kvi_u64_t u)
108
inline kvi_u16_t littleEndianToLocalCpu16(kvi_u16_t u)
113
inline kvi_u32_t littleEndianToLocalCpu32(kvi_u32_t u)
118
inline kvi_u64_t littleEndianToLocalCpu64(kvi_u64_t u)
123
#else //!BIG_ENDIAN_MACHINE_BYTE_ORDER
125
// We ASSUME that the local cpu is little endian.. if it isn't.. well :)
126
#define LOCAL_CPU_LITTLE_ENDIAN 1
128
inline kvi_u16_t localCpuToLittleEndian16(kvi_u16_t u)
133
inline kvi_u32_t localCpuToLittleEndian32(kvi_u32_t u)
138
inline kvi_u64_t localCpuToLittleEndian64(kvi_u64_t u)
143
inline kvi_u16_t littleEndianToLocalCpu16(kvi_u16_t u)
148
inline kvi_u32_t littleEndianToLocalCpu32(kvi_u32_t u)
153
inline kvi_u64_t littleEndianToLocalCpu64(kvi_u64_t u)
158
#endif //!BIG_ENDIAN_MACHINE_BYTE_ORDER
160
} // namespace KviByteOrder
168
#endif // !_KVI_BYTEORDER_H_