comp.lang.ada
 help / color / mirror / Atom feed
* Feature or Bug?  2#1111# shifted by 4 is 224
@ 2008-06-02 15:17 Dennis Hoppe
  2008-06-02 16:11 ` Tero Koskinen
  2008-06-02 22:32 ` Gautier
  0 siblings, 2 replies; 13+ messages in thread
From: Dennis Hoppe @ 2008-06-02 15:17 UTC (permalink / raw)


Hi,

we probably found a bug in Ada or we cannot realize, that this is indeed 
a feature ;)

In Example A, we defined a simple modular shift to the left by means of 
Item * 2**N. N is defined as an Integer. This example works well and 
Shift_Left(2#1111#, 4) returns 0.

-- Example A
type Modular_Type is mod 2**4;
function Shift_Left (Item : in Modular_Type;
                      N    : in Integer) return Modular_Type is
begin
   return Item * 2**N;
end Shift_Left;


In Example B, we limit the parameter N to Positive, because we do not 
want "negative" shifts. The result of Shift_Left(2#1111#, 4) is 224
(2#11100000#) !

-- Example B
type Modular_Type is mod 2**4;
function Shift_Left (Item : in Modular_Type;
                      N    : in Positive) return Modular_Type is
begin
   return Item * 2**N;
end Shift_Left;



Why does this happen? Positive is defined as all Integer values starting 
at 1 through the highest value of Integer, so the result should also be 
0. Especially, because the returning value is additionally limited by 
mod 2**4.

Thanks,
   Dennis Hoppe



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 15:17 Feature or Bug? 2#1111# shifted by 4 is 224 Dennis Hoppe
@ 2008-06-02 16:11 ` Tero Koskinen
  2008-06-02 17:59   ` Ludovic Brenta
  2008-06-02 22:32 ` Gautier
  1 sibling, 1 reply; 13+ messages in thread
From: Tero Koskinen @ 2008-06-02 16:11 UTC (permalink / raw)


On Mon, 02 Jun 2008 17:17:19 +0200 Dennis Hoppe wrote:
> Hi,
> 
> we probably found a bug in Ada or we cannot realize, that this is indeed 
> a feature ;)
[snip]
> 
> In Example B, we limit the parameter N to Positive, because we do not 
> want "negative" shifts. The result of Shift_Left(2#1111#, 4) is 224
> (2#11100000#) !
> 
> -- Example B
> type Modular_Type is mod 2**4;
> function Shift_Left (Item : in Modular_Type;
>                       N    : in Positive) return Modular_Type is
> begin
>    return Item * 2**N;
> end Shift_Left;
> 
> Why does this happen?

It is a bug in GCC on i386 platform. I can repeat it with GNAT GPL 2007
and GCC 4.3.0 (OpenBSD/i386). With GCC 4.3.0 optimization flags
(-O2 vs. -O0) seem to affect to the result.

On Solaris (5.10 Generic_127111-05 sun4u sparc SUNW,Sun-Fire-V240) with
GCC 4.2.1 the example returns 0 always.

Here is more complete test case:
with Ada.Text_IO; use Ada.Text_IO;
procedure Shift is

type Modular_Type is mod 2**4;
function Shift_Left (Item : in Modular_Type;
                     N    : in Positive) return Modular_Type is
begin
   return Item * (2**N);
end Shift_Left;

    X : Modular_Type;
begin
    X := Shift_Left(2#1111#, 4);
    Put_Line (X'Img);
end Shift;

-- 
Tero Koskinen - http://iki.fi/tero.koskinen/



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 16:11 ` Tero Koskinen
@ 2008-06-02 17:59   ` Ludovic Brenta
  2008-06-02 18:52     ` Dennis Hoppe
  2008-06-04  3:36     ` Tero Koskinen
  0 siblings, 2 replies; 13+ messages in thread
From: Ludovic Brenta @ 2008-06-02 17:59 UTC (permalink / raw)


Tero Koskinen <tero.koskinen@iki.fi> writes:
> On Mon, 02 Jun 2008 17:17:19 +0200 Dennis Hoppe wrote:
>> Hi,
>> 
>> we probably found a bug in Ada or we cannot realize, that this is indeed 
>> a feature ;)
> [snip]
>> 
>> In Example B, we limit the parameter N to Positive, because we do not 
>> want "negative" shifts. The result of Shift_Left(2#1111#, 4) is 224
>> (2#11100000#) !
>> 
>> -- Example B
>> type Modular_Type is mod 2**4;
>> function Shift_Left (Item : in Modular_Type;
>>                       N    : in Positive) return Modular_Type is
>> begin
>>    return Item * 2**N;
>> end Shift_Left;
>> 
>> Why does this happen?
>
> It is a bug in GCC on i386 platform. I can repeat it with GNAT GPL 2007
> and GCC 4.3.0 (OpenBSD/i386). With GCC 4.3.0 optimization flags
> (-O2 vs. -O0) seem to affect to the result.
>
> On Solaris (5.10 Generic_127111-05 sun4u sparc SUNW,Sun-Fire-V240) with
> GCC 4.2.1 the example returns 0 always.
>
> Here is more complete test case:
> with Ada.Text_IO; use Ada.Text_IO;
> procedure Shift is
>
> type Modular_Type is mod 2**4;
> function Shift_Left (Item : in Modular_Type;
>                      N    : in Positive) return Modular_Type is
> begin
>    return Item * (2**N);
> end Shift_Left;
>
>     X : Modular_Type;
> begin
>     X := Shift_Left(2#1111#, 4);
>     Put_Line (X'Img);
> end Shift;

This looks like http://gcc.gnu.org/PR30740 which is fixed in Debian
unstable :)

Could you confirm that this is the same bug indeed?

-- 
Ludovic Brenta.




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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 17:59   ` Ludovic Brenta
@ 2008-06-02 18:52     ` Dennis Hoppe
  2008-06-03  0:51       ` Jeffrey R. Carter
                         ` (2 more replies)
  2008-06-04  3:36     ` Tero Koskinen
  1 sibling, 3 replies; 13+ messages in thread
From: Dennis Hoppe @ 2008-06-02 18:52 UTC (permalink / raw)


Ludovic Brenta wrote:
 > This looks like http://gcc.gnu.org/PR30740 which is fixed in Debian
 > unstable :)
 >
 > Could you confirm that this is the same bug indeed?
 >


Hello Ludovic,

i tried the example source given at http://gcc.gnu.org/PR30740 and it
fails on my machine. If I compile the same source with -gnato, the
results are correct.

The same behaviour can be reproduced by the code I posted above.
Again, if I compile my source mit the -gnato flag, the program
compute the shift operation correct.

The bug seems still to exist.

Tested on Mac OS X 10.5
gcc (GCC) 4.4.0 20080314 (experimental) [trunk revision 133226]
Intel Core 2 Duo



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 15:17 Feature or Bug? 2#1111# shifted by 4 is 224 Dennis Hoppe
  2008-06-02 16:11 ` Tero Koskinen
@ 2008-06-02 22:32 ` Gautier
  1 sibling, 0 replies; 13+ messages in thread
From: Gautier @ 2008-06-02 22:32 UTC (permalink / raw)


Dennis Hoppe:

> we probably found a bug in Ada or we cannot realize, that this is indeed 
> a feature ;)

Ada /= GNAT...
_________________________________________________________
Gautier's Ada programming -- http://sf.net/users/gdemont/

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 18:52     ` Dennis Hoppe
@ 2008-06-03  0:51       ` Jeffrey R. Carter
  2008-06-03  4:26         ` Gautier
  2008-06-03 17:56         ` Adam Beneschan
  2008-06-03  8:10       ` Ludovic Brenta
  2008-06-03 12:59       ` Samuel Tardieu
  2 siblings, 2 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-06-03  0:51 UTC (permalink / raw)


Dennis Hoppe wrote:
> 
> The same behaviour can be reproduced by the code I posted above.
> Again, if I compile my source mit the -gnato flag, the program
> compute the shift operation correct.

With -gnato and -fstack-check (and some would say -gnatE), GNAT is not an Ada 
compiler.

-- 
Jeff Carter
"He didn't get that nose from playing ping-pong."
Never Give a Sucker an Even Break
110



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-03  0:51       ` Jeffrey R. Carter
@ 2008-06-03  4:26         ` Gautier
  2008-06-03 19:36           ` Jeffrey R. Carter
  2008-06-03 17:56         ` Adam Beneschan
  1 sibling, 1 reply; 13+ messages in thread
From: Gautier @ 2008-06-03  4:26 UTC (permalink / raw)


Jeffrey R. Carter wrote:

> With -gnato and -fstack-check (and some would say -gnatE), GNAT is not 
> an Ada compiler.

Didn't you mean "without" ?...

_________________________________________________________
Gautier's Ada programming -- http://sf.net/users/gdemont/

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Feature or Bug? 2#1111# shifted by 4 is 224
  2008-06-02 18:52     ` Dennis Hoppe
  2008-06-03  0:51       ` Jeffrey R. Carter
@ 2008-06-03  8:10       ` Ludovic Brenta
  2008-06-03 12:59       ` Samuel Tardieu
  2 siblings, 0 replies; 13+ messages in thread
From: Ludovic Brenta @ 2008-06-03  8:10 UTC (permalink / raw)


Dennis Hoppe wrote:
> i tried the example source given at http://gcc.gnu.org/PR30740 and it
> fails on my machine. If I compile the same source with -gnato, the
> results are correct.
>
> The same behaviour can be reproduced by the code I posted above.
> Again, if I compile my source mit the -gnato flag, the program
> compute the shift operation correct.
>
> The bug seems still to exist.
>
> Tested on Mac OS X 10.5
> gcc (GCC) 4.4.0 20080314 (experimental) [trunk revision 133226]
> Intel Core 2 Duo

Of course it still exists in 20080314, since it was fixed on
2008-05-20.

--
Ludovic Brenta.



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 18:52     ` Dennis Hoppe
  2008-06-03  0:51       ` Jeffrey R. Carter
  2008-06-03  8:10       ` Ludovic Brenta
@ 2008-06-03 12:59       ` Samuel Tardieu
  2 siblings, 0 replies; 13+ messages in thread
From: Samuel Tardieu @ 2008-06-03 12:59 UTC (permalink / raw)


Dennis> The bug seems still to exist.

Dennis> Tested on Mac OS X 10.5
Dennis> gcc (GCC) 4.4.0 20080314 (experimental) [trunk revision 133226]
Dennis> Intel Core 2 Duo

It has been fixed ni GCC *after* the 14th of March. You need to get a
more recent version to have it fixed.



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

* Re: Feature or Bug? 2#1111# shifted by 4 is 224
  2008-06-03  0:51       ` Jeffrey R. Carter
  2008-06-03  4:26         ` Gautier
@ 2008-06-03 17:56         ` Adam Beneschan
  2008-06-03 19:39           ` Jeffrey R. Carter
  1 sibling, 1 reply; 13+ messages in thread
From: Adam Beneschan @ 2008-06-03 17:56 UTC (permalink / raw)


On Jun 2, 5:51 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> Dennis Hoppe wrote:
>
> > The same behaviour can be reproduced by the code I posted above.
> > Again, if I compile my source mit the -gnato flag, the program
> > compute the shift operation correct.
>
> With[out] -gnato and -fstack-check (and some would say -gnatE), GNAT is not an Ada
> compiler.

So how is that relevant?  Without those flags, the compiler will
violate Ada semantics in some specific and well-defined cases, but
users can still rightly expect Ada semantics to be followed in every
other case.

                                -- Adam





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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-03  4:26         ` Gautier
@ 2008-06-03 19:36           ` Jeffrey R. Carter
  0 siblings, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-06-03 19:36 UTC (permalink / raw)


Gautier wrote:
> Jeffrey R. Carter wrote:
> 
>> With -gnato and -fstack-check (and some would say -gnatE), GNAT is not 
>> an Ada compiler.
> 
> Didn't you mean "without" ?...

Of course. A minor typo :)

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59



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

