[libc-commits] [libc] [libc] implement endian related macros (PR #126368)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Feb 10 09:19:49 PST 2025


================
@@ -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))
----------------
nickdesaulniers wrote:

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.

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


More information about the libc-commits mailing list