[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`.
This commit is contained in:
c8ef
2025-02-19 23:28:11 +08:00
committed by GitHub
parent 4624087328
commit 826af1757c
2 changed files with 20 additions and 12 deletions

View File

@@ -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

View File

@@ -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))