Skip to content

Commit 826af17

Browse files
authored
[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`.
1 parent 4624087 commit 826af17

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

libc/include/__llvm-libc-common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
#define __NOEXCEPT throw()
4848
#endif
4949

50+
// This macro serves as a generic cast implementation for use in both C and C++,
51+
// similar to `__BIONIC_CAST` in Android.
52+
#undef __LLVM_LIBC_CAST
53+
#define __LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
54+
5055
#else // not __cplusplus
5156

5257
#undef __BEGIN_C_DECLS
@@ -85,6 +90,9 @@
8590
#undef _Returns_twice
8691
#define _Returns_twice __attribute__((returns_twice))
8792

93+
#undef __LLVM_LIBC_CAST
94+
#define __LLVM_LIBC_CAST(cast, type, value) ((type)(value))
95+
8896
#endif // __cplusplus
8997

9098
#endif // _LLVM_LIBC_COMMON_H

libc/include/llvm-libc-macros/endian-macros.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@
2020
#define htobe16(x) __builtin_bswap16((x))
2121
#define htobe32(x) __builtin_bswap32((x))
2222
#define htobe64(x) __builtin_bswap64((x))
23-
#define htole16(x) ((uint16_t)(x))
24-
#define htole32(x) ((uint32_t)(x))
25-
#define htole64(x) ((uint64_t)(x))
23+
#define htole16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
24+
#define htole32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
25+
#define htole64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
2626
#define be16toh(x) __builtin_bswap16((x))
2727
#define be32toh(x) __builtin_bswap32((x))
2828
#define be64toh(x) __builtin_bswap64((x))
29-
#define le16toh(x) ((uint16_t)(x))
30-
#define le32toh(x) ((uint32_t)(x))
31-
#define le64toh(x) ((uint64_t)(x))
29+
#define le16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
30+
#define le32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
31+
#define le64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
3232

3333
#else
3434

35-
#define htobe16(x) ((uint16_t)(x))
36-
#define htobe32(x) ((uint32_t)(x))
37-
#define htobe64(x) ((uint64_t)(x))
35+
#define htobe16(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
36+
#define htobe32(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
37+
#define htobe64(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
3838
#define htole16(x) __builtin_bswap16((x))
3939
#define htole32(x) __builtin_bswap32((x))
4040
#define htole64(x) __builtin_bswap64((x))
41-
#define be16toh(x) ((uint16_t)(x))
42-
#define be32toh(x) ((uint32_t)(x))
43-
#define be64toh(x) ((uint64_t)(x))
41+
#define be16toh(x) __LLVM_LIBC_CAST(static_cast, uint16_t, x)
42+
#define be32toh(x) __LLVM_LIBC_CAST(static_cast, uint32_t, x)
43+
#define be64toh(x) __LLVM_LIBC_CAST(static_cast, uint64_t, x)
4444
#define le16toh(x) __builtin_bswap16((x))
4545
#define le32toh(x) __builtin_bswap32((x))
4646
#define le64toh(x) __builtin_bswap64((x))

0 commit comments

Comments
 (0)