lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] RE:Re: Checksum problem with lwip_chksum()


From: paulc
Subject: Re: [lwip-users] RE:Re: Checksum problem with lwip_chksum()
Date: Fri, 10 Sep 2004 08:03:58 +0930

Heres my fix, it uses word access so it should be faster than the byte
acess code.

Paul


static u16_t
lwip_chksum(void *dataptr, int len)
{
  u32_t acc;
  u8_t  OddStart;

  OddStart = (u8_t)dataptr & 0x01;
  if (OddStart)
  {
    acc =  *(u8_t *)dataptr;
    dataptr = (void *)((u8_t *)dataptr + 1);
    len--;
  } else
  {
    acc = 0;
  }
  
  LWIP_DEBUGF(INET_DEBUG, ("lwip_chksum(%p, %d)\n", (void *)dataptr,
  len));
  for( ; len > 1; len -= 2) {
      /*    acc = acc + *((u16_t *)dataptr)++;*/
    acc += *(u16_t *)dataptr;
    dataptr = (void *)((u16_t *)dataptr + 1);
  }

  /* add up any odd byte */
  if (len == 1) {
    acc += htons((u16_t)((*(u8_t *)dataptr) & 0xff) << 8);
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: odd byte %d\n", (unsigned
    int)(*(u8_t *)dataptr)));
  } else {
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: no odd byte\n"));
  }
  acc = (acc >> 16) + (acc & 0xffffUL);

  if ((acc & 0xffff0000) != 0) {
    acc = (acc >> 16) + (acc & 0xffffUL);
  }
  
  if (OddStart) {
    acc = ((acc & 0xff00)>>8) | ((acc & 0x00ff)<<8);
  }
  
  return (u16_t)acc;
}

On Thu, 2 Sep 2004 20:14:35 +0100, "Chris WIlliams"
<address@hidden> said:
> We all have our own way of coding. This is my routine at the moment.
> 
> I can't tell if this is faster of slower than John Taylors. If people
> find this of use, could it be included properly?
> 
> Good luck.
> 
> static u16_t
> lwip_chksum(void *dataptr, int len)
> {
>   u32_t acc = 0;
>         unsigned char *dp = (unsigned char *)dataptr;
>         int half_len;
> 
>         for(half_len = len / 2;half_len;half_len--){
>                 acc += ((u16_t)*dp++) << 8;
>                 acc += *dp++;
>         }
>         if(len & 1){
>                 acc += ((u16_t)*dp++) << 8;
>         }
> 
>   acc = (acc >> 16) + (acc & 0xffffUL);
> 
>   if ((acc & 0xffff0000L) != 0) {
>          acc = (acc >> 16) + (acc & 0xffffUL);
>   }
>   return (u16_t)acc;
> }
> 
> -- 
> ---------------------------------------------------------------------------
> | Chris Williams    EMail address@hidden                     
> |
> |                   Tel/Fax  01686 688065                                
> |
> | Chrysalis Design. Electronics, Computers, Hardware, Software.          
> |
> |                   Design and development to meet all your needs.       
> |
> ---------------------------------------------------------------------------
> 
> 
> _______________________________________________
> lwip-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/lwip-users




reply via email to

[Prev in Thread] Current Thread [Next in Thread]