linux-old/arch/x86_64/lib/csum-partial.c
<<
>>
Prefs
   1/*
   2 * arch/x86_64/lib/csum-partial.c
   3 *
   4 * This file contains network checksum routines that are better done
   5 * in an architecture-specific manner due to speed.
   6 */
   7 
   8#include <linux/compiler.h>
   9#include <linux/module.h>
  10#include <asm/checksum.h>
  11
  12#define __force_inline inline __attribute__((always_inline))
  13
  14static inline unsigned short from32to16(unsigned a) 
  15{
  16        unsigned short b = a >> 16; 
  17        asm("addw %w2,%w0\n\t"
  18            "adcw $0,%w0\n" 
  19            : "=r" (b)
  20            : "0" (b), "r" (a));
  21        return b;
  22}
  23
  24/*
  25 * Do a 64-bit checksum on an arbitrary memory area.
  26 * Returns a 32bit checksum.
  27 *
  28 * This isn't as time critical as it used to be because many NICs
  29 * do hardware checksumming these days.
  30 * 
  31 * Things tried and found to not make it faster:
  32 * Manual Prefetching
  33 * Unrolling to an 128 bytes inner loop.
  34 * Using interleaving with more registers to break the carry chains.
  35 */
  36static __force_inline unsigned do_csum(const unsigned char *buff, unsigned len)
  37{
  38        unsigned odd, count;
  39        unsigned long result = 0;
  40
  41        if (unlikely(len == 0))
  42                return result; 
  43        odd = 1 & (unsigned long) buff;
  44        if (unlikely(odd)) {
  45                result = *buff << 8;
  46                len--;
  47                buff++;
  48        }
  49        count = len >> 1;               /* nr of 16-bit words.. */
  50        if (count) {
  51                if (2 & (unsigned long) buff) {
  52                        result += *(unsigned short *)buff;
  53                        count--;
  54                        len -= 2;
  55                        buff += 2;
  56                }
  57                count >>= 1;            /* nr of 32-bit words.. */
  58                if (count) {
  59                        if (4 & (unsigned long) buff) {
  60                                result += *(unsigned int *) buff;
  61                                count--;
  62                                len -= 4;
  63                                buff += 4;
  64                        }
  65                        count >>= 1;    /* nr of 64-bit words.. */
  66
  67                        /* main loop using 64byte blocks */
  68                                unsigned long zero = 0; 
  69                        unsigned count64 = count >> 3; 
  70                        while (count64) { 
  71                                asm("addq 0*8(%[src]),%[res]\n\t"
  72                                    "adcq 1*8(%[src]),%[res]\n\t"
  73                                    "adcq 2*8(%[src]),%[res]\n\t"
  74                                    "adcq 3*8(%[src]),%[res]\n\t"
  75                                    "adcq 4*8(%[src]),%[res]\n\t"
  76                                    "adcq 5*8(%[src]),%[res]\n\t"
  77                                    "adcq 6*8(%[src]),%[res]\n\t"
  78                                    "adcq 7*8(%[src]),%[res]\n\t"
  79                                    "adcq %[zero],%[res]"
  80                                    : [res] "=r" (result)
  81                                    : [src] "r" (buff), [zero] "r" (zero),
  82                                    "[res]" (result));
  83                                buff += 64;
  84                                count64--;
  85                        }
  86
  87                        /* last upto 7 8byte blocks */
  88                        count %= 8; 
  89                        while (count) { 
  90                                asm("addq %1,%0\n\t"
  91                                    "adcq %2,%0\n" 
  92                                            : "=r" (result)
  93                                    : "m" (*(unsigned long *)buff), 
  94                                    "r" (zero),  "0" (result));
  95                                --count; 
  96                                        buff += 8;
  97                        }
  98                        result = add32_with_carry(result>>32,
  99                                                  result&0xffffffff); 
 100
 101                        if (len & 4) {
 102                                result += *(unsigned int *) buff;
 103                                buff += 4;
 104                        }
 105                }
 106                if (len & 2) {
 107                        result += *(unsigned short *) buff;
 108                        buff += 2;
 109                }
 110        }
 111        if (len & 1)
 112                result += *buff;
 113        result = add32_with_carry(result>>32, result & 0xffffffff); 
 114        if (unlikely(odd)) { 
 115                result = from32to16(result);
 116                result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
 117        }
 118        return result;
 119}
 120
 121/*
 122 * computes the checksum of a memory block at buff, length len,
 123 * and adds in "sum" (32-bit)
 124 *
 125 * returns a 32-bit number suitable for feeding into itself
 126 * or csum_tcpudp_magic
 127 *
 128 * this function must be called with even lengths, except
 129 * for the last fragment, which may be odd
 130 *
 131 * it's best to have buff aligned on a 64-bit boundary
 132 */
 133unsigned csum_partial(const unsigned char *buff, unsigned len, unsigned sum)
 134{
 135        return add32_with_carry(do_csum(buff, len), sum); 
 136}
 137
 138/*
 139 * this routine is used for miscellaneous IP-like checksums.
 140 */
 141unsigned short ip_compute_csum(unsigned char * buff, int len)
 142{
 143        return csum_fold(csum_partial(buff,len,0)); 
 144}
 145
 146
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.