rc4-x86_64.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # 2.22x RC4 tune-up:-) It should be noted though that my hand [as in
  11. # "hand-coded assembler"] doesn't stand for the whole improvement
  12. # coefficient. It turned out that eliminating RC4_CHAR from config
  13. # line results in ~40% improvement (yes, even for C implementation).
  14. # Presumably it has everything to do with AMD cache architecture and
  15. # RAW or whatever penalties. Once again! The module *requires* config
  16. # line *without* RC4_CHAR! As for coding "secret," I bet on partial
  17. # register arithmetics. For example instead of 'inc %r8; and $255,%r8'
  18. # I simply 'inc %r8b'. Even though optimization manual discourages
  19. # to operate on partial registers, it turned out to be the best bet.
  20. # At least for AMD... How IA32E would perform remains to be seen...
  21. # As was shown by Marc Bevand reordering of couple of load operations
  22. # results in even higher performance gain of 3.3x:-) At least on
  23. # Opteron... For reference, 1x in this case is RC4_CHAR C-code
  24. # compiled with gcc 3.3.2, which performs at ~54MBps per 1GHz clock.
  25. # Latter means that if you want to *estimate* what to expect from
  26. # *your* Opteron, then multiply 54 by 3.3 and clock frequency in GHz.
  27. # Intel P4 EM64T core was found to run the AMD64 code really slow...
  28. # The only way to achieve comparable performance on P4 was to keep
  29. # RC4_CHAR. Kind of ironic, huh? As it's apparently impossible to
  30. # compose blended code, which would perform even within 30% marginal
  31. # on either AMD and Intel platforms, I implement both cases. See
  32. # rc4_skey.c for further details...
  33. # P4 EM64T core appears to be "allergic" to 64-bit inc/dec. Replacing
  34. # those with add/sub results in 50% performance improvement of folded
  35. # loop...
  36. # As was shown by Zou Nanhai loop unrolling can improve Intel EM64T
  37. # performance by >30% [unlike P4 32-bit case that is]. But this is
  38. # provided that loads are reordered even more aggressively! Both code
  39. # pathes, AMD64 and EM64T, reorder loads in essentially same manner
  40. # as my IA-64 implementation. On Opteron this resulted in modest 5%
  41. # improvement [I had to test it], while final Intel P4 performance
  42. # achieves respectful 432MBps on 2.8GHz processor now. For reference.
  43. # If executed on Xeon, current RC4_CHAR code-path is 2.7x faster than
  44. # RC4_INT code-path. While if executed on Opteron, it's only 25%
  45. # slower than the RC4_INT one [meaning that if CPU µ-arch detection
  46. # is not implemented, then this final RC4_CHAR code-path should be
  47. # preferred, as it provides better *all-round* performance].
  48. # Intel Core2 was observed to perform poorly on both code paths:-( It
  49. # apparently suffers from some kind of partial register stall, which
  50. # occurs in 64-bit mode only [as virtually identical 32-bit loop was
  51. # observed to outperform 64-bit one by almost 50%]. Adding two movzb to
  52. # cloop1 boosts its performance by 80%! This loop appears to be optimal
  53. # fit for Core2 and therefore the code was modified to skip cloop8 on
  54. # this CPU.
  55. $output=shift;
  56. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  57. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  58. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  59. die "can't locate x86_64-xlate.pl";
  60. open STDOUT,"| $^X $xlate $output";
  61. $dat="%rdi"; # arg1
  62. $len="%rsi"; # arg2
  63. $inp="%rdx"; # arg3
  64. $out="%rcx"; # arg4
  65. @XX=("%r8","%r10");
  66. @TX=("%r9","%r11");
  67. $YY="%r12";
  68. $TY="%r13";
  69. $code=<<___;
  70. .text
  71. .globl RC4
  72. .type RC4,\@function,4
  73. .align 16
  74. RC4: or $len,$len
  75. jne .Lentry
  76. ret
  77. .Lentry:
  78. push %r12
  79. push %r13
  80. add \$8,$dat
  81. movl -8($dat),$XX[0]#d
  82. movl -4($dat),$YY#d
  83. cmpl \$-1,256($dat)
  84. je .LRC4_CHAR
  85. inc $XX[0]#b
  86. movl ($dat,$XX[0],4),$TX[0]#d
  87. test \$-8,$len
  88. jz .Lloop1
  89. jmp .Lloop8
  90. .align 16
  91. .Lloop8:
  92. ___
  93. for ($i=0;$i<8;$i++) {
  94. $code.=<<___;
  95. add $TX[0]#b,$YY#b
  96. mov $XX[0],$XX[1]
  97. movl ($dat,$YY,4),$TY#d
  98. ror \$8,%rax # ror is redundant when $i=0
  99. inc $XX[1]#b
  100. movl ($dat,$XX[1],4),$TX[1]#d
  101. cmp $XX[1],$YY
  102. movl $TX[0]#d,($dat,$YY,4)
  103. cmove $TX[0],$TX[1]
  104. movl $TY#d,($dat,$XX[0],4)
  105. add $TX[0]#b,$TY#b
  106. movb ($dat,$TY,4),%al
  107. ___
  108. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  109. }
  110. $code.=<<___;
  111. ror \$8,%rax
  112. sub \$8,$len
  113. xor ($inp),%rax
  114. add \$8,$inp
  115. mov %rax,($out)
  116. add \$8,$out
  117. test \$-8,$len
  118. jnz .Lloop8
  119. cmp \$0,$len
  120. jne .Lloop1
  121. ___
  122. $code.=<<___;
  123. .Lexit:
  124. sub \$1,$XX[0]#b
  125. movl $XX[0]#d,-8($dat)
  126. movl $YY#d,-4($dat)
  127. pop %r13
  128. pop %r12
  129. ret
  130. .align 16
  131. .Lloop1:
  132. add $TX[0]#b,$YY#b
  133. movl ($dat,$YY,4),$TY#d
  134. movl $TX[0]#d,($dat,$YY,4)
  135. movl $TY#d,($dat,$XX[0],4)
  136. add $TY#b,$TX[0]#b
  137. inc $XX[0]#b
  138. movl ($dat,$TX[0],4),$TY#d
  139. movl ($dat,$XX[0],4),$TX[0]#d
  140. xorb ($inp),$TY#b
  141. inc $inp
  142. movb $TY#b,($out)
  143. inc $out
  144. dec $len
  145. jnz .Lloop1
  146. jmp .Lexit
  147. .align 16
  148. .LRC4_CHAR:
  149. add \$1,$XX[0]#b
  150. movzb ($dat,$XX[0]),$TX[0]#d
  151. test \$-8,$len
  152. jz .Lcloop1
  153. cmpl \$0,260($dat)
  154. jnz .Lcloop1
  155. push %rbx
  156. jmp .Lcloop8
  157. .align 16
  158. .Lcloop8:
  159. mov ($inp),%eax
  160. mov 4($inp),%ebx
  161. ___
  162. # unroll 2x4-wise, because 64-bit rotates kill Intel P4...
  163. for ($i=0;$i<4;$i++) {
  164. $code.=<<___;
  165. add $TX[0]#b,$YY#b
  166. lea 1($XX[0]),$XX[1]
  167. movzb ($dat,$YY),$TY#d
  168. movzb $XX[1]#b,$XX[1]#d
  169. movzb ($dat,$XX[1]),$TX[1]#d
  170. movb $TX[0]#b,($dat,$YY)
  171. cmp $XX[1],$YY
  172. movb $TY#b,($dat,$XX[0])
  173. jne .Lcmov$i # Intel cmov is sloooow...
  174. mov $TX[0],$TX[1]
  175. .Lcmov$i:
  176. add $TX[0]#b,$TY#b
  177. xor ($dat,$TY),%al
  178. ror \$8,%eax
  179. ___
  180. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  181. }
  182. for ($i=4;$i<8;$i++) {
  183. $code.=<<___;
  184. add $TX[0]#b,$YY#b
  185. lea 1($XX[0]),$XX[1]
  186. movzb ($dat,$YY),$TY#d
  187. movzb $XX[1]#b,$XX[1]#d
  188. movzb ($dat,$XX[1]),$TX[1]#d
  189. movb $TX[0]#b,($dat,$YY)
  190. cmp $XX[1],$YY
  191. movb $TY#b,($dat,$XX[0])
  192. jne .Lcmov$i # Intel cmov is sloooow...
  193. mov $TX[0],$TX[1]
  194. .Lcmov$i:
  195. add $TX[0]#b,$TY#b
  196. xor ($dat,$TY),%bl
  197. ror \$8,%ebx
  198. ___
  199. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  200. }
  201. $code.=<<___;
  202. lea -8($len),$len
  203. mov %eax,($out)
  204. lea 8($inp),$inp
  205. mov %ebx,4($out)
  206. lea 8($out),$out
  207. test \$-8,$len
  208. jnz .Lcloop8
  209. pop %rbx
  210. cmp \$0,$len
  211. jne .Lcloop1
  212. jmp .Lexit
  213. ___
  214. $code.=<<___;
  215. .align 16
  216. .Lcloop1:
  217. add $TX[0]#b,$YY#b
  218. movzb ($dat,$YY),$TY#d
  219. movb $TX[0]#b,($dat,$YY)
  220. movb $TY#b,($dat,$XX[0])
  221. add $TX[0]#b,$TY#b
  222. add \$1,$XX[0]#b
  223. movzb $TY#b,$TY#d
  224. movzb $XX[0]#b,$XX[0]#d
  225. movzb ($dat,$TY),$TY#d
  226. movzb ($dat,$XX[0]),$TX[0]#d
  227. xorb ($inp),$TY#b
  228. lea 1($inp),$inp
  229. movb $TY#b,($out)
  230. lea 1($out),$out
  231. sub \$1,$len
  232. jnz .Lcloop1
  233. jmp .Lexit
  234. .size RC4,.-RC4
  235. ___
  236. $idx="%r8";
  237. $ido="%r9";
  238. $code.=<<___;
  239. .extern OPENSSL_ia32cap_P
  240. .globl RC4_set_key
  241. .type RC4_set_key,\@function,3
  242. .align 16
  243. RC4_set_key:
  244. lea 8($dat),$dat
  245. lea ($inp,$len),$inp
  246. neg $len
  247. mov $len,%rcx
  248. xor %eax,%eax
  249. xor $ido,$ido
  250. xor %r10,%r10
  251. xor %r11,%r11
  252. mov OPENSSL_ia32cap_P(%rip),$idx#d
  253. bt \$20,$idx#d
  254. jnc .Lw1stloop
  255. bt \$30,$idx#d
  256. setc $ido#b
  257. mov $ido#d,260($dat)
  258. jmp .Lc1stloop
  259. .align 16
  260. .Lw1stloop:
  261. mov %eax,($dat,%rax,4)
  262. add \$1,%al
  263. jnc .Lw1stloop
  264. xor $ido,$ido
  265. xor $idx,$idx
  266. .align 16
  267. .Lw2ndloop:
  268. mov ($dat,$ido,4),%r10d
  269. add ($inp,$len,1),$idx#b
  270. add %r10b,$idx#b
  271. add \$1,$len
  272. mov ($dat,$idx,4),%r11d
  273. cmovz %rcx,$len
  274. mov %r10d,($dat,$idx,4)
  275. mov %r11d,($dat,$ido,4)
  276. add \$1,$ido#b
  277. jnc .Lw2ndloop
  278. jmp .Lexit_key
  279. .align 16
  280. .Lc1stloop:
  281. mov %al,($dat,%rax)
  282. add \$1,%al
  283. jnc .Lc1stloop
  284. xor $ido,$ido
  285. xor $idx,$idx
  286. .align 16
  287. .Lc2ndloop:
  288. mov ($dat,$ido),%r10b
  289. add ($inp,$len),$idx#b
  290. add %r10b,$idx#b
  291. add \$1,$len
  292. mov ($dat,$idx),%r11b
  293. jnz .Lcnowrap
  294. mov %rcx,$len
  295. .Lcnowrap:
  296. mov %r10b,($dat,$idx)
  297. mov %r11b,($dat,$ido)
  298. add \$1,$ido#b
  299. jnc .Lc2ndloop
  300. movl \$-1,256($dat)
  301. .align 16
  302. .Lexit_key:
  303. xor %eax,%eax
  304. mov %eax,-8($dat)
  305. mov %eax,-4($dat)
  306. ret
  307. .size RC4_set_key,.-RC4_set_key
  308. .globl RC4_options
  309. .type RC4_options,\@function,0
  310. .align 16
  311. RC4_options:
  312. .picmeup %rax
  313. lea .Lopts-.(%rax),%rax
  314. mov OPENSSL_ia32cap_P(%rip),%edx
  315. bt \$20,%edx
  316. jnc .Ldone
  317. add \$12,%rax
  318. bt \$30,%edx
  319. jnc .Ldone
  320. add \$13,%rax
  321. .Ldone:
  322. ret
  323. .align 64
  324. .Lopts:
  325. .asciz "rc4(8x,int)"
  326. .asciz "rc4(8x,char)"
  327. .asciz "rc4(1x,char)"
  328. .asciz "RC4 for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  329. .align 64
  330. .size RC4_options,.-RC4_options
  331. ___
  332. $code =~ s/#([bwd])/$1/gm;
  333. $code =~ s/RC4_set_key/private_RC4_set_key/g if ($ENV{FIPSCANLIB} ne "");
  334. print $code;
  335. close STDOUT;