1 /* byteswap.h - Byte swapping
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA. */
21 /* Given an unsigned 16-bit argument X, return the value corresponding to
22 X with reversed byte order. */
23 #define bswap_16(x) ((((x) & 0x00FF) << 8) | \
24 (((x) & 0xFF00) >> 8))
26 /* Given an unsigned 32-bit argument X, return the value corresponding to
27 X with reversed byte order. */
28 #define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
29 (((x) & 0x0000FF00) << 8) | \
30 (((x) & 0x00FF0000) << 8) | \
31 (((x) & 0xFF000000) >> 24))
33 /* Given an unsigned 64-bit argument X, return the value corresponding to
34 X with reversed byte order. */
35 #define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
36 (((x) & 0x000000000000FF00ULL) << 40) | \
37 (((x) & 0x0000000000FF0000ULL) << 24) | \
38 (((x) & 0x00000000FF000000ULL) << 8) | \
39 (((x) & 0x000000FF00000000ULL) >> 8) | \
40 (((x) & 0x0000FF0000000000ULL) >> 24) | \
41 (((x) & 0x00FF000000000000ULL) >> 40) | \
42 (((x) & 0xFF00000000000000ULL) >> 56))