checksum.S 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * A fast checksum routine using movem
  3. * Copyright (c) 1998-2001 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. movem $r8,[$sp]
  24. ;; do a movem checksum
  25. subq 10*4,$r11 ; update length for the first loop
  26. _mloop: movem [$r10+],$r9 ; read 10 longwords
  27. ;; perform dword checksumming on the 10 longwords
  28. add.d $r0,$r12
  29. ax
  30. add.d $r1,$r12
  31. ax
  32. add.d $r2,$r12
  33. ax
  34. add.d $r3,$r12
  35. ax
  36. add.d $r4,$r12
  37. ax
  38. add.d $r5,$r12
  39. ax
  40. add.d $r6,$r12
  41. ax
  42. add.d $r7,$r12
  43. ax
  44. add.d $r8,$r12
  45. ax
  46. add.d $r9,$r12
  47. ;; fold the carry into the checksum, to avoid having to loop the carry
  48. ;; back into the top
  49. ax
  50. addq 0,$r12
  51. subq 10*4,$r11
  52. bge _mloop
  53. nop
  54. addq 10*4,$r11 ; compensate for last loop underflowing length
  55. movem [$sp+],$r8 ; restore regs
  56. _word_loop:
  57. ;; only fold if there is anything to fold.
  58. cmpq 0,$r12
  59. beq _no_fold
  60. ;; fold 32-bit checksum into a 16-bit checksum, to avoid carries below.
  61. ;; r9 and r13 can be used as temporaries.
  62. moveq -1,$r9 ; put 0xffff in r9, faster than move.d 0xffff,r9
  63. lsrq 16,$r9
  64. move.d $r12,$r13
  65. lsrq 16,$r13 ; r13 = checksum >> 16
  66. and.d $r9,$r12 ; checksum = checksum & 0xffff
  67. add.d $r13,$r12 ; checksum += r13
  68. _no_fold:
  69. cmpq 2,$r11
  70. blt _no_words
  71. nop
  72. ;; checksum the rest of the words
  73. subq 2,$r11
  74. _wloop: subq 2,$r11
  75. bge _wloop
  76. addu.w [$r10+],$r12
  77. addq 2,$r11
  78. _no_words:
  79. ;; see if we have one odd byte more
  80. cmpq 1,$r11
  81. beq _do_byte
  82. nop
  83. ret
  84. move.d $r12, $r10
  85. _do_byte:
  86. ;; copy and checksum the last byte
  87. addu.b [$r10],$r12
  88. ret
  89. move.d $r12, $r10