Inlining "unexpected behaviour"

Andrew Haley aph@redhat.com
Mon Nov 17 18:39:00 GMT 2008


Hazael Maldonado Torres wrote:

Hi.  This should not be sent to gcc@gcc.gnu.org.  I'm redirecting to gcc-help@gcc.gnu.org.

> I am coding a library to perform a statistical method. I have been using a -O2
> optimisation level but I have just realised that switching it on and off
> yields different results.
> 
> I have two functions cal_probability_var() and cal_heterozygosity(), the first
> calls the second (see below). If I compile my code without optimisation I get
> the expected result but if optimisations are allowed I get the wrong result.
> My best guess is that the problem is that with optimisations the function
> cal_heterozygosity() is inlined within cal_probability_var() (using gdb I
> confirmed it because gdb behaves as expected for inlined functions).
> 
> To my surprise, if I swap the operators of the multiplication from:
>   ln_2z =  log(2.0) * cal_heterozygosity(dist_matrix, table_type);
> to:
>   ln_2z = cal_heterozygosity(dist_matrix, table_type) * log(2.0);
> then the problem disappears.
> 
> My main concerned is whether the way I have coded cal_heterozygosity()
> conflicts with GCC or whether there is a bug in GCC. If it is indeed my
> mistake I would like to know what is wrong with the coding as I may be using
> the same standards to code other functions in my library. Or, it is possible
> to get access to the source code once it has been optimised?.

I'm guessing that you're doing something bogus in some code you haven't sent.
It may be in the macro OFFSET.  Or maybe not, hard to say.

If you send us an example of this problem that we can compile and run, along
with the expected output, we can have a look.

Andrew.



More information about the Gcc-help mailing list