Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][runtime] Fix octal input of REAL(10) #74658

Merged
merged 1 commit into from
Dec 11, 2023
Merged

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Dec 6, 2023

The overflow check didn't work for a 27-digit octal field containing an 80-bit x87 extended precision real value. The count of bytes required was too large because the leading digit (1) was assumed to require a full three bits. And the actual transmission of the octal input digits to the output buffer was incorrect, too.

Fixes llvm-test-suite/Fortran/gfortran/regression/boz_15.f90.

@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Dec 6, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 6, 2023

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

The overflow check didn't work for a 27-digit octal field containing an 80-bit x87 extended precision real value. The count of bytes required was too large because the leading digit (1) was assumed to require a full three bits. And the actual transmission of the octal input digits to the output buffer was incorrect, too.

Fixes llvm-test-suite/Fortran/gfortran/regression/boz_15.f90.


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

1 Files Affected:

  • (modified) flang/runtime/edit-input.cpp (+20-13)
diff --git a/flang/runtime/edit-input.cpp b/flang/runtime/edit-input.cpp
index 4e8c9aa868a69..db1724ec3a622 100644
--- a/flang/runtime/edit-input.cpp
+++ b/flang/runtime/edit-input.cpp
@@ -64,11 +64,9 @@ static bool EditBOZInput(
   }
   // Count significant digits after any leading white space & zeroes
   int digits{0};
+  int significantBits{0};
   for (; next; next = io.NextInField(remaining, edit)) {
     char32_t ch{*next};
-    if (ch == ' ' || ch == '\t') {
-      continue;
-    }
     if (ch >= '0' && ch <= '1') {
     } else if (LOG2_BASE >= 3 && ch >= '2' && ch <= '7') {
     } else if (LOG2_BASE >= 4 && ch >= '8' && ch <= '9') {
@@ -79,9 +77,22 @@ static bool EditBOZInput(
           "Bad character '%lc' in B/O/Z input field", ch);
       return false;
     }
-    ++digits;
+    if (digits++ == 0) {
+      significantBits = 4;
+      if (ch >= '0' && ch <= '1') {
+        significantBits = 1;
+      } else if (ch >= '2' && ch <= '3') {
+        significantBits = 2;
+      } else if (ch >= '4' && ch <= '7') {
+        significantBits = 3;
+      } else {
+        significantBits = 4;
+      }
+    } else {
+      significantBits += LOG2_BASE;
+    }
   }
-  auto significantBytes{static_cast<std::size_t>(digits * LOG2_BASE + 7) / 8};
+  auto significantBytes{static_cast<std::size_t>(significantBits + 7) / 8};
   if (significantBytes > bytes) {
     io.GetIoErrorHandler().SignalError(IostatBOZInputOverflow,
         "B/O/Z input of %d digits overflows %zd-byte variable", digits, bytes);
@@ -96,10 +107,7 @@ static bool EditBOZInput(
   auto *data{reinterpret_cast<unsigned char *>(n) +
       (isHostLittleEndian ? significantBytes - 1 : 0)};
   int shift{((digits - 1) * LOG2_BASE) & 7};
-  if (shift + LOG2_BASE > 8) {
-    shift -= 8; // misaligned octal
-  }
-  while (digits > 0) {
+  while (digits-- > 0) {
     char32_t ch{*io.NextInField(remaining, edit)};
     int digit{0};
     if (ch >= '0' && ch <= '9') {
@@ -111,12 +119,11 @@ static bool EditBOZInput(
     } else {
       continue;
     }
-    --digits;
     if (shift < 0) {
-      shift += 8;
-      if (shift + LOG2_BASE > 8) { // misaligned octal
-        *data |= digit >> (8 - shift);
+      if (shift + LOG2_BASE > 0) { // misaligned octal
+        *data |= digit >> -shift;
       }
+      shift += 8;
       data += increment;
     }
     *data |= digit << shift;

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The overflow check didn't work for a 27-digit octal field
containing an 80-bit x87 extended precision real value.
The count of bytes required was too large because the leading
digit (1) was assumed to require a full three bits.
And the actual transmission of the octal input digits to
the output buffer was incorrect, too.

Fixes llvm-test-suite/Fortran/gfortran/regression/boz_15.f90.
@klausler klausler merged commit 4f9cb79 into llvm:main Dec 11, 2023
4 checks passed
@klausler klausler deleted the boz15 branch December 11, 2023 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants