[libc-commits] [libc] [libc] add `LLVM_LIBC_CAST` macro. (PR #127319)

via libc-commits libc-commits at lists.llvm.org
Tue Feb 18 16:56:35 PST 2025


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

>From 866374c4fb133bcb3d22b3ce2dfcbc9fb7559dac Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 15 Feb 2025 20:56:03 +0800
Subject: [PATCH 1/3] add LLVM_LIBC_CAST macro

---
 libc/include/__llvm-libc-common.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index a0fa506c01ab8..83682e0a440d8 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,6 +47,9 @@
 #define __NOEXCEPT throw()
 #endif
 
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
+
 #else // not __cplusplus
 
 #undef __BEGIN_C_DECLS
@@ -85,6 +88,9 @@
 #undef _Returns_twice
 #define _Returns_twice __attribute__((returns_twice))
 
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) ((type)(value))
+
 #endif // __cplusplus
 
 #endif // LLVM_LIBC_COMMON_H

>From a536acbe206b1211a35a523c08717c99552f704f Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 17 Feb 2025 08:40:29 +0800
Subject: [PATCH 2/3] address review comments

---
 libc/include/__llvm-libc-common.h             | 10 ++++----
 libc/include/llvm-libc-macros/endian-macros.h | 24 +++++++++----------
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index 83682e0a440d8..650eff65831e8 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,8 +47,10 @@
 #define __NOEXCEPT throw()
 #endif
 
-#undef LLVM_LIBC_CAST
-#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
+// This macro serves as a generic cast implementation for use in both C and C++,
+// similar to `__BIONIC_CAST` in Android.
+#undef __LLVM_LIBC_CAST
+#define __LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
 
 #else // not __cplusplus
 
@@ -88,8 +90,8 @@
 #undef _Returns_twice
 #define _Returns_twice __attribute__((returns_twice))
 
-#undef LLVM_LIBC_CAST
-#define LLVM_LIBC_CAST(cast, type, value) ((type)(value))
+#undef __LLVM_LIBC_CAST
+#define __LLVM_LIBC_CAST(cast, type, value) ((type)(value))
 
 #endif // __cplusplus
 
diff --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
index e1e105d50c1c6..492ded3dcdfbb 100644
--- a/libc/include/llvm-libc-macros/endian-macros.h
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -20,27 +20,27 @@
 #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 htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
+#define htole32(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
+#define htole64(x) __LLVM_LIBC_CAST(reinterpret_cast, 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))
+#define le16toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
+#define le32toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
+#define le64toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
 
 #else
 
-#define htobe16(x) ((uint16_t)(x))
-#define htobe32(x) ((uint32_t)(x))
-#define htobe64(x) ((uint64_t)(x))
+#define htobe16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
+#define htobe32(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
+#define htobe64(x) __LLVM_LIBC_CAST(reinterpret_cast, 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 be16toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
+#define be32toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
+#define be64toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
 #define le16toh(x) __builtin_bswap16((x))
 #define le32toh(x) __builtin_bswap32((x))
 #define le64toh(x) __builtin_bswap64((x))

>From 6f04a86e5182fccdf822829cbed5dfc31fc7000b Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 19 Feb 2025 08:56:05 +0800
Subject: [PATCH 3/3] static cast

---
 libc/include/llvm-libc-macros/endian-macros.h | 24 +++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
index 492ded3dcdfbb..52d95dc01cd83 100644
--- a/libc/include/llvm-libc-macros/endian-macros.h
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -20,27 +20,27 @@
 #define htobe16(x) __builtin_bswap16((x))
 #define htobe32(x) __builtin_bswap32((x))
 #define htobe64(x) __builtin_bswap64((x))
-#define htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
-#define htole32(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
-#define htole64(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
+#define htole16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
+#define htole32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
+#define htole64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
 #define be16toh(x) __builtin_bswap16((x))
 #define be32toh(x) __builtin_bswap32((x))
 #define be64toh(x) __builtin_bswap64((x))
-#define le16toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
-#define le32toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
-#define le64toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
+#define le16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
+#define le32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
+#define le64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
 
 #else
 
-#define htobe16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
-#define htobe32(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
-#define htobe64(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
+#define htobe16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
+#define htobe32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
+#define htobe64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
 #define htole16(x) __builtin_bswap16((x))
 #define htole32(x) __builtin_bswap32((x))
 #define htole64(x) __builtin_bswap64((x))
-#define be16toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
-#define be32toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint32_t, x)
-#define be64toh(x) __LLVM_LIBC_CAST(reinterpret_cast, uint64_t, x)
+#define be16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
+#define be32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
+#define be64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
 #define le16toh(x) __builtin_bswap16((x))
 #define le32toh(x) __builtin_bswap32((x))
 #define le64toh(x) __builtin_bswap64((x))



More information about the libc-commits mailing list