checksum_32.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IP/TCP/UDP checksumming routines
  7. *
  8. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  9. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  10. * Tom May, <ftom@netcom.com>
  11. * Pentium Pro/II routines:
  12. * Alexander Kjeldaas <astor@guardian.no>
  13. * Finn Arne Gangstad <finnag@guardian.no>
  14. * Lots of code moved from tcp.c and ip.c; see those files
  15. * for more names.
  16. *
  17. * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
  18. * handling.
  19. * Andi Kleen, add zeroing on error
  20. * converted to pure assembler
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License
  24. * as published by the Free Software Foundation; either version
  25. * 2 of the License, or (at your option) any later version.
  26. */
  27. #include <asm/errno.h>
  28. #include <asm/asm.h>
  29. #include <asm/export.h>
  30. /*
  31. * computes a partial checksum, e.g. for TCP/UDP fragments
  32. */
  33. /*
  34. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
  35. */
  36. .text
  37. .align 4
  38. .globl csum_partial
  39. #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
  40. /*
  41. * Experiments with Ethernet and SLIP connections show that buff
  42. * is aligned on either a 2-byte or 4-byte boundary. We get at
  43. * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
  44. * Fortunately, it is easy to convert 2-byte alignment to 4-byte
  45. * alignment for the unrolled loop.
  46. */
  47. csum_partial:
  48. pushl %esi
  49. pushl %ebx
  50. movl 20(%esp),%eax # Function arg: unsigned int sum
  51. movl 16(%esp),%ecx # Function arg: int len
  52. movl 12(%esp),%esi # Function arg: unsigned char *buff
  53. testl $2, %esi # Check alignment.
  54. jz 2f # Jump if alignment is ok.
  55. subl $2, %ecx # Alignment uses up two bytes.
  56. jae 1f # Jump if we had at least two bytes.
  57. addl $2, %ecx # ecx was < 2. Deal with it.
  58. jmp 4f
  59. 1: movw (%esi), %bx
  60. addl $2, %esi
  61. addw %bx, %ax
  62. adcl $0, %eax
  63. 2:
  64. movl %ecx, %edx
  65. shrl $5, %ecx
  66. jz 2f
  67. testl %esi, %esi
  68. 1: movl (%esi), %ebx
  69. adcl %ebx, %eax
  70. movl 4(%esi), %ebx
  71. adcl %ebx, %eax
  72. movl 8(%esi), %ebx
  73. adcl %ebx, %eax
  74. movl 12(%esi), %ebx
  75. adcl %ebx, %eax
  76. movl 16(%esi), %ebx
  77. adcl %ebx, %eax
  78. movl 20(%esi), %ebx
  79. adcl %ebx, %eax
  80. movl 24(%esi), %ebx
  81. adcl %ebx, %eax
  82. movl 28(%esi), %ebx
  83. adcl %ebx, %eax
  84. lea 32(%esi), %esi
  85. dec %ecx
  86. jne 1b
  87. adcl $0, %eax
  88. 2: movl %edx, %ecx
  89. andl $0x1c, %edx
  90. je 4f
  91. shrl $2, %edx # This clears CF
  92. 3: adcl (%esi), %eax
  93. lea 4(%esi), %esi
  94. dec %edx
  95. jne 3b
  96. adcl $0, %eax
  97. 4: andl $3, %ecx
  98. jz 7f
  99. cmpl $2, %ecx
  100. jb 5f
  101. movw (%esi),%cx
  102. leal 2(%esi),%esi
  103. je 6f
  104. shll $16,%ecx
  105. 5: movb (%esi),%cl
  106. 6: addl %ecx,%eax
  107. adcl $0, %eax
  108. 7:
  109. popl %ebx
  110. popl %esi
  111. ret
  112. #else
  113. /* Version for PentiumII/PPro */
  114. csum_partial:
  115. pushl %esi
  116. pushl %ebx
  117. movl 20(%esp),%eax # Function arg: unsigned int sum
  118. movl 16(%esp),%ecx # Function arg: int len
  119. movl 12(%esp),%esi # Function arg: const unsigned char *buf
  120. testl $2, %esi
  121. jnz 30f
  122. 10:
  123. movl %ecx, %edx
  124. movl %ecx, %ebx
  125. andl $0x7c, %ebx
  126. shrl $7, %ecx
  127. addl %ebx,%esi
  128. shrl $2, %ebx
  129. negl %ebx
  130. lea 45f(%ebx,%ebx,2), %ebx
  131. testl %esi, %esi
  132. jmp *%ebx
  133. # Handle 2-byte-aligned regions
  134. 20: addw (%esi), %ax
  135. lea 2(%esi), %esi
  136. adcl $0, %eax
  137. jmp 10b
  138. 30: subl $2, %ecx
  139. ja 20b
  140. je 32f
  141. movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
  142. addl %ebx, %eax
  143. adcl $0, %eax
  144. jmp 80f
  145. 32:
  146. addw (%esi), %ax # csumming 2 bytes, 2-aligned
  147. adcl $0, %eax
  148. jmp 80f
  149. 40:
  150. addl -128(%esi), %eax
  151. adcl -124(%esi), %eax
  152. adcl -120(%esi), %eax
  153. adcl -116(%esi), %eax
  154. adcl -112(%esi), %eax
  155. adcl -108(%esi), %eax
  156. adcl -104(%esi), %eax
  157. adcl -100(%esi), %eax
  158. adcl -96(%esi), %eax
  159. adcl -92(%esi), %eax
  160. adcl -88(%esi), %eax
  161. adcl -84(%esi), %eax
  162. adcl -80(%esi), %eax
  163. adcl -76(%esi), %eax
  164. adcl -72(%esi), %eax
  165. adcl -68(%esi), %eax
  166. adcl -64(%esi), %eax
  167. adcl -60(%esi), %eax
  168. adcl -56(%esi), %eax
  169. adcl -52(%esi), %eax
  170. adcl -48(%esi), %eax
  171. adcl -44(%esi), %eax
  172. adcl -40(%esi), %eax
  173. adcl -36(%esi), %eax
  174. adcl -32(%esi), %eax
  175. adcl -28(%esi), %eax
  176. adcl -24(%esi), %eax
  177. adcl -20(%esi), %eax
  178. adcl -16(%esi), %eax
  179. adcl -12(%esi), %eax
  180. adcl -8(%esi), %eax
  181. adcl -4(%esi), %eax
  182. 45:
  183. lea 128(%esi), %esi
  184. adcl $0, %eax
  185. dec %ecx
  186. jge 40b
  187. movl %edx, %ecx
  188. 50: andl $3, %ecx
  189. jz 80f
  190. # Handle the last 1-3 bytes without jumping
  191. notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
  192. movl $0xffffff,%ebx # by the shll and shrl instructions
  193. shll $3,%ecx
  194. shrl %cl,%ebx
  195. andl -128(%esi),%ebx # esi is 4-aligned so should be ok
  196. addl %ebx,%eax
  197. adcl $0,%eax
  198. 80:
  199. popl %ebx
  200. popl %esi
  201. ret
  202. #endif
  203. EXPORT_SYMBOL(csum_partial)