Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 15 Nov 1999 19:43:57 +0100
From:      Pierre Beyssac <beyssac@enst.fr>
To:        Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
Cc:        Sheldon Hearn <sheldonh@uunet.co.za>, freebsd-current@FreeBSD.ORG
Subject:   Re: egcs -O breaks ping.c:in_cksum()
Message-ID:  <19991115194357.T28348@enst.fr>
In-Reply-To: <199911151835.NAA07676@khavrinen.lcs.mit.edu>; from Garrett Wollman on Mon, Nov 15, 1999 at 01:35:15PM -0500
References:  <19991115174831.B30139@enst.fr> <92805.942684743@axl.noc.iafrica.com> <19991115180145.A31542@enst.fr> <199911151835.NAA07676@khavrinen.lcs.mit.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
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




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