checksumcopy.S 2.6 KB

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