]> icculus.org git repositories - icculus/xz.git/blob - src/common/bswap.h
8f82a8f4e10e82b56aa17eac29e5f495b73bcf2b
[icculus/xz.git] / src / common / bswap.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       bswap.h
4 /// \brief      Byte swapping
5 //
6 //  This code has been put into the public domain.
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.
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #ifndef LZMA_BSWAP_H
15 #define LZMA_BSWAP_H
16
17 // NOTE: We assume that config.h is already #included.
18
19 // byteswap.h is a GNU extension. It contains inline assembly versions
20 // for byteswapping. When byteswap.h is not available, we use generic code.
21 #ifdef HAVE_BYTESWAP_H
22 #       include <byteswap.h>
23 #else
24 #       define bswap_16(num) \
25                 (((num) << 8) | ((num) >> 8))
26
27 #       define bswap_32(num) \
28                 ( (((num) << 24)                       ) \
29                 | (((num) <<  8) & UINT32_C(0x00FF0000)) \
30                 | (((num) >>  8) & UINT32_C(0x0000FF00)) \
31                 | (((num) >> 24)                       ) )
32
33 #       define bswap_64(num) \
34                 ( (((num) << 56)                               ) \
35                 | (((num) << 40) & UINT64_C(0x00FF000000000000)) \
36                 | (((num) << 24) & UINT64_C(0x0000FF0000000000)) \
37                 | (((num) <<  8) & UINT64_C(0x000000FF00000000)) \
38                 | (((num) >>  8) & UINT64_C(0x00000000FF000000)) \
39                 | (((num) >> 24) & UINT64_C(0x0000000000FF0000)) \
40                 | (((num) >> 40) & UINT64_C(0x000000000000FF00)) \
41                 | (((num) >> 56)                               ) )
42 #endif
43
44 #endif