[libc-commits] [libc] [libc] Add the `<endian.h>` header. (PR #125168)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 6 03:36:40 PST 2025


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/125168

>From b3630d699f7a3467ea12743efe900406b988fe7f Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 31 Jan 2025 05:02:56 +0000
Subject: [PATCH 1/4] add endian

---
 libc/config/linux/aarch64/headers.txt         |  1 +
 libc/config/linux/x86_64/headers.txt          |  1 +
 libc/include/CMakeLists.txt                   |  9 +++++++++
 libc/include/endian.h.def                     | 17 +++++++++++++++++
 libc/include/endian.yaml                      |  9 +++++++++
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 ++++++
 libc/include/llvm-libc-macros/endian-macros.h | 16 ++++++++++++++++
 7 files changed, 59 insertions(+)
 create mode 100644 libc/include/endian.h.def
 create mode 100644 libc/include/endian.yaml
 create mode 100644 libc/include/llvm-libc-macros/endian-macros.h

diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 05f15a0e4e5cbb5..7ff558998055088 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.ctype
     libc.include.dlfcn
     libc.include.elf
+    libc.include.endian
     libc.include.errno
     libc.include.features
     libc.include.fenv
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 8750100302ea7e8..fe1fd596960dd3a 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -5,6 +5,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.dirent
     libc.include.dlfcn
     libc.include.elf
+    libc.include.endian
     libc.include.errno
     libc.include.fcntl
     libc.include.features
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index e5ceea360d3965e..96d715cde9de9d1 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -73,6 +73,15 @@ add_header_macro(
     .llvm_libc_common_h
 )
 
+add_header_macro(
+  endian
+  ../libc/include/endian.yaml
+  endian.h
+  DEPENDS
+    .llvm-libc-macros.endian_macros
+    .llvm_libc_common_h
+)
+
 add_header_macro(
   features
   ../libc/include/features.yaml
diff --git a/libc/include/endian.h.def b/libc/include/endian.h.def
new file mode 100644
index 000000000000000..8448d93a86acc38
--- /dev/null
+++ b/libc/include/endian.h.def
@@ -0,0 +1,17 @@
+//===-- System V header endian.h ------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_ENDIAN_H
+#define LLVM_LIBC_ENDIAN_H
+
+#include "__llvm-libc-common.h"
+#include "llvm-libc-macros/endian-macros.h"
+
+%%public_api()
+
+#endif // LLVM_LIBC_ENDIAN_H
diff --git a/libc/include/endian.yaml b/libc/include/endian.yaml
new file mode 100644
index 000000000000000..6497f897b63933a
--- /dev/null
+++ b/libc/include/endian.yaml
@@ -0,0 +1,9 @@
+header: endian.h
+header_template: endian.h.def
+standards:
+  - POSIX
+macros: []
+types: []
+enums: []
+objects: []
+functions: []
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 9d5d9f65442889a..d165ada6612c840 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -310,6 +310,12 @@ add_macro_header(
     elf-macros.h
 )
 
+add_macro_header(
+  endian_macros
+  HDR
+    endian-macros.h
+)
+
 add_macro_header(
   locale_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
new file mode 100644
index 000000000000000..94e1d60f8ff404f
--- /dev/null
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -0,0 +1,16 @@
+//===-- Definition of macros from endian.h --------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_ENDIAN_MACROS_H
+#define LLVM_LIBC_MACROS_ENDIAN_MACROS_H
+
+#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
+#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
+#define BYTE_ORDER __BYTE_ORDER__
+
+#endif // LLVM_LIBC_MACROS_ENDIAN_MACROS_H

>From 5650441a69ab33804bd7b867e49525acad10fe42 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 31 Jan 2025 13:46:09 +0800
Subject: [PATCH 2/4] treat it as POSIX header, NFC

---
 libc/include/endian.h.def | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/endian.h.def b/libc/include/endian.h.def
index 8448d93a86acc38..f331f08edc68964 100644
--- a/libc/include/endian.h.def
+++ b/libc/include/endian.h.def
@@ -1,4 +1,4 @@
-//===-- System V header endian.h ------------------------------------------===//
+//===-- POSIX header endian.h ---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From 5fcc927ed65ba9c028a6ef678fd7b41ea72c74cb Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 5 Feb 2025 21:59:40 +0800
Subject: [PATCH 3/4] add doc for endian

---
 libc/docs/CMakeLists.txt      | 1 +
 libc/docs/headers/index.rst   | 1 +
 libc/utils/docgen/endian.yaml | 8 ++++++++
 3 files changed, 10 insertions(+)
 create mode 100644 libc/utils/docgen/endian.yaml

diff --git a/libc/docs/CMakeLists.txt b/libc/docs/CMakeLists.txt
index fc5e505c3be6915..97f27fe6a6e0c6b 100644
--- a/libc/docs/CMakeLists.txt
+++ b/libc/docs/CMakeLists.txt
@@ -39,6 +39,7 @@ if (SPHINX_FOUND)
       assert
       cpio
       ctype
+      endian
       errno
       fenv
       float
diff --git a/libc/docs/headers/index.rst b/libc/docs/headers/index.rst
index bd48dd5989bcd0d..745b6f44750dbcf 100644
--- a/libc/docs/headers/index.rst
+++ b/libc/docs/headers/index.rst
@@ -10,6 +10,7 @@ Implementation Status
    complex
    cpio
    ctype
+   endian
    errno
    fenv
    float
diff --git a/libc/utils/docgen/endian.yaml b/libc/utils/docgen/endian.yaml
new file mode 100644
index 000000000000000..e4d56c5635ff670
--- /dev/null
+++ b/libc/utils/docgen/endian.yaml
@@ -0,0 +1,8 @@
+macros:
+  LITTLE_ENDIAN:
+    in-latest-posix: ''
+  BIG_ENDIAN:
+    in-latest-posix: ''
+  BYTE_ORDER:
+    in-latest-posix: ''
+

>From 2cfed7c1eb7603cf412532bf0b5c3b90aecd6005 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 6 Feb 2025 19:36:09 +0800
Subject: [PATCH 4/4] address review comments

---
 libc/utils/docgen/endian.yaml | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/libc/utils/docgen/endian.yaml b/libc/utils/docgen/endian.yaml
index e4d56c5635ff670..f100a11260667be 100644
--- a/libc/utils/docgen/endian.yaml
+++ b/libc/utils/docgen/endian.yaml
@@ -5,4 +5,28 @@ macros:
     in-latest-posix: ''
   BYTE_ORDER:
     in-latest-posix: ''
+  be16toh:
+    in-latest-posix: ''
+  be32toh:
+    in-latest-posix: ''
+  be64toh:
+    in-latest-posix: ''
+  htobe16:
+    in-latest-posix: ''
+  htobe32:
+    in-latest-posix: ''
+  htobe64:
+    in-latest-posix: ''
+  htole16:
+    in-latest-posix: ''
+  htole32:
+    in-latest-posix: ''
+  htole64:
+    in-latest-posix: ''
+  le16toh:
+    in-latest-posix: ''
+  le32toh:
+    in-latest-posix: ''
+  le64toh:
+    in-latest-posix: ''
 



More information about the libc-commits mailing list