From nobody Sun Jun 14 23:04:29 2026 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D26F18D636 for ; Sun, 5 Apr 2026 15:31:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775403070; cv=none; b=ms8WY61pOqvguD/4kbWqnhl8dgDdOwVQ+dLeIIxtmSEJP/ILD54P+iWsFIbCxSypYD0FFhbicgSpmFM2Sk178FlFwNPrZDwjMzqblGacWDM8jtGHZJVAa+BbKmcGVGXAvhSZ04vxJxHW5BanD79TLsg/zcGPqpZYE7cwiIPvx74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775403070; c=relaxed/simple; bh=aMcEceLf1z/sI7kz6QscHpNLPFHFV0v9VDqPV3/VsO8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=Zzjzw8WNVhVnV4QFwJ5OmTW2mG3kbEIjai0MB9rQq1s/fOgJejYYHjij/p5cNJna3bKoejN2NwyrpWHA7y25/Cd4FnJ2o+40KjRif1sO3tSBznaH2uJv5f5mfgYHR1iWy5bMNfyUFfH7p3AM9gaVow4SRHleGFMquwr1pBv8aaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=KbDnHBRg; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="KbDnHBRg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1775403066; bh=aMcEceLf1z/sI7kz6QscHpNLPFHFV0v9VDqPV3/VsO8=; h=From:Date:Subject:To:Cc:From; b=KbDnHBRghSfpslWkSyFldXPVzZkJOi892IFVZI88YxdoUxszWwoCiZ67KPj+u/2Bm 5CApb4Dr4tdYLYW85YMDRwdMNUOZPTMfqHmgdmhsBtAPNug1zPdXuTMe8/3wINblt+ nQ/x75hVGtEPOIQUeWNBXSjePStdd+hKcYs2Vi6c= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sun, 05 Apr 2026 17:31:05 +0200 Subject: [PATCH] tools/nolibc: add byteorder conversions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260405-nolibc-bswap-v1-1-f7699ca9cee0@weissschuh.net> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIzMDEwNT3bz8nMykZN2k4vLEAl1TA1OjJNM0M8NkU2MloJaCotS0zAqwcdG xEH5xaVJWanIJyAyl2loA4FMBpHAAAAA= X-Change-ID: 20260405-nolibc-bswap-5052b5f61c53 To: Willy Tarreau Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775403065; l=5524; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=aMcEceLf1z/sI7kz6QscHpNLPFHFV0v9VDqPV3/VsO8=; b=SikCoMED94XIbeWfAKJZeChgoM5sqN3xb9T3C9eZpJlTOS4CtF2pQ3W9OHVrglfLCwZ00bZYF Gp/6ft4i0TBDsDhS5H1oCTyd+KqdYmw6jBZkCZjFYKl7SNWjhOwTnKH X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Add some standard functions to convert between different byte orders. Conveniently the UAPI headers provide all the necessary functionality. Signed-off-by: Thomas Wei=C3=9Fschuh Acked-by: Willy Tarreau --- 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 ?=3D $(CURDIR)/ architectures :=3D arm arm64 loongarch m68k mips powerpc riscv s390 sh spa= rc x86 arch_files :=3D arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)= )) all_files :=3D \ + 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/byteswa= p.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=C3=9Fschuh + */ + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#ifndef _NOLIBC_BYTESWAP_H +#define _NOLIBC_BYTESWAP_H + +#include "stdint.h" + +#include + +#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=C3=9Fschuh + */ + +/* make sure to include all global symbols */ +#include "nolibc.h" + +#ifndef _NOLIBC_ENDIAN_H +#define _NOLIBC_ENDIAN_H + +#include "stdint.h" + +#include + +#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" =20 /* Used by programs to avoid std includes */ #define NOLIBC diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/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 #include #include +#include +#include =20 #pragma GCC diagnostic ignored "-Wmissing-prototypes" =20 @@ -67,6 +69,8 @@ static const char *argv0; /* will be used by constructor tests */ static int constructor_test_value; =20 +static const int is_le =3D __BYTE_ORDER__ =3D=3D __ORDER_LITTLE_ENDIAN__; + static const int is_nolibc =3D #ifdef NOLIBC 1 @@ -1742,6 +1746,15 @@ int run_stdlib(int min, int max) CASE_TEST(major_big); EXPECT_EQ(1, major(0x112235566773448= 8), 0x11223344); break; CASE_TEST(minor_big); EXPECT_EQ(1, minor(0x112235566773448= 8), 0x55667788); break; CASE_TEST(malloc); EXPECT_ZR(1, test_malloc()); break; + CASE_TEST(bswap_16); EXPECT_EQ(1, bswap_16(0x0123), 0x230= 1); break; + CASE_TEST(bswap_32); EXPECT_EQ(1, bswap_32(0x01234567), 0= x67452301); break; + CASE_TEST(bswap_64); EXPECT_EQ(1, bswap_64(0x0123456789ab= cdef), 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 ? 0x01234= 567 : 0x67452301), 0x67452301); break; + CASE_TEST(htole32); EXPECT_EQ(1, htole32(is_le ? 0x01234= 567 : 0x67452301), 0x01234567); break; + CASE_TEST(htobe64); EXPECT_EQ(1, htobe64(is_le ? 0x01234= 56789000000 : 0x8967452301), 0x8967452301); break; + CASE_TEST(htole64); EXPECT_EQ(1, htole64(is_le ? 0x01234= 56789 : 0x8967452301000000), 0x0123456789); break; =20 case __LINE__: return ret; /* must be last */ --- base-commit: 1efa4e0374b43a021a34ba611951aac8d3e41f17 change-id: 20260405-nolibc-bswap-5052b5f61c53 Best regards, -- =20 Thomas Wei=C3=9Fschuh