public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug ada/30740]  New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
@ 2007-02-09  0:53 jaffem at erau dot edu
  2007-02-09 14:34 ` [Bug ada/30740] " ludovic at ludovic-brenta dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: jaffem at erau dot edu @ 2007-02-09  0:53 UTC (permalink / raw)
  To: gcc-bugs

The problem appears in the evaluation of certain expressions with modular
variables.  Without the use of the -gnato option during compilation, the
executable produced by gnat 3.15p on Solaris for the source code shown below
produces incorrect results.  Here's a sample output line from this program:
______________________________________________________________________________
When J=2, Test_Pattern*2**J =124 or 2#1111100#   but Test_Pattern*2**2 = 60 or
2#111100#
______________________________________________________________________________

60 is correct, 124 is incorrect.

So the default behavior of the compiler does not appear to match the semantics
of the Ada language standard for modular values and the "workaround" requires
use of an option (-gnato) that should not be influencing the behavior of this
program. (Overflow is not possible for modular values; they must always wrap
around.) 

See
http://groups.google.com/group/comp.lang.ada/browse_thread/thread/4eca860272d4832b/3bca35e8bb0bdb52?hl=en#3bca35e8bb0bdb52
for a complete discussion of this problem.  

Here's the source code of a program that demonstrates this problem in my
environment and, according to others on comp.lang.ada, other environments as
well:

with Ada.Text_IO; use Ada.Text_IO;
procedure Strange_Typing is

   type Six_Bits is mod 2**6;

   package Six_Bit_IO is new Ada.Text_IO.Modular_IO(Six_Bits);
   use Six_Bit_IO;

   Test_Pattern : Six_Bits := 31;         -- 5 bits of ones

begin

   Put("The test pattern is a modulo 2**6 integer = ");
   Put(Test_Pattern, 0);
                                           Put(" or ");
   Put(Test_Pattern, 0, 2);
   New_Line(2);

   for J in 0 .. 5 loop

      Put("When J=");
      Put(Six_Bits(J), 0); Put(", ");
      Put("Test_Pattern*2**J =");
      Six_Bit_IO.Put( Test_Pattern*2**J);             Put(" or ");
      Six_Bit_IO.Put( Test_Pattern*2**J, 0, 2);

      Put(Ascii.Ht & " but ");

      case J is
         when 0 =>
            Put("Test_Pattern*2**0 = ");
            Put( Test_Pattern*2**0, 0);    Put(" or ");
            Put( Test_Pattern*2**0, 0, 2);
        when 1 =>
            Put("Test_Pattern*2**1 = ");
            Put( Test_Pattern*2**1, 0);    Put(" or ");
            Put( Test_Pattern*2**1, 0, 2);
         when 2 =>
            Put("Test_Pattern*2**2 = ");
            Put( Test_Pattern*2**2, 0);    Put(" or ");
            Put( Test_Pattern*2**2, 0, 2);
          when 3 =>
            Put("Test_Pattern*2**3 = ");
            Put( Test_Pattern*2**3, 0);    Put(" or ");
            Put( Test_Pattern*2**3, 0, 2);
         when 4 =>
            Put("Test_Pattern*2**4 = ");
            Put( Test_Pattern*2**4, 0);    Put(" or ");
            Put( Test_Pattern*2**4, 0, 2);
         when 5 =>
            Put("Test_Pattern*2**5 = ");
            Put( Test_Pattern*2**5, 0);    Put(" or ");
            Put( Test_Pattern*2**5, 0, 2);
      end case;

      New_Line;

   end loop;

   New_Line;

end Strange_Typing;


-- 
           Summary: Improper semantics in gnat's compilation of certain
                    expressions involving modular arithmetic
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jaffem at erau dot edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
@ 2007-02-09 14:34 ` ludovic at ludovic-brenta dot org
  2007-02-10  0:22 ` jaffem at erau dot edu
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: ludovic at ludovic-brenta dot org @ 2007-02-09 14:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from ludovic at ludovic-brenta dot org  2007-02-09 14:34 -------
Shorter test case that reproduces the problem.

with Ada.Text_IO;
procedure test1 is
   type T1 is mod 9;
   package T1_IO is new Ada.Text_IO.Modular_IO(T1);
   X: T1 := 8;
   J1: constant := 5;
begin
   for J2 in 5..5 loop
      pragma assert(X*(2**J1) = X*(2**J2));
      if X*(2**J1) /= X*(2**J2) then
         Ada.Text_IO.Put_Line("Failed");
         T1_IO.Put(X*(2**J1));
         T1_IO.Put(X*(2**J2));
         Ada.Text_IO.New_Line;
      end if;
   end loop;
end test1;

prints:

   Failed
    4 0

To reproduce: gnatmake test1

To reproduce with correct behaviour (no output): gnatmake -gnato test1

Notice that when multiplying by 2**J1, the result is always correct because J2
is a named number and the calculation is done at compile time with the correct
semantics.  In contrast, J2 is non-static and exhibits the wrong semantics
unless we add -gnato.

Problem reproduced with GCC 4.1.1 but it's been there ever since 3.15p or even
before.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
  2007-02-09 14:34 ` [Bug ada/30740] " ludovic at ludovic-brenta dot org
