public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43262]  New: inline asm with -O2 breaks logic if comarisons are involved
@ 2010-03-04 21:58 s dot v dot savenko at gmail dot com
  2010-03-04 22:08 ` [Bug c++/43262] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: s dot v dot savenko at gmail dot com @ 2010-03-04 21:58 UTC (permalink / raw)
  To: gcc-bugs

I put here a simple example to illustrate what looks like a bug to me

#include<iostream>

inline unsigned next_exp2(unsigned x)
{
        unsigned n;
        asm volatile("bsr %0, %1" : "=r" (n) : "r" (x)); // most significant
bit
        return((x == (1u << n)) ? n : n);
};
inline unsigned next_exp2_(unsigned x)
{
        unsigned n;
        asm volatile("bsr %0, %1" : "=r" (n) : "r" (x)); // most significant
bit
        return((x == (1u << n)) ? n : n+1);
};

int main()
{
        unsigned x = 1025;
        std::cout << "n1 = " << next_exp2(x) << std::endl;
        std::cout << "n2 = " << next_exp2_(x) << std::endl;

        return(0);
}


The difference between two function is n+1 returned from the underscored
version. Compiled with g++ -O2 ./foo.cpp, my output  is:

n1 = 10
n2 = 3078596801

while I would expect n2 = 11.

If I compile it with g++ -g foo.cpp, everything works as expected.


-- 
           Summary: inline asm with -O2 breaks logic if comarisons are
                    involved
           Product: gcc
           Version: 4.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: s dot v dot savenko at gmail dot com


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


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

* [Bug c++/43262] inline asm with -O2 breaks logic if comarisons are involved
  2010-03-04 21:58 [Bug c++/43262] New: inline asm with -O2 breaks logic if comarisons are involved s dot v dot savenko at gmail dot com
@ 2010-03-04 22:08 ` pinskia at gcc dot gnu dot org
  2010-03-04 22:25 ` [Bug c++/43262] inline asm with -O2 breaks logic if comparisons " pinskia at gcc dot gnu dot org
  2010-03-04 22:26 ` s dot v dot savenko at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-03-04 22:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-03-04 22:08 -------
The problem you are seeing is Intel vs AT&T asm formats.  GNU as defaults to
AT&T format in that it is src, dst. So you have the operands swapped.  Note
also you also don't clobber the flags register as bsr sets the Zero flag.
asm volatile("bsr %1, %0" : "=r" (n) : "r" (x) : "flags");
Is the correct code you want.  It just happened to work at -O0 because the
register allocator used the same registers for the input and output.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c++/43262] inline asm with -O2 breaks logic if comparisons are involved
  2010-03-04 21:58 [Bug c++/43262] New: inline asm with -O2 breaks logic if comarisons are involved s dot v dot savenko at gmail dot com
  2010-03-04 22:08 ` [Bug c++/43262] " pinskia at gcc dot gnu dot org
@ 2010-03-04 22:25 ` pinskia at gcc dot gnu dot org
  2010-03-04 22:26 ` s dot v dot savenko at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-03-04 22:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2010-03-04 22:24 -------
*** Bug 43263 has been marked as a duplicate of this bug. ***


-- 


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


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

* [Bug c++/43262] inline asm with -O2 breaks logic if comparisons are involved
  2010-03-04 21:58 [Bug c++/43262] New: inline asm with -O2 breaks logic if comarisons are involved s dot v dot savenko at gmail dot com
  2010-03-04 22:08 ` [Bug c++/43262] " pinskia at gcc dot gnu dot org
  2010-03-04 22:25 ` [Bug c++/43262] inline asm with -O2 breaks logic if comparisons " pinskia at gcc dot gnu dot org
@ 2010-03-04 22:26 ` s dot v dot savenko at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: s dot v dot savenko at gmail dot com @ 2010-03-04 22:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from s dot v dot savenko at gmail dot com  2010-03-04 22:26 -------
You're absolutely right. Sorry for the false alarm.


-- 


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


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

end of thread, other threads:[~2010-03-04 22:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-04 21:58 [Bug c++/43262] New: inline asm with -O2 breaks logic if comarisons are involved s dot v dot savenko at gmail dot com
2010-03-04 22:08 ` [Bug c++/43262] " pinskia at gcc dot gnu dot org
2010-03-04 22:25 ` [Bug c++/43262] inline asm with -O2 breaks logic if comparisons " pinskia at gcc dot gnu dot org
2010-03-04 22:26 ` s dot v dot savenko at gmail dot com

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