diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -509,6 +509,15 @@ return (rawData[bitPos / CHAR_BIT] & (1 << (bitPos % CHAR_BIT))) != 0; } +/// Get start position of actual data in `rawData`. Actual data is +/// stored in last `bitWidth`/`CHAR_BIT` bytes in big endian. +static const char *getDataStartPos(const char *dataPos, size_t bitWidth) { + if (llvm::support::endian::system_endianness() == + llvm::support::endianness::big) + dataPos = dataPos + 8 - llvm::divideCeil(bitWidth, CHAR_BIT); + return dataPos; +} + /// Writes value to the bit position `bitPos` in array `rawData`. static void writeBits(char *rawData, size_t bitPos, APInt value) { size_t bitWidth = value.getBitWidth(); @@ -519,11 +528,9 @@ // Otherwise, the bit position is guaranteed to be byte aligned. assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned"); - const char *cptr = reinterpret_cast(value.getRawData()); - if (llvm::support::endian::system_endianness() == - llvm::support::endianness::big) - cptr = cptr + 8 - llvm::divideCeil(bitWidth, CHAR_BIT); - std::copy_n(cptr, llvm::divideCeil(bitWidth, CHAR_BIT), + const char *dataPos = reinterpret_cast(value.getRawData()); + dataPos = getDataStartPos(dataPos, bitWidth); + std::copy_n(dataPos, llvm::divideCeil(bitWidth, CHAR_BIT), rawData + (bitPos / CHAR_BIT)); } @@ -537,13 +544,11 @@ // Otherwise, the bit position must be 8-bit aligned. assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned"); APInt result(bitWidth, 0); - char *cptr = - const_cast(reinterpret_cast(result.getRawData())); - if (llvm::support::endian::system_endianness() == - llvm::support::endianness::big) - cptr = cptr + 8 - llvm::divideCeil(bitWidth, CHAR_BIT); + const char *dataPos = reinterpret_cast(result.getRawData()); + dataPos = getDataStartPos(dataPos, bitWidth); std::copy_n(rawData + (bitPos / CHAR_BIT), - llvm::divideCeil(bitWidth, CHAR_BIT), cptr); + llvm::divideCeil(bitWidth, CHAR_BIT), + const_cast(dataPos)); return result; }