[PATCH] D78076: [mlir] Support big endian in DenseElementsAttr

Haruki Imai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 09:06:44 PDT 2020


imaihal updated this revision to Diff 260660.
imaihal added a comment.
Herald added a subscriber: Kayjukh.

[mlir] Added copy_n() in static helper


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78076/new/

https://reviews.llvm.org/D78076

Files:
  mlir/lib/IR/Attributes.cpp


Index: mlir/lib/IR/Attributes.cpp
===================================================================
--- mlir/lib/IR/Attributes.cpp
+++ mlir/lib/IR/Attributes.cpp
@@ -524,15 +524,29 @@
   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) {
+/// Get start position of actual data in `value`. Actual data is
+/// stored in last `bitWidth`/CHAR_BIT bytes in big endian.
+static char *getAPIntDataPos(APInt &value, size_t bitWidth) {
+  char *dataPos =
+      const_cast<char *>(reinterpret_cast<const char *>(value.getRawData()));
   if (llvm::support::endian::system_endianness() ==
       llvm::support::endianness::big)
     dataPos = dataPos + 8 - llvm::divideCeil(bitWidth, CHAR_BIT);
   return dataPos;
 }
 
+/// Read APInt `value` from appropriate position.
+static void readAPInt(APInt &value, size_t bitWidth, char *outData) {
+  char *dataPos = getAPIntDataPos(value, bitWidth);
+  std::copy_n(dataPos, llvm::divideCeil(bitWidth, CHAR_BIT), outData);
+}
+
+/// Write `inData` to appropriate position of APInt `value`.
+static void writeAPInt(const char *inData, size_t bitWidth, APInt &value) {
+  char *dataPos = getAPIntDataPos(value, bitWidth);
+  std::copy_n(inData, llvm::divideCeil(bitWidth, CHAR_BIT), 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();
@@ -543,10 +557,7 @@
 
   // Otherwise, the bit position is guaranteed to be byte aligned.
   assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned");
-  const char *dataPos = reinterpret_cast<const char *>(value.getRawData());
-  dataPos = getDataStartPos(dataPos, bitWidth);
-  std::copy_n(dataPos, llvm::divideCeil(bitWidth, CHAR_BIT),
-              rawData + (bitPos / CHAR_BIT));
+  readAPInt(value, bitWidth, rawData + (bitPos / CHAR_BIT));
 }
 
 /// Reads the next `bitWidth` bits from the bit position `bitPos` in array
@@ -559,11 +570,7 @@
   // 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);
-  const char *dataPos = reinterpret_cast<const char *>(result.getRawData());
-  dataPos = getDataStartPos(dataPos, bitWidth);
-  std::copy_n(rawData + (bitPos / CHAR_BIT),
-              llvm::divideCeil(bitWidth, CHAR_BIT),
-              const_cast<char *>(dataPos));
+  writeAPInt(rawData + (bitPos / CHAR_BIT), bitWidth, result);
   return result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78076.260660.patch
Type: text/x-patch
Size: 2731 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200428/c3603c6a/attachment-0001.bin>


More information about the llvm-commits mailing list