x86_64-gf2m.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@openssl.org> 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. # May 2011
  11. #
  12. # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
  13. # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
  14. # the time being... Except that it has two code paths: code suitable
  15. # for any x86_64 CPU and PCLMULQDQ one suitable for Westmere and
  16. # later. Improvement varies from one benchmark and µ-arch to another.
  17. # Vanilla code path is at most 20% faster than compiler-generated code
  18. # [not very impressive], while PCLMULQDQ - whole 85%-160% better on
  19. # 163- and 571-bit ECDH benchmarks on Intel CPUs. Keep in mind that
  20. # these coefficients are not ones for bn_GF2m_mul_2x2 itself, as not
  21. # all CPU time is burnt in it...
  22. $flavour = shift;
  23. $output = shift;
  24. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  25. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  26. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  27. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  28. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  29. die "can't locate x86_64-xlate.pl";
  30. open STDOUT,"| $^X $xlate $flavour $output";
  31. ($lo,$hi)=("%rax","%rdx"); $a=$lo;
  32. ($i0,$i1)=("%rsi","%rdi");
  33. ($t0,$t1)=("%rbx","%rcx");
  34. ($b,$mask)=("%rbp","%r8");
  35. ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(9..15));
  36. ($R,$Tx)=("%xmm0","%xmm1");
  37. $code.=<<___;
  38. .text
  39. .type _mul_1x1,\@abi-omnipotent
  40. .align 16
  41. _mul_1x1:
  42. sub \$128+8,%rsp
  43. mov \$-1,$a1
  44. lea ($a,$a),$i0
  45. shr \$3,$a1
  46. lea (,$a,4),$i1
  47. and $a,$a1 # a1=a&0x1fffffffffffffff
  48. lea (,$a,8),$a8
  49. sar \$63,$a # broadcast 63rd bit
  50. lea ($a1,$a1),$a2
  51. sar \$63,$i0 # broadcast 62nd bit
  52. lea (,$a1,4),$a4
  53. and $b,$a
  54. sar \$63,$i1 # boardcast 61st bit
  55. mov $a,$hi # $a is $lo
  56. shl \$63,$lo
  57. and $b,$i0
  58. shr \$1,$hi
  59. mov $i0,$t1
  60. shl \$62,$i0
  61. and $b,$i1
  62. shr \$2,$t1
  63. xor $i0,$lo
  64. mov $i1,$t0
  65. shl \$61,$i1
  66. xor $t1,$hi
  67. shr \$3,$t0
  68. xor $i1,$lo
  69. xor $t0,$hi
  70. mov $a1,$a12
  71. movq \$0,0(%rsp) # tab[0]=0
  72. xor $a2,$a12 # a1^a2
  73. mov $a1,8(%rsp) # tab[1]=a1
  74. mov $a4,$a48
  75. mov $a2,16(%rsp) # tab[2]=a2
  76. xor $a8,$a48 # a4^a8
  77. mov $a12,24(%rsp) # tab[3]=a1^a2
  78. xor $a4,$a1
  79. mov $a4,32(%rsp) # tab[4]=a4
  80. xor $a4,$a2
  81. mov $a1,40(%rsp) # tab[5]=a1^a4
  82. xor $a4,$a12
  83. mov $a2,48(%rsp) # tab[6]=a2^a4
  84. xor $a48,$a1 # a1^a4^a4^a8=a1^a8
  85. mov $a12,56(%rsp) # tab[7]=a1^a2^a4
  86. xor $a48,$a2 # a2^a4^a4^a8=a1^a8
  87. mov $a8,64(%rsp) # tab[8]=a8
  88. xor $a48,$a12 # a1^a2^a4^a4^a8=a1^a2^a8
  89. mov $a1,72(%rsp) # tab[9]=a1^a8
  90. xor $a4,$a1 # a1^a8^a4
  91. mov $a2,80(%rsp) # tab[10]=a2^a8
  92. xor $a4,$a2 # a2^a8^a4
  93. mov $a12,88(%rsp) # tab[11]=a1^a2^a8
  94. xor $a4,$a12 # a1^a2^a8^a4
  95. mov $a48,96(%rsp) # tab[12]=a4^a8
  96. mov $mask,$i0
  97. mov $a1,104(%rsp) # tab[13]=a1^a4^a8
  98. and $b,$i0
  99. mov $a2,112(%rsp) # tab[14]=a2^a4^a8
  100. shr \$4,$b
  101. mov $a12,120(%rsp) # tab[15]=a1^a2^a4^a8
  102. mov $mask,$i1
  103. and $b,$i1
  104. shr \$4,$b
  105. movq (%rsp,$i0,8),$R # half of calculations is done in SSE2
  106. mov $mask,$i0
  107. and $b,$i0
  108. shr \$4,$b
  109. ___
  110. for ($n=1;$n<8;$n++) {
  111. $code.=<<___;
  112. mov (%rsp,$i1,8),$t1
  113. mov $mask,$i1
  114. mov $t1,$t0
  115. shl \$`8*$n-4`,$t1
  116. and $b,$i1
  117. movq (%rsp,$i0,8),$Tx
  118. shr \$`64-(8*$n-4)`,$t0
  119. xor $t1,$lo
  120. pslldq \$$n,$Tx
  121. mov $mask,$i0
  122. shr \$4,$b
  123. xor $t0,$hi
  124. and $b,$i0
  125. shr \$4,$b
  126. pxor $Tx,$R
  127. ___
  128. }
  129. $code.=<<___;
  130. mov (%rsp,$i1,8),$t1
  131. mov $t1,$t0
  132. shl \$`8*$n-4`,$t1
  133. movq $R,$i0
  134. shr \$`64-(8*$n-4)`,$t0
  135. xor $t1,$lo
  136. psrldq \$8,$R
  137. xor $t0,$hi
  138. movq $R,$i1
  139. xor $i0,$lo
  140. xor $i1,$hi
  141. add \$128+8,%rsp
  142. ret
  143. .Lend_mul_1x1:
  144. .size _mul_1x1,.-_mul_1x1
  145. ___
  146. ($rp,$a1,$a0,$b1,$b0) = $win64? ("%rcx","%rdx","%r8", "%r9","%r10") : # Win64 order
  147. ("%rdi","%rsi","%rdx","%rcx","%r8"); # Unix order
  148. $code.=<<___;
  149. .extern OPENSSL_ia32cap_P
  150. .globl bn_GF2m_mul_2x2
  151. .type bn_GF2m_mul_2x2,\@abi-omnipotent
  152. .align 16
  153. bn_GF2m_mul_2x2:
  154. mov OPENSSL_ia32cap_P(%rip),%rax
  155. bt \$33,%rax
  156. jnc .Lvanilla_mul_2x2
  157. movq $a1,%xmm0
  158. movq $b1,%xmm1
  159. movq $a0,%xmm2
  160. ___
  161. $code.=<<___ if ($win64);
  162. movq 40(%rsp),%xmm3
  163. ___
  164. $code.=<<___ if (!$win64);
  165. movq $b0,%xmm3
  166. ___
  167. $code.=<<___;
  168. movdqa %xmm0,%xmm4
  169. movdqa %xmm1,%xmm5
  170. pclmulqdq \$0,%xmm1,%xmm0 # a1·b1
  171. pxor %xmm2,%xmm4
  172. pxor %xmm3,%xmm5
  173. pclmulqdq \$0,%xmm3,%xmm2 # a0·b0
  174. pclmulqdq \$0,%xmm5,%xmm4 # (a0+a1)·(b0+b1)
  175. xorps %xmm0,%xmm4
  176. xorps %xmm2,%xmm4 # (a0+a1)·(b0+b1)-a0·b0-a1·b1
  177. movdqa %xmm4,%xmm5
  178. pslldq \$8,%xmm4
  179. psrldq \$8,%xmm5
  180. pxor %xmm4,%xmm2
  181. pxor %xmm5,%xmm0
  182. movdqu %xmm2,0($rp)
  183. movdqu %xmm0,16($rp)
  184. ret
  185. .align 16
  186. .Lvanilla_mul_2x2:
  187. lea -8*17(%rsp),%rsp
  188. ___
  189. $code.=<<___ if ($win64);
  190. mov `8*17+40`(%rsp),$b0
  191. mov %rdi,8*15(%rsp)
  192. mov %rsi,8*16(%rsp)
  193. ___
  194. $code.=<<___;
  195. mov %r14,8*10(%rsp)
  196. mov %r13,8*11(%rsp)
  197. mov %r12,8*12(%rsp)
  198. mov %rbp,8*13(%rsp)
  199. mov %rbx,8*14(%rsp)
  200. .Lbody_mul_2x2:
  201. mov $rp,32(%rsp) # save the arguments
  202. mov $a1,40(%rsp)
  203. mov $a0,48(%rsp)
  204. mov $b1,56(%rsp)
  205. mov $b0,64(%rsp)
  206. mov \$0xf,$mask
  207. mov $a1,$a
  208. mov $b1,$b
  209. call _mul_1x1 # a1·b1
  210. mov $lo,16(%rsp)
  211. mov $hi,24(%rsp)
  212. mov 48(%rsp),$a
  213. mov 64(%rsp),$b
  214. call _mul_1x1 # a0·b0
  215. mov $lo,0(%rsp)
  216. mov $hi,8(%rsp)
  217. mov 40(%rsp),$a
  218. mov 56(%rsp),$b
  219. xor 48(%rsp),$a
  220. xor 64(%rsp),$b
  221. call _mul_1x1 # (a0+a1)·(b0+b1)
  222. ___
  223. @r=("%rbx","%rcx","%rdi","%rsi");
  224. $code.=<<___;
  225. mov 0(%rsp),@r[0]
  226. mov 8(%rsp),@r[1]
  227. mov 16(%rsp),@r[2]
  228. mov 24(%rsp),@r[3]
  229. mov 32(%rsp),%rbp
  230. xor $hi,$lo
  231. xor @r[1],$hi
  232. xor @r[0],$lo
  233. mov @r[0],0(%rbp)
  234. xor @r[2],$hi
  235. mov @r[3],24(%rbp)
  236. xor @r[3],$lo
  237. xor @r[3],$hi
  238. xor $hi,$lo
  239. mov $hi,16(%rbp)
  240. mov $lo,8(%rbp)
  241. mov 8*10(%rsp),%r14
  242. mov 8*11(%rsp),%r13
  243. mov 8*12(%rsp),%r12
  244. mov 8*13(%rsp),%rbp
  245. mov 8*14(%rsp),%rbx
  246. ___
  247. $code.=<<___ if ($win64);
  248. mov 8*15(%rsp),%rdi
  249. mov 8*16(%rsp),%rsi
  250. ___
  251. $code.=<<___;
  252. lea 8*17(%rsp),%rsp
  253. ret
  254. .Lend_mul_2x2:
  255. .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
  256. .asciz "GF(2^m) Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  257. .align 16
  258. ___
  259. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  260. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  261. if ($win64) {
  262. $rec="%rcx";
  263. $frame="%rdx";
  264. $context="%r8";
  265. $disp="%r9";
  266. $code.=<<___;
  267. .extern __imp_RtlVirtualUnwind
  268. .type se_handler,\@abi-omnipotent
  269. .align 16
  270. se_handler:
  271. push %rsi
  272. push %rdi
  273. push %rbx
  274. push %rbp
  275. push %r12
  276. push %r13
  277. push %r14
  278. push %r15
  279. pushfq
  280. sub \$64,%rsp
  281. mov 152($context),%rax # pull context->Rsp
  282. mov 248($context),%rbx # pull context->Rip
  283. lea .Lbody_mul_2x2(%rip),%r10
  284. cmp %r10,%rbx # context->Rip<"prologue" label
  285. jb .Lin_prologue
  286. mov 8*10(%rax),%r14 # mimic epilogue
  287. mov 8*11(%rax),%r13
  288. mov 8*12(%rax),%r12
  289. mov 8*13(%rax),%rbp
  290. mov 8*14(%rax),%rbx
  291. mov 8*15(%rax),%rdi
  292. mov 8*16(%rax),%rsi
  293. mov %rbx,144($context) # restore context->Rbx
  294. mov %rbp,160($context) # restore context->Rbp
  295. mov %rsi,168($context) # restore context->Rsi
  296. mov %rdi,176($context) # restore context->Rdi
  297. mov %r12,216($context) # restore context->R12
  298. mov %r13,224($context) # restore context->R13
  299. mov %r14,232($context) # restore context->R14
  300. .Lin_prologue:
  301. lea 8*17(%rax),%rax
  302. mov %rax,152($context) # restore context->Rsp
  303. mov 40($disp),%rdi # disp->ContextRecord
  304. mov $context,%rsi # context
  305. mov \$154,%ecx # sizeof(CONTEXT)
  306. .long 0xa548f3fc # cld; rep movsq
  307. mov $disp,%rsi
  308. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  309. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  310. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  311. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  312. mov 40(%rsi),%r10 # disp->ContextRecord
  313. lea 56(%rsi),%r11 # &disp->HandlerData
  314. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  315. mov %r10,32(%rsp) # arg5
  316. mov %r11,40(%rsp) # arg6
  317. mov %r12,48(%rsp) # arg7
  318. mov %rcx,56(%rsp) # arg8, (NULL)
  319. call *__imp_RtlVirtualUnwind(%rip)
  320. mov \$1,%eax # ExceptionContinueSearch
  321. add \$64,%rsp
  322. popfq
  323. pop %r15
  324. pop %r14
  325. pop %r13
  326. pop %r12
  327. pop %rbp
  328. pop %rbx
  329. pop %rdi
  330. pop %rsi
  331. ret
  332. .size se_handler,.-se_handler
  333. .section .pdata
  334. .align 4
  335. .rva _mul_1x1
  336. .rva .Lend_mul_1x1
  337. .rva .LSEH_info_1x1
  338. .rva .Lvanilla_mul_2x2
  339. .rva .Lend_mul_2x2
  340. .rva .LSEH_info_2x2
  341. .section .xdata
  342. .align 8
  343. .LSEH_info_1x1:
  344. .byte 0x01,0x07,0x02,0x00
  345. .byte 0x07,0x01,0x11,0x00 # sub rsp,128+8
  346. .LSEH_info_2x2:
  347. .byte 9,0,0,0
  348. .rva se_handler
  349. ___
  350. }
  351. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  352. print $code;
  353. close STDOUT;