public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Assembly optimizations
@ 2004-11-30 22:58 Klaus Winter
  2004-12-01  9:21 ` Nathan Sidwell
  0 siblings, 1 reply; 9+ messages in thread
From: Klaus Winter @ 2004-11-30 22:58 UTC (permalink / raw)
  To: gcc-help

Hi all,

I compiled the following code with gcc-3.3.1. Once without
optimization and one time with optimization -O. With optimization the
Decrement function takes a LOT longer than before whereas the
Increment function stays constant. I haven't found anything in the
manpage about it ... Any help is appreciated.

cheers,
Klaus

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int g_Lock_Mini = 0;

inline int Increment( int* count)
{
	__asm__ __volatile__( "movl $1, %%eax\n\t" 
			      "lock; xaddl %%eax, (%%ecx)\n\t"
			      "incl %%eax\n\t" : : "c" (count) );
}
inline int Decrement( int * count)
{
	__asm__ __volatile__( "lock; decl (%%ecx)\n\t"
				  "movl (%%ecx), %%eax\n\t" : : "c" (count));
}

int Timeval_Substract (struct timeval *result, struct timeval* x,
struct timeval* y)
     {
       /* Perform the carry for the later subtraction by updating y. */
       if (x->tv_usec < y->tv_usec) {
         int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
         y->tv_usec -= 1000000 * nsec;
         y->tv_sec += nsec;
       }
       if (x->tv_usec - y->tv_usec > 1000000) {
         int nsec = (x->tv_usec - y->tv_usec) / 1000000;
         y->tv_usec += 1000000 * nsec;
         y->tv_sec -= nsec;
       }
     
       /* Compute the time remaining to wait.
          tv_usec is certainly positive. */
       result->tv_sec = x->tv_sec - y->tv_sec;
       result->tv_usec = x->tv_usec - y->tv_usec;
     
       /* Return 1 if result is negative. */
       return x->tv_sec < y->tv_sec;
}

int main( void )
{
	int * inc;
	*inc = 0;	
	int i;
	struct timeval startTime, endTime, resultTime;

	gettimeofday( &startTime, NULL );
	for( i = 0; i < 40000000; i++ )
		Increment( inc );
	gettimeofday( &endTime, NULL );
	Timeval_Substract( &resultTime, &endTime, &startTime );
	printf( "Increment Used Time: %i sec, %i nanosec\n",
resultTime.tv_sec, resultTime.tv_usec);
	
	gettimeofday( &startTime, NULL );
	for( i = 0; i < 40000000; i++ )
		Decrement( inc );
	gettimeofday( &endTime, NULL );
	Timeval_Substract( &resultTime, &endTime, &startTime );
	printf( "Decrement Used Time: %i sec, %i nanosec\n",
resultTime.tv_sec, resultTime.tv_usec);

    return 2;
}

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

* Re: Assembly optimizations
  2004-11-30 22:58 Assembly optimizations Klaus Winter
@ 2004-12-01  9:21 ` Nathan Sidwell
  2004-12-01  9:39   ` Is this a bug in gcc?? Name lastlong
  0 siblings, 1 reply; 9+ messages in thread
From: Nathan Sidwell @ 2004-12-01  9:21 UTC (permalink / raw)
  To: Klaus Winter; +Cc: gcc-help

Klaus Winter wrote:
> Hi all,
> 
> I compiled the following code with gcc-3.3.1. Once without
> optimization and one time with optimization -O. With optimization the
> Decrement function takes a LOT longer than before whereas the
> Increment function stays constant. I haven't found anything in the
> manpage about it ... Any help is appreciated.
> 
> cheers,
> Klaus
> 
> #include <stdio.h>
> #include <time.h>
> #include <sys/time.h>
> 
> int g_Lock_Mini = 0;
> 
> inline int Increment( int* count)
> {
> 	__asm__ __volatile__( "movl $1, %%eax\n\t" 
> 			      "lock; xaddl %%eax, (%%ecx)\n\t"
> 			      "incl %%eax\n\t" : : "c" (count) );
> }
> inline int Decrement( int * count)
> {
> 	__asm__ __volatile__( "lock; decl (%%ecx)\n\t"
> 				  "movl (%%ecx), %%eax\n\t" : : "c" (count));
> }
these asms are broken. I think you want something like

asm volatile("lock;decl (%1)\n\tmovl (%1),%0" : "=r" (result) : "r" (count) : "memory")

gcc does not otherwise know what registers are used by the asm.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

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

* Is this a bug in gcc??
  2004-12-01  9:21 ` Nathan Sidwell
