summaryrefslogblamecommitdiff
path: root/experimental/spirv-llvm-translator/endian.patch
blob: 66a4e88bd7f2618e672ad4b9c25a63d6b6bf79f2 (plain) (tree)




































                                                                                                           
--- SPIRV-LLVM-Translator-18.1.6/lib/SPIRV/libSPIRV/SPIRVStream.h.old	2024-10-30 05:39:48.000000000 -0500
+++ SPIRV-LLVM-Translator-18.1.6/lib/SPIRV/libSPIRV/SPIRVStream.h	2024-11-22 20:17:06.139226884 -0600
@@ -43,6 +43,16 @@
 #include "SPIRVDebug.h"
 #include "SPIRVExtInst.h"
 #include "SPIRVModule.h"
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#include <byteswap.h>
+#define cpu_to_le32(x) __bswap32(x)
+#define le32_to_cpu(x) __bswap32(x)
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define cpu_to_le32(x) x
+#define le32_to_cpu(x) x
+#else
+#error Endianness unknown (neither big nor little)
+#endif
 #include <cctype>
 #include <cstdint>
 #include <iostream>
@@ -102,7 +112,7 @@
 const SPIRVDecoder &decodeBinary(const SPIRVDecoder &I, T &V) {
   uint32_t W;
   I.IS.read(reinterpret_cast<char *>(&W), sizeof(W));
-  V = static_cast<T>(W);
+  V = static_cast<T>(le32_to_cpu(W));
   SPIRVDBG(spvdbgs() << "Read word: W = " << W << " V = " << V << '\n');
   return I;
 }
@@ -185,7 +195,7 @@
     return O;
   }
 #endif
-  uint32_t W = static_cast<uint32_t>(V);
+  uint32_t W = cpu_to_le32(static_cast<uint32_t>(V));
   O.OS.write(reinterpret_cast<char *>(&W), sizeof(W));
   return O;
 }