public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Statement incorrect in doc
@ 2002-10-28  5:30 Rob Aberg
  2002-10-28  5:58 ` Andrew Pinski
  0 siblings, 1 reply; 2+ messages in thread
From: Rob Aberg @ 2002-10-28  5:30 UTC (permalink / raw)
  To: gcc

While looking in Google for discussions on stategies for optionally
inlining C functions in a semi-portable way, I ran across your online
doc:

 http://gcc.gnu.org/onlinedocs/gcc/Inline.html

The section heading is "An Inline Function is As Fast As a Macro". 
That's not entirely true.  Often, an inline function is faster:  if a
macro uses its "input arg" more than once and the arg is an expression,
the arg-expression is evaluated multiple times unless other
optimizations in gcc are active and see the common subexpression and
eliminate it.  Consider this macro:

#define my_min(X,Y) ( ((X) < (Y)) ? (X) : (Y) )

It will evaluate either the (X) or the (Y) expression twice.  It can be
arbitrarily worse for nested macros.   Or am I just missing something?


Now that I am reminded, I'll order this one as well -- I have the gdb
book, it is very well written...



Thanks,

  Rob Aberg
  Grafton, MA


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

* Re: Statement incorrect in doc
  2002-10-28  5:30 Statement incorrect in doc Rob Aberg
@ 2002-10-28  5:58 ` Andrew Pinski
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Pinski @ 2002-10-28  5:58 UTC (permalink / raw)
  To: Rob Aberg; +Cc: gcc


On Sunday, Oct 27, 2002, at 10:28 US/Pacific, Rob Aberg wrote:

> While looking in Google for discussions on stategies for optionally
> inlining C functions in a semi-portable way, I ran across your online
> doc:
>
>  http://gcc.gnu.org/onlinedocs/gcc/Inline.html
>
> The section heading is "An Inline Function is As Fast As a Macro".
> That's not entirely true.  Often, an inline function is faster:  if a
> macro uses its "input arg" more than once and the arg is an expression,
> the arg-expression is evaluated multiple times unless other
> optimizations in gcc are active and see the common subexpression and
> eliminate it.  Consider this macro:
>
> #define my_min(X,Y) ( ((X) < (Y)) ? (X) : (Y) )

Because you can use gcc extensions to make it the same:

#define my_min(x,y) ({ __typeof__(x) x1=x; __typeof__(y) y1=y; 
((x1<y1)?x1:y1) })

This will be the almost same as the inline function (in c++ at least 
because c does not have a concept of a template):

template <typename __t> __t min(__t x, __t y)
{
	return (x<y)?x:y;
}



>
> It will evaluate either the (X) or the (Y) expression twice.  It can be
> arbitrarily worse for nested macros.   Or am I just missing something?
>
>
> Now that I am reminded, I'll order this one as well -- I have the gdb
> book, it is very well written...
>
>
>
> Thanks,
>
>   Rob Aberg
>   Grafton, MA
>
>
>
>

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

end of thread, other threads:[~2002-10-27 18:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-28  5:30 Statement incorrect in doc Rob Aberg
2002-10-28  5:58 ` 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).