473,695 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SampleRateDefau lt;
static const double DenormalOffset;
};

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

static const float pi;
static const float piDoubled;
static const float SampleRateDefau lt;
static const float DenormalOffset;
};

In my implementation file, I have this

#include "Constants. h"

const double Constants<true> ::pi = 3.1415926535897 9;
const double Constants<true> ::piDoubled = Constants<true> ::pi * 2.0;
const double Constants<true> ::SampleRateDef ault = 44100.0;
const double Constants<true> ::DenormalOffse t = 1.0E-25;

const float Constants<false >::pi = 3.141593f;
const float Constants<false >::piDoubled = 6.283185f;
const float Constants<false >::SampleRateDe fault = 44100.0f;
const float Constants<false >::DenormalOffs et = 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 2513
In article <g7**********@r egistered.motza rella.org>, "Leslie Sanford"
<ja**********@b itemehotmail.co mwrote:
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.1415926535897 9;
[ ...]
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.1415926535897 9;
...

You could also put the constants in inline member functions.

template<typena me T>
struct Constants
{
typedef T SampleType;

static SampleType pi() { return 3.1415926535897 9; }
};

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<typena me T>
struct Constants
{
typedef T SampleType;

struct pi_ {
operator SampleType () const { return 3.1415926535897 9; }
};

static pi_ const pi;
};

template<typena me T>
Constants<T>::p i_ const Constants<T>::p i;

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...@b itemehotmail.co m>
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 SampleRateDefau lt;
static const double DenormalOffset;
};
template<>
struct Constants<false >
{
typedef float SampleType;

static const float pi;
static const float piDoubled;
static const float SampleRateDefau lt;
static const float DenormalOffset;
};
In my implementation file, I have this
#include "Constants. h"
const double Constants<true> ::pi = 3.1415926535897 9;
const double Constants<true> ::piDoubled = Constants<true> ::pi * 2.0;
const double Constants<true> ::SampleRateDef ault = 44100.0;
const double Constants<true> ::DenormalOffse t = 1.0E-25;
const float Constants<false >::pi = 3.141593f;
const float Constants<false >::piDoubled = 6.283185f;
const float Constants<false >::SampleRateDe fault = 44100.0f;
const float Constants<false >::DenormalOffs et = 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 objektorientier ter Datenverarbeitu ng
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
1459
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 platforms, but recently we have discovered an annoying problem. In a module, there is the following code fragment: Deg2Rad = math.pi/180.0 angleEPS = 0.5
10
6714
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 exception for testing purposes? 2. What is the fastest wat to suppress floating point exceptions? Take the following situation as example. float x = 1.0e38, //max float value
5
3743
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 using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
10
1566
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 literals? float f=3.; int v = f==3.; is v guaranteed to be one? What about situations involving conversions
10
18760
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
2149
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 working, but it fails on one test case. Here is my function:
70
3600
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 register or in memory. For example, an implicit spilling of a register is not permitted to alter the value. Also, an explicit store and load is required to round to the precision of the storage type. In particular, casts and assignments are...
3
311
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 point operations. As it is a big project it is not possible to check every line manually, so is there any other method to detect floating point operations in my project?
2
1311
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 one write something like this with a single class declaration: struct s_05 { static const double TF = 0.5;
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.