This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Statement incorrect in doc
- From: Andrew Pinski <pinskia at physics dot uc dot edu>
- To: Rob Aberg <roa at charter dot net>
- Cc: gcc at gnu dot org
- Date: Sun, 27 Oct 2002 10:36:30 -0800
- Subject: Re: Statement incorrect in doc
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