Today while programming I stumbled upon the following question - are there any compilers which optimize based on mathematical assumptions?
For instance in cases like
unsigned int i,b;
(i,b not constant)
if(sqrt(i) == b)
...
In this case it would be a lot more effective to use
unsigned int i,b;
(i,b not constant)
if(i == b*b)
...
Assuming a sqrt() function, that handles unsigned integers and rounds sensefully.
Since I was not able to find useful information (probably because I did not know what to search for specifically), can someone please tell me or point me to a relevant source?
Are there compilers (for imperative languages) who optimize such things using some kind of heuristic? Or more specifically - what about gcc and microsoft visual c++ and matlab?
sqrt(x)
andoperator*(y,y)
are equivalent in this context? You need to answer that question first. – user22815 Jan 22 '16 at 21:25sqrt
of a negative to cause an error that you are removing with your change just as something to note. – JB King Jan 22 '16 at 21:34