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 3166
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. Example:
<?php
function Test($firstcall) {
echo "(enter) firstcall=$firstcall<br>\n";
|
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:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fileName="test.avi"
def saveSomething(aString):
|
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 safe and bug-free.
Now, it also occurs to me that a const something-or-other could be
passed as a reference (since it's guaranteed not to change) , or that
the address of the object could be passed rather than the whole thing in
the case of a...
|
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, 1> and A<int, 2> are different types. Now, if the A template
|
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 before
processing the xml. The parameters are not totally unknown but they are
not static, they are built by an attribute in the xml and a constant
name.
Is it possible to define a "runtime" parameter and use the value passed
| |
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 a new value. On the
following postbacks I want this variable to remain constant which means I
need to clear the parameter out of the URL before any of the postbacks
occur. Otherwise if this parameter is still in the URL when the page loads...
|
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 program bombs, but when I use a hardcoded
value it works fine. Any ideas?
Failing Version:
<SelectParameters>
|
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 parameter
will not be changed - so don't worry.
2. It tells the implementor and the maintainer of this function that
the parameter should not be changed inside the function. And it is for
this reason that some people advocate that it is a good idea to...
|
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
'----------------------------------------------------------------------------------------------------------
Before - This Works without a paramater
'----------------------------------------------------------------------------------------------------------
|
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...
|
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,...
| |
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...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |