[libc-commits] [libc] [libc] Add byteswap.h header with bswap_16/bswap_32/bswap_64 (PR #196278)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Thu May 7 03:55:14 PDT 2026


https://github.com/kaladron created https://github.com/llvm/llvm-project/pull/196278

Added the Linux byteswap.h header providing bswap_16, bswap_32, and bswap_64 as macros expanding to __builtin_bswap{16,32,64}. These are always inlined by the compiler to single instructions.

Follows the existing endian.h / endian-macros.h pattern.

Assisted-by: Automated tooling, human reviewed.

>From d4cd65be70db47f59c15e9075e3ee5696a035c52 Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Thu, 7 May 2026 11:44:05 +0100
Subject: [PATCH] [libc] Add byteswap.h header with bswap_16/bswap_32/bswap_64

Added the Linux byteswap.h header providing bswap_16, bswap_32,
and bswap_64 as macros expanding to __builtin_bswap{16,32,64}.
These are always inlined by the compiler to single instructions.

Follows the existing endian.h / endian-macros.h pattern.

Assisted-by: Automated tooling, human reviewed.
---
 libc/docs/CMakeLists.txt                      |  1 +
 libc/docs/headers/index.rst                   |  1 +
 libc/include/CMakeLists.txt                   |  9 ++++++++
 libc/include/byteswap.yaml                    | 14 +++++++++++++
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 ++++++
 .../llvm-libc-macros/byteswap-macros.h        | 21 +++++++++++++++++++
 6 files changed, 52 insertions(+)
 create mode 100644 libc/include/byteswap.yaml
 create mode 100644 libc/include/llvm-libc-macros/byteswap-macros.h

diff --git a/libc/docs/CMakeLists.txt b/libc/docs/CMakeLists.txt
index 0a9135114ff53..800894c1d5f75 100644
--- a/libc/docs/CMakeLists.txt
+++ b/libc/docs/CMakeLists.txt
@@ -42,6 +42,7 @@ if (SPHINX_FOUND)
       aio
       arpa/inet
       assert
+      byteswap
       cpio
       ctype
       dirent
diff --git a/libc/docs/headers/index.rst b/libc/docs/headers/index.rst
index 528f995cc8147..a10f2b6d91744 100644
--- a/libc/docs/headers/index.rst
+++ b/libc/docs/headers/index.rst
@@ -7,6 +7,7 @@ Implementation Status
    aio
    arpa/inet
    assert
+   byteswap
    complex
    cpio
    ctype
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index aa3791b2ead2c..fac2b40ca45a8 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -229,6 +229,15 @@ add_header_macro(
     .llvm-libc-macros.assert_macros
 )
 
+add_header_macro(
+  byteswap
+  ../libc/include/byteswap.yaml
+  byteswap.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.byteswap_macros
+)
+
 add_header_macro(
   complex
   ../libc/include/complex.yaml
diff --git a/libc/include/byteswap.yaml b/libc/include/byteswap.yaml
new file mode 100644
index 0000000000000..3b47c3b4331c5
--- /dev/null
+++ b/libc/include/byteswap.yaml
@@ -0,0 +1,14 @@
+header: byteswap.h
+standards:
+  - linux
+macros:
+  - macro_name: bswap_16
+    macro_header: byteswap-macros.h
+  - macro_name: bswap_32
+    macro_header: byteswap-macros.h
+  - macro_name: bswap_64
+    macro_header: byteswap-macros.h
+types: []
+enums: []
+objects: []
+functions: []
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 86085b50d4c4c..af74d483956a3 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -48,6 +48,12 @@ add_macro_header(
     assert-macros.h
 )
 
+add_macro_header(
+  byteswap_macros
+  HDR
+    byteswap-macros.h
+)
+
 add_macro_header(
   error_number_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/byteswap-macros.h b/libc/include/llvm-libc-macros/byteswap-macros.h
new file mode 100644
index 0000000000000..aa8d047bddc60
--- /dev/null
+++ b/libc/include/llvm-libc-macros/byteswap-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of macros from byteswap.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_BYTESWAP_MACROS_H
+#define LLVM_LIBC_MACROS_BYTESWAP_MACROS_H
+
+#define bswap_16(x) __builtin_bswap16((x))
+#define bswap_32(x) __builtin_bswap32((x))
+#define bswap_64(x) __builtin_bswap64((x))
+
+#endif // LLVM_LIBC_MACROS_BYTESWAP_MACROS_H



More information about the libc-commits mailing list