By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

forcing code inline

P: n/a
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
Jun 27 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

Lynn McGuire wrote:
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.

Jun 27 '06 #2

P: n/a
Lynn McGuire wrote:
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

Jun 27 '06 #3

P: n/a
>> 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 want the code duplicated for another reason. I dont care about
speed of this code.
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.


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

Thanks,
Lynn McGuire
Jun 27 '06 #4

P: n/a
"Lynn McGuire" wrote:
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.

--
Robbie Hatley
Tustin, CA, USA
lonewolfintj atsign pacbell period net
home period pacbell period net slantbar earnur slantbar
Jun 28 '06 #5

P: n/a
>> 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. ...


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

Lynn
Jun 28 '06 #6

P: n/a
In message <12*************@corp.supernews.com>, Lynn McGuire
<no****@nospam.com> writes
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. ...


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

What's the product?

--
Richard Herring
Jun 29 '06 #7

P: n/a
>>>> 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. ...


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

What's the product?


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

Lynn
Jun 29 '06 #8

P: n/a
In message <12*************@corp.supernews.com>, Lynn McGuire
<no****@nospam.com> writes
> 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. ...

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

What's the product?


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.

--
Richard Herring
Jun 29 '06 #9

P: n/a
>>> What's the product?

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.


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
Jun 29 '06 #10

P: n/a
In message <12*************@corp.supernews.com>, Lynn McGuire
<no****@nospam.com> writes

[on "forcing inline" by multiply #including boilerplate code]
What's the product?

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.


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.

--
Richard Herring
Jun 30 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.