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          [EMAIL PROTECTED]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message

Reply via email to