Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 09:30:01 -0500 (EST)
From:      Andrew Gallatin <gallatin@cs.duke.edu>
To:        Harti Brandt <brandt@fokus.gmd.de>
Cc:        FreeBSD-current@FreeBSD.ORG
Subject:   Re: gcc 3.2.1 optimization bug ?
Message-ID:  <15823.48873.236346.943980@grasshopper.cs.duke.edu>
In-Reply-To: <20021111135051.H11404-100000@beagle.fokus.gmd.de>
References:  <ywlvg34jn21.fsf@cerberus.proc.flab.fujitsu.co.jp> <20021111135051.H11404-100000@beagle.fokus.gmd.de>

next in thread | previous in thread | raw e-mail | index | archive | help

Harti Brandt writes:
 > On Mon, 11 Nov 2002, TOMITA Yoshinori wrote:
 > 
 > This is probably not a bug, but a feature. You are not expected to access
 > a variable through a pointer to a non-compatible type. int and short are
 > not compatible. (see your ISO C standard on this topic).
 > 
 > Try to use ntohl(), htonl() for your problem.
 > 

On a similar theme, I assume the following is also not safe:

static __inline u_int64_t
__bswap64 (u_int64_t x)
{
  u_int64_t ret;
  u_int32_t *x_ptr, *ret_ptr;

  x_ptr = (u_int32_t *)&x;
  ret_ptr = (u_int32_ *)&ret;
  ret_ptr[0] = __bswap32 (x_ptr[1]);
  ret_ptr[1] = __bswap32 (x_ptr[0]);

  return ret;
}


But does using a union make it safe?

static __inline u_int64_t
__bswap64 (u_int64_t x)
{
  union
  {
    u_int64_t u64;
    u_int32_t u32[2];
  }
  ret, old;

  old.u64 = x;
  ret.u32[0] = __bswap32 (old.u32[1]);
  ret.u32[1] = __bswap32 (old.u32[0]);
  return ret.u64;
}


FWIW, both *seem* to work correctly using gcc version 3.2.1 and high
optimization. 

Thanks,

Drew

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15823.48873.236346.943980>