public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c/8221: return value of a function is forgotten
@ 2002-10-14  8:54 zack
  0 siblings, 0 replies; 4+ messages in thread
From: zack @ 2002-10-14  8:54 UTC (permalink / raw)
  To: c.kaliszyk, cek, gcc-bugs, gcc-prs, nobody

Synopsis: return value of a function is forgotten

State-Changed-From-To: open->closed
State-Changed-By: zack
State-Changed-When: Mon Oct 14 08:54:27 2002
State-Changed-Why:
    Not a bug.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8221


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

* Re: c/8221: return value of a function is forgotten
@ 2002-10-14  8:46 Zack Weinberg
  0 siblings, 0 replies; 4+ messages in thread
From: Zack Weinberg @ 2002-10-14  8:46 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/8221; it has been noted by GNATS.

From: Zack Weinberg <zack@codesourcery.com>
To: c.kaliszyk@zodiac.mimuw.edu.pl
Cc: gcc-gnats@gcc.gnu.org, cek@wp.pl
Subject: Re: c/8221: return value of a function is forgotten
Date: Mon, 14 Oct 2002 08:43:14 -0700

 On Mon, Oct 14, 2002 at 08:44:09AM -0000, c.kaliszyk@zodiac.mimuw.edu.pl wrote:
 > inline int getcputime(void) {
 >   int i;
 >   __asm__(
 >     "rdtsc
 >     movl %%eax, %0"
 >     : :"g" (i) :"ax", "dx");
 >   return i;
 > }
 
 This asm statement claims that I is an input to the instruction, and
 that there aren't any outputs.  Thus, GCC assumes that I is not
 modified by the asm.
 
 The correct way to write getcputime() would be
 
   static inline unsigned int
   getcputime(void)
   {
     unsigned int i;
     asm volatile ("rdtsc" : "=a" (i) : : "edx");
     return i;
   }
 
 The key change is to put the I parameter in the _first_ block of asm
 operands, and add a leading = to its constraint.  That tells the
 compiler that I contains an output.
 
 Changing "g" to "a" in the constraint tells GCC that the output
 appears in %eax, which obviates the need for the move instruction you
 wrote.  This also means you need not (must not) clobber edx.
 
 All the other changes are also necessary.  I leave as an exercise for
 you to figure out why I made them.
 
 zw


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

* Re: c/8221: return value of a function is forgotten
@ 2002-10-14  2:16 Andrew Pinski
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Pinski @ 2002-10-14  2:16 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/8221; it has been noted by GNATS.

From: Andrew Pinski <pinskia@physics.uc.edu>
To: c.kaliszyk@zodiac.mimuw.edu.pl
Cc: gcc-gnats@gcc.gnu.org, cek@wp.pl
Subject: Re: c/8221: return value of a function is forgotten
Date: Mon, 14 Oct 2002 02:12:23 -0700

 On Monday, Oct 14, 2002, at 01:44 US/Pacific, 
 c.kaliszyk@zodiac.mimuw.edu.pl wrote:
 > I call "gcc -O2" on this:
 > --cut--
 > inline int getcputime(void) {
 >   int i;
 >   __asm__(
 >     "rdtsc
 >     movl %%eax, %0"
 >     : :"g" (i) :"ax", "dx");
 >   return i;
 > }
 >
 > int main(void) {
 >   int x =3D getcputime();
 >   int y =3D getcputime();
 >   printf ("Time measure resolution: %i\n", y - x);
 >   return 0;
 > }
 
 
 I think you constraints are wrong, this works for me:
 inline int getcputime(void) {
    int i;
    __asm__ volatile(
      "rdtsc\n\
      movl %%eax, %0"
      : "=g" (i) : :"eax", "edx");
    return i;
 }
 
 int main(void) {
    int x = getcputime();
    int y = getcputime();
    printf ("Time measure resolution: %i\n", y - x);
    return 0;
 }
 
 
 read
 http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Extended-Asm.html
 for information, this helped me get the correct result.
 
 Thanks,
 Andrew Pinski
 
 PS I think this bug is user error so can someone close it.
 


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

* c/8221: return value of a function is forgotten
@ 2002-10-14  1:46 c.kaliszyk
  0 siblings, 0 replies; 4+ messages in thread
From: c.kaliszyk @ 2002-10-14  1:46 UTC (permalink / raw)
  To: gcc-gnats; +Cc: cek


>Number:         8221
>Category:       c
>Synopsis:       return value of a function is forgotten
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Oct 14 01:46:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     c.kaliszyk@zodiac.mimuw.edu.pl
>Release:        3.2
>Organization:
>Environment:
Linux orka 2.5.40 #1 Thu Oct 3 07:30:38 CEST 2002 i686 unknown
configured with: /home/c/tmp/gcc-3.2/configure --prefix=3D/usr --enable-thr=
eads
>Description:
I tried to measure time twice, and substract these results.

When the function is inlined both results are stored in %ecx and after that
%ecx is substracted from itself, and I get zero.
Without inlining it works correct.
>How-To-Repeat:
I call "gcc -O2" on this:
--cut--
inline int getcputime(void) {
  int i;
  __asm__(
    "rdtsc
    movl %%eax, %0"
    : :"g" (i) :"ax", "dx");
  return i;
}

int main(void) {
  int x =3D getcputime();
  int y =3D getcputime();
  printf ("Time measure resolution: %i\n", y - x);
  return 0;
}
--cut--
>Fix:
Remove "inline" before the function definition or call gcc without -O
If the function is not inlined the return values of both calls are stored on
the stack in diffrent places and correctly substracted.
>Release-Note:
>Audit-Trail:
>Unformatted:


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

end of thread, other threads:[~2002-10-14 15:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-14  8:54 c/8221: return value of a function is forgotten zack
  -- strict thread matches above, loose matches on Subject: below --
2002-10-14  8:46 Zack Weinberg
2002-10-14  2:16 Andrew Pinski
2002-10-14  1:46 c.kaliszyk

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