[comp.sys.mac.programmer] C/LSC question

mcdonald@fornax.UUCP (Ken Mcdonald) (11/04/89)

I'm writing a program in C which requires lots of calls to a small number
of very small functions.  Because of the number of calls made to them,
these functions need to be as fast as possible; because of their size,
it would be quite worthwhile to have the code for these functions
inserted by the compiler directly wherever said function is called,
rather than having the compiler generate all the necessary stack
manipulations and JSRs involved in a "real" function call.  Is there
any way to have C do this?  Is there any way to have Lightspeed C (okay,
okay, THINK C, but I liked the old name better) do this?  If not, is 
there any good way to fake it?  How about any bad way?  Please don't
say I have to use the built in assembler, I'd prefer to avoid going down
to that level.

Thanks for the help.

Ken McDonald
{mcdonald@fornax.uucp}

lim@iris.ucdavis.edu (Lloyd Lim) (11/06/89)

In article <113@fornax.UUCP> mcdonald@fornax.UUCP (Ken Mcdonald) writes:
>I'm writing a program in C which requires lots of calls to a small number
>of very small functions.  Because of the number of calls made to them,
>these functions need to be as fast as possible; because of their size,
>it would be quite worthwhile to have the code for these functions
>inserted by the compiler directly wherever said function is called,
>rather than having the compiler generate all the necessary stack
>manipulations and JSRs involved in a "real" function call.  Is there
>any way to have C do this?  Is there any way to have Lightspeed C (okay,
>okay, THINK C, but I liked the old name better) do this?  If not, is 
>there any good way to fake it?  How about any bad way?  Please don't
>say I have to use the built in assembler, I'd prefer to avoid going down
>to that level.

I know you don't want to but as a reminder for those that don't mind assembly
you can define a assembly language routine as inline.

My only other suggestion would be to code it as a macro.  Just put the same
statements into a macro with a backslash at the end of each line and
parentheses around each occurence of a macro argument.  I've coded some of the
more basic QuickDraw routines this way for slight speed increases.  If you need
local storage you can just put your statements in a block and declare some
variables.  Make sure you don't do anything funny in the macro calls which
would be have side effects (e.g. SWAP(++a, ++b)).  Here's a very simple example
that I just made up and which seems to run through THINK's syntax checker ok.

#define SWAP(x, y)   \
   {                 \
      short temp;    \
                     \
      temp = (x);    \
      (x) = (y);     \
      (y) = temp;    \
   }

void main(void);

void main()
{
   short a, b;
   
   SWAP(a, b);
}

+++
Lloyd Lim     Internet: lim@iris.ucdavis.edu (128.120.57.20)
              Compuserve: 72647,660
              US Mail: 146 Lysle Leach Hall, U.C. Davis, Davis, CA 95616

sho@maxwell.physics.purdue.edu (Sho Kuwamoto) (11/06/89)

In article <113@fornax.UUCP> mcdonald@fornax.UUCP (Ken Mcdonald) writes:
>I'm writing a program in C which requires lots of calls to a small number
>of very small functions.  [...]
>it would be quite worthwhile to have the code for these functions
>inserted by the compiler directly wherever said function is called,
>rather than having the compiler generate all the necessary stack
>manipulations and JSRs involved in a "real" function call.  

1) Get MPW C++ and use the 'inline' construct.

2) Use THINK C and use macros.  (Standard C.  Nothing fancy)
   be careful of side effects, though.

-Sho
--
sho@physics.purdue.edu  <<-- why doesn't THINK C use #pragma once
                             like everyone else?

sdh@thumper.bellcore.com (Retief of the CDT) (11/08/89)

Ok, what you want is inline expansion of your functions.
I think that's an ANSIism that is not in lightspeed C, but
that's ok, because you can fake it:

#define fastfunc(arg1, arg2, arg3) {\
	int local1, local2, local3;\
	/* do whatever your function needs to do\
	 * but beware that the paramters are no longer\
	 * pass by value! */\
}

Note that any local variables you create will generate their own stack
frame -this is overhead.

Steve Hawley
sdh@flash.bellcore.com