@ 2007-02-10  0:22 ` jaffem at erau dot edu
  2007-11-25 14:26 ` sam at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jaffem at erau dot edu @ 2007-02-10  0:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from jaffem at erau dot edu  2007-02-10 00:21 -------
Subject: Re:  Improper semantics in gnat's compilation of certain
 expressions involving modular arithmetic

For what it's worth (not much, I suspect ;-) I concur that this program, 
below, correctly demonstrates the same bug that I reported --- and does 
so a great deal more concisely than the demo program I sent in with the 
bug report.

ludovic at ludovic-brenta dot org wrote:
> ------- Comment #1 from ludovic at ludovic-brenta dot org  2007-02-09 14:34 -------
> Shorter test case that reproduces the problem.
>
> with Ada.Text_IO;
> procedure test1 is
>    type T1 is mod 9;
>    package T1_IO is new Ada.Text_IO.Modular_IO(T1);
>    X: T1 := 8;
>    J1: constant := 5;
> begin
>    for J2 in 5..5 loop
>       pragma assert(X*(2**J1) = X*(2**J2));
>       if X*(2**J1) /= X*(2**J2) then
>          Ada.Text_IO.Put_Line("Failed");
>          T1_IO.Put(X*(2**J1));
>          T1_IO.Put(X*(2**J2));
>          Ada.Text_IO.New_Line;
>       end if;
>    end loop;
> end test1;
>
> prints:
>
>    Failed
>     4 0
>
> To reproduce: gnatmake test1
>
> To reproduce with correct behaviour (no output): gnatmake -gnato test1
>
> Notice that when multiplying by 2**J1, the result is always correct because J2
> is a named number and the calculation is done at compile time with the correct
> semantics.  In contrast, J2 is non-static and exhibits the wrong semantics
> unless we add -gnato.
>
> Problem reproduced with GCC 4.1.1 but it's been there ever since 3.15p or even
> before.
>
>
>   


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
  2007-02-09 14:34 ` [Bug ada/30740] " ludovic at ludovic-brenta dot org
  2007-02-10  0:22 ` jaffem at erau dot edu
@ 2007-11-25 14:26 ` sam at gcc dot gnu dot org
  2008-05-20 12:39 ` charlet at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: sam at gcc dot gnu dot org @ 2007-11-25 14:26 UTC (permalink / raw)
  To: gcc-bugs



-- 

sam at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |sam at gcc dot gnu dot org
                   |dot org                     |
             Status|UNCONFIRMED                 |ASSIGNED
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2007-11-25 14:26:23
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
                   ` (2 preceding siblings ...)
  2007-11-25 14:26 ` sam at gcc dot gnu dot org
