[libc-commits] [libc] [libc] implement endian related macros (PR #126368)
via libc-commits
libc-commits at lists.llvm.org
Sat Feb 8 05:45:26 PST 2025
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/126368
None
>From 84031b3431a362d6c6d6cc03308b1b5891909202 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 8 Feb 2025 21:44:56 +0800
Subject: [PATCH] implement endian related macros
---
libc/include/llvm-libc-macros/CMakeLists.txt | 2 ++
libc/include/llvm-libc-macros/endian-macros.h | 34 +++++++++++++++++++
2 files changed, 36 insertions(+)
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
More information about the libc-commits
mailing list