7029
|
1 #ifndef BSWAP_H_INCLUDED
|
|
2 #define BSWAP_H_INCLUDED
|
|
3
|
|
4 /*
|
|
5 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
|
|
6 * Håkan Hjort <d95hjort@dtek.chalmers.se>
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
|
23 #if defined(WORDS_BIGENDIAN)
|
|
24 /* All bigendian systems are fine, just ignore the swaps. */
|
|
25 #define B2N_16(x) (void)(x)
|
|
26 #define B2N_32(x) (void)(x)
|
|
27 #define B2N_64(x) (void)(x)
|
|
28
|
|
29 #else
|
|
30
|
|
31 #if defined(__linux__)
|
|
32 #include <byteswap.h>
|
|
33 #define B2N_16(x) x = bswap_16(x)
|
|
34 #define B2N_32(x) x = bswap_32(x)
|
|
35 #define B2N_64(x) x = bswap_64(x)
|
|
36
|
|
37 #elif defined(__NetBSD__)
|
|
38 #include <sys/endian.h>
|
|
39 #define B2N_16(x) BE16TOH(x)
|
|
40 #define B2N_32(x) BE32TOH(x)
|
|
41 #define B2N_64(x) BE64TOH(x)
|
|
42
|
|
43 #elif defined(__OpenBSD__)
|
|
44 #include <sys/endian.h>
|
|
45 #define B2N_16(x) x = swap16(x)
|
|
46 #define B2N_32(x) x = swap32(x)
|
|
47 #define B2N_64(x) x = swap64(x)
|
|
48
|
7033
|
49 #elif defined(ARCH_X86)
|
|
50 inline static unsigned short bswap_16(unsigned short x)
|
|
51 {
|
|
52 __asm("xchgb %b0,%h0" :
|
|
53 "=q" (x) :
|
|
54 "0" (x));
|
|
55 return x;
|
|
56 }
|
|
57 #define B2N_16(x) x = bswap_16(x)
|
|
58
|
|
59 inline static unsigned int bswap_32(unsigned int x)
|
|
60 {
|
|
61 __asm(
|
|
62 #if __CPU__ > 386
|
|
63 "bswap %0":
|
|
64 "=r" (x) :
|
|
65 #else
|
|
66 "xchgb %b0,%h0\n"
|
|
67 " rorl $16,%0\n"
|
|
68 " xchgb %b0,%h0":
|
|
69 "=q" (x) :
|
|
70 #endif
|
|
71 "0" (x));
|
|
72 return x;
|
|
73 }
|
|
74 #define B2N_32(x) x = bswap_32(x)
|
|
75
|
|
76 inline static unsigned long long int bswap_64(unsigned long long int x)
|
|
77 {
|
|
78 register union { __extension__ uint64_t __ll;
|
|
79 uint32_t __l[2]; } __x;
|
|
80 asm("xchgl %0,%1":
|
|
81 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
|
|
82 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
|
|
83 return __x.__ll;
|
|
84 }
|
|
85 #define B2N_64(x) x = bswap_64(x)
|
|
86
|
7029
|
87 /* This is a slow but portable implementation, it has multiple evaluation
|
|
88 * problems so beware.
|
|
89 * FreeBSD and Solaris don't have <byteswap.h> or any other such
|
|
90 * functionality!
|
|
91 */
|
|
92
|
7033
|
93 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(__CYGWIN__)
|
7029
|
94 #define B2N_16(x) \
|
|
95 x = ((((x) & 0xff00) >> 8) | \
|
|
96 (((x) & 0x00ff) << 8))
|
|
97 #define B2N_32(x) \
|
|
98 x = ((((x) & 0xff000000) >> 24) | \
|
|
99 (((x) & 0x00ff0000) >> 8) | \
|
|
100 (((x) & 0x0000ff00) << 8) | \
|
|
101 (((x) & 0x000000ff) << 24))
|
|
102 #define B2N_64(x) \
|
|
103 x = ((((x) & 0xff00000000000000) >> 56) | \
|
|
104 (((x) & 0x00ff000000000000) >> 40) | \
|
|
105 (((x) & 0x0000ff0000000000) >> 24) | \
|
|
106 (((x) & 0x000000ff00000000) >> 8) | \
|
|
107 (((x) & 0x00000000ff000000) << 8) | \
|
|
108 (((x) & 0x0000000000ff0000) << 24) | \
|
|
109 (((x) & 0x000000000000ff00) << 40) | \
|
|
110 (((x) & 0x00000000000000ff) << 56))
|
|
111
|
|
112 #else
|
|
113
|
|
114 /* If there isn't a header provided with your system with this functionality
|
|
115 * add the relevant || define( ) to the portable implementation above.
|
|
116 */
|
|
117 #error "You need to add endian swap macros for you're system"
|
|
118
|
|
119 #endif
|
|
120
|
|
121 #endif /* WORDS_BIGENDIAN */
|
|
122
|
|
123 #endif /* BSWAP_H_INCLUDED */
|