470,810 Members | 855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

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 2974
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael Kochendoerfer | last post: by
7 posts views Thread by Richard Cavell | last post: by
8 posts views Thread by Tony Johansson | last post: by
2 posts views Thread by systemutvecklare | last post: by
4 posts views Thread by moondaddy | last post: by
7 posts views Thread by dwg1011 | last post: by
16 posts views Thread by hzmonte | last post: by
5 posts views Thread by Dennis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.