2 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
3 * HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifndef BSWAP_H_INCLUDED
21 #define BSWAP_H_INCLUDED
25 #if defined(WORDS_BIGENDIAN)
26 /* All bigendian systems are fine, just ignore the swaps. */
27 #define B2N_16(x) (void)(x)
28 #define B2N_32(x) (void)(x)
29 #define B2N_64(x) (void)(x)
33 #if defined(__linux__)
35 #define B2N_16(x) x = bswap_16(x)
36 #define B2N_32(x) x = bswap_32(x)
37 #define B2N_64(x) x = bswap_64(x)
39 #elif defined(__NetBSD__)
40 #include <sys/endian.h>
41 #define B2N_16(x) BE16TOH(x)
42 #define B2N_32(x) BE32TOH(x)
43 #define B2N_64(x) BE64TOH(x)
45 #elif defined(__OpenBSD__)
46 #include <sys/endian.h>
47 #define B2N_16(x) x = swap16(x)
48 #define B2N_32(x) x = swap32(x)
49 #define B2N_64(x) x = swap64(x)
51 /* This is a slow but portable implementation, it has multiple evaluation
53 * FreeBSD and Solaris don't have <byteswap.h> or any other such
57 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__)
59 x = ((((x) & 0xff00) >> 8) | \
60 (((x) & 0x00ff) << 8))
62 x = ((((x) & 0xff000000) >> 24) | \
63 (((x) & 0x00ff0000) >> 8) | \
64 (((x) & 0x0000ff00) << 8) | \
65 (((x) & 0x000000ff) << 24))
67 x = ((((x) & 0xff00000000000000) >> 56) | \
68 (((x) & 0x00ff000000000000) >> 40) | \
69 (((x) & 0x0000ff0000000000) >> 24) | \
70 (((x) & 0x000000ff00000000) >> 8) | \
71 (((x) & 0x00000000ff000000) << 8) | \
72 (((x) & 0x0000000000ff0000) << 24) | \
73 (((x) & 0x000000000000ff00) << 40) | \
74 (((x) & 0x00000000000000ff) << 56))
78 /* If there isn't a header provided with your system with this functionality
79 * add the relevant || define( ) to the portable implementation above.
81 #error "You need to add endian swap macros for you're system"
85 #endif /* WORDS_BIGENDIAN */
87 #endif /* BSWAP_H_INCLUDED */