Floating Point Constants - Inlining Questions

Discussion in 'C++' started by Leslie Sanford, Aug 4, 2008.

  1. I want to define a set of floating point constants using templates insteand
    of macros. I'd like to determine whether these constants are floats or
    doubles at compile time. In my header file, I have this:

    template<bool DoublePrecision>
    struct Constants
    {
    typedef double SampleType;

    static const double pi;
    static const double piDoubled;
    static const double SampleRateDefault;
    static const double DenormalOffset;
    };

    template<>
    struct Constants<false>
    {
    typedef float SampleType;

    static const float pi;
    static const float piDoubled;
    static const float SampleRateDefault;
    static const float DenormalOffset;
    };

    In my implementation file, I have this

    #include "Constants.h"

    const double Constants<true>::pi = 3.14159265358979;
    const double Constants<true>::piDoubled = Constants<true>::pi * 2.0;
    const double Constants<true>::SampleRateDefault = 44100.0;
    const double Constants<true>::DenormalOffset = 1.0E-25;

    const float Constants<false>::pi = 3.141593f;
    const float Constants<false>::piDoubled = 6.283185f;
    const float Constants<false>::SampleRateDefault = 44100.0f;
    const float Constants<false>::DenormalOffset = 1.0E-25f;

    My question is whether the values definined here will ultimately be inlined
    by the compiler where they are used. They are definined in one compilation
    unit but will be used in other compilation units. Will this prevent them
    from being inlined? In other words, will this ultimately be less efficient
    than using macros?
     
    Leslie Sanford, Aug 4, 2008
    #1
    1. Advertising

  2. Leslie Sanford

    James Kanze Guest

    On Aug 4, 7:49 am, "Leslie Sanford" <>
    wrote:
    > I want to define a set of floating point constants using
    > templates insteand of macros. I'd like to determine whether
    > these constants are floats or doubles at compile time. In my
    > header file, I have this:


    > template<bool DoublePrecision>
    > struct Constants
    > {
    > typedef double SampleType;
    >
    > static const double pi;
    > static const double piDoubled;
    > static const double SampleRateDefault;
    > static const double DenormalOffset;
    > };


    > template<>
    > struct Constants<false>
    > {
    > typedef float SampleType;
    >
    > static const float pi;
    > static const float piDoubled;
    > static const float SampleRateDefault;
    > static const float DenormalOffset;
    > };


    > In my implementation file, I have this


    > #include "Constants.h"


    > const double Constants<true>::pi = 3.14159265358979;
    > const double Constants<true>::piDoubled = Constants<true>::pi * 2.0;
    > const double Constants<true>::SampleRateDefault = 44100.0;
    > const double Constants<true>::DenormalOffset = 1.0E-25;


    > const float Constants<false>::pi = 3.141593f;
    > const float Constants<false>::piDoubled = 6.283185f;
    > const float Constants<false>::SampleRateDefault = 44100.0f;
    > const float Constants<false>::DenormalOffset = 1.0E-25f;


    > My question is whether the values definined here will
    > ultimately be inlined by the compiler where they are used.


    What does it mean to "inline" a double. A double is not code.

    And of course, what code the compiler generates will depend on
    the compiler (and the machine it is generating the code for).

    > They are definined in one compilation unit but will be used in
    > other compilation units. Will this prevent them from being
    > inlined? In other words, will this ultimately be less
    > efficient than using macros?


    The only way to find out is to measure both. And that will only
    give you an answer for one particular compiler.

    As a general rule, however: the more the compiler knows, the
    better it can do its job. Arranging for the constant to be
    visible in the translation unit (e.g. by making it the return
    value of an inline function) cannot generally hurt, and it may
    help for some compilers, on some machines.

    I might also add that on a lot of machines, whether you use
    float or double, or even if you mix them, makes no difference in
    terms of spead. So your best solution might be to just declare
    your constants as static doubles (or even long doubles) in a
    namespace, and forget the template and the class.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 4, 2008
    #2
    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. Tomasz Lisowski
    Replies:
    4
    Views:
    360
    Tomasz Lisowski
    May 25, 2005
  2. Ioannis Vranos
    Replies:
    2
    Views:
    288
    Ioannis Vranos
    Mar 27, 2009
  3. Ioannis Vranos
    Replies:
    0
    Views:
    277
    Ioannis Vranos
    Mar 26, 2009
  4. Ioannis Vranos

    Using type prefixes with floating point constants

    Ioannis Vranos, Mar 26, 2009, in forum: C Programming
    Replies:
    4
    Views:
    333
    Keith Thompson
    Mar 27, 2009
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,390
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page