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

Haruki Imai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 07:49:11 PDT 2020


imaihal updated this revision to Diff 258054.
imaihal added a comment.

[mlir] Create a static helper and add comments


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
@@ -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<const char *>(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<const char *>(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<char *>(reinterpret_cast<const char *>(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<const char *>(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<char *>(dataPos));
   return result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78076.258054.patch
Type: text/x-patch
Size: 2316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200416/0606749a/attachment.bin>


More information about the llvm-commits mailing list