public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/21699] New: asm and cast-to-reference -> bad code
@ 2005-05-21 15:48 thome at lix dot polytechnique dot fr
  2005-05-21 15:55 ` Andrew Pinski
  0 siblings, 1 reply; 2+ messages in thread
From: thome at lix dot polytechnique dot fr @ 2005-05-21 15:48 UTC (permalink / raw)
  To: gcc-bugs

The code below is an attempt at having the macro add_ssaaaa from GNU
MP accepted by G++. I am casting the outputs of the asm statement to
unsigned int& (in GMP, USItype is unsigned int + attrib SI, which are the
same on i386).

First, I'd like to be told if this is not a reasonable thing to do. I
think it is, but I'm not too sure.

The problem is that g++ 3.4.x produces wrong code at -O2 level for this.
The asm produced has no trace of the two tests at the end (it must,
really). No warning is issued.

The problem vanishes if I cast to unsigned long& instead, or if I remove the 
cast.

4.0 does not have the problem.

See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2803

---------------------------------------------------------------------------
const unsigned long f_p = 4194301;
extern unsigned long mpinv_mod_r;

unsigned long redc(unsigned long zhi, unsigned long zlo)
{
        unsigned long t, u, m, __w1, __xm0, __xm1;
        m = zlo * mpinv_mod_r;
        __xm0 = m;
        __xm1 = f_p;
        __asm__("mull %3": "=a"(u), "=d"(__w1):"%0"(__xm0),
                "rm"(__xm1));
        t = __w1 - (-(__xm0 >> 31) & __xm1) - (-(__xm1 >> 31) & __xm0);
        __asm__("addl %5,%1\n\tadcl %3,%0"
                        : "=r" ((unsigned int&) t), "=&r" ((unsigned int&) u)
                        :"0"(t), "g"(zhi), "%1"(u), "g"(zlo));
        if ((int) t < 0)
                t += f_p;
        if (t >= f_p)
                t -= f_p;
        return t;
}
---------------------------------------------------------------------------

E.

-- 
           Summary: asm and cast-to-reference -> bad code
           Product: gcc
           Version: 3.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: thome at lix dot polytechnique dot fr
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i386-redhat-linux


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


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

* Re: [Bug c++/21699] New: asm and cast-to-reference -> bad code
  2005-05-21 15:48 [Bug c++/21699] New: asm and cast-to-reference -> bad code thome at lix dot polytechnique dot fr
@ 2005-05-21 15:55 ` Andrew Pinski
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Pinski @ 2005-05-21 15:55 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs


On May 21, 2005, at 11:48 AM, thome at lix dot polytechnique dot fr 
wrote:

> The code below is an attempt at having the macro add_ssaaaa from GNU
> MP accepted by G++. I am casting the outputs of the asm statement to
> unsigned int& (in GMP, USItype is unsigned int + attrib SI, which are 
> the
> same on i386).
>
> First, I'd like to be told if this is not a reasonable thing to do. I
> think it is, but I'm not too sure.
>
> The problem is that g++ 3.4.x produces wrong code at -O2 level for 
> this.
> The asm produced has no trace of the two tests at the end (it must,
> really). No warning is issued.

You are violating C++ aliasing rules.
You are accessing a long variable as an int.

Thanks,
Andrew Pinski


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

end of thread, other threads:[~2005-05-21 15:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-21 15:48 [Bug c++/21699] New: asm and cast-to-reference -> bad code thome at lix dot polytechnique dot fr
2005-05-21 15:55 ` Andrew Pinski

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).