rc4-x86_64.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. Rights for redistribution and usage in source and binary
  6. # forms are granted according to the OpenSSL license.
  7. # ====================================================================
  8. #
  9. # 2.22x RC4 tune-up:-) It should be noted though that my hand [as in
  10. # "hand-coded assembler"] doesn't stand for the whole improvement
  11. # coefficient. It turned out that eliminating RC4_CHAR from config
  12. # line results in ~40% improvement (yes, even for C implementation).
  13. # Presumably it has everything to do with AMD cache architecture and
  14. # RAW or whatever penalties. Once again! The module *requires* config
  15. # line *without* RC4_CHAR! As for coding "secret," I bet on partial
  16. # register arithmetics. For example instead of 'inc %r8; and $255,%r8'
  17. # I simply 'inc %r8b'. Even though optimization manual discourages
  18. # to operate on partial registers, it turned out to be the best bet.
  19. # At least for AMD... How IA32E would perform remains to be seen...
  20. # As was shown by Marc Bevand reordering of couple of load operations
  21. # results in even higher performance gain of 3.3x:-) At least on
  22. # Opteron... For reference, 1x in this case is RC4_CHAR C-code
  23. # compiled with gcc 3.3.2, which performs at ~54MBps per 1GHz clock.
  24. # Latter means that if you want to *estimate* what to expect from
  25. # *your* Opteron, then multiply 54 by 3.3 and clock frequency in GHz.
  26. # Intel P4 EM64T core was found to run the AMD64 code really slow...
  27. # The only way to achieve comparable performance on P4 was to keep
  28. # RC4_CHAR. Kind of ironic, huh? As it's apparently impossible to
  29. # compose blended code, which would perform even within 30% marginal
  30. # on either AMD and Intel platforms, I implement both cases. See
  31. # rc4_skey.c for further details...
  32. # P4 EM64T core appears to be "allergic" to 64-bit inc/dec. Replacing
  33. # those with add/sub results in 50% performance improvement of folded
  34. # loop...
  35. # As was shown by Zou Nanhai loop unrolling can improve Intel EM64T
  36. # performance by >30% [unlike P4 32-bit case that is]. But this is
  37. # provided that loads are reordered even more aggressively! Both code
  38. # pathes, AMD64 and EM64T, reorder loads in essentially same manner
  39. # as my IA-64 implementation. On Opteron this resulted in modest 5%
  40. # improvement [I had to test it], while final Intel P4 performance
  41. # achieves respectful 432MBps on 2.8GHz processor now. For reference.
  42. # If executed on Xeon, current RC4_CHAR code-path is 2.7x faster than
  43. # RC4_INT code-path. While if executed on Opteron, it's only 25%
  44. # slower than the RC4_INT one [meaning that if CPU µ-arch detection
  45. # is not implemented, then this final RC4_CHAR code-path should be
  46. # preferred, as it provides better *all-round* performance].
  47. $output=shift;
  48. open STDOUT,"| $^X ../perlasm/x86_64-xlate.pl $output";
  49. $dat="%rdi"; # arg1
  50. $len="%rsi"; # arg2
  51. $inp="%rdx"; # arg3
  52. $out="%rcx"; # arg4
  53. @XX=("%r8","%r10");
  54. @TX=("%r9","%r11");
  55. $YY="%r12";
  56. $TY="%r13";
  57. $code=<<___;
  58. .text
  59. .globl RC4
  60. .type RC4,\@function,4
  61. .align 16
  62. RC4: or $len,$len
  63. jne .Lentry
  64. ret
  65. .Lentry:
  66. push %r12
  67. push %r13
  68. add \$8,$dat
  69. movl -8($dat),$XX[0]#d
  70. movl -4($dat),$YY#d
  71. cmpl \$-1,256($dat)
  72. je .LRC4_CHAR
  73. inc $XX[0]#b
  74. movl ($dat,$XX[0],4),$TX[0]#d
  75. test \$-8,$len
  76. jz .Lloop1
  77. jmp .Lloop8
  78. .align 16
  79. .Lloop8:
  80. ___
  81. for ($i=0;$i<8;$i++) {
  82. $code.=<<___;
  83. add $TX[0]#b,$YY#b
  84. mov $XX[0],$XX[1]
  85. movl ($dat,$YY,4),$TY#d
  86. ror \$8,%rax # ror is redundant when $i=0
  87. inc $XX[1]#b
  88. movl ($dat,$XX[1],4),$TX[1]#d
  89. cmp $XX[1],$YY
  90. movl $TX[0]#d,($dat,$YY,4)
  91. cmove $TX[0],$TX[1]
  92. movl $TY#d,($dat,$XX[0],4)
  93. add $TX[0]#b,$TY#b
  94. movb ($dat,$TY,4),%al
  95. ___
  96. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  97. }
  98. $code.=<<___;
  99. ror \$8,%rax
  100. sub \$8,$len
  101. xor ($inp),%rax
  102. add \$8,$inp
  103. mov %rax,($out)
  104. add \$8,$out
  105. test \$-8,$len
  106. jnz .Lloop8
  107. cmp \$0,$len
  108. jne .Lloop1
  109. ___
  110. $code.=<<___;
  111. .Lexit:
  112. sub \$1,$XX[0]#b
  113. movl $XX[0]#d,-8($dat)
  114. movl $YY#d,-4($dat)
  115. pop %r13
  116. pop %r12
  117. ret
  118. .align 16
  119. .Lloop1:
  120. add $TX[0]#b,$YY#b
  121. movl ($dat,$YY,4),$TY#d
  122. movl $TX[0]#d,($dat,$YY,4)
  123. movl $TY#d,($dat,$XX[0],4)
  124. add $TY#b,$TX[0]#b
  125. inc $XX[0]#b
  126. movl ($dat,$TX[0],4),$TY#d
  127. movl ($dat,$XX[0],4),$TX[0]#d
  128. xorb ($inp),$TY#b
  129. inc $inp
  130. movb $TY#b,($out)
  131. inc $out
  132. dec $len
  133. jnz .Lloop1
  134. jmp .Lexit
  135. .align 16
  136. .LRC4_CHAR:
  137. add \$1,$XX[0]#b
  138. movzb ($dat,$XX[0]),$TX[0]#d
  139. test \$-8,$len
  140. jz .Lcloop1
  141. push %rbx
  142. jmp .Lcloop8
  143. .align 16
  144. .Lcloop8:
  145. mov ($inp),%eax
  146. mov 4($inp),%ebx
  147. ___
  148. # unroll 2x4-wise, because 64-bit rotates kill Intel P4...
  149. for ($i=0;$i<4;$i++) {
  150. $code.=<<___;
  151. add $TX[0]#b,$YY#b
  152. lea 1($XX[0]),$XX[1]
  153. movzb ($dat,$YY),$TY#d
  154. movzb $XX[1]#b,$XX[1]#d
  155. movzb ($dat,$XX[1]),$TX[1]#d
  156. movb $TX[0]#b,($dat,$YY)
  157. cmp $XX[1],$YY
  158. movb $TY#b,($dat,$XX[0])
  159. jne .Lcmov$i # Intel cmov is sloooow...
  160. mov $TX[0],$TX[1]
  161. .Lcmov$i:
  162. add $TX[0]#b,$TY#b
  163. xor ($dat,$TY),%al
  164. ror \$8,%eax
  165. ___
  166. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  167. }
  168. for ($i=4;$i<8;$i++) {
  169. $code.=<<___;
  170. add $TX[0]#b,$YY#b
  171. lea 1($XX[0]),$XX[1]
  172. movzb ($dat,$YY),$TY#d
  173. movzb $XX[1]#b,$XX[1]#d
  174. movzb ($dat,$XX[1]),$TX[1]#d
  175. movb $TX[0]#b,($dat,$YY)
  176. cmp $XX[1],$YY
  177. movb $TY#b,($dat,$XX[0])
  178. jne .Lcmov$i # Intel cmov is sloooow...
  179. mov $TX[0],$TX[1]
  180. .Lcmov$i:
  181. add $TX[0]#b,$TY#b
  182. xor ($dat,$TY),%bl
  183. ror \$8,%ebx
  184. ___
  185. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  186. }
  187. $code.=<<___;
  188. lea -8($len),$len
  189. mov %eax,($out)
  190. lea 8($inp),$inp
  191. mov %ebx,4($out)
  192. lea 8($out),$out
  193. test \$-8,$len
  194. jnz .Lcloop8
  195. pop %rbx
  196. cmp \$0,$len
  197. jne .Lcloop1
  198. jmp .Lexit
  199. ___
  200. $code.=<<___;
  201. .align 16
  202. .Lcloop1:
  203. add $TX[0]#b,$YY#b
  204. movzb ($dat,$YY),$TY#d
  205. movb $TX[0]#b,($dat,$YY)
  206. movb $TY#b,($dat,$XX[0])
  207. add $TX[0]#b,$TY#b
  208. add \$1,$XX[0]#b
  209. movzb ($dat,$TY),$TY#d
  210. movzb ($dat,$XX[0]),$TX[0]#d
  211. xorb ($inp),$TY#b
  212. lea 1($inp),$inp
  213. movb $TY#b,($out)
  214. lea 1($out),$out
  215. sub \$1,$len
  216. jnz .Lcloop1
  217. jmp .Lexit
  218. .size RC4,.-RC4
  219. ___
  220. $code =~ s/#([bwd])/$1/gm;
  221. print $code;
  222. close STDOUT;