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