From owner-freebsd-current Mon Nov 11 6:30:34 2002 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 04E5B37B401 for ; Mon, 11 Nov 2002 06:30:33 -0800 (PST) Received: from duke.cs.duke.edu (duke.cs.duke.edu [152.3.140.1]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4846743E42 for ; Mon, 11 Nov 2002 06:30:32 -0800 (PST) (envelope-from gallatin@cs.duke.edu) Received: from grasshopper.cs.duke.edu (grasshopper.cs.duke.edu [152.3.145.30]) by duke.cs.duke.edu (8.9.3/8.9.3) with ESMTP id JAA03312; Mon, 11 Nov 2002 09:30:31 -0500 (EST) Received: (from gallatin@localhost) by grasshopper.cs.duke.edu (8.11.6/8.9.1) id gABEU1K47896; Mon, 11 Nov 2002 09:30:01 -0500 (EST) (envelope-from gallatin@cs.duke.edu) From: Andrew Gallatin MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15823.48873.236346.943980@grasshopper.cs.duke.edu> Date: Mon, 11 Nov 2002 09:30:01 -0500 (EST) To: Harti Brandt Cc: FreeBSD-current@FreeBSD.ORG Subject: Re: gcc 3.2.1 optimization bug ? In-Reply-To: <20021111135051.H11404-100000@beagle.fokus.gmd.de> References: <20021111135051.H11404-100000@beagle.fokus.gmd.de> X-Mailer: VM 6.75 under 21.1 (patch 12) "Channel Islands" XEmacs Lucid Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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