[cairo] Compiler warnings of type-punned pointers in fbmmx.c

Behdad Esfahbod behdad at behdad.org
Sun Jan 21 12:42:19 PST 2007


On Sun, 2007-01-21 at 07:05 -0500, Behdad Esfahbod wrote:
> 
> Patch attached. 

I went ahead and pushed this:

commit 6aa8e80cc722774191c4418c9a2cd434c0538508
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun Jan 21 15:06:02 2007 -0500

    [pixman/src/fbmmx.c] Hide "dereferencing type-punned pointer" warnings
    
    The warning happens all the place when the code converts from ullong to __m64.
    The way the conversion is done is a C idiom: 1) get a pointer to the value, 2)
    convert it to the suitable pointer type for the target, 3) dereference it.
    That is "*(__m64*)(&m)" in this case.  This is necessarily (as opposed to just
    casting to target type) because the two types may not be "compatible" from the
    compiler's point of view.  Example of types that are not compatbile is structs
    vs anything.
    
    The "dereferencing type-punned pointer will break strict-aliasing rules" from
    gcc exactly means: "some code may be assuming that pointers with different
    types do not compare equal (or otherwise share the same target object).  If
    you case a pointer to a different type and dereference it, it may happen
    here."  However, in our usecase, it's clear that the compiler cannot make any
    false assumptions.  So we just go ahead and hide it by using a middle cast to
    "void *".  Since the compiler does not many any aliasing assumptions about
    generic pointers, it will not warn either. (Though the problems if any, will
    still occure.  So this is not an ideal solution to this problem and should be
    used very carefully, to no hide useful warnings for when things go loose on
    some weird architecture.)
    
    Another solution would have been to use gcc's "may_alias" function attribute,
    but trying to define a may_alias version of __m64 hit a bug in gcc.  That is,
    try replacing "__m64" with "m64" and define:
    
      typedef __m64 m64 __attribute__((may_alias));
    
    and see it fail to compile.  This seems to be because of the special vector
    type that __m64 has.


-- 
behdad
http://behdad.org/

"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759





More information about the cairo mailing list