Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > forcing code inline

Reply
Thread Tools

forcing code inline

 
 
Lynn McGuire
Guest
Posts: n/a
 
      06-27-2006
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


 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      06-27-2006

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.

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      06-27-2006
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

 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      06-27-2006
>> 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


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      06-28-2006
"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


 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      06-28-2006
>> 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


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      06-29-2006
In message <(E-Mail Removed)>, Lynn McGuire
<(E-Mail Removed)> 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
 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      06-29-2006
>>>> 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


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      06-29-2006
In message <(E-Mail Removed)>, Lynn McGuire
<(E-Mail Removed)> 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
 
Reply With Quote
 
Lynn McGuire
Guest
Posts: n/a
 
      06-29-2006
>>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
forcing compiler to consider inline function inline. Ajay C++ 5 04-01-2006 02:03 PM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments