]> icculus.org git repositories - icculus/xz.git/blob - src/common/bswap.h
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / common / bswap.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       bswap.h
4 /// \brief      Byte swapping
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef LZMA_BSWAP_H
14 #define LZMA_BSWAP_H
15
16 // NOTE: We assume that config.h is already #included.
17
18 // At least glibc has byteswap.h which contains inline assembly code for
19 // byteswapping. Some systems have byteswap.h but lack one or more of the
20 // bswap_xx macros/functions, which is why we check them separately even
21 // if byteswap.h is available.
22
23 #ifdef HAVE_BYTESWAP_H
24 #       include <byteswap.h>
25 #endif
26
27 #ifndef HAVE_BSWAP_16
28 #       define bswap_16(num) \
29                 (((num) << 8) | ((num) >> 8))
30 #endif
31
32 #ifndef HAVE_BSWAP_32
33 #       define bswap_32(num) \
34                 ( (((num) << 24)                       ) \
35                 | (((num) <<  8) & UINT32_C(0x00FF0000)) \
36                 | (((num) >>  8) & UINT32_C(0x0000FF00)) \
37                 | (((num) >> 24)                       ) )
38 #endif
39
40 #ifndef HAVE_BSWAP_64
41 #       define bswap_64(num) \
42                 ( (((num) << 56)                               ) \
43                 | (((num) << 40) & UINT64_C(0x00FF000000000000)) \
44                 | (((num) << 24) & UINT64_C(0x0000FF0000000000)) \
45                 | (((num) <<  8) & UINT64_C(0x000000FF00000000)) \
46                 | (((num) >>  8) & UINT64_C(0x00000000FF000000)) \
47                 | (((num) >> 24) & UINT64_C(0x0000000000FF0000)) \
48                 | (((num) >> 40) & UINT64_C(0x000000000000FF00)) \
49                 | (((num) >> 56)                               ) )
50 #endif
51
52 #endif