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

via libc-commits libc-commits at lists.llvm.org
Wed Feb 19 07:28:16 PST 2025


Author: c8ef
Date: 2025-02-19T23:28:11+08:00
New Revision: 826af1757c99e98c5816fc3ffdb1cece78107991

URL: https://github.com/llvm/llvm-project/commit/826af1757c99e98c5816fc3ffdb1cece78107991
DIFF: https://github.com/llvm/llvm-project/commit/826af1757c99e98c5816fc3ffdb1cece78107991.diff

LOG: [libc] add `LLVM_LIBC_CAST` macro. (#127319)

related: #127238

This patch adds a macro called `LLVM_LIBC_CAST`, similar to
`__BIONIC_CAST`, for type conversion in `endian.h`.

Added: 
    

Modified: 
    libc/include/__llvm-libc-common.h
    libc/include/llvm-libc-macros/endian-macros.h

Removed: 
    


################################################################################
diff  --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index 212e3c6a9446c..c6fd33a55532c 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,6 +47,11 @@
 #define __NOEXCEPT throw()
 #endif
 
+// 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
 
 #undef __BEGIN_C_DECLS
@@ -85,6 +90,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

diff  --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
index e1e105d50c1c6..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) ((uint16_t)(x))
-#define htole32(x) ((uint32_t)(x))
-#define htole64(x) ((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) ((uint16_t)(x))
-#define le32toh(x) ((uint32_t)(x))
-#define le64toh(x) ((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) ((uint16_t)(x))
-#define htobe32(x) ((uint32_t)(x))
-#define htobe64(x) ((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) ((uint16_t)(x))
-#define be32toh(x) ((uint32_t)(x))
-#define be64toh(x) ((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