Ignore:
Timestamp:
06/01/13 13:38:04 (11 years ago)
Author:
tbretz
Message:
Improved performance of checksum calculation by partly unrolling the loop (avoiding modulo operation, which avoids the integert and an improved byte-swap).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/checksum.h

    r13988 r16546  
    11#ifndef MARS_checksum
    22#define MARS_checksum
     3
     4#include <arpa/inet.h>
    35
    46namespace std
     
    6567        }
    6668
    67         const unsigned short *sbuf = reinterpret_cast<const unsigned short *>(buf);
     69        const uint32_t *sbuf = reinterpret_cast<const uint32_t*>(buf);
    6870
    6971        uint32_t *hilo  = reinterpret_cast<uint32_t*>(&buffer);
    7072
     73        /*
    7174        for (size_t i = 0; i < len/2; i++)
    7275        {
    7376            //swap the bytes of the 32 bits value. but...
    7477            //the hi and lo values are stored in fits-like order. do not swap them
    75             hilo[i%2] += (sbuf[i]&0xff00)>>8 | (sbuf[i]&0x00ff)<<8;
    76         }
     78            hilo[i%2] += ntohs(sbuf[i]); //(sbuf[i]&0xff00)>>8 | (sbuf[i]&0x00ff)<<8;
     79        }*/
     80
     81        // This is about as twice as fast as the loop above
     82        // ntohs is CPU optimized, hilo[n] doesn't need to be computed
     83        const uint32_t *end = sbuf + len/2;
     84        while (1)
     85        {
     86            hilo[0] += ntohs(*sbuf++);
     87            if (sbuf==end)
     88                break;
     89
     90            hilo[1] += ntohs(*sbuf++);
     91            if (sbuf==end)
     92                break;
     93        }
     94
    7795        HandleCarryBits();
    7896
Note: See TracChangeset for help on using the changeset viewer.