libdballe 9.13
byteswap.h
1#ifndef DBALLE_CORE_BYTESWAP_H
2#define DBALLE_CORE_BYTESWAP_H
3
4#include "config.h"
5
6#if USE_OWN_BSWAP
7
8/* byteswap.h - Byte swapping
9 Copyright (C) 2005, 2007, 2009-2011 Free Software Foundation, Inc.
10 Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24
25/* Given an unsigned 16-bit argument X, return the value corresponding to
26 X with reversed byte order. */
27#define bswap_16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
28
29/* Given an unsigned 32-bit argument X, return the value corresponding to
30 X with reversed byte order. */
31#define bswap_32(x) \
32 ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | \
33 (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
34
35/* Given an unsigned 64-bit argument X, return the value corresponding to
36 X with reversed byte order. */
37#define bswap_64(x) \
38 ((((x) & 0x00000000000000FFULL) << 56) | \
39 (((x) & 0x000000000000FF00ULL) << 40) | \
40 (((x) & 0x0000000000FF0000ULL) << 24) | \
41 (((x) & 0x00000000FF000000ULL) << 8) | \
42 (((x) & 0x000000FF00000000ULL) >> 8) | \
43 (((x) & 0x0000FF0000000000ULL) >> 24) | \
44 (((x) & 0x00FF000000000000ULL) >> 40) | \
45 (((x) & 0xFF00000000000000ULL) >> 56))
46#else
47
48#include <byteswap.h>
49
50#endif
51
52#endif