aboutsummaryrefslogtreecommitdiff
path: root/include/byteswap.h
diff options
context:
space:
mode:
authorWarner Losh <imp@FreeBSD.org>2023-01-20 23:33:37 +0000
committerWarner Losh <imp@FreeBSD.org>2023-01-20 23:37:39 +0000
commit1761b09bf42d2842e82c1ac614c23d31c4d4c0dc (patch)
tree30077a430641a0e830e95567ff43f8bb7e554da0 /include/byteswap.h
parent30e0d2a51026830e5141ce3dd43854819bba910c (diff)
downloadsrc-1761b09bf42d2842e82c1ac614c23d31c4d4c0dc.tar.gz
src-1761b09bf42d2842e82c1ac614c23d31c4d4c0dc.zip
Diffstat (limited to 'include/byteswap.h')
-rw-r--r--include/byteswap.h41
1 files changed, 41 insertions, 0 deletions
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_ */