Skip to content

[libc] implement endian related macros #126368

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 1 commit into from
Feb 12, 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
2 changes: 2 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ add_macro_header(
endian_macros
HDR
endian-macros.h
DEPENDS
.stdint_macros
)

add_macro_header(
Expand Down
34 changes: 34 additions & 0 deletions libc/include/llvm-libc-macros/endian-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,42 @@
#ifndef LLVM_LIBC_MACROS_ENDIAN_MACROS_H
#define LLVM_LIBC_MACROS_ENDIAN_MACROS_H

#include "stdint-macros.h"

#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
#define BYTE_ORDER __BYTE_ORDER__

#if BYTE_ORDER == LITTLE_ENDIAN

#define htobe16(x) __builtin_bswap16((x))
#define htobe32(x) __builtin_bswap32((x))
#define htobe64(x) __builtin_bswap64((x))
#define htole16(x) ((uint16_t)(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the casts? For example, bionic elides them.
https://android.googlesource.com/platform/bionic/+/main/libc/include/sys/endian.h#85
If we don't then we can also drop stdint-macros.h.
If we do, then we should fully qualify the include path to stdint-macros.h. While it may build with cmake, we do have downstream consumers that aren't using cmake; and they require fully qualified paths for these headers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do, then we should fully qualify the include path to stdint-macros.h. While it may build with cmake, we do have downstream consumers that aren't using cmake; and they require fully qualified paths for these headers.

Sorry, that's wrong. Nevermind that point. But the question still stands about the casts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the standard, including the <endian.h> header may also make all symbols from <stdint.h> visible. I believe we should include the related header.
Regarding type conversion, we could maintain consistency with not only glibc, musl, and freebsd, but also with the function signature provided by the standard. And the compiler may optimize unnecessary type conversions for us.

#define htole32(x) ((uint32_t)(x))
#define htole64(x) ((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))

#else

#define htobe16(x) ((uint16_t)(x))
#define htobe32(x) ((uint32_t)(x))
#define htobe64(x) ((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 le16toh(x) __builtin_bswap16((x))
#define le32toh(x) __builtin_bswap32((x))
#define le64toh(x) __builtin_bswap64((x))

#endif

#endif // LLVM_LIBC_MACROS_ENDIAN_MACROS_H