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 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
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.
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
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
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.
>> 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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....
|
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:...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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
...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |