[gnu.g++.bug] The power of inline functions

gordon%stats.ucl.ac.uk@NSFNET-RELAY.AC.UK (Gordon Joly) (06/05/89)

There are some rather alarming differences between the code
generated by gcc, g++ and /bin/cc; are we missing something?
We now believe that gcc uses /usr/include/math.h.

Tim Day, user.
Gordon Joly, installer.

Configurations:-
config.gcc sun3-os4,  config.g++ sun3-os4.
gcc 1.35, g++ 1.35.1-, libg++-1.35.0.
SUN OS 4.0.1, gas 1.33.
gcc/g++ on karl, SUN-3/160.
a.out run on phoebe, SUN-3/60.

------- Forwarded Message

Received: from cs.ucl.ac.uk by Stats.Ucl.AC.UK   via Satnet with SMTP
           id aa04194; 26 May 89 14:24 GMT
To: otto@uk.ac.ucl.cs, gordon@uk.ac.ucl.cs
cc: tday@uk.ac.ucl.cs, sam@uk.ac.ucl.cs, gillian@uk.ac.ucl.cs
Subject: The power of inline functions (cc, gcc, g++)
Date: Fri, 26 May 89 15:24:07 +0100
> From: Tim Day <T.Day@uk.ac.ucl.cs>
Source-Info:  phoebe.ps.ucl.ac.uk
Source-Info:  karl.stats.ucl.ac.uk

I was recently shocked and amazed by the effect of inline functions on
the following trivial prog (2*10^5 fp add, 10^5 sqrt):
#include <stdio.h>
#include <math.h>
main()
{	int i;double f,t;
	for (i=1,f=0.0,t=0.0;i<=100000;i++,f+=0.125,t+=sqrt(f));
	fprintf(stderr,"%f\n",t);
	exit((int)log(t));
}
All CPU times on phoebe.  Gnu stuff compiled 1.35 on karl.                 Time
cc -O4 -f68881 ctest.c -lm                                                 38.8
cc -O4 -f68881 ctest.c /usr/lib/f68881/libm.il -lm                          2.6
gcc -m68020 -m68881 -O -finline-functions -fomit-frame-pointer ctest.c -lm 51.4
g++ -m68020 -m68881 -O -finline-functions -fomit-frame-pointer ctest.c      1.2

(Note: these are _execution_ times.  I am quite prepared to pay extra at
compile time for this kind of acceleration !)

The difference between gcc and g++ is worrying... which math.h does gcc use ?
gcc-include/ doesn't actually contain a math.h, whereas g++-include/math.h
includes g++-include/math-68881.h, which is full of floppy __inline __asm stuff.

The slow progs both use jbsr _sqrt; the fast ones use fsqrtd sp@,fp0 (cc), or
fsqrtx fp1,fp0 (g++; none of this messing around doing double <-> extended
transfers on the stack !).

This might also explain why gcc compiled evileye was about 1/2 the speed
of the cc compiled version.  Is there anything which can be done to gcc
to get it to do similar things to g++, or should I try and force c through
g++ ?


------- End of Forwarded Message

grunwald@flute.cs.uiuc.edu (Dirk Grunwald) (06/05/89)

At my request, Doug Lea included modifications to the g++ version math.h
that include math-m68881.h, which was previously distributed via gnu.gcc.bug.

This version of math.h simply checks to see if you're using the 68881, and,
if you are, includes a header file that uses m68881 functions to perform
common functions. There are caveats to this approach, and I think they're
included in the header file.

To get the same functionality from `gcc', simply incant a math.h that
checks for _USE_68881_ & include math-m68881.h if it does.

The last I heard, these feature will be supplanted by new __builtins that
allow similar functionality but allow even greater optimizations.
--
Dirk Grunwald -- Univ. of Illinois 		  (grunwald@flute.cs.uiuc.edu)