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

[libc] implement endian related macros #126368

merged 1 commit into from
Feb 12, 2025

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Feb 8, 2025

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.

@c8ef c8ef marked this pull request as ready for review February 8, 2025 13:48
@llvmbot llvmbot added the libc label Feb 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 8, 2025

@llvm/pr-subscribers-libc

Author: None (c8ef)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/126368.diff

2 Files Affected:

  • (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+2)
  • (modified) libc/include/llvm-libc-macros/endian-macros.h (+34)
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

Copy link
Member

@nickdesaulniers nickdesaulniers left a 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))
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.

@c8ef
Copy link
Contributor Author

c8ef commented Feb 12, 2025

Thanks for your review!

@c8ef c8ef merged commit 6de4de8 into llvm:main Feb 12, 2025
17 checks passed
@c8ef c8ef deleted the endian branch February 12, 2025 02:17
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
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.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
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.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants