[libc-commits] [libc] [libc] implement endian related macros (PR #126368)
via libc-commits
libc-commits at lists.llvm.org
Sat Feb 8 05:48:59 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: None (c8ef)
<details>
<summary>Changes</summary>
Follow up of #<!-- -->125168.
This patch adds endian-related macros to `endian.h`. We utilize compiler built-ins for byte swap functions, which are already included in our minimal supported compiler version.
---
Full diff: https://github.com/llvm/llvm-project/pull/126368.diff
2 Files Affected:
- (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+2)
- (modified) libc/include/llvm-libc-macros/endian-macros.h (+34)
``````````diff
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index ea892a87dbe7ab9..6124fd136ce6d0c 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -314,6 +314,8 @@ add_macro_header(
endian_macros
HDR
endian-macros.h
+ DEPENDS
+ .stdint_macros
)
add_macro_header(
diff --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
index 94e1d60f8ff404f..e1e105d50c1c68a 100644
--- a/libc/include/llvm-libc-macros/endian-macros.h
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -9,8 +9,42 @@
#ifndef LLVM_LIBC_MACROS_ENDIAN_MACROS_H
#define LLVM_LIBC_MACROS_ENDIAN_MACROS_H
+#include "stdint-macros.h"
+
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
#define BYTE_ORDER __BYTE_ORDER__
+#if BYTE_ORDER == LITTLE_ENDIAN
+
+#define htobe16(x) __builtin_bswap16((x))
+#define htobe32(x) __builtin_bswap32((x))
+#define htobe64(x) __builtin_bswap64((x))
+#define htole16(x) ((uint16_t)(x))
+#define htole32(x) ((uint32_t)(x))
+#define htole64(x) ((uint64_t)(x))
+#define be16toh(x) __builtin_bswap16((x))
+#define be32toh(x) __builtin_bswap32((x))
+#define be64toh(x) __builtin_bswap64((x))
+#define le16toh(x) ((uint16_t)(x))
+#define le32toh(x) ((uint32_t)(x))
+#define le64toh(x) ((uint64_t)(x))
+
+#else
+
+#define htobe16(x) ((uint16_t)(x))
+#define htobe32(x) ((uint32_t)(x))
+#define htobe64(x) ((uint64_t)(x))
+#define htole16(x) __builtin_bswap16((x))
+#define htole32(x) __builtin_bswap32((x))
+#define htole64(x) __builtin_bswap64((x))
+#define be16toh(x) ((uint16_t)(x))
+#define be32toh(x) ((uint32_t)(x))
+#define be64toh(x) ((uint64_t)(x))
+#define le16toh(x) __builtin_bswap16((x))
+#define le32toh(x) __builtin_bswap32((x))
+#define le64toh(x) __builtin_bswap64((x))
+
+#endif
+
#endif // LLVM_LIBC_MACROS_ENDIAN_MACROS_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/126368
More information about the libc-commits
mailing list