[flang-commits] [flang] [Flang] Detect endianness in the preprocessor (PR #132767)

Michael Kruse via flang-commits flang-commits at lists.llvm.org
Mon Mar 24 13:53:52 PDT 2025


================
@@ -189,4 +189,19 @@
 #define RT_OPTNONE_ATTR
 #endif
 
+/* Detect system endianness if it was not explicitly set. */
+#if !defined(FLANG_LITTLE_ENDIAN) && !defined(FLANG_BIG_ENDIAN)
+
+/* We always assume Windows is little endian, otherwise use the GCC compatible
+ * flags. */
+#if defined(_MSC_VER) || defined(_WIN32)
+#define FLANG_LITTLE_ENDIAN 1
+#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+#define FLANG_LITTLE_ENDIAN 1
+#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+#define FLANG_BIG_ENDIAN 1
+#endif
----------------
Meinersbur wrote:

Can you make an `#else` with `#error`? Otherwise it will just hit the `#error` in `Evaluate/common.h` where it is not clear where to fix it. `common.h` should probably `#include` `api-attr.h` directly. I think it is the only use of `FLANG_BIG_ENDIAN`/`FLANG_LITTLE_ENDIAN` anyway, so could be defined there directly.

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


More information about the flang-commits mailing list