ptclbsum.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. static int16_t endian = 1;
  13. static uint8_t* aendian = (uint8_t*)&endian;
  14. #define LITTLE *aendian
  15. uint16_t
  16. ptclbsum(uint8_t *addr, int len)
  17. {
  18. uint32_t losum, hisum, mdsum, x;
  19. uint32_t t1, t2;
  20. losum = 0;
  21. hisum = 0;
  22. mdsum = 0;
  23. x = 0;
  24. if((uintptr)addr & 1) {
  25. if(len) {
  26. hisum += addr[0];
  27. len--;
  28. addr++;
  29. }
  30. x = 1;
  31. }
  32. while(len >= 16) {
  33. t1 = *(uint16_t*)(addr+0);
  34. t2 = *(uint16_t*)(addr+2); mdsum += t1;
  35. t1 = *(uint16_t*)(addr+4); mdsum += t2;
  36. t2 = *(uint16_t*)(addr+6); mdsum += t1;
  37. t1 = *(uint16_t*)(addr+8); mdsum += t2;
  38. t2 = *(uint16_t*)(addr+10); mdsum += t1;
  39. t1 = *(uint16_t*)(addr+12); mdsum += t2;
  40. t2 = *(uint16_t*)(addr+14); mdsum += t1;
  41. mdsum += t2;
  42. len -= 16;
  43. addr += 16;
  44. }
  45. while(len >= 2) {
  46. mdsum += *(uint16_t*)addr;
  47. len -= 2;
  48. addr += 2;
  49. }
  50. if(x) {
  51. if(len)
  52. losum += addr[0];
  53. if(LITTLE)
  54. losum += mdsum;
  55. else
  56. hisum += mdsum;
  57. } else {
  58. if(len)
  59. hisum += addr[0];
  60. if(LITTLE)
  61. hisum += mdsum;
  62. else
  63. losum += mdsum;
  64. }
  65. losum += hisum >> 8;
  66. losum += (hisum & 0xff) << 8;
  67. while((hisum = losum>>16) != 0)
  68. losum = hisum + (losum & 0xffff);
  69. return losum & 0xffff;
  70. }