@ 2008-05-20 12:39 ` charlet at gcc dot gnu dot org
  2008-05-20 12:46 ` charlet at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: charlet at gcc dot gnu dot org @ 2008-05-20 12:39 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from charlet at gcc dot gnu dot org  2008-05-20 12:38 -------
About to commit proper patch from Robert Dewar.


-- 

charlet at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|sam at gcc dot gnu dot org  |charlet at gcc dot gnu dot
                   |                            |org
   Target Milestone|---                         |4.4.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
                   ` (3 preceding siblings ...)
  2008-05-20 12:39 ` charlet at gcc dot gnu dot org
@ 2008-05-20 12:46 ` charlet at gcc dot gnu dot org
  2008-05-20 13:08 ` charlet at gcc dot gnu dot org
  2008-05-20 20:26 ` sam at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: charlet at gcc dot gnu dot org @ 2008-05-20 12:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from charlet at gcc dot gnu dot org  2008-05-20 12:45 -------
Subject: Bug 30740

Author: charlet
Date: Tue May 20 12:44:55 2008
New Revision: 135619

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135619
Log:
2008-05-20  Robert Dewar  <dewar@adacore.com>

        PR ada/30740
        * einfo.ads, einfo.adb (Non_Binary_Modulus): Applies to all types and
        subtypes, always False for non-modular types.
        Shared_Var_Assign_Proc (node22) and Shared_Var_Read_Proc (node 15)
        entry nodes have been replaced by Shared_Var_Procs_Instance (node22)
        for Shared_Storage package.
        (Is_RACW_Stub_Type): New entity flag.

        * exp_ch4.adb
        (Expand_N_Op_Expon): Avoid incorrect optimization of a*(2**b) in the
        case where we have a modular type with a non-binary modules.
        Comments reformattings.

        * sem_intr.adb: Simplify code not that Non_Binary_Modulus applies to
        all types.


Modified:
    trunk/gcc/ada/einfo.adb
    trunk/gcc/ada/einfo.ads
    trunk/gcc/ada/exp_ch4.adb
    trunk/gcc/ada/sem_intr.adb


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
                   ` (4 preceding siblings ...)
  2008-05-20 12:46 ` charlet at gcc dot gnu dot org
@ 2008-05-20 13:08 ` charlet at gcc dot gnu dot org
  2008-05-20 20:26 ` sam at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: charlet at gcc dot gnu dot org @ 2008-05-20 13:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from charlet at gcc dot gnu dot org  2008-05-20 13:07 -------
Fixed.


-- 

charlet at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug ada/30740] Improper semantics in gnat's compilation of certain expressions involving modular arithmetic
  2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
                   ` (5 preceding siblings ...)
  2008-05-20 13:08 ` charlet at gcc dot gnu dot org
@ 2008-05-20 20:26 ` sam at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: sam at gcc dot gnu dot org @ 2008-05-20 20:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from sam at gcc dot gnu dot org  2008-05-20 20:25 -------
Subject: Bug 30740

Author: sam
Date: Tue May 20 20:24:33 2008
New Revision: 135675

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135675
Log:
    gcc/testsuite/
        PR ada/30740
        * gnat.dg/modular.adb: New test.

Added:
    trunk/gcc/testsuite/gnat.dg/modular.adb
Modified:
    trunk/gcc/testsuite/ChangeLog


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30740


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-05-20 20:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-09  0:53 [Bug ada/30740] New: Improper semantics in gnat's compilation of certain expressions involving modular arithmetic jaffem at erau dot edu
2007-02-09 14:34 ` [Bug ada/30740] " ludovic at ludovic-brenta dot org
2007-02-10  0:22 ` jaffem at erau dot edu
2007-11-25 14:26 ` sam at gcc dot gnu dot org
2008-05-20 12:39 ` charlet at gcc dot gnu dot org
2008-05-20 12:46 ` charlet at gcc dot gnu dot org
2008-05-20 13:08 ` charlet at gcc dot gnu dot org
2008-05-20 20:26 ` sam at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).