@ 2004-12-01  9:39   ` Name lastlong
  2004-12-01 11:03     ` Nathan Sidwell
  0 siblings, 1 reply; 9+ messages in thread
From: Name lastlong @ 2004-12-01  9:39 UTC (permalink / raw)
  To: gcc-help

Hi,

I am expecting a error (redefinition of "a") from
following piece of code. But gcc (3.4.2) doesnot give
any error.
/////////////////
int a;
char b;
int a;

void foo(void)
{
return;
}
/////////////////
Instead, if we try initialising "a" at BOTH the
places, then it gives error.
VC++ gives this error anyway.

Is it a bug???

Regards.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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

* Re: Is this a bug in gcc??
  2004-12-01  9:39   ` Is this a bug in gcc?? Name lastlong
@ 2004-12-01 11:03     ` Nathan Sidwell
  2004-12-01 11:55       ` Name lastlong
  0 siblings, 1 reply; 9+ messages in thread
From: Nathan Sidwell @ 2004-12-01 11:03 UTC (permalink / raw)
  To: Name lastlong; +Cc: gcc-help

Name lastlong wrote:
> Hi,
> 
> I am expecting a error (redefinition of "a") from
> following piece of code. But gcc (3.4.2) doesnot give
> any error.
> /////////////////
> int a;
> char b;
> int a;
> 
> void foo(void)
> {
> return;
> }
> /////////////////
> Instead, if we try initialising "a" at BOTH the
> places, then it gives error.
> VC++ gives this error anyway.

VC++ is a C++ compiler, where the above is an error
gcc is a C compiler, where the above is not an error

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

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

* Re: Is this a bug in gcc??
  2004-12-01 11:03     ` Nathan Sidwell
@ 2004-12-01 11:55       ` Name lastlong
  2004-12-01 13:30         ` Eljay Love-Jensen
  2004-12-01 13:39         ` Nathan Sidwell
  0 siblings, 2 replies; 9+ messages in thread
From: Name lastlong @ 2004-12-01 11:55 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: gcc-help

Ok agreed!

But don't you think that C compiler (gcc) also output
this error? 
My observation is gcc outputs error only if we try to
initialise both instant of "a".

Regards.

--- Nathan Sidwell <nathan@codesourcery.com> wrote:

> Name lastlong wrote:
> > Hi,
> > 
> > I am expecting a error (redefinition of "a") from
> > following piece of code. But gcc (3.4.2) doesnot
> give
> > any error.
> > /////////////////
> > int a;
> > char b;
> > int a;
> > 
> > void foo(void)
> > {
> > return;
> > }
> > /////////////////
> > Instead, if we try initialising "a" at BOTH the
> > places, then it gives error.
> > VC++ gives this error anyway.
> 
> VC++ is a C++ compiler, where the above is an error
> gcc is a C compiler, where the above is not an error
> 
> nathan
> 
> -- 
> Nathan Sidwell    ::   http://www.codesourcery.com  
> ::     CodeSourcery LLC
> nathan@codesourcery.com    ::    
> http://www.planetfall.pwp.blueyonder.co.uk
> 
> 



	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail

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

* Re: Is this a bug in gcc??
  2004-12-01 11:55       ` Name lastlong
