[flang-commits] [flang] cd117fa - [flang][runtime] Fix return value for MINVAL/MAXVAL for CHARACTER(kind > 1)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Aug 18 14:30:51 PDT 2022


Author: Peter Klausler
Date: 2022-08-18T14:26:26-07:00
New Revision: cd117fa04b620c9ce4d63ca4d475aa3ec5a1a122

URL: https://github.com/llvm/llvm-project/commit/cd117fa04b620c9ce4d63ca4d475aa3ec5a1a122
DIFF: https://github.com/llvm/llvm-project/commit/cd117fa04b620c9ce4d63ca4d475aa3ec5a1a122.diff

LOG: [flang][runtime] Fix return value for MINVAL/MAXVAL for CHARACTER(kind > 1)

CharacterExtremumAccumulator::GetResult() needs to use byte counts, not wide
character counts, when calling memcpy() & memset().

Differential Revision: https://reviews.llvm.org/D132156

Added: 
    

Modified: 
    flang/runtime/extrema.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/extrema.cpp b/flang/runtime/extrema.cpp
index 709a032911d4..8290ad3c56c4 100644
--- a/flang/runtime/extrema.cpp
+++ b/flang/runtime/extrema.cpp
@@ -419,11 +419,16 @@ template <int KIND, bool IS_MAXVAL> class CharacterExtremumAccumulator {
   void Reinitialize() { extremum_ = nullptr; }
   template <typename A> void GetResult(A *p, int /*zeroBasedDim*/ = -1) const {
     static_assert(std::is_same_v<A, Type>);
+    std::size_t byteSize{array_.ElementBytes()};
     if (extremum_) {
-      std::memcpy(p, extremum_, charLen_);
+      std::memcpy(p, extremum_, byteSize);
     } else {
-      // empty array: result is all zero-valued characters
-      std::memset(p, 0, charLen_);
+      // empty array
+      if constexpr (KIND == 1) { // ASCII
+        *p = IS_MAXVAL ? 0 : 127; // 127 required by standard
+      } else {
+        std::memset(p, IS_MAXVAL ? 0 : 255, byteSize);
+      }
     }
   }
   bool Accumulate(const Type *x) {


        


More information about the flang-commits mailing list