Path: utzoo!attcan!uunet!cs.utexas.edu!news-server.csri.toronto.edu!helios.physics.utoronto.ca!physics.utoronto.ca!quinlan From: quinlan@physics.utoronto.ca (Gerald Quinlan) Newsgroups: comp.lang.fortran Subject: Using cpp for macro substitution. Message-ID: <1990Jul6.162153.17644@helios.physics.utoronto.ca> Date: 6 Jul 90 20:21:54 GMT Distribution: na Organization: University of Toronto Physics/Astronomy/CITA Lines: 64 I have a question about using the C preprocessor to perform macro substitutions in a Fortran program. The problem is the following. I have a short subroutine which adds two numbers (a,b) in extended precision, returning their sum (sum) and the error made in the addition (error). This routine gets called about 8 times in a heavily-used do loop in my program, so I thought I could speed things up by eliminating the subroutine call and doing the add in-line. I tried to get the preprocessor cpp to do the in-line substitution automatically for me, as shown below in the sample program adtest.F. --------------------------- adtest.F ---------------------------------- #define CALL_ADD(a,b,sum,error)\ dum=a+b \ if(abs(a).gt.abs(b)) then \ error=a-dum \ error=error+b \ else \ error=b-dum \ error=error+a \ endif \ sum=dum program adtest double precision dum,err,resid,rn,sum integer n sum=0.d0 resid=0.d0 do 10 n=1,500000 rn=sqrt(dble(n)) CALL_ADD(sum,rn,sum,err) resid=resid+err 10 continue sum=sum+resid write(6,*) 'addt:',sum,resid stop end When I run this through cpp (on a Sun) to get adtest.f I get the result shown below. --------------------------- adtest.f ---------------------------------- program adtest double precision dum,err,resid,rn,sum integer n sum=0.d0 resid=0.d0 do 10 n=1,500000 rn=sqrt(dble(n)) dum=sum+rn if(abs(sum).gt.abs(rn)) then err=sum-dum err=err+rn else err=rn-dum err=err+sum endif sum=dum resid=resid+err 10 continue sum=sum+resid write(6,*) 'addt:',sum,resid stop end Note how cpp has combined all the statements of the macro into one long line. I suppose this isn't a problem in C, where the statements end with ";" and more than one statement per line is allowed. But in Fortran this doesn't work; the compiler won't accept adtest.f. Does anyone know how to get around this? I've tried various tricks to get the macro inserted properly, but can't seem to find anything that works.