-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-libc Author: None (c8ef) ChangesFollow up of #125168. This patch adds endian-related macros to Full diff: https://github.com/llvm/llvm-project/pull/126368.diff 2 Files Affected:
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index ea892a87dbe7ab9..6124fd136ce6d0c 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -314,6 +314,8 @@ add_macro_header(
endian_macros
HDR
endian-macros.h
+ DEPENDS
+ .stdint_macros
)
add_macro_header(
diff --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
index 94e1d60f8ff404f..e1e105d50c1c68a 100644
--- a/libc/include/llvm-libc-macros/endian-macros.h
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -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))
+#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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch!
#define htobe16(x) __builtin_bswap16((x)) | ||
#define htobe32(x) __builtin_bswap32((x)) | ||
#define htobe64(x) __builtin_bswap64((x)) | ||
#define htole16(x) ((uint16_t)(x)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thanks for your review! |
Follow up of llvm#125168. This patch adds endian-related macros to `endian.h`. We utilize compiler built-ins for byte swap functions, which are already included in our minimal supported compiler version.
Follow up of llvm#125168. This patch adds endian-related macros to `endian.h`. We utilize compiler built-ins for byte swap functions, which are already included in our minimal supported compiler version.
Follow up of llvm#125168. This patch adds endian-related macros to `endian.h`. We utilize compiler built-ins for byte swap functions, which are already included in our minimal supported compiler version.
Follow up of #125168.
This patch adds endian-related macros to
endian.h
. We utilize compiler built-ins for byte swap functions, which are already included in our minimal supported compiler version.