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

via libc-commits libc-commits at lists.llvm.org
Thu May 7 06:33:39 PDT 2026


Author: Jeff Bailey
Date: 2026-05-07T14:33:33+01:00
New Revision: 7554007c806e5fdcfd4b034c2a462089428890da

URL: https://github.com/llvm/llvm-project/commit/7554007c806e5fdcfd4b034c2a462089428890da
DIFF: https://github.com/llvm/llvm-project/commit/7554007c806e5fdcfd4b034c2a462089428890da.diff

LOG: [libc] Add byteswap.h header with bswap_16/bswap_32/bswap_64 (#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.

Added: 
    libc/include/byteswap.yaml
    libc/include/llvm-libc-macros/byteswap-macros.h
    libc/utils/docgen/byteswap.yaml

Modified: 
    libc/docs/CMakeLists.txt
    libc/docs/headers/index.rst
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt

Removed: 
    


################################################################################
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

diff  --git a/libc/utils/docgen/byteswap.yaml b/libc/utils/docgen/byteswap.yaml
new file mode 100644
index 0000000000000..202946ff3776f
--- /dev/null
+++ b/libc/utils/docgen/byteswap.yaml
@@ -0,0 +1,8 @@
+macros:
+  bswap_16:
+    c-definition: ''
+  bswap_32:
+    c-definition: ''
+  bswap_64:
+    c-definition: ''
+


        


More information about the libc-commits mailing list