x86_64-gf2m.pl 8.4 KB

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