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

Behdad Esfahbod behdad at behdad.org
Sun Jan 21 04:04:59 PST 2007


On Sat, 2007-01-20 at 23:38 -0500, Daniel Amelang wrote:
> 
> On 1/20/07, Carl Worth <cworth at cworth.org> wrote:
> > In fbmmx there is currently a raft of compiler warnings[*] about
> > type-punned pointer. Can someone who is more intimate with code than
> > I am please clean it up?
> 
> I don't claim to be "intimate" with this code, but I do have a simple
> fix that might be all that's needed. I see no change in "cairo test"
> results nor "cairo perf" results, so I figure it's good enough to post
> for review.
> 
> I'll push it out myself if I don't hear any complaints over the next
> few days.
>
> Dan
> 
> (inlining the patch to keep gmail from base64 encoding it)
> 
> >From 55478c3e89739299d1385df3210ff0c6b67f6a28 Mon Sep 17 00:00:00
> 2001
> From: Dan Amelang <dan at amelang.net>
> Date: Sat, 20 Jan 2007 18:34:09 -0800
> Subject: [PATCH] Fix fbmmx compile warnings
> 
> Just changed each instance of "*((type*)(&data))" into "(type)data".
> No changes in behavior (neither correctness nor performance) noted.

Except that there is no guarantee that this will work.  The entire cast
dance is there to make it legal C code.  Direct casting assumes that the
two types are "compatible", and since __m64 is a special int type with
vector_size 8, the C standard doesn't buy you anything here.  Gcc can
make that stop compiling, and it's not tested on VC.

The classic way to shut gcc up about this warning is to add a (void*)
cast in the middle.  But that just make it shut up.  It doesn't /fix/
the possible problem.  The correct fix, or rather, a correct fix would
be to use the gcc may_alias attribute, but I tried that just to hit a
compiler bug...

In our case, since all we are trying to do is cast one value type to
another value type, we know we don't have any aliasing problems, so the
(void*) approach may as well be all we need.  Still, I defined a macro
for it, so we can tweak it later if we need.  There was just one
instance of converting a __m64 to a ullong, so I coded that one
in-place.

Patch attached.

-- 
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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: cairo-fbmmx-aliasing.patch
Type: text/x-patch
Size: 4424 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20070121/7b906681/cairo-fbmmx-aliasing-0001.bin


More information about the cairo mailing list