]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc: add byteorder conversions
authorThomas Weißschuh <linux@weissschuh.net>
Sun, 5 Apr 2026 15:31:05 +0000 (17:31 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Tue, 7 Apr 2026 07:27:25 +0000 (09:27 +0200)
Add some standard functions to convert between different byte orders.
Conveniently the UAPI headers provide all the necessary functionality.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260405-nolibc-bswap-v1-1-f7699ca9cee0@weissschuh.net
tools/include/nolibc/Makefile
tools/include/nolibc/byteswap.h [new file with mode: 0644]
tools/include/nolibc/endian.h [new file with mode: 0644]
tools/include/nolibc/nolibc.h
tools/testing/selftests/nolibc/nolibc-test.c

index db6db4e6d99e6c10eab989ef5a96d09f7b1f6d8e..7455097cff693f964134f1f35a40073bdc079b68 100644 (file)
@@ -20,11 +20,13 @@ OUTPUT ?= $(CURDIR)/
 architectures := arm arm64 loongarch m68k mips powerpc riscv s390 sh sparc x86
 arch_files := arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)))
 all_files := \
+               byteswap.h \
                compiler.h \
                crt.h \
                ctype.h \
                dirent.h \
                elf.h \
+               endian.h \
                err.h \
                errno.h \
                fcntl.h \
diff --git a/tools/include/nolibc/byteswap.h b/tools/include/nolibc/byteswap.h
new file mode 100644 (file)
index 0000000..45bbf96
--- /dev/null
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * Byte swapping for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_BYTESWAP_H
+#define _NOLIBC_BYTESWAP_H
+
+#include "stdint.h"
+
+#include <linux/swab.h>
+
+#define bswap_16(_x) __swab16(_x)
+#define bswap_32(_x) __swab32(_x)
+#define bswap_64(_x) __swab64(_x)
+
+#endif /* _NOLIBC_BYTESWAP_H */
diff --git a/tools/include/nolibc/endian.h b/tools/include/nolibc/endian.h
new file mode 100644 (file)
index 0000000..ccc016e
--- /dev/null
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * Byte order conversion for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_ENDIAN_H
+#define _NOLIBC_ENDIAN_H
+
+#include "stdint.h"
+
+#include <asm/byteorder.h>
+
+#define htobe16(_x) __cpu_to_be16(_x)
+#define htole16(_x) __cpu_to_le16(_x)
+#define be16toh(_x) __be16_to_cpu(_x)
+#define le16toh(_x) __le16_to_cpu(_x)
+
+#define htobe32(_x) __cpu_to_be32(_x)
+#define htole32(_x) __cpu_to_le32(_x)
+#define be32toh(_x) __be32_to_cpu(_x)
+#define le32toh(_x) __le32_to_cpu(_x)
+
+#define htobe64(_x) __cpu_to_be64(_x)
+#define htole64(_x) __cpu_to_le64(_x)
+#define be64toh(_x) __be64_to_cpu(_x)
+#define le64toh(_x) __le64_to_cpu(_x)
+
+#endif /* _NOLIBC_ENDIAN_H */
index 294bac1b9039a183ade23f52678cd152f0406403..f4120f65fe794c28e98e2e28f254afdafa4fc344 100644 (file)
 #include "poll.h"
 #include "math.h"
 #include "err.h"
+#include "byteswap.h"
+#include "endian.h"
 
 /* Used by programs to avoid std includes */
 #define NOLIBC
index 4bcf4686760369a9c815ea7bb4fbc608f642d922..bb13f9f9aa7c21cf00b7aec3419f8a058e7c992b 100644 (file)
@@ -43,6 +43,8 @@
 #include <limits.h>
 #include <ctype.h>
 #include <stdbool.h>
+#include <byteswap.h>
+#include <endian.h>
 
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
@@ -67,6 +69,8 @@ static const char *argv0;
 /* will be used by constructor tests */
 static int constructor_test_value;
 
+static const int is_le = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
+
 static const int is_nolibc =
 #ifdef NOLIBC
        1
@@ -1746,6 +1750,15 @@ int run_stdlib(int min, int max)
                CASE_TEST(major_big);               EXPECT_EQ(1, major(0x1122355667734488), 0x11223344); break;
                CASE_TEST(minor_big);               EXPECT_EQ(1, minor(0x1122355667734488), 0x55667788); break;
                CASE_TEST(malloc);                  EXPECT_ZR(1, test_malloc()); break;
+               CASE_TEST(bswap_16);                EXPECT_EQ(1, bswap_16(0x0123), 0x2301); break;
+               CASE_TEST(bswap_32);                EXPECT_EQ(1, bswap_32(0x01234567), 0x67452301); break;
+               CASE_TEST(bswap_64);                EXPECT_EQ(1, bswap_64(0x0123456789abcdef), 0xefcdab8967452301); break;
+               CASE_TEST(htobe16);                 EXPECT_EQ(1, htobe16(is_le ? 0x0123 : 0x2301), 0x2301); break;
+               CASE_TEST(htole16);                 EXPECT_EQ(1, htole16(is_le ? 0x0123 : 0x2301), 0x0123); break;
+               CASE_TEST(htobe32);                 EXPECT_EQ(1, htobe32(is_le ? 0x01234567 : 0x67452301), 0x67452301); break;
+               CASE_TEST(htole32);                 EXPECT_EQ(1, htole32(is_le ? 0x01234567 : 0x67452301), 0x01234567); break;
+               CASE_TEST(htobe64);                 EXPECT_EQ(1, htobe64(is_le ? 0x0123456789000000 : 0x8967452301), 0x8967452301); break;
+               CASE_TEST(htole64);                 EXPECT_EQ(1, htole64(is_le ? 0x0123456789 : 0x8967452301000000), 0x0123456789); break;
 
                case __LINE__:
                        return ret; /* must be last */