From owner-freebsd-current Mon Nov 15 10:44: 7 1999 Delivered-To: freebsd-current@freebsd.org Received: from enst.enst.fr (enst.enst.fr [137.194.2.16]) by hub.freebsd.org (Postfix) with ESMTP id 8590414A26 for ; Mon, 15 Nov 1999 10:43:59 -0800 (PST) (envelope-from beyssac@enst.fr) Received: from bofh.enst.fr (bofh-2.enst.fr [137.194.2.37]) by enst.enst.fr (8.9.1a/8.9.1) with ESMTP id TAA05310; Mon, 15 Nov 1999 19:43:58 +0100 (MET) Received: by bofh.enst.fr (Postfix, from userid 12426) id A91B2D226; Mon, 15 Nov 1999 19:43:57 +0100 (CET) Message-ID: <19991115194357.T28348@enst.fr> Date: Mon, 15 Nov 1999 19:43:57 +0100 From: Pierre Beyssac To: Garrett Wollman Cc: Sheldon Hearn , freebsd-current@FreeBSD.ORG Subject: Re: egcs -O breaks ping.c:in_cksum() References: <19991115174831.B30139@enst.fr> <92805.942684743@axl.noc.iafrica.com> <19991115180145.A31542@enst.fr> <199911151835.NAA07676@khavrinen.lcs.mit.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.93.2i In-Reply-To: <199911151835.NAA07676@khavrinen.lcs.mit.edu>; from Garrett Wollman on Mon, Nov 15, 1999 at 01:35:15PM -0500 Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Mon, Nov 15, 1999 at 01:35:15PM -0500, Garrett Wollman wrote: > If, rather than casting pointers, the code used a union (containing > one u_int16_t and one array[2] of u_int8_t), the compiler would have > enough information to know about the aliases. You're right, this seems to work even with optimization turned on. If nobody objects, I'll commit it. --- ck.c.old Mon Nov 15 19:41:35 1999 +++ ck.c Mon Nov 15 19:39:43 1999 @@ -13,7 +13,10 @@ register int nleft = len; register u_short *w = addr; int sum = 0; - volatile u_short answer = 0; + union { + u_int16_t us; + u_int8_t uc[2]; + } answer; /* * Our algorithm is simple, using a 32 bit accumulator (sum), we add @@ -27,15 +30,16 @@ /* mop up an odd byte, if necessary */ if (nleft == 1) { - *(u_char *)(&answer) = *(u_char *)w ; - sum += answer; + answer.uc[0] = *(u_char *)w ; + answer.uc[1] = 0; + sum += answer.us; } /* add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ - answer = ~sum; /* truncate to 16 bits */ - return(answer); + answer.us = ~sum; /* truncate to 16 bits */ + return(answer.us); } int main() -- Pierre Beyssac pb@enst.fr To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message