473,507 Members | 5,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Way to tell if parameter is variable vs constant?

Is there a way to determine if a parameter to a function is a constant
(e.g. 2.0f) versus a variable? Is there some way to determine if this
is the case? (Say some metaprogramming tip or type trait?)

I have a function with an if statement, that I would like to optimize
away somehow. I was hoping the compiler would do it for me, but it
doesn't seem to.

Jay

May 1 '06 #1
7 3155
icosahedron wrote:
Is there a way to determine if a parameter to a function is a constant
(e.g. 2.0f) versus a variable? Is there some way to determine if this
is the case? (Say some metaprogramming tip or type trait?)

I have a function with an if statement, that I would like to optimize
away somehow. I was hoping the compiler would do it for me, but it
doesn't seem to.


Please post the code you want to change. So far your question does not
make sense. If your function is declared so that you may pass a constant
to it, then the argument is passed by value or by a reference to const.
Even if you could determine where the argument originated, how would it
help you? What kind of optimization are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '06 #2

icosahedron wrote:
Is there a way to determine if a parameter to a function is a constant
(e.g. 2.0f) versus a variable? Is there some way to determine if this
is the case? (Say some metaprogramming tip or type trait?)

I have a function with an if statement, that I would like to optimize
away somehow. I was hoping the compiler would do it for me, but it
doesn't seem to.


A compiler does it for you provided the definition of the function is
available in the same translation unit as the call site. I.e. it will
propagate a compile time constant through the call eliminating
conditions and dead code branches.

May 1 '06 #3
icosahedron wrote:
Is there a way to determine if a parameter to a function is a constant
(e.g. 2.0f) versus a variable? Is there some way to determine if this
is the case? (Say some metaprogramming tip or type trait?) I have a function with an if statement, that I would like to optimize
away somehow. I was hoping the compiler would do it for me, but it
doesn't seem to.
Please post the code you want to change. So far your
question does not make sense. If your function is declared
so that you may pass a constant to it, then the argument is
passed by value or by a reference to const. Even if you could determine where the argument originated,
how would it help you? What kind of optimization are you
trying to accomplish?


template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int index )
{
if( index == 0 ) {
return array[ 0 ] * scale;
}
else {
return array[ index ];
}
}
int main( void )
{
float test_array[ 5 ] = { 3.0f, 2.0f, 1.0f, 0.0f, -1.0f };
int i = 0;
cout << scale( test_array, 3.0f, 0 ) << endl;
cout << scale( test_array, 3.0f, i ) << endl;

return 0;
}

Okay, so I would like to optimize the case when a 0 is passed into
scale, so that the if will automatically recognize it's a constant and
return array[index] * scale. When a variable is passed, I can
understand the compiler keeping the if. If a constant is passed, I
would have thought that the if would be optimized away, but that
doesn't seem to be the case.

So, what I would like to do is something similar to using
boost::mpl::if_c would do with a type trait or something that could
tell if index were a constant passed or a variable.

I know this is probably not doable. I was just asking in the event
there were something I was unaware of.

Thanks,

Jay

May 1 '06 #4
icosahedron wrote:
[...]
template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int index )
{
if( index == 0 ) {
return array[ 0 ] * scale;
}
else {
return array[ index ];
}
}
int main( void )
{
float test_array[ 5 ] = { 3.0f, 2.0f, 1.0f, 0.0f, -1.0f };
int i = 0;
cout << scale( test_array, 3.0f, 0 ) << endl;
cout << scale( test_array, 3.0f, i ) << endl;

return 0;
}

Okay, so I would like to optimize the case when a 0 is passed into
scale, so that the if will automatically recognize it's a constant and
return array[index] * scale. When a variable is passed, I can
understand the compiler keeping the if. If a constant is passed, I
would have thought that the if would be optimized away, but that
doesn't seem to be the case.

So, what I would like to do is something similar to using
boost::mpl::if_c would do with a type trait or something that could
tell if index were a constant passed or a variable.

I know this is probably not doable. I was just asking in the event
there were something I was unaware of.


How about overloading:

template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int& index)
{
if ( index == 0 ) {
return array[ 0 ] * scale;
}
else {
return array[ index ];
}
}

template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int*)
{
return array[ 0 ] * scale;
}

If you pass explicit 0 (a compile-time constant), then the compiler
should be unable to bind it to a reference to non-const, but should
be able to convert it into a pointer to int. Now, if you somehow
would want to also pass 1 or 2 or 666, it won't work.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '06 #5

icosahedron wrote:

[]
template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int index )
{
if( index == 0 ) {
return array[ 0 ] * scale;
}
else {
return array[ index ];
}
}
int main( void )
{
float test_array[ 5 ] = { 3.0f, 2.0f, 1.0f, 0.0f, -1.0f };
int i = 0;
cout << scale( test_array, 3.0f, 0 ) << endl;
cout << scale( test_array, 3.0f, i ) << endl;

return 0;
}

Okay, so I would like to optimize the case when a 0 is passed into
scale, so that the if will automatically recognize it's a constant and
return array[index] * scale. When a variable is passed, I can
understand the compiler keeping the if. If a constant is passed, I
would have thought that the if would be optimized away, but that
doesn't seem to be the case.


It is the case. Compile the code with a high optimization level and
check the assembly language output.

May 2 '06 #6
>> Okay, so I would like to optimize the case when a 0 is passed into
scale, so that the if will automatically recognize it's a constant and
return array[index] * scale. When a variable is passed, I can
understand the compiler keeping the if. If a constant is passed, I
would have thought that the if would be optimized away, but that
doesn't seem to be the case.
It is the case. Compile the code with a high optimization level and
check the assembly language output.


Yes, it does indeed appear that way. Thanks for your reply.

May 22 '06 #7

icosahedron wrote:
template <typename Element, int N>
float scale( Element (&array)[N], Element scale, int index )
{
if( index == 0 ) {
return array[ 0 ] * scale;
}
else {
return array[ index ];
}
}


Shouldnt you add inline to function signature, to get earlier
optimisation?

regards
Andy Little

May 23 '06 #8

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

Similar topics

1
2215
by: Michael Kochendoerfer | last post by:
Hi, I'm new to PHP and have a small problem. Assume you define a function that is called with a constant as parameter. Within that function, based on some condition it calls itself recursively....
4
3117
by: ulysses | last post by:
Hi, I use PyQt 3.8 non-commercial version in win32. I get a big question. I Can't show PY variable in QT filedialog as initially parameter. Code sample is following:...
7
1438
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
8
3135
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
4610
by: systemutvecklare | last post by:
Hi! I have an application that generates an html-form from an xml-file using an xsl-file. My problem is that I want the xsl to use some "unknown" parameters that I pass to the xslt processor...
4
4926
by: moondaddy | last post by:
I'm passing a parameter in the url when I open up a particular page. When the page loads and finds this parameter, I know that I need to clear a variable out of the session cache and reset it with...
7
1138
by: dwg1011 | last post by:
I have a global class GClass with 2 contants. Both are declared public in the form: PUBLIC Const Const1 = "xxx". When I try to use the constant as a default value (see code excerpt below) the...
16
3144
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
5
3614
by: Dennis | last post by:
Hi I'm trying to alter my stored procedure to take a parameter for the Database Name, but as usual the syntax is killing me. Thanks for any help Dennis ...
0
7223
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,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
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,...
0
7372
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.