]> icculus.org git repositories - icculus/xz.git/blob - src/common/bswap.h
Move some LZMA2 constants to lzma2_encoder.h so that they
[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 // At least glibc has byteswap.h which contains inline assembly code for
20 // byteswapping. Some systems have byteswap.h but lack one or more of the
21 // bswap_xx macros/functions, which is why we check them separately even
22 // if byteswap.h is available.
23
24 #ifdef HAVE_BYTESWAP_H
25 #       include <byteswap.h>
26 #endif
27
28 #ifndef HAVE_BSWAP_16
29 #       define bswap_16(num) \
30                 (((num) << 8) | ((num) >> 8))
31 #endif
32
33 #ifndef HAVE_BSWAP_32
34 #       define bswap_32(num) \
35                 ( (((num) << 24)                       ) \
36                 | (((num) <<  8) & UINT32_C(0x00FF0000)) \
37                 | (((num) >>  8) & UINT32_C(0x0000FF00)) \
38                 | (((num) >> 24)                       ) )
39 #endif
40
41 #ifndef HAVE_BSWAP_64
42 #       define bswap_64(num) \
43                 ( (((num) << 56)                               ) \
44                 | (((num) << 40) & UINT64_C(0x00FF000000000000)) \
45                 | (((num) << 24) & UINT64_C(0x0000FF0000000000)) \
46                 | (((num) <<  8) & UINT64_C(0x000000FF00000000)) \
47                 | (((num) >>  8) & UINT64_C(0x00000000FF000000)) \
48                 | (((num) >> 24) & UINT64_C(0x0000000000FF0000)) \
49                 | (((num) >> 40) & UINT64_C(0x000000000000FF00)) \
50                 | (((num) >> 56)                               ) )
51 #endif
52
53 #endif