Skip to content

[libc] add LLVM_LIBC_CAST macro. #127319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions libc/include/__llvm-libc-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 12 additions & 12 deletions libc/include/llvm-libc-macros/endian-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading