git: 1761b09bf42d - main - byteswap.h: Add a glibc/linux compatible byteswap.h

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 20 Jan 2023 23:49:56 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=1761b09bf42d2842e82c1ac614c23d31c4d4c0dc

commit 1761b09bf42d2842e82c1ac614c23d31c4d4c0dc
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-01-20 23:33:37 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-20 23:37:39 +0000

    byteswap.h: Add a glibc/linux compatible byteswap.h
    
    For endian.h to work instead of sys/endian.h, some software needs
    byteswap.h available. It must define {__,}byteswap_{16,32,64}.
    Included sys/_endian.h to get an appropriate __byteswap16, etc
    and defines the new macros in terms of them. Enhance _endian.h
    to allow it to be included from here too.
    
    Sponsored by:           Netflix
    Reviewed by:            markj
    Differential Revision:  https://reviews.freebsd.org/D32051
---
 include/Makefile   |  3 ++-
 include/byteswap.h | 41 +++++++++++++++++++++++++++++++++++++++++
 sys/sys/_endian.h  |  2 +-
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/include/Makefile b/include/Makefile
index aa4c115a3931..24124a1f366b 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -12,7 +12,8 @@ SUBDIR= arpa protocols rpcsvc rpc xlocale
 SUBDIR+=	i386
 .endif
 SUBDIR_PARALLEL=
-INCS=	a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \
+INCS=	a.out.h ar.h assert.h bitstring.h byteswap.h \
+	complex.h cpio.h _ctype.h ctype.h \
 	db.h \
 	dirent.h dlfcn.h elf.h elf-hints.h endian.h err.h fmtmsg.h fnmatch.h \
 	fstab.h fts.h ftw.h getopt.h glob.h grp.h \
diff --git a/include/byteswap.h b/include/byteswap.h
new file mode 100644
index 000000000000..a7b816ee82b0
--- /dev/null
+++ b/include/byteswap.h
@@ -0,0 +1,41 @@
+/*-
+ * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * A mostly Linux/glibc-compatible byteswap.h
+ */
+
+#ifndef _BYTESWAP_H_
+#define _BYTESWAP_H_
+
+/*
+ * sys/_endian.h brings in the shared interfaces between BSD's sys/endian.h, and
+ * glibc's endian.h. However, we need to include it here to get the
+ * __bswap{16,32,64} definitions that we use. sys/_endian.h has been consturcted to
+ * be compatible with including <endian.h>, <byteswap.h> or both in either order,
+ * as well as providing the BSD the bulk of sys/endian.h functionality.
+ */
+#include <sys/_endian.h>
+
+/*
+ * glibc's <byteswap.h> defines the bswap_* and __bswap_* macros below. Most
+ * software uses either just <sys/endian.h>, or both <endian.h> and
+ * <byteswap.h>. However, one can't define bswap16, etc in <endian.h> because
+ * several software packages will define them only when they detect <endian.h>
+ * is included (but not when sys/endian.h is included). Defining bswap16, etc
+ * here causes compilation errors for those packages. <endian.h> and
+ * <byteswap.h> need to be paired together, with the below defines here, for
+ * the highest level of glibc compatibility.
+ */
+#define __bswap_16(x) __bswap16(x)
+#define __bswap_32(x) __bswap32(x)
+#define __bswap_64(x) __bswap64(x)
+
+#define bswap_16(x) __bswap16(x)
+#define bswap_32(x) __bswap32(x)
+#define bswap_64(x) __bswap64(x)
+
+#endif /* _BYTESWAP_H_ */
diff --git a/sys/sys/_endian.h b/sys/sys/_endian.h
index 72e1a40a8deb..ff909f532663 100644
--- a/sys/sys/_endian.h
+++ b/sys/sys/_endian.h
@@ -32,7 +32,7 @@
 #ifndef _SYS__ENDIAN_H_
 #define	_SYS__ENDIAN_H_
 
-#if !defined(_MACHINE_ENDIAN_H_) && !defined(_ENDIAN_H_)
+#if !defined(_MACHINE_ENDIAN_H_) && !defined(_BYTESWAP_H_) && !defined(_ENDIAN_H_)
 #error "sys/_endian.h should not be included directly"
 #endif