forcing code inline

Discussion in 'C++' started by Lynn McGuire, Jun 27, 2006.

  1. Lynn McGuire

    Lynn McGuire Guest

    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
     
    Lynn McGuire, Jun 27, 2006
    #1
    1. Advertising

  2. Lynn McGuire

    Noah Roberts Guest

    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.
     
    Noah Roberts, Jun 27, 2006
    #2
    1. Advertising

  3. 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
     
    Thomas Tutone, Jun 27, 2006
    #3
  4. Lynn McGuire

    Lynn McGuire Guest

    >> 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
     
    Lynn McGuire, Jun 27, 2006
    #4
  5. "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
     
    Robbie Hatley, Jun 28, 2006
    #5
  6. Lynn McGuire

    Lynn McGuire Guest

    >> 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
     
    Lynn McGuire, Jun 28, 2006
    #6
  7. In message <>, Lynn McGuire
    <> 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
     
    Richard Herring, Jun 29, 2006
    #7
  8. Lynn McGuire

    Lynn McGuire Guest

    >>>> 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
     
    Lynn McGuire, Jun 29, 2006
    #8
  9. In message <>, Lynn McGuire
    <> 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
     
    Richard Herring, Jun 29, 2006
    #9
  10. Lynn McGuire

    Lynn McGuire Guest

    >>> 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
     
    Lynn McGuire, Jun 29, 2006
    #10
  11. In message <>, Lynn McGuire
    <> 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
     
    Richard Herring, Jun 30, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    759
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    500
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    484
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    2,009
    Kevin Bracey
    Nov 21, 2003
  5. Ajay
    Replies:
    5
    Views:
    412
    Pete Becker
    Apr 1, 2006
Loading...

Share This Page