* [PATCH] tools/nolibc: add byteorder conversions
@ 2026-04-05 15:31 Thomas Weißschuh
2026-04-07 7:20 ` Willy Tarreau
0 siblings, 1 reply; 2+ messages in thread
From: Thomas Weißschuh @ 2026-04-05 15:31 UTC (permalink / raw)
To: Willy Tarreau; +Cc: linux-kernel, Thomas Weißschuh
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>
---
tools/include/nolibc/Makefile | 2 ++
tools/include/nolibc/byteswap.h | 21 ++++++++++++++++++
tools/include/nolibc/endian.h | 32 ++++++++++++++++++++++++++++
tools/include/nolibc/nolibc.h | 2 ++
tools/testing/selftests/nolibc/nolibc-test.c | 13 +++++++++++
5 files changed, 70 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index db6db4e6d99e..7455097cff69 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -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
index 000000000000..45bbf9609d7a
--- /dev/null
+++ b/tools/include/nolibc/byteswap.h
@@ -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
index 000000000000..ccc016ecd5ec
--- /dev/null
+++ b/tools/include/nolibc/endian.h
@@ -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 */
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 294bac1b9039..f4120f65fe79 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -131,6 +131,8 @@
#include "poll.h"
#include "math.h"
#include "err.h"
+#include "byteswap.h"
+#include "endian.h"
/* Used by programs to avoid std includes */
#define NOLIBC
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index dd10402267ee..52684466a4cb 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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
@@ -1742,6 +1746,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 */
---
base-commit: 1efa4e0374b43a021a34ba611951aac8d3e41f17
change-id: 20260405-nolibc-bswap-5052b5f61c53
Best regards,
--
Thomas Weißschuh <linux@weissschuh.net>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] tools/nolibc: add byteorder conversions
2026-04-05 15:31 [PATCH] tools/nolibc: add byteorder conversions Thomas Weißschuh
@ 2026-04-07 7:20 ` Willy Tarreau
0 siblings, 0 replies; 2+ messages in thread
From: Willy Tarreau @ 2026-04-07 7:20 UTC (permalink / raw)
To: Thomas Weißschuh; +Cc: linux-kernel
On Sun, Apr 05, 2026 at 05:31:05PM +0200, Thomas Weißschuh wrote:
> 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>
Nice, pretty ocnvenient indeed!
Acked-by: Willy Tarreau <w@1wt.eu>
thanks,
Willy
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-04-07 7:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-05 15:31 [PATCH] tools/nolibc: add byteorder conversions Thomas Weißschuh
2026-04-07 7:20 ` Willy Tarreau
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox