Question on strict aliasing in C.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

this it not a question on GCC, so I apologize for asking a question on
C strict aliasing rules here. As I know that some people reading this
list are much more familiar with C standard than I am, allow me to ask
that question, anyway.

Suppose the following C code that tries to implement the standard
copysign function, i.e. copy the sign of y into x and return that
value. double be 64 bits wide:


#define MASK ((unsigned short) 0x8000)

double copysign (double x, double y)
{
    unsigned short * const px = (unsigned short*)(char*)&x + 3;
    unsigned short * const py = (unsigned short*)(char*)&y + 3;

    *px = *px & ~MASK | *py & MASK;
    return x;
}


While I say that this code is not correct because it breaks C's strict
aliasing rules (e.g C89/90, Chapter 6.3; C98/99, Chapter 6.5, Clause
7), some other person very well familiar with the standard claims that
is correct and no problem.

So I want to reassure me if the code is ok or not.

I am aware of gcc's -f[no]-strict-aliasing switch, but that's rather
technical detail to cope with such presumably incorrect code.

Thanks,

Johann

BTW, gcc does just what I would expect: it returns x unchanged and
that is not an "optimizer bug".



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux