xref: /freebsd/include/byteswap.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 /*
8  * A mostly Linux/glibc-compatible byteswap.h
9  */
10 
11 #ifndef _BYTESWAP_H_
12 #define _BYTESWAP_H_
13 
14 /*
15  * sys/_endian.h brings in the shared interfaces between BSD's sys/endian.h, and
16  * glibc's endian.h. However, we need to include it here to get the
17  * __bswap{16,32,64} definitions that we use. sys/_endian.h has been consturcted to
18  * be compatible with including <endian.h>, <byteswap.h> or both in either order,
19  * as well as providing the BSD the bulk of sys/endian.h functionality.
20  */
21 #include <sys/_endian.h>
22 
23 /*
24  * glibc's <byteswap.h> defines the bswap_* and __bswap_* macros below. Most
25  * software uses either just <sys/endian.h>, or both <endian.h> and
26  * <byteswap.h>. However, one can't define bswap16, etc in <endian.h> because
27  * several software packages will define them only when they detect <endian.h>
28  * is included (but not when sys/endian.h is included). Defining bswap16, etc
29  * here causes compilation errors for those packages. <endian.h> and
30  * <byteswap.h> need to be paired together, with the below defines here, for
31  * the highest level of glibc compatibility.
32  */
33 #define __bswap_16(x) __bswap16(x)
34 #define __bswap_32(x) __bswap32(x)
35 #define __bswap_64(x) __bswap64(x)
36 
37 #define bswap_16(x) __bswap16(x)
38 #define bswap_32(x) __bswap32(x)
39 #define bswap_64(x) __bswap64(x)
40 
41 #endif /* _BYTESWAP_H_ */
42