forcing code inline

L

Lynn McGuire

Is there anyway to force code to be inline other than making that
code a macro ? We have about 100 lines of code that is duplicated
using a macro in about a dozen places. We would rather make this
code inline but inline seems that it would probably make this code
into a function.

I do know about the MS __forceinline command. However, we use
three compilers for this code, VC++ 2003, Open Watcom 1.5 and
gcc 2.95 (on a FreeBSD box).

Thanks,
Lynn McGuire
 
N

Noah Roberts

Lynn said:
Is there anyway to force code to be inline other than making that
code a macro ?

Nope. There is no standard way. You can make the inline suggestion
but that is it.

Why not just trust the compiler to do the right thing? You're using
good compilers...crank up some optimizations and most of your small
functions will inline *when it is best for the computer*. Unless there
is some profound argument to do otherwise, including several redundant
stats, I trust the compiler to be better at its job than I am.
 
T

Thomas Tutone

Lynn said:
Is there anyway to force code to be inline other than making that
code a macro ?

Not in a platform-independent way.
We have about 100 lines of code that is duplicated
using a macro in about a dozen places. We would rather make this
code inline but inline seems that it would probably make this code
into a function.

Why is that a bad thing? If it's 100 lines, then any marginal speed-up
by making it inline will most likely be some tiny fraction of the time
it takes to execute the code. In fact, it is quite possible that by
making it inline, you actually slow down execution because of cache
misses.
I do know about the MS __forceinline command. However, we use
three compilers for this code, VC++ 2003, Open Watcom 1.5 and
gcc 2.95 (on a FreeBSD box).

gcc has its own, nonstandard way of accomplishing that as well. I
don't use Watcom, but I assume the same is true of it too.

Best regards,

Tom
 
L

Lynn McGuire

We have about 100 lines of code that is duplicated
Why is that a bad thing? If it's 100 lines, then any marginal speed-up
by making it inline will most likely be some tiny fraction of the time
it takes to execute the code. In fact, it is quite possible that by
making it inline, you actually slow down execution because of cache
misses.

I want the code duplicated for another reason. I dont care about
speed of this code.
gcc has its own, nonstandard way of accomplishing that as well. I
don't use Watcom, but I assume the same is true of it too.

OW does not have a way of doing this (other than using a macro).

Thanks,
Lynn McGuire
 
R

Robbie Hatley

Lynn McGuire said:
Is there anyway to force code to be inline other than making that
code a macro ?

Ummm, yes. Put the 100 lines of code into a file called hundred.h
and #include it at all the points in your code where you want to
use it:

// ... a bunch of code ...
#include hundred.h
// ... a bunch more code ...
#include hundred.h
// ... etc. ...

(Note: unlike with normal headers, be sure NOT to use include guards,
because you actually WANT multiple inclusion here. In fact, try to
avoid all preprocessor directives in hundred.h.)


Or, make a multi-line macro:
#define MY_BIG_MACRO \
Blat[0] = Argle[0]; \
Blat[1] = Argle[1]; \
Range = 82 - Domain; \
Slyvern.host(73); \
LastLine = 3.8 * Fargle;

(Notes: don't allow any whitespace after the backslashes, and
don't use a backslash on the last line.)


Or, just make a function, inline or otherwise:

inline double trouble(char actor, int eger) // MIGHT be inline
{
... 100 lines of code ...
return Widget;
}


The function approach is best, even if the inlining fails,
unless you absolutely need to avoid this for some reason
(eg, limited stack space).

If you're doing firmware for a CPU with a tiny RAM, use the
function-like macro or the #included header. Either will
avoid the stack usage inherent to function calls. I've
had to do this at work because the CPU we use for embedded
firmware only has 500 bytes of RAM.
 
L

Lynn McGuire

Is there anyway to force code to be inline other than making that
Ummm, yes. Put the 100 lines of code into a file called hundred.h
and #include it at all the points in your code where you want to
use it:

// ... a bunch of code ...
#include hundred.h
// ... a bunch more code ...
#include hundred.h
// ... etc. ...

Thanks ! I totally forgot about doing that (and we do that already
in another portion of the code !).

Lynn
 
L

Lynn McGuire

Is there anyway to force code to be inline other than making that
What's the product?

I would rather not say. However, this is commercial software and we are
fighting off the Chinese software pirates.

Lynn
 
R

Richard Herring

Lynn McGuire said:
I would rather not say. However, this is commercial software and we are
fighting off the Chinese software pirates.
That's OK. I just want to be sure I never end up buying anything
containing such fragile and unmaintainable code.
 
L

Lynn McGuire

What's the product?
That's OK. I just want to be sure I never end up buying anything containing such fragile and unmaintainable code.

Yup, that is is what I thought. Making decisions based on the
internals of something rather than the end user functionality is
always fraught with danger.

Are you speaking for yourself or your employer at this point ?
One of your coworkers did take a look at our software back in
2002 but nothing came of it.

Lynn
 
R

Richard Herring

In message <[email protected]>, Lynn McGuire

[on "forcing inline" by multiply #including boilerplate code]
Yup, that is is what I thought. Making decisions based on the
internals of something rather than the end user functionality is
always fraught with danger.

As is the reverse.
Are you speaking for yourself or your employer at this point ?

Myself, as is always the case here. They don't pay me enough to speak
for anyone else ;)
One of your coworkers did take a look at our software back in
2002 but nothing came of it.
Even if I were privy to that, I wouldn't be allowed to comment on it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top