checksum.S 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * A fast checksum routine using movem
  3. * Copyright (c) 1998-2007 Axis Communications AB
  4. *
  5. * csum_partial(const unsigned char * buff, int len, unsigned int sum)
  6. */
  7. .globl csum_partial
  8. .type csum_partial,@function
  9. csum_partial:
  10. ;; r10 - src
  11. ;; r11 - length
  12. ;; r12 - checksum
  13. ;; Optimized for large packets
  14. subq 10*4, $r11
  15. blt _word_loop
  16. move.d $r11, $acr
  17. subq 9*4,$sp
  18. clearf c
  19. movem $r8,[$sp]
  20. ;; do a movem checksum
  21. _mloop: movem [$r10+],$r9 ; read 10 longwords
  22. ;; Loop count without touching the c flag.
  23. addoq -10*4, $acr, $acr
  24. ;; perform dword checksumming on the 10 longwords
  25. addc $r0,$r12
  26. addc $r1,$r12
  27. addc $r2,$r12
  28. addc $r3,$r12
  29. addc $r4,$r12
  30. addc $r5,$r12
  31. addc $r6,$r12
  32. addc $r7,$r12
  33. addc $r8,$r12
  34. addc $r9,$r12
  35. ;; test $acr without trashing carry.
  36. move.d $acr, $acr
  37. bpl _mloop
  38. ;; r11 <= acr is not really needed in the mloop, just using the dslot
  39. ;; to prepare for what is needed after mloop.
  40. move.d $acr, $r11
  41. ;; fold the last carry into r13
  42. addc 0, $r12
  43. movem [$sp+],$r8 ; restore regs
  44. _word_loop:
  45. addq 10*4,$r11 ; compensate for last loop underflowing length
  46. moveq -1,$r9 ; put 0xffff in r9, faster than move.d 0xffff,r9
  47. lsrq 16,$r9
  48. move.d $r12,$r13
  49. lsrq 16,$r13 ; r13 = checksum >> 16
  50. and.d $r9,$r12 ; checksum = checksum & 0xffff
  51. _no_fold:
  52. subq 2,$r11
  53. blt _no_words
  54. add.d $r13,$r12 ; checksum += r13
  55. ;; checksum the rest of the words
  56. _wloop: subq 2,$r11
  57. bge _wloop
  58. addu.w [$r10+],$r12
  59. _no_words:
  60. addq 2,$r11
  61. ;; see if we have one odd byte more
  62. bne _do_byte
  63. nop
  64. ret
  65. move.d $r12,$r10
  66. _do_byte:
  67. ;; copy and checksum the last byte
  68. addu.b [$r10],$r12
  69. ret
  70. move.d $r12,$r10
  71. .size csum_partial, .-csum_partial