@ 2004-12-01 13:30         ` Eljay Love-Jensen
  2004-12-01 13:35           ` Name lastlong
  2004-12-01 13:39         ` Nathan Sidwell
  1 sibling, 1 reply; 9+ messages in thread
From: Eljay Love-Jensen @ 2004-12-01 13:30 UTC (permalink / raw)
  To: Name lastlong; +Cc: gcc-help

Hi Name lastlong,

 >But don't you think that C compiler (gcc) also output this error?

It's not an error in C, therefore a C compiler would be non-compliant to 
the C standard if it produced an error.  (And it would break a lot of 
correct code.)

 >My observation is gcc outputs error only if we try to initialise both 
instant of "a".

In C, there is only one instance of "a".

Stroustrup talks about C/C++ incompatibility in his C++ Programming 
Language (3rd and Special editions) on B.2.2.

HTH,
--Eljay

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

* Re: Is this a bug in gcc??
  2004-12-01 13:30         ` Eljay Love-Jensen
@ 2004-12-01 13:35           ` Name lastlong
  0 siblings, 0 replies; 9+ messages in thread
From: Name lastlong @ 2004-12-01 13:35 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help

Hi Eljay Love-Jensen,

Thanks a lot for ur reply. 
I understand it this time. :)

Regards,
Brew.

--- Eljay Love-Jensen <eljay@adobe.com> wrote:

> Hi Name lastlong,
> 
>  >But don't you think that C compiler (gcc) also
> output this error?
> 
> It's not an error in C, therefore a C compiler would
> be non-compliant to 
> the C standard if it produced an error.  (And it
> would break a lot of 
> correct code.)
> 
>  >My observation is gcc outputs error only if we try
> to initialise both 
> instant of "a".
> 
> In C, there is only one instance of "a".
> 
> Stroustrup talks about C/C++ incompatibility in his
> C++ Programming 
> Language (3rd and Special editions) on B.2.2.
> 
> HTH,
> --Eljay
> 
> 



		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 

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

* Re: Is this a bug in gcc??
  2004-12-01 11:55       ` Name lastlong
  2004-12-01 13:30         ` Eljay Love-Jensen
@ 2004-12-01 13:39         ` Nathan Sidwell
  1 sibling, 0 replies; 9+ messages in thread
From: Nathan Sidwell @ 2004-12-01 13:39 UTC (permalink / raw)
  To: Name lastlong; +Cc: gcc-help

Name lastlong wrote:
> Ok agreed!
> 
> But don't you think that C compiler (gcc) also output
> this error? 
if it did that, it would not be a C compiler.  What part
of 'gcc is a C compiler, where the above is not an error'
was unclear?

> My observation is gcc outputs error only if we try to
> initialise both instant of "a".
again, correct. doing that is an error in C

nathan
-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk

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

* RE: Is this a bug in gcc??
@ 2004-12-01 15:09 Martin York
  0 siblings, 0 replies; 9+ messages in thread
From: Martin York @ 2004-12-01 15:09 UTC (permalink / raw)
  To: gcc-help

 Name lastlong wrote:
> Hi,
> 
> I am expecting a error (redefinition of "a") from following piece of 
> code. But gcc (3.4.2) doesnot give any error.
> /////////////////
> int a;
> char b;
> int a;

Just to expand on what has already been said.
gcc/g++ behave as required by the standard. But to make sure we compare
apples to apples.

If you add the above to the file: test.c

gcc -c test.c   # Works Fine     (as expected)
g++ -c test.c   # Outputs errors (as expected)

Note that gcc is a C compiler (or should I say invokes the underlying C
Compiler by default) while g++ is a C++ compiler. NB If you name the
file test.cpp the content is C++ and the C++ compiler will be invoked no
matter how 'Gcc' is invoked (unless you explicitly override the
defaults).


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

end of thread, other threads:[~2004-12-01 15:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-30 22:58 Assembly optimizations Klaus Winter
2004-12-01  9:21 ` Nathan Sidwell
2004-12-01  9:39   ` Is this a bug in gcc?? Name lastlong
2004-12-01 11:03     ` Nathan Sidwell
2004-12-01 11:55       ` Name lastlong
2004-12-01 13:30         ` Eljay Love-Jensen
2004-12-01 13:35           ` Name lastlong
2004-12-01 13:39         ` Nathan Sidwell
2004-12-01 15:09 Martin York

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