[flang-commits] [flang] [Flang] Shift the data from lower to higher order bits in the big endian environment (PR #73670)

via flang-commits flang-commits at lists.llvm.org
Tue Nov 28 08:58:55 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

Author: None (madanial0)

<details>
<summary>Changes</summary>

Shift the data from lower to higher order bits when memcpy the value in the namelist in the big endian environment


---
Full diff: https://github.com/llvm/llvm-project/pull/73670.diff


1 Files Affected:

- (modified) flang/runtime/edit-input.cpp (+9-1) 


``````````diff
diff --git a/flang/runtime/edit-input.cpp b/flang/runtime/edit-input.cpp
index 4e8c9aa868a691c..305d85afe480ffe 100644
--- a/flang/runtime/edit-input.cpp
+++ b/flang/runtime/edit-input.cpp
@@ -222,7 +222,15 @@ bool EditIntegerInput(
     value = -value;
   }
   if (any || !io.GetConnectionState().IsAtEOF()) {
-    std::memcpy(n, &value, kind); // a blank field means zero
+    // For integer kind <= 4, the value is stored in the lower order bits on
+    // the big endian platform. When memcpy the value, shift the value, shift
+    // the value to the higher order bit.
+    if (!isHostLittleEndian && kind <= 4) {
+      auto l{value.low() << (8 * (sizeof(value.low()) - kind))};
+      std::memcpy(n, &l, kind);
+    } else {
+      std::memcpy(n, &value, kind); // a blank field means zero
+    }
   }
   return any;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/73670


More information about the flang-commits mailing list