checksum.S 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * A fast checksum routine using movem
  3. * Copyright (c) 1998-2001, 2003 Axis Communications AB
  4. *
  5. * csum_partial(const unsigned char * buff, int len, unsigned int sum)
  6. */
  7. .globl csum_partial
  8. csum_partial:
  9. ;; r10 - src
  10. ;; r11 - length
  11. ;; r12 - checksum
  12. ;; check for breakeven length between movem and normal word looping versions
  13. ;; we also do _NOT_ want to compute a checksum over more than the
  14. ;; actual length when length < 40
  15. cmpu.w 80,$r11
  16. blo _word_loop
  17. nop
  18. ;; need to save the registers we use below in the movem loop
  19. ;; this overhead is why we have a check above for breakeven length
  20. ;; only r0 - r8 have to be saved, the other ones are clobber-able
  21. ;; according to the ABI
  22. subq 9*4,$sp
  23. subq 10*4,$r11 ; update length for the first loop
  24. movem $r8,[$sp]
  25. ;; do a movem checksum
  26. _mloop: movem [$r10+],$r9 ; read 10 longwords
  27. ;; perform dword checksumming on the 10 longwords
  28. add.d $r0,$r12
  29. addc $r1,$r12
  30. addc $r2,$r12
  31. addc $r3,$r12
  32. addc $r4,$r12
  33. addc $r5,$r12
  34. addc $r6,$r12
  35. addc $r7,$r12
  36. addc $r8,$r12
  37. addc $r9,$r12
  38. ;; fold the carry into the checksum, to avoid having to loop the carry
  39. ;; back into the top
  40. addc 0,$r12
  41. addc 0,$r12 ; do it again, since we might have generated a carry
  42. subq 10*4,$r11
  43. bge _mloop
  44. nop
  45. addq 10*4,$r11 ; compensate for last loop underflowing length
  46. movem [$sp+],$r8 ; restore regs
  47. _word_loop:
  48. ;; only fold if there is anything to fold.
  49. cmpq 0,$r12
  50. beq _no_fold
  51. ;; fold 32-bit checksum into a 16-bit checksum, to avoid carries below.
  52. ;; r9 and r13 can be used as temporaries.
  53. moveq -1,$r9 ; put 0xffff in r9, faster than move.d 0xffff,r9
  54. lsrq 16,$r9
  55. move.d $r12,$r13
  56. lsrq 16,$r13 ; r13 = checksum >> 16
  57. and.d $r9,$r12 ; checksum = checksum & 0xffff
  58. add.d $r13,$r12 ; checksum += r13
  59. move.d $r12,$r13 ; do the same again, maybe we got a carry last add
  60. lsrq 16,$r13
  61. and.d $r9,$r12
  62. add.d $r13,$r12
  63. _no_fold:
  64. cmpq 2,$r11
  65. blt _no_words
  66. nop
  67. ;; checksum the rest of the words
  68. subq 2,$r11
  69. _wloop: subq 2,$r11
  70. bge _wloop
  71. addu.w [$r10+],$r12
  72. addq 2,$r11
  73. _no_words:
  74. ;; see if we have one odd byte more
  75. cmpq 1,$r11
  76. beq _do_byte
  77. nop
  78. ret
  79. move.d $r12,$r10
  80. _do_byte:
  81. ;; copy and checksum the last byte
  82. addu.b [$r10],$r12
  83. ret
  84. move.d $r12,$r10