Update Red Hat Copyright Notices
[nbdkit.git] / common / include / byte-swapping.h
blob085ba4753b85798fbd70fd8fa4eb924943ad76b1
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 /* The job of this header is to define macros (or functions) called
34 * things like 'htobe32' and 'le64toh' which byte swap N-bit integers
35 * between host representation, and little and big endian. Also
36 * bswap_16, bswap_32 and bswap_64 which simply swap endianness.
38 * The core code and plugins in nbdkit uses these names and relies on
39 * this header to provide the platform-specific implementation. On
40 * GNU/Linux these are defined in <endian.h> and <byteswap.h> but
41 * other platforms have other requirements.
44 #ifndef NBDKIT_BYTE_SWAPPING_H
45 #define NBDKIT_BYTE_SWAPPING_H
47 #ifdef HAVE_BYTESWAP_H
48 #include <byteswap.h>
49 #endif
51 #ifdef HAVE_ENDIAN_H
52 #include <endian.h>
53 #endif
55 #ifdef HAVE_SYS_ENDIAN_H
56 #include <sys/endian.h>
57 #endif
59 #ifdef __HAIKU__
60 #include <ByteOrder.h>
61 #define htobe16(x) B_HOST_TO_BENDIAN_INT16 (x)
62 #define htole16(x) B_HOST_TO_LENDIAN_INT16 (x)
63 #define be16toh(x) B_BENDIAN_TO_HOST_INT16 (x)
64 #define le16toh(x) B_LENDIAN_TO_HOST_INT16 (x)
66 #define htobe32(x) B_HOST_TO_BENDIAN_INT32 (x)
67 #define htole32(x) B_HOST_TO_LENDIAN_INT32 (x)
68 #define be32toh(x) B_BENDIAN_TO_HOST_INT32 (x)
69 #define le32toh(x) B_LENDIAN_TO_HOST_INT32 (x)
71 #define htobe64(x) B_HOST_TO_BENDIAN_INT64 (x)
72 #define htole64(x) B_HOST_TO_LENDIAN_INT64 (x)
73 #define be64toh(x) B_BENDIAN_TO_HOST_INT64 (x)
74 #define le64toh(x) B_LENDIAN_TO_HOST_INT64 (x)
75 #endif
77 /* If we didn't define bswap_16, bswap_32 and bswap_64 already above,
78 * create macros. GCC >= 4.8 and Clang have builtins.
80 #ifndef bswap_16
81 #define bswap_16 __builtin_bswap16
82 #endif
84 #ifndef bswap_32
85 #define bswap_32 __builtin_bswap32
86 #endif
88 #ifndef bswap_64
89 #define bswap_64 __builtin_bswap64
90 #endif
92 /* If we didn't define htobe* above then define them in terms of
93 * bswap_* macros.
95 #ifndef htobe32
96 # ifndef WORDS_BIGENDIAN /* little endian */
97 # define htobe16(x) bswap_16 (x)
98 # define htole16(x) (x)
99 # define be16toh(x) bswap_16 (x)
100 # define le16toh(x) (x)
102 # define htobe32(x) bswap_32 (x)
103 # define htole32(x) (x)
104 # define be32toh(x) bswap_32 (x)
105 # define le32toh(x) (x)
107 # define htobe64(x) bswap_64 (x)
108 # define htole64(x) (x)
109 # define be64toh(x) bswap_64 (x)
110 # define le64toh(x) (x)
112 # else /* big endian */
113 # define htobe16(x) (x)
114 # define htole16(x) bswap_16 (x)
115 # define be16toh(x) (x)
116 # define le16toh(x) bswap_16 (x)
118 # define htobe32(x) (x)
119 # define htole32(x) bswap_32 (x)
120 # define be32toh(x) (x)
121 # define le32toh(x) bswap_32 (x)
123 # define htobe64(x) (x)
124 # define htole64(x) bswap_64 (x)
125 # define be64toh(x) (x)
126 # define le64toh(x) bswap_64 (x)
127 # endif
128 #endif
130 #endif /* NBDKIT_BYTE_SWAPPING_H */