Move variable declaration to appropriate place
[mplayer/glamo.git] / dvdread / bswap.h
blobc612025b3186d83f3993eceacc2323e206c6c4fd
1 #ifndef BSWAP_H_INCLUDED
2 #define BSWAP_H_INCLUDED
4 /*
5 * Copyright (C) 2000, 2001 Billy Biggs <vektor@dumbterm.net>,
6 * HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
8 * Modified for use with MPlayer, changes contained in libdvdread_changes.diff.
9 * detailed changelog at http://svn.mplayerhq.hu/mplayer/trunk/
10 * $Id$
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 2 of the License, or
15 * (at your option) any later version.
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.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <config.h>
29 #if defined(WORDS_BIGENDIAN)
30 /* All bigendian systems are fine, just ignore the swaps. */
31 #define B2N_16(x) (void)(x)
32 #define B2N_32(x) (void)(x)
33 #define B2N_64(x) (void)(x)
35 #else
37 /* For __FreeBSD_version */
38 #if defined(HAVE_SYS_PARAM_H)
39 #include <sys/param.h>
40 #endif
42 #if defined(__linux__)
43 #include <byteswap.h>
44 #define B2N_16(x) x = bswap_16(x)
45 #define B2N_32(x) x = bswap_32(x)
46 #define B2N_64(x) x = bswap_64(x)
48 #elif defined(__NetBSD__)
49 #include <sys/endian.h>
50 #define B2N_16(x) BE16TOH(x)
51 #define B2N_32(x) BE32TOH(x)
52 #define B2N_64(x) BE64TOH(x)
54 #elif defined(__OpenBSD__)
55 #include <sys/endian.h>
56 #define B2N_16(x) x = swap16(x)
57 #define B2N_32(x) x = swap32(x)
58 #define B2N_64(x) x = swap64(x)
60 #elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
61 #include <sys/endian.h>
62 #define B2N_16(x) x = be16toh(x)
63 #define B2N_32(x) x = be32toh(x)
64 #define B2N_64(x) x = be64toh(x)
66 #elif defined(__DragonFly__)
67 #include <sys/endian.h>
68 #define B2N_16(x) x = be16toh(x)
69 #define B2N_32(x) x = be32toh(x)
70 #define B2N_64(x) x = be64toh(x)
72 #elif defined(ARCH_X86)
73 inline static unsigned short bswap_16(unsigned short x)
75 __asm("xchgb %b0,%h0" :
76 "=q" (x) :
77 "0" (x));
78 return x;
80 #define B2N_16(x) x = bswap_16(x)
82 inline static unsigned int bswap_32(unsigned int x)
84 __asm(
85 #if __CPU__ != 386
86 "bswap %0":
87 "=r" (x) :
88 #else
89 "xchgb %b0,%h0\n"
90 " rorl $16,%0\n"
91 " xchgb %b0,%h0":
92 "=q" (x) :
93 #endif
94 "0" (x));
95 return x;
97 #define B2N_32(x) x = bswap_32(x)
99 inline static unsigned long long int bswap_64(unsigned long long int x)
101 register union { __extension__ uint64_t __ll;
102 uint32_t __l[2]; } __x;
103 asm("xchgl %0,%1":
104 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
105 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
106 return __x.__ll;
108 #define B2N_64(x) x = bswap_64(x)
110 /* This is a slow but portable implementation, it has multiple evaluation
111 * problems so beware.
112 * Old FreeBSD's and Solaris don't have <byteswap.h> or any other such
113 * functionality!
116 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(__CYGWIN__)
117 #define B2N_16(x) \
118 x = ((((x) & 0xff00) >> 8) | \
119 (((x) & 0x00ff) << 8))
120 #define B2N_32(x) \
121 x = ((((x) & 0xff000000) >> 24) | \
122 (((x) & 0x00ff0000) >> 8) | \
123 (((x) & 0x0000ff00) << 8) | \
124 (((x) & 0x000000ff) << 24))
125 #define B2N_64(x) \
126 x = ((((x) & 0xff00000000000000) >> 56) | \
127 (((x) & 0x00ff000000000000) >> 40) | \
128 (((x) & 0x0000ff0000000000) >> 24) | \
129 (((x) & 0x000000ff00000000) >> 8) | \
130 (((x) & 0x00000000ff000000) << 8) | \
131 (((x) & 0x0000000000ff0000) << 24) | \
132 (((x) & 0x000000000000ff00) << 40) | \
133 (((x) & 0x00000000000000ff) << 56))
135 #else
137 /* If there isn't a header provided with your system with this functionality
138 * add the relevant || define( ) to the portable implementation above.
140 #error "You need to add endian swap macros for you're system"
142 #endif
144 #endif /* WORDS_BIGENDIAN */
146 #endif /* BSWAP_H_INCLUDED */