* Re: Feature or Bug? 2#1111# shifted by 4 is 224
  2008-06-03 17:56         ` Adam Beneschan
@ 2008-06-03 19:39           ` Jeffrey R. Carter
  0 siblings, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2008-06-03 19:39 UTC (permalink / raw)


Adam Beneschan wrote:
> On Jun 2, 5:51 pm, "Jeffrey R. Carter"
> <spam.jrcarter....@spam.acm.org> wrote:
>>
>> With[out] -gnato and -fstack-check (and some would say -gnatE), GNAT is not an Ada
>> compiler.
> 
> So how is that relevant?  Without those flags, the compiler will
> violate Ada semantics in some specific and well-defined cases, but
> users can still rightly expect Ada semantics to be followed in every
> other case.

It may not be relevant to the specific problem, but I thought the OP might find 
it useful information, since many new users of GNAT are not aware that it is not 
an Ada compiler with its default settings.

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59



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

* Re: Feature or Bug?  2#1111# shifted by 4 is 224
  2008-06-02 17:59   ` Ludovic Brenta
  2008-06-02 18:52     ` Dennis Hoppe
@ 2008-06-04  3:36     ` Tero Koskinen
  1 sibling, 0 replies; 13+ messages in thread
From: Tero Koskinen @ 2008-06-04  3:36 UTC (permalink / raw)


On Mon, 02 Jun 2008 19:59:33 +0200 Ludovic Brenta wrote:
> Tero Koskinen <tero.koskinen@iki.fi> writes:
> > On Mon, 02 Jun 2008 17:17:19 +0200 Dennis Hoppe wrote:
> >> In Example B, we limit the parameter N to Positive, because we do not 
> >> want "negative" shifts. The result of Shift_Left(2#1111#, 4) is 224
> >> (2#11100000#) !
> >
> > type Modular_Type is mod 2**4;
> > function Shift_Left (Item : in Modular_Type;
> >                      N    : in Positive) return Modular_Type is
> > begin
> >    return Item * (2**N);
> > end Shift_Left;
> 
> This looks like http://gcc.gnu.org/PR30740 which is fixed in Debian
> unstable :)
> 
> Could you confirm that this is the same bug indeed?

Applying org.debian.gcc-4.3/debian/patches/pr30740.dpatch (from
ada-france.org server) solves the problem for me (GCC 4.3.0,
OpenBSD/i386 as earlier). On Solaris the compiler used -gnato and
-fstack-check flags by default, so the bug didn't occur there.

-- 
Tero Koskinen - http://iki.fi/tero.koskinen/



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

end of thread, other threads:[~2008-06-04  3:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-02 15:17 Feature or Bug? 2#1111# shifted by 4 is 224 Dennis Hoppe
2008-06-02 16:11 ` Tero Koskinen
2008-06-02 17:59   ` Ludovic Brenta
2008-06-02 18:52     ` Dennis Hoppe
2008-06-03  0:51       ` Jeffrey R. Carter
2008-06-03  4:26         ` Gautier
2008-06-03 19:36           ` Jeffrey R. Carter
2008-06-03 17:56         ` Adam Beneschan
2008-06-03 19:39           ` Jeffrey R. Carter
2008-06-03  8:10       ` Ludovic Brenta
2008-06-03 12:59       ` Samuel Tardieu
2008-06-04  3:36     ` Tero Koskinen
2008-06-02 22:32 ` Gautier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox