Annotation of 43BSDReno/sys/tahoe/ns_cksum.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1982, 1988 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution is only permitted until one year after the first shipment
        !             6:  * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
        !             7:  * binary forms are permitted provided that: (1) source distributions retain
        !             8:  * this entire copyright notice and comment, and (2) distributions including
        !             9:  * binaries display the following acknowledgement:  This product includes
        !            10:  * software developed by the University of California, Berkeley and its
        !            11:  * contributors'' in the documentation or other materials provided with the
        !            12:  * distribution and in all advertising materials mentioning features or use
        !            13:  * of this software.  Neither the name of the University nor the names of
        !            14:  * its contributors may be used to endorse or promote products derived from
        !            15:  * this software without specific prior written permission.
        !            16:  * THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            17:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            18:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            19:  *
        !            20:  *     @(#)ns_cksum.c  7.5 (Berkeley) 6/28/90
        !            21:  */
        !            22: 
        !            23: #include "param.h"
        !            24: #include "mbuf.h"
        !            25: /*
        !            26:  * Checksum routine for Network Systems Protocol Packets (Big-Endian).
        !            27:  *
        !            28:  * This routine is very heavily used in the network
        !            29:  * code and should be modified for each CPU to be as fast as possible.
        !            30:  */
        !            31: 
        !            32: #define ADDCARRY(x)  { if ((x) > 65535) (x) -= 65535; }
        !            33: #define FOLD(x) {l_util.l = (x); (x) = l_util.s[0] + l_util.s[1]; ADDCARRY(x);}
        !            34: 
        !            35: u_short
        !            36: ns_cksum(m, len)
        !            37:        register struct mbuf *m;
        !            38:        register int len;
        !            39: {
        !            40:        register u_short *w;
        !            41:        register int sum = 0;
        !            42:        register int mlen = 0;
        !            43:        register int sum2;
        !            44: 
        !            45:        union {
        !            46:                u_short s[2];
        !            47:                long    l;
        !            48:        } l_util;
        !            49: 
        !            50:        for (;m && len; m = m->m_next) {
        !            51:                if (m->m_len == 0)
        !            52:                        continue;
        !            53:                /*
        !            54:                 * Each trip around loop adds in
        !            55:                 * word from one mbuf segment.
        !            56:                 */
        !            57:                w = mtod(m, u_short *);
        !            58:                if (mlen == -1) {
        !            59:                        /*
        !            60:                         * There is a byte left from the last segment;
        !            61:                         * ones-complement add it into the checksum.
        !            62:                         */
        !            63:                        sum  += *(u_char *)w; /* Big-Endian, else << 8 */
        !            64:                        sum += sum;
        !            65:                        w = (u_short *)(1 + (char *)w);
        !            66:                        mlen = m->m_len - 1;
        !            67:                        len--;
        !            68:                        FOLD(sum);
        !            69:                } else
        !            70:                        mlen = m->m_len;
        !            71:                if (len < mlen)
        !            72:                        mlen = len;
        !            73:                len -= mlen;
        !            74:                /*
        !            75:                 * We can do a 16 bit ones complement sum using
        !            76:                 * 32 bit arithmetic registers for adding,
        !            77:                 * with carries from the low added
        !            78:                 * into the high (by normal carry-chaining)
        !            79:                 * so long as we fold back before 16 carries have occured.
        !            80:                 */
        !            81:                if (1 & (int) w)
        !            82:                        goto uuuuglyy;
        !            83: #ifndef TINY
        !            84: /* -DTINY reduces the size from 1250 to 550, but slows it down by 22% */
        !            85:                while ((mlen -= 32) >= 0) {
        !            86:                        sum += w[0]; sum += sum; sum += w[1]; sum += sum;
        !            87:                        sum += w[2]; sum += sum; sum += w[3]; sum += sum;
        !            88:                        sum += w[4]; sum += sum; sum += w[5]; sum += sum;
        !            89:                        sum += w[6]; sum += sum; sum += w[7]; sum += sum;
        !            90:                        FOLD(sum);
        !            91:                        sum += w[8]; sum += sum; sum += w[9]; sum += sum;
        !            92:                        sum += w[10]; sum += sum; sum += w[11]; sum += sum;
        !            93:                        sum += w[12]; sum += sum; sum += w[13]; sum += sum;
        !            94:                        sum += w[14]; sum += sum; sum += w[15]; sum += sum;
        !            95:                        FOLD(sum);
        !            96:                        w += 16;
        !            97:                }
        !            98:                mlen += 32;
        !            99: #endif
        !           100:                while ((mlen -= 8) >= 0) {
        !           101:                        sum += w[0]; sum += sum; sum += w[1]; sum += sum;
        !           102:                        sum += w[2]; sum += sum; sum += w[3]; sum += sum;
        !           103:                        FOLD(sum);
        !           104:                        w += 4;
        !           105:                }
        !           106:                mlen += 8;
        !           107:                while ((mlen -= 2) >= 0) {
        !           108:                        sum += *w++; sum += sum;
        !           109:                }
        !           110:                goto commoncase;
        !           111: uuuuglyy:
        !           112: /* Big-Endian; else reverse ww and vv */
        !           113: #define ww(n) (((u_char *)w)[n + n + 1])
        !           114: #define vv(n) (((u_char *)w)[n + n])
        !           115:                sum2 = 0;
        !           116: #ifndef TINY
        !           117:                while ((mlen -= 32) >= 0) {
        !           118:                    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
        !           119:                    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
        !           120:                    sum += ww(4); sum += sum; sum += ww(5); sum += sum;
        !           121:                    sum += ww(6); sum += sum; sum += ww(7); sum += sum;
        !           122:                    FOLD(sum);
        !           123:                    sum += ww(8); sum += sum; sum += ww(9); sum += sum;
        !           124:                    sum += ww(10); sum += sum; sum += ww(11); sum += sum;
        !           125:                    sum += ww(12); sum += sum; sum += ww(13); sum += sum;
        !           126:                    sum += ww(14); sum += sum; sum += ww(15); sum += sum;
        !           127:                    FOLD(sum);
        !           128:                    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
        !           129:                    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
        !           130:                    sum2 += vv(4); sum2 += sum2; sum2 += vv(5); sum2 += sum2;
        !           131:                    sum2 += vv(6); sum2 += sum2; sum2 += vv(7); sum2 += sum2;
        !           132:                    FOLD(sum2);
        !           133:                    sum2 += vv(8); sum2 += sum2; sum2 += vv(9); sum2 += sum2;
        !           134:                    sum2 += vv(10); sum2 += sum2; sum2 += vv(11); sum2 += sum2;
        !           135:                    sum2 += vv(12); sum2 += sum2; sum2 += vv(13); sum2 += sum2;
        !           136:                    sum2 += vv(14); sum2 += sum2; sum2 += vv(15); sum2 += sum2;
        !           137:                    FOLD(sum2);
        !           138:                    w += 16;
        !           139:                }
        !           140:                mlen += 32;
        !           141: #endif
        !           142:                while ((mlen -= 8) >= 0) {
        !           143:                    sum += ww(0); sum += sum; sum += ww(1); sum += sum;
        !           144:                    sum += ww(2); sum += sum; sum += ww(3); sum += sum;
        !           145:                    FOLD(sum);
        !           146:                    sum2 += vv(0); sum2 += sum2; sum2 += vv(1); sum2 += sum2;
        !           147:                    sum2 += vv(2); sum2 += sum2; sum2 += vv(3); sum2 += sum2;
        !           148:                    FOLD(sum2);
        !           149:                    w += 4;
        !           150:                }
        !           151:                mlen += 8;
        !           152:                while ((mlen -= 2) >= 0) {
        !           153:                        sum += ww(0); sum += sum;
        !           154:                        sum2 += vv(0); sum2 += sum2;
        !           155:                        w++;
        !           156:                }
        !           157:                sum += (sum2 << 8);
        !           158: commoncase:
        !           159:                if (mlen == -1) {
        !           160:                        sum += *(u_char *)w << 8; /* Big-Endian, else no << 8 */
        !           161:                }
        !           162:                FOLD(sum);
        !           163:        }
        !           164:        if (mlen == -1) {
        !           165:                /* We had an odd number of bytes to sum; assume a garbage
        !           166:                   byte of zero and clean up */
        !           167:                sum += sum;
        !           168:                FOLD(sum);
        !           169:        }
        !           170:        /*
        !           171:         * sum has already been kept to low sixteen bits.
        !           172:         * just examine result and exit.
        !           173:         */
        !           174:        if(sum==0xffff) sum = 0;
        !           175:        return (sum);
        !           176: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.