Debian Codegraph @

src/bswap.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2000, 2001 Billy Biggs <[email protected]>,
3 * Håkan Hjort <[email protected]>
4 *
5 * This file is part of libdvdread.
6 *
7 * libdvdread is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * libdvdread is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #ifndef LIBDVDREAD_BSWAP_H
23 #define LIBDVDREAD_BSWAP_H
24
25 #include <config.h>
26
27 #if defined(WORDS_BIGENDIAN)
28 /* All bigendian systems are fine, just ignore the swaps. */
29 #define B2N_16(x) (void)(x)
30 #define B2N_32(x) (void)(x)
31 #define B2N_64(x) (void)(x)
32
33 #else /* WORDS_BIGENDIAN */
34
35 #if defined(__clang__)
36 # if __has_builtin (__builtin_bswap16)
37 # define BSWAP_BUILTIN 1
38 # endif
39
40 #elif defined (__GNUC__) && ((__GNUC__ > (4)) || (__GNUC__ == (4) && __GNUC_MINOR__ >= (8)))
41 # define BSWAP_BUILTIN 1
42 #endif
43
44 #if defined(BSWAP_BUILTIN)
45
46 #define B2N_16(x) x = __builtin_bswap16(x)
47 #define B2N_32(x) x = __builtin_bswap32(x)
48 #define B2N_64(x) x = __builtin_bswap64(x)
49
50 #else /* BSWAP_BUILTIN */
51
52 /* For __FreeBSD_version */
53 #if defined(HAVE_SYS_PARAM_H)
54 #include <sys/param.h>
55 #endif
56
57 #if defined(__linux__) || defined(__GLIBC__)
58 #include <byteswap.h>
59 #define B2N_16(x) x = bswap_16(x)
60 #define B2N_32(x) x = bswap_32(x)
61 #define B2N_64(x) x = bswap_64(x)
62
63 #elif defined(__APPLE__)
64 #include <libkern/OSByteOrder.h>
65 #define B2N_16(x) x = OSSwapBigToHostInt16(x)
66 #define B2N_32(x) x = OSSwapBigToHostInt32(x)
67 #define B2N_64(x) x = OSSwapBigToHostInt64(x)
68
69 #elif defined(__NetBSD__)
70 #include <sys/endian.h>
71 #define B2N_16(x) BE16TOH(x)
72 #define B2N_32(x) BE32TOH(x)
73 #define B2N_64(x) BE64TOH(x)
74
75 #elif defined(__OpenBSD__)
76 #include <sys/endian.h>
77 #define B2N_16(x) x = swap16(x)
78 #define B2N_32(x) x = swap32(x)
79 #define B2N_64(x) x = swap64(x)
80
81 #elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
82 #include <sys/endian.h>
83 #define B2N_16(x) x = be16toh(x)
84 #define B2N_32(x) x = be32toh(x)
85 #define B2N_64(x) x = be64toh(x)
86
87 #elif defined(__QNXNTO__)
88 #include <gulliver.h>
89 #define B2N_16(x) x = ENDIAN_RET16(x)
90 #define B2N_32(x) x = ENDIAN_RET32(x)
91 #define B2N_64(x) x = ENDIAN_RET64(x)
92
93 #elif defined(__DragonFly__)
94 #include <sys/endian.h>
95 #define B2N_16(x) x = bswap16(x)
96 #define B2N_32(x) x = bswap32(x)
97 #define B2N_64(x) x = bswap64(x)
98
99 /* This is a slow but portable implementation, it has multiple evaluation
100 * problems so beware.
101 * Old FreeBSD's and Solaris don't have <byteswap.h> or any other such
102 * functionality!
103 */
104
105 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__BEOS__) || defined(__OS2__)
106 #define B2N_16(x) \
107 x = ((((x) & 0xff00) >> 8) | \
108 (((x) & 0x00ff) << 8))
109 #define B2N_32(x) \
110 x = ((((x) & 0xff000000) >> 24) | \
111 (((x) & 0x00ff0000) >> 8) | \
112 (((x) & 0x0000ff00) << 8) | \
113 (((x) & 0x000000ff) << 24))
114 #define B2N_64(x) \
115 x = ((((x) & 0xff00000000000000ULL) >> 56) | \
116 (((x) & 0x00ff000000000000ULL) >> 40) | \
117 (((x) & 0x0000ff0000000000ULL) >> 24) | \
118 (((x) & 0x000000ff00000000ULL) >> 8) | \
119 (((x) & 0x00000000ff000000ULL) << 8) | \
120 (((x) & 0x0000000000ff0000ULL) << 24) | \
121 (((x) & 0x000000000000ff00ULL) << 40) | \
122 (((x) & 0x00000000000000ffULL) << 56))
123
124 #else
125
126 /* If there isn't a header provided with your system with this functionality
127 * add the relevant || define( ) to the portable implementation above.
128 */
129 #error "You need to add endian swap macros for your system"
130
131 #endif
132
133 #endif /* BSWAP_BUILTIN */
134
135 #endif /* WORDS_BIGENDIAN */
136
137 #endif /* LIBDVDREAD_BSWAP_H */