GCC 4.6.1 and strict-aliasing bug?

Torquil Macdonald Sørensen torquil@gmail.com
Fri Aug 12 08:25:00 GMT 2011


On 12/08/11 06:27, Ian Lance Taylor wrote:
> Torquil Macdonald Sørensen<torquil@gmail.com>  writes:
>
>> #include "iostream"
>> #include "blitz/array.h"
>>
>> int main()
>> {
>> 	blitz::Array<double,1>  X(3); X = 0.0;
>>
>> 	blitz::Array<double,1>  var(1); var = 0.0;
>>
>> 	for(int i = 0; i != 1; ++i) {
>> 		for(int c = 0; c != 3; ++c) X(c) = double(c);
>>
>> 		var(i) = 0.0;
>> 		for(int c = 0; c != 3; ++c) var(i) += X(c)*X(c);
>> 		var(i) /= 3.0;
>> 	}
>>
>> 	std::cout<<  var(0)<<  std::endl;
>>
>> 	return(0);
>> }
>> ---------------------------
>>
>>
>> If I compile with "-O2", it prints 0, which is wrong.
>>
>> If I compile with "-O2 -fno-strict-aliasing", it prints the correct answer 1.6666..
>
> In every case I've looked at, when code gives the expected answer only
> when using -fno-strict-aliasing, it is because the code is using a type
> cast in an invalid way.
>
> In this case I would assume that the invalid type cast occurs in the
> blitz library code.  Look for invalid type casts there.

Hi, thanks for the response!  I've just posted my problem over at the blitz++ 
mailing list to see what they think about the matter.

It seems a bit strange to me that it would be a Blitz++ problem, since 
everything has worked fine using the same version of blitz++ (0.9) for a long 
time, with compiler versions all the way from after 4.1.3 up to and including 
4.5.3. But not 4.6.1... Right now I don't remember if the strict-alising issue I 
had with 4.1.3 broke compilation or generated incorrect numerical results, though.

I'm no expert on GCC or Blitz++, so I'm not discounting anything. I'll await 
their opinion.

Torquil



More information about the Gcc-help mailing list