Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!pasteur!ucbvax!oliver.CRAY.COM!dab From: dab@oliver.CRAY.COM (Dave Borman) Newsgroups: comp.protocols.tcp-ip Subject: Re: Checksums (again) Message-ID: <8803301729.AA04851@oliver.cray.com> Date: 30 Mar 88 17:29:58 GMT Sender: usenet@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 24 > Having come in at only at the tail end of this discussion, I don't know > if anyone has pointed out that, assuming a packet length of < 2**16-1 > words, the checksum algorithm can wait to add back all the carry bits > until *after* the checksum loop is completed. I.e.: > > while (computing-the-checksum) { > checksum += buf[i++]; > } > checksum = checksum && 0xFFFF + (checksum >> 16); > checksum = checksum && 0xFFFF + (checksum >> 16); /* sic - the first carry */ > /* "add back" may cause */ > /* a second carry out */ You can only wait to do the folding in of checksum overflow if you are adding 16 bit quantites in a 32 bit sum. The BSD implementation adds 32 bits at a time in a 32 bit sum, so you have to add in the carry bit as you go. On just about any machine it will probaboy be faster to do one memory reference to get the 32 bit quantity, and then add in the carry, rather than doing two 16 bit memory references. (As a side note, on a CRAY computer you do not have a carry bit, so we do the sum by reading up 64 bit quantities, splitting them into two 32 bit quantites, and summing into a 64 bit sum. We then add in all the carry bits at the end.) -Dave Borman CRAY Research, Inc.