[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-07-01 Thread anlauf at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

anlauf at gcc dot gnu.org changed:

   What|Removed |Added

   Target Milestone|--- |10.5
 Status|ASSIGNED|RESOLVED
 Resolution|--- |FIXED

--- Comment #12 from anlauf at gcc dot gnu.org ---
Fixed for all open branches.  Closing.

Thanks for the report!

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-07-01 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #11 from CVS Commits  ---
The releases/gcc-10 branch has been updated by Harald Anlauf
:

https://gcc.gnu.org/g:e6db08d9183b80b0ada5122fae79412544279f56

commit r10-10877-ge6db08d9183b80b0ada5122fae79412544279f56
Author: Harald Anlauf 
Date:   Tue Jun 21 23:20:18 2022 +0200

Fortran: fix simplification of INDEX(str1,str2) [PR105691]

gcc/fortran/ChangeLog:

PR fortran/105691
* simplify.c (gfc_simplify_index): Replace old simplification
code by the equivalent of the runtime library implementation.  Use
HOST_WIDE_INT instead of int for string index, length variables.

gcc/testsuite/ChangeLog:

PR fortran/105691
* gfortran.dg/index_6.f90: New test.

(cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-07-01 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #10 from CVS Commits  ---
The releases/gcc-11 branch has been updated by Harald Anlauf
:

https://gcc.gnu.org/g:614d9e76b71df6c5ad42f2b9c2a8156e8b3ebd35

commit r11-10102-g614d9e76b71df6c5ad42f2b9c2a8156e8b3ebd35
Author: Harald Anlauf 
Date:   Tue Jun 21 23:20:18 2022 +0200

Fortran: fix simplification of INDEX(str1,str2) [PR105691]

gcc/fortran/ChangeLog:

PR fortran/105691
* simplify.c (gfc_simplify_index): Replace old simplification
code by the equivalent of the runtime library implementation.  Use
HOST_WIDE_INT instead of int for string index, length variables.

gcc/testsuite/ChangeLog:

PR fortran/105691
* gfortran.dg/index_6.f90: New test.

(cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-30 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #9 from CVS Commits  ---
The releases/gcc-12 branch has been updated by Harald Anlauf
:

https://gcc.gnu.org/g:26ea506a1e8719f8b1f559e70bee9f5d3392eb37

commit r12-8530-g26ea506a1e8719f8b1f559e70bee9f5d3392eb37
Author: Harald Anlauf 
Date:   Tue Jun 21 23:20:18 2022 +0200

Fortran: fix simplification of INDEX(str1,str2) [PR105691]

gcc/fortran/ChangeLog:

PR fortran/105691
* simplify.cc (gfc_simplify_index): Replace old simplification
code by the equivalent of the runtime library implementation.  Use
HOST_WIDE_INT instead of int for string index, length variables.

gcc/testsuite/ChangeLog:

PR fortran/105691
* gfortran.dg/index_6.f90: New test.

(cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-26 Thread anlauf at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #8 from anlauf at gcc dot gnu.org ---
Fixed on mainline so far.

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-26 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #7 from CVS Commits  ---
The master branch has been updated by Harald Anlauf :

https://gcc.gnu.org/g:ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd

commit r13-1277-gff35dbc02092fbcd3d814fcd9fe8e871c3f741fd
Author: Harald Anlauf 
Date:   Tue Jun 21 23:20:18 2022 +0200

Fortran: fix simplification of INDEX(str1,str2) [PR105691]

gcc/fortran/ChangeLog:

PR fortran/105691
* simplify.cc (gfc_simplify_index): Replace old simplification
code by the equivalent of the runtime library implementation.  Use
HOST_WIDE_INT instead of int for string index, length variables.

gcc/testsuite/ChangeLog:

PR fortran/105691
* gfortran.dg/index_6.f90: New test.

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-21 Thread sgk at troutmask dot apl.washington.edu via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #6 from Steve Kargl  ---
On Tue, Jun 21, 2022 at 09:28:27PM +, anlauf at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691
> 
> --- Comment #5 from anlauf at gcc dot gnu.org ---
> Submitted version: https://gcc.gnu.org/pipermail/fortran/2022-June/057940.html
> 

Harald, looks like a straight forward translation of the 
library algorithm.  OK to commit.

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-21 Thread anlauf at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #5 from anlauf at gcc dot gnu.org ---
Submitted version: https://gcc.gnu.org/pipermail/fortran/2022-June/057940.html

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-06-21 Thread anlauf at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

anlauf at gcc dot gnu.org changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
 CC||anlauf at gcc dot gnu.org
   Assignee|unassigned at gcc dot gnu.org  |anlauf at gcc dot 
gnu.org

--- Comment #4 from anlauf at gcc dot gnu.org ---
(In reply to kargl from comment #3)
> There is no need to special case the substring length of 1 case with my
> suggested patch.  Here's an update to eliminate the special case.

Frankly speaking, gfc_simplify_index is a mess.  The library code is a relief.
I'd take that as reference.

Thus taking.

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-05-22 Thread kargl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #3 from kargl at gcc dot gnu.org ---
There is no need to special case the substring length of 1 case with my
suggested patch.  Here's an update to eliminate the special case.

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 233cc42137f..fa9938d6a1e 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3589,49 +3589,31 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr
*b, gfc_expr *kind)
  mpz_set_si (result->value.integer, len + 1);
  return result;
}
-  else if (lensub == 1)
+  else
{
- for (i = 0; i < len; i++)
+ /* Start at the tail of the string, offset by the length of the
+substring, and search for a match moving towards the head of
+string.  */
+ for (i = len - lensub; i >= 0; i--)
{
- for (j = 0; j < lensub; j++)
+ /* If the first character does not match, then the rest of the
+string cannot match.  */
+ if (x->value.character.string[i]
+ == y->value.character.string[0])
{
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
+
+ /* Compare substring to starting location in string.  */  
+ for (j = 0, count = 0; j < lensub; j++, count++)
{
- index = len - i + 1;
- goto done;
+ if (x->value.character.string[i + j]
+ != y->value.character.string[j])
+   break;
}
-   }
-   }
-   }
-  else
-   {
- for (i = 0; i < len; i++)
-   {
- for (j = 0; j < lensub; j++)
-   {
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
+
+ if (count == lensub)
{
- start = len - i;
- if (start <= len - lensub)
-   {
- count = 0;
- for (k = 0; k < lensub; k++)
-   if (y->value.character.string[k]
-   == x->value.character.string[k + start])
- count++;
-
- if (count == lensub)
-   {
- index = start + 1;
- goto done;
-   }
-   }
- else
-   {
- continue;
-   }
+ index = i + 1;
+ goto done;
}
}
}

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-05-22 Thread kargl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

kargl at gcc dot gnu.org changed:

   What|Removed |Added

   Last reconfirmed||2022-05-22
 CC||kargl at gcc dot gnu.org
 Ever confirmed|0   |1
   Priority|P3  |P4
 Status|UNCONFIRMED |NEW

--- Comment #2 from kargl at gcc dot gnu.org ---
There is some torched logic looking for the substring, which can never find it.

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 233cc42137f..16b231f5707 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3606,32 +3606,29 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr
*b, gfc_expr *kind)
}
   else
{
- for (i = 0; i < len; i++)
+ /* Start at the tail of the string, offset by the length of the
+substring, and search for a match moving towards the head of
+string.  */
+ for (i = len - lensub; i >= 0; i--)
{
- for (j = 0; j < lensub; j++)
+ /* If the first character does not match, then the rest of the
+string cannot match.  */
+ if (x->value.character.string[i]
+ == y->value.character.string[0])
{
- if (y->value.character.string[j]
- == x->value.character.string[len - i])
+
+ /* Compare substring to starting location in string.  */  
+ for (j = 0, count = 0; j < lensub; j++, count++)
{
- start = len - i;
- if (start <= len - lensub)
-   {
- count = 0;
- for (k = 0; k < lensub; k++)
-   if (y->value.character.string[k]
-   == x->value.character.string[k + start])
- count++;
-
- if (count == lensub)
-   {
- index = start + 1;
- goto done;
-   }
-   }
- else
-   {
- continue;
-   }
+ if (x->value.character.string[i + j]
+ != y->value.character.string[j])
+   break;
+   }
+
+ if (count == lensub)
+   {
+ index = i + 1;
+ goto done;
}
}
}

[Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time

2022-05-22 Thread xecej4 at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #1 from mecej4  ---
When a program contains an expression that involves the INDEX intrinsic
function, and it is possible to calculate its result value at compile time, the
computed value is sometimes incorrect.

program main
   i = index("fortran.f90", "fortran", back=.true.) ! It should get '1'
   call prnt(i)
contains
   subroutine prnt(i)
   integer i
   print *,'i = ',i
   end subroutine
end program

The program prints '0' instead of '1'. Looking at the assembly code generated
shows that the argument value in the argument I passed to PRNT() is zero.