|
18 | 18 | #include "llvm/Support/Allocator.h"
|
19 | 19 | #include "llvm/Support/ErrorHandling.h"
|
20 | 20 | #include "llvm/Support/MathExtras.h"
|
| 21 | +#include "llvm/System/Host.h" |
21 | 22 | #include <cassert>
|
22 | 23 | #include <cstring>
|
23 | 24 | using namespace llvm;
|
@@ -110,18 +111,32 @@ void FoldingSetNodeID::AddString(StringRef String) {
|
110 | 111 | Pos = (Units + 1) * 4;
|
111 | 112 | } else {
|
112 | 113 | // Otherwise do it the hard way.
|
113 |
| - for (Pos += 4; Pos <= Size; Pos += 4) { |
114 |
| - unsigned V = ((unsigned char)String[Pos - 4] << 24) | |
115 |
| - ((unsigned char)String[Pos - 3] << 16) | |
116 |
| - ((unsigned char)String[Pos - 2] << 8) | |
117 |
| - (unsigned char)String[Pos - 1]; |
118 |
| - Bits.push_back(V); |
| 114 | + // To be compatible with above bulk transfer, we need to take endianness |
| 115 | + // into account. |
| 116 | + if (sys::isBigEndianHost()) { |
| 117 | + for (Pos += 4; Pos <= Size; Pos += 4) { |
| 118 | + unsigned V = ((unsigned char)String[Pos - 4] << 24) | |
| 119 | + ((unsigned char)String[Pos - 3] << 16) | |
| 120 | + ((unsigned char)String[Pos - 2] << 8) | |
| 121 | + (unsigned char)String[Pos - 1]; |
| 122 | + Bits.push_back(V); |
| 123 | + } |
| 124 | + } else { |
| 125 | + assert(sys::isLittleEndianHost() && "Unexpected host endianness"); |
| 126 | + for (Pos += 4; Pos <= Size; Pos += 4) { |
| 127 | + unsigned V = ((unsigned char)String[Pos - 1] << 24) | |
| 128 | + ((unsigned char)String[Pos - 2] << 16) | |
| 129 | + ((unsigned char)String[Pos - 3] << 8) | |
| 130 | + (unsigned char)String[Pos - 4]; |
| 131 | + Bits.push_back(V); |
| 132 | + } |
119 | 133 | }
|
120 | 134 | }
|
121 | 135 |
|
122 | 136 | // With the leftover bits.
|
123 | 137 | unsigned V = 0;
|
124 |
| - // Pos will have overshot size by 4 - #bytes left over. |
| 138 | + // Pos will have overshot size by 4 - #bytes left over. |
| 139 | + // No need to take endianness into account here - this is always executed. |
125 | 140 | switch (Pos - Size) {
|
126 | 141 | case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
|
127 | 142 | case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
|
|
0 commit comments