This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: GCC 4.8.4 4.9.2 nifti_swap_2bytes optimization bug
- From: Andreas Schuh <andreas dot schuh dot 84 at gmail dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 1 Apr 2015 19:07:02 +0100
- Subject: Re: GCC 4.8.4 4.9.2 nifti_swap_2bytes optimization bug
- Authentication-results: sourceware.org; auth=none
- References: <3ED91A29-66B4-46D0-B4B1-0078A0EAE5A7 at gmail dot com> <551C0567 dot 60406 at redhat dot com>
Thanks for the quick reply. As I just noticed, this problem has been fixed already in the latest NIfTI C library v2.0. We just still used an old copy it :S.
> On 1 Apr 2015, at 15:49, Andrew Haley <aph@redhat.com> wrote:
>
> On 04/01/2015 03:36 PM, Andreas Schuh wrote:
>
>> I believe I have encountered a bug in the optimization at least in
>> versions 4.8.4 and 4.9.2. The problem is not present in version
>> 4.6.3. The attached complete example code can be used to reproduce
>> the error (my system is Ubuntu 12.04). This code is crucial in
>> medical image processing where the NIfTI-1 file format is commonly
>> used. The respective function that is optimized incorrectly by most
>> recent C/C++ compilers of GCC was copied from the NIfTI-1 C library
>> and is used by many medical image processing tools. It is
>> responsible for swapping the bytes if necessary when the image data
>> was written on a system with differing endianness.
>
> Sure, but it's not standard C. You really don't need to alias a
> struct of two bytes and a short, and these are not compatible types.
> (I can give you chapter and verse of the C standard if you really want
> it, but the rule is simple: don't expect a cast between pointers to
> different types to work. Doing so via a hidden void* doesn't help.)
>
> It'd be perfectly legal to do this swap with an array of bytes,
> because byte arrays can alias anything.
>
> Like this:
>
> char *p = ar;
> for( ii=0 ; ii < 2*n ; ii +=2 ){
> tt = p[ii]; p[ii] = p[ii+1] ; p[ii+1] = tt ;
> }
>
> If you really need to compile stuff like this you can use
> -fno-strict-aliasing, but I'd fix the code if I were you.
>
> Andrew.