473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Floating Point Constants - Inlining Questions

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?


Aug 4 '08 #1
2 2484
In article <g7**********@registered.motzarella.org>, "Leslie Sanford"
<ja**********@bitemehotmail.comwrote:
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 implementation file, I have this

#include "Constants.h"

const double Constants<true>::pi = 3.14159265358979;
[ ...]
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?
Probably. If the compiler knows the value where it's used, it can do
constant propagation, for example

float f( float n ) { return n * (pi/2); }

could be compiled to a single multiply. With static constants defined
elsewhere, it's going to be hard to compile it to anything except what's
written.

You don't need macros, though, just constants in a header file.

#include "config.h" // typedef float/double Real;

const Real pi = 3.14159265358979;
...

You could also put the constants in inline member functions.

template<typename T>
struct Constants
{
typedef T SampleType;

static SampleType pi() { return 3.14159265358979; }
};

If treating them as values is important, you could make each a class which
had an operator SampleType(). That should allow the compiler to optimize
fully.

template<typename T>
struct Constants
{
typedef T SampleType;

struct pi_ {
operator SampleType () const { return 3.14159265358979; }
};

static pi_ const pi;
};

template<typename T>
Constants<T>::pi_ const Constants<T>::pi;

You could use a macro to generate the structs for each constant.
Aug 4 '08 #2
On Aug 4, 7:49 am, "Leslie Sanford" <jabberdab...@bitemehotmail.com>
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:ja*********@gmail.com
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
Aug 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tomasz Lisowski | last post by:
Hi, We are distributing our Python application as the short main script (.py file) and a set of modules compiled to the .pyc files. So far, we have always treated .pyc files as portable between...
10
by: Vinny | last post by:
I have a few floating point questions/issues I wish to ask. An answer or discussion would be nice too :) Any links discussion the subject would be nice too.. 1. How do I generate an underflow...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
10
by: Peter Ammon | last post by:
I'm fuzzy on exactly what I can depend on when doing floating point comparisons. I know that calculated values can differ, e.g. I'm not guaranteed that 1. + 2. == 3. What about comparisons with...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
27
by: G Patel | last post by:
Hello, I'm having trouble with floating point problems. I'm trying to write a function that calculates the distance between two cartesian points (integer coordinates). I have the function...
70
by: Robert Gamble | last post by:
9899:1999 5.1.2.3 Example 4 reads: "EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a...
3
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
2
by: mathieu | last post by:
Hello, I am trying to work around the issue with floating point as template parameter. Basically all I want is be able to take advantage of the compile time optimization. For instance how would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.