Re: libalias patch for review / testing

From: John Polstra <jdp_at_polstra.com>
Date: Thu, 18 Mar 2004 08:48:58 -0800 (PST)
On 16-Mar-2004 Dag-Erling Smørgrav wrote:
> Ruslan Ermilov <ru_at_FreeBSD.org> writes:
>> I know this code quite well.  Where do you suspect could be a bug
>> affecting -O2 compiles, or you just simply fixed -O2 and hope it
>> will auto-fix the (possible) bugs in -O2?
> 
> Since there is no inline asm, the most likely suspect is aliasing,
> which is what my patch tries to address.

To eliminate aliasing problems reliably, I think you're going to
have to use a union.  That's the only kind of type punning that gcc
promises to allow even at high optimization levels.  From the info
pages (in the description of "-fstrict-aliasing"):

     Pay special attention to code like this:
          union a_union {
            int i;
            double d;
          };
          
          int f() {
            a_union t;
            t.d = 3.0;
            return t.i;
          }
     The practice of reading from a different union member than the one
     most recently written to (called "type-punning") is common.  Even
     with `-fstrict-aliasing', type-punning is allowed, provided the
     memory is accessed through the union type.  So, the code above
     will work as expected.  However, this code might not:
          int f() {
            a_union t;
            int* ip;
            t.d = 3.0;
            ip = &t.i;
            return *ip;
          }

Even if you use a union, it won't necessarily work with compilers
other than gcc.

John
Received on Thu Mar 18 2004 - 07:49:03 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:48 UTC