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 [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message

Reply via email to