x86_64-mont.pl 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # October 2005.
  15. #
  16. # Montgomery multiplication routine for x86_64. While it gives modest
  17. # 9% improvement of rsa4096 sign on Opteron, rsa512 sign runs more
  18. # than twice, >2x, as fast. Most common rsa1024 sign is improved by
  19. # respectful 50%. It remains to be seen if loop unrolling and
  20. # dedicated squaring routine can provide further improvement...
  21. # July 2011.
  22. #
  23. # Add dedicated squaring procedure. Performance improvement varies
  24. # from platform to platform, but in average it's ~5%/15%/25%/33%
  25. # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
  26. # August 2011.
  27. #
  28. # Unroll and modulo-schedule inner loops in such manner that they
  29. # are "fallen through" for input lengths of 8, which is critical for
  30. # 1024-bit RSA *sign*. Average performance improvement in comparison
  31. # to *initial* version of this module from 2005 is ~0%/30%/40%/45%
  32. # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
  33. # June 2013.
  34. #
  35. # Optimize reduction in squaring procedure and improve 1024+-bit RSA
  36. # sign performance by 10-16% on Intel Sandy Bridge and later
  37. # (virtually same on non-Intel processors).
  38. # August 2013.
  39. #
  40. # Add MULX/ADOX/ADCX code path.
  41. $flavour = shift;
  42. $output = shift;
  43. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  44. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  45. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  46. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  47. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  48. die "can't locate x86_64-xlate.pl";
  49. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  50. *STDOUT=*OUT;
  51. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  52. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  53. $addx = ($1>=2.23);
  54. }
  55. if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  56. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  57. $addx = ($1>=2.10);
  58. }
  59. if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  60. `ml64 2>&1` =~ /Version ([0-9]+)\./) {
  61. $addx = ($1>=12);
  62. }
  63. if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)/) {
  64. my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
  65. $addx = ($ver>=3.03);
  66. }
  67. # int bn_mul_mont(
  68. $rp="%rdi"; # BN_ULONG *rp,
  69. $ap="%rsi"; # const BN_ULONG *ap,
  70. $bp="%rdx"; # const BN_ULONG *bp,
  71. $np="%rcx"; # const BN_ULONG *np,
  72. $n0="%r8"; # const BN_ULONG *n0,
  73. $num="%r9"; # int num);
  74. $lo0="%r10";
  75. $hi0="%r11";
  76. $hi1="%r13";
  77. $i="%r14";
  78. $j="%r15";
  79. $m0="%rbx";
  80. $m1="%rbp";
  81. $code=<<___;
  82. .text
  83. .extern OPENSSL_ia32cap_P
  84. .globl bn_mul_mont
  85. .type bn_mul_mont,\@function,6
  86. .align 16
  87. bn_mul_mont:
  88. .cfi_startproc
  89. mov ${num}d,${num}d
  90. mov %rsp,%rax
  91. .cfi_def_cfa_register %rax
  92. test \$3,${num}d
  93. jnz .Lmul_enter
  94. cmp \$8,${num}d
  95. jb .Lmul_enter
  96. ___
  97. $code.=<<___ if ($addx);
  98. mov OPENSSL_ia32cap_P+8(%rip),%r11d
  99. ___
  100. $code.=<<___;
  101. cmp $ap,$bp
  102. jne .Lmul4x_enter
  103. test \$7,${num}d
  104. jz .Lsqr8x_enter
  105. jmp .Lmul4x_enter
  106. .align 16
  107. .Lmul_enter:
  108. push %rbx
  109. .cfi_push %rbx
  110. push %rbp
  111. .cfi_push %rbp
  112. push %r12
  113. .cfi_push %r12
  114. push %r13
  115. .cfi_push %r13
  116. push %r14
  117. .cfi_push %r14
  118. push %r15
  119. .cfi_push %r15
  120. neg $num
  121. mov %rsp,%r11
  122. lea -16(%rsp,$num,8),%r10 # future alloca(8*(num+2))
  123. neg $num # restore $num
  124. and \$-1024,%r10 # minimize TLB usage
  125. # An OS-agnostic version of __chkstk.
  126. #
  127. # Some OSes (Windows) insist on stack being "wired" to
  128. # physical memory in strictly sequential manner, i.e. if stack
  129. # allocation spans two pages, then reference to farmost one can
  130. # be punishable by SEGV. But page walking can do good even on
  131. # other OSes, because it guarantees that villain thread hits
  132. # the guard page before it can make damage to innocent one...
  133. sub %r10,%r11
  134. and \$-4096,%r11
  135. lea (%r10,%r11),%rsp
  136. mov (%rsp),%r11
  137. cmp %r10,%rsp
  138. ja .Lmul_page_walk
  139. jmp .Lmul_page_walk_done
  140. .align 16
  141. .Lmul_page_walk:
  142. lea -4096(%rsp),%rsp
  143. mov (%rsp),%r11
  144. cmp %r10,%rsp
  145. ja .Lmul_page_walk
  146. .Lmul_page_walk_done:
  147. mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
  148. .cfi_cfa_expression %rsp+8,$num,8,mul,plus,deref,+8
  149. .Lmul_body:
  150. mov $bp,%r12 # reassign $bp
  151. ___
  152. $bp="%r12";
  153. $code.=<<___;
  154. mov ($n0),$n0 # pull n0[0] value
  155. mov ($bp),$m0 # m0=bp[0]
  156. mov ($ap),%rax
  157. xor $i,$i # i=0
  158. xor $j,$j # j=0
  159. mov $n0,$m1
  160. mulq $m0 # ap[0]*bp[0]
  161. mov %rax,$lo0
  162. mov ($np),%rax
  163. imulq $lo0,$m1 # "tp[0]"*n0
  164. mov %rdx,$hi0
  165. mulq $m1 # np[0]*m1
  166. add %rax,$lo0 # discarded
  167. mov 8($ap),%rax
  168. adc \$0,%rdx
  169. mov %rdx,$hi1
  170. lea 1($j),$j # j++
  171. jmp .L1st_enter
  172. .align 16
  173. .L1st:
  174. add %rax,$hi1
  175. mov ($ap,$j,8),%rax
  176. adc \$0,%rdx
  177. add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
  178. mov $lo0,$hi0
  179. adc \$0,%rdx
  180. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  181. mov %rdx,$hi1
  182. .L1st_enter:
  183. mulq $m0 # ap[j]*bp[0]
  184. add %rax,$hi0
  185. mov ($np,$j,8),%rax
  186. adc \$0,%rdx
  187. lea 1($j),$j # j++
  188. mov %rdx,$lo0
  189. mulq $m1 # np[j]*m1
  190. cmp $num,$j
  191. jne .L1st
  192. add %rax,$hi1
  193. mov ($ap),%rax # ap[0]
  194. adc \$0,%rdx
  195. add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
  196. adc \$0,%rdx
  197. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  198. mov %rdx,$hi1
  199. mov $lo0,$hi0
  200. xor %rdx,%rdx
  201. add $hi0,$hi1
  202. adc \$0,%rdx
  203. mov $hi1,-8(%rsp,$num,8)
  204. mov %rdx,(%rsp,$num,8) # store upmost overflow bit
  205. lea 1($i),$i # i++
  206. jmp .Louter
  207. .align 16
  208. .Louter:
  209. mov ($bp,$i,8),$m0 # m0=bp[i]
  210. xor $j,$j # j=0
  211. mov $n0,$m1
  212. mov (%rsp),$lo0
  213. mulq $m0 # ap[0]*bp[i]
  214. add %rax,$lo0 # ap[0]*bp[i]+tp[0]
  215. mov ($np),%rax
  216. adc \$0,%rdx
  217. imulq $lo0,$m1 # tp[0]*n0
  218. mov %rdx,$hi0
  219. mulq $m1 # np[0]*m1
  220. add %rax,$lo0 # discarded
  221. mov 8($ap),%rax
  222. adc \$0,%rdx
  223. mov 8(%rsp),$lo0 # tp[1]
  224. mov %rdx,$hi1
  225. lea 1($j),$j # j++
  226. jmp .Linner_enter
  227. .align 16
  228. .Linner:
  229. add %rax,$hi1
  230. mov ($ap,$j,8),%rax
  231. adc \$0,%rdx
  232. add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
  233. mov (%rsp,$j,8),$lo0
  234. adc \$0,%rdx
  235. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  236. mov %rdx,$hi1
  237. .Linner_enter:
  238. mulq $m0 # ap[j]*bp[i]
  239. add %rax,$hi0
  240. mov ($np,$j,8),%rax
  241. adc \$0,%rdx
  242. add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
  243. mov %rdx,$hi0
  244. adc \$0,$hi0
  245. lea 1($j),$j # j++
  246. mulq $m1 # np[j]*m1
  247. cmp $num,$j
  248. jne .Linner
  249. add %rax,$hi1
  250. mov ($ap),%rax # ap[0]
  251. adc \$0,%rdx
  252. add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
  253. mov (%rsp,$j,8),$lo0
  254. adc \$0,%rdx
  255. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  256. mov %rdx,$hi1
  257. xor %rdx,%rdx
  258. add $hi0,$hi1
  259. adc \$0,%rdx
  260. add $lo0,$hi1 # pull upmost overflow bit
  261. adc \$0,%rdx
  262. mov $hi1,-8(%rsp,$num,8)
  263. mov %rdx,(%rsp,$num,8) # store upmost overflow bit
  264. lea 1($i),$i # i++
  265. cmp $num,$i
  266. jb .Louter
  267. xor $i,$i # i=0 and clear CF!
  268. mov (%rsp),%rax # tp[0]
  269. mov $num,$j # j=num
  270. .align 16
  271. .Lsub: sbb ($np,$i,8),%rax
  272. mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
  273. mov 8(%rsp,$i,8),%rax # tp[i+1]
  274. lea 1($i),$i # i++
  275. dec $j # doesn't affect CF!
  276. jnz .Lsub
  277. sbb \$0,%rax # handle upmost overflow bit
  278. mov \$-1,%rbx
  279. xor %rax,%rbx # not %rax
  280. xor $i,$i
  281. mov $num,$j # j=num
  282. .Lcopy: # conditional copy
  283. mov ($rp,$i,8),%rcx
  284. mov (%rsp,$i,8),%rdx
  285. and %rbx,%rcx
  286. and %rax,%rdx
  287. mov $num,(%rsp,$i,8) # zap temporary vector
  288. or %rcx,%rdx
  289. mov %rdx,($rp,$i,8) # rp[i]=tp[i]
  290. lea 1($i),$i
  291. sub \$1,$j
  292. jnz .Lcopy
  293. mov 8(%rsp,$num,8),%rsi # restore %rsp
  294. .cfi_def_cfa %rsi,8
  295. mov \$1,%rax
  296. mov -48(%rsi),%r15
  297. .cfi_restore %r15
  298. mov -40(%rsi),%r14
  299. .cfi_restore %r14
  300. mov -32(%rsi),%r13
  301. .cfi_restore %r13
  302. mov -24(%rsi),%r12
  303. .cfi_restore %r12
  304. mov -16(%rsi),%rbp
  305. .cfi_restore %rbp
  306. mov -8(%rsi),%rbx
  307. .cfi_restore %rbx
  308. lea (%rsi),%rsp
  309. .cfi_def_cfa_register %rsp
  310. .Lmul_epilogue:
  311. ret
  312. .cfi_endproc
  313. .size bn_mul_mont,.-bn_mul_mont
  314. ___
  315. {{{
  316. my @A=("%r10","%r11");
  317. my @N=("%r13","%rdi");
  318. $code.=<<___;
  319. .type bn_mul4x_mont,\@function,6
  320. .align 16
  321. bn_mul4x_mont:
  322. .cfi_startproc
  323. mov ${num}d,${num}d
  324. mov %rsp,%rax
  325. .cfi_def_cfa_register %rax
  326. .Lmul4x_enter:
  327. ___
  328. $code.=<<___ if ($addx);
  329. and \$0x80100,%r11d
  330. cmp \$0x80100,%r11d
  331. je .Lmulx4x_enter
  332. ___
  333. $code.=<<___;
  334. push %rbx
  335. .cfi_push %rbx
  336. push %rbp
  337. .cfi_push %rbp
  338. push %r12
  339. .cfi_push %r12
  340. push %r13
  341. .cfi_push %r13
  342. push %r14
  343. .cfi_push %r14
  344. push %r15
  345. .cfi_push %r15
  346. neg $num
  347. mov %rsp,%r11
  348. lea -32(%rsp,$num,8),%r10 # future alloca(8*(num+4))
  349. neg $num # restore
  350. and \$-1024,%r10 # minimize TLB usage
  351. sub %r10,%r11
  352. and \$-4096,%r11
  353. lea (%r10,%r11),%rsp
  354. mov (%rsp),%r11
  355. cmp %r10,%rsp
  356. ja .Lmul4x_page_walk
  357. jmp .Lmul4x_page_walk_done
  358. .Lmul4x_page_walk:
  359. lea -4096(%rsp),%rsp
  360. mov (%rsp),%r11
  361. cmp %r10,%rsp
  362. ja .Lmul4x_page_walk
  363. .Lmul4x_page_walk_done:
  364. mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
  365. .cfi_cfa_expression %rsp+8,$num,8,mul,plus,deref,+8
  366. .Lmul4x_body:
  367. mov $rp,16(%rsp,$num,8) # tp[num+2]=$rp
  368. mov %rdx,%r12 # reassign $bp
  369. ___
  370. $bp="%r12";
  371. $code.=<<___;
  372. mov ($n0),$n0 # pull n0[0] value
  373. mov ($bp),$m0 # m0=bp[0]
  374. mov ($ap),%rax
  375. xor $i,$i # i=0
  376. xor $j,$j # j=0
  377. mov $n0,$m1
  378. mulq $m0 # ap[0]*bp[0]
  379. mov %rax,$A[0]
  380. mov ($np),%rax
  381. imulq $A[0],$m1 # "tp[0]"*n0
  382. mov %rdx,$A[1]
  383. mulq $m1 # np[0]*m1
  384. add %rax,$A[0] # discarded
  385. mov 8($ap),%rax
  386. adc \$0,%rdx
  387. mov %rdx,$N[1]
  388. mulq $m0
  389. add %rax,$A[1]
  390. mov 8($np),%rax
  391. adc \$0,%rdx
  392. mov %rdx,$A[0]
  393. mulq $m1
  394. add %rax,$N[1]
  395. mov 16($ap),%rax
  396. adc \$0,%rdx
  397. add $A[1],$N[1]
  398. lea 4($j),$j # j++
  399. adc \$0,%rdx
  400. mov $N[1],(%rsp)
  401. mov %rdx,$N[0]
  402. jmp .L1st4x
  403. .align 16
  404. .L1st4x:
  405. mulq $m0 # ap[j]*bp[0]
  406. add %rax,$A[0]
  407. mov -16($np,$j,8),%rax
  408. adc \$0,%rdx
  409. mov %rdx,$A[1]
  410. mulq $m1 # np[j]*m1
  411. add %rax,$N[0]
  412. mov -8($ap,$j,8),%rax
  413. adc \$0,%rdx
  414. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  415. adc \$0,%rdx
  416. mov $N[0],-24(%rsp,$j,8) # tp[j-1]
  417. mov %rdx,$N[1]
  418. mulq $m0 # ap[j]*bp[0]
  419. add %rax,$A[1]
  420. mov -8($np,$j,8),%rax
  421. adc \$0,%rdx
  422. mov %rdx,$A[0]
  423. mulq $m1 # np[j]*m1
  424. add %rax,$N[1]
  425. mov ($ap,$j,8),%rax
  426. adc \$0,%rdx
  427. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  428. adc \$0,%rdx
  429. mov $N[1],-16(%rsp,$j,8) # tp[j-1]
  430. mov %rdx,$N[0]
  431. mulq $m0 # ap[j]*bp[0]
  432. add %rax,$A[0]
  433. mov ($np,$j,8),%rax
  434. adc \$0,%rdx
  435. mov %rdx,$A[1]
  436. mulq $m1 # np[j]*m1
  437. add %rax,$N[0]
  438. mov 8($ap,$j,8),%rax
  439. adc \$0,%rdx
  440. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  441. adc \$0,%rdx
  442. mov $N[0],-8(%rsp,$j,8) # tp[j-1]
  443. mov %rdx,$N[1]
  444. mulq $m0 # ap[j]*bp[0]
  445. add %rax,$A[1]
  446. mov 8($np,$j,8),%rax
  447. adc \$0,%rdx
  448. lea 4($j),$j # j++
  449. mov %rdx,$A[0]
  450. mulq $m1 # np[j]*m1
  451. add %rax,$N[1]
  452. mov -16($ap,$j,8),%rax
  453. adc \$0,%rdx
  454. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  455. adc \$0,%rdx
  456. mov $N[1],-32(%rsp,$j,8) # tp[j-1]
  457. mov %rdx,$N[0]
  458. cmp $num,$j
  459. jb .L1st4x
  460. mulq $m0 # ap[j]*bp[0]
  461. add %rax,$A[0]
  462. mov -16($np,$j,8),%rax
  463. adc \$0,%rdx
  464. mov %rdx,$A[1]
  465. mulq $m1 # np[j]*m1
  466. add %rax,$N[0]
  467. mov -8($ap,$j,8),%rax
  468. adc \$0,%rdx
  469. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  470. adc \$0,%rdx
  471. mov $N[0],-24(%rsp,$j,8) # tp[j-1]
  472. mov %rdx,$N[1]
  473. mulq $m0 # ap[j]*bp[0]
  474. add %rax,$A[1]
  475. mov -8($np,$j,8),%rax
  476. adc \$0,%rdx
  477. mov %rdx,$A[0]
  478. mulq $m1 # np[j]*m1
  479. add %rax,$N[1]
  480. mov ($ap),%rax # ap[0]
  481. adc \$0,%rdx
  482. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  483. adc \$0,%rdx
  484. mov $N[1],-16(%rsp,$j,8) # tp[j-1]
  485. mov %rdx,$N[0]
  486. xor $N[1],$N[1]
  487. add $A[0],$N[0]
  488. adc \$0,$N[1]
  489. mov $N[0],-8(%rsp,$j,8)
  490. mov $N[1],(%rsp,$j,8) # store upmost overflow bit
  491. lea 1($i),$i # i++
  492. .align 4
  493. .Louter4x:
  494. mov ($bp,$i,8),$m0 # m0=bp[i]
  495. xor $j,$j # j=0
  496. mov (%rsp),$A[0]
  497. mov $n0,$m1
  498. mulq $m0 # ap[0]*bp[i]
  499. add %rax,$A[0] # ap[0]*bp[i]+tp[0]
  500. mov ($np),%rax
  501. adc \$0,%rdx
  502. imulq $A[0],$m1 # tp[0]*n0
  503. mov %rdx,$A[1]
  504. mulq $m1 # np[0]*m1
  505. add %rax,$A[0] # "$N[0]", discarded
  506. mov 8($ap),%rax
  507. adc \$0,%rdx
  508. mov %rdx,$N[1]
  509. mulq $m0 # ap[j]*bp[i]
  510. add %rax,$A[1]
  511. mov 8($np),%rax
  512. adc \$0,%rdx
  513. add 8(%rsp),$A[1] # +tp[1]
  514. adc \$0,%rdx
  515. mov %rdx,$A[0]
  516. mulq $m1 # np[j]*m1
  517. add %rax,$N[1]
  518. mov 16($ap),%rax
  519. adc \$0,%rdx
  520. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
  521. lea 4($j),$j # j+=2
  522. adc \$0,%rdx
  523. mov $N[1],(%rsp) # tp[j-1]
  524. mov %rdx,$N[0]
  525. jmp .Linner4x
  526. .align 16
  527. .Linner4x:
  528. mulq $m0 # ap[j]*bp[i]
  529. add %rax,$A[0]
  530. mov -16($np,$j,8),%rax
  531. adc \$0,%rdx
  532. add -16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
  533. adc \$0,%rdx
  534. mov %rdx,$A[1]
  535. mulq $m1 # np[j]*m1
  536. add %rax,$N[0]
  537. mov -8($ap,$j,8),%rax
  538. adc \$0,%rdx
  539. add $A[0],$N[0]
  540. adc \$0,%rdx
  541. mov $N[0],-24(%rsp,$j,8) # tp[j-1]
  542. mov %rdx,$N[1]
  543. mulq $m0 # ap[j]*bp[i]
  544. add %rax,$A[1]
  545. mov -8($np,$j,8),%rax
  546. adc \$0,%rdx
  547. add -8(%rsp,$j,8),$A[1]
  548. adc \$0,%rdx
  549. mov %rdx,$A[0]
  550. mulq $m1 # np[j]*m1
  551. add %rax,$N[1]
  552. mov ($ap,$j,8),%rax
  553. adc \$0,%rdx
  554. add $A[1],$N[1]
  555. adc \$0,%rdx
  556. mov $N[1],-16(%rsp,$j,8) # tp[j-1]
  557. mov %rdx,$N[0]
  558. mulq $m0 # ap[j]*bp[i]
  559. add %rax,$A[0]
  560. mov ($np,$j,8),%rax
  561. adc \$0,%rdx
  562. add (%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
  563. adc \$0,%rdx
  564. mov %rdx,$A[1]
  565. mulq $m1 # np[j]*m1
  566. add %rax,$N[0]
  567. mov 8($ap,$j,8),%rax
  568. adc \$0,%rdx
  569. add $A[0],$N[0]
  570. adc \$0,%rdx
  571. mov $N[0],-8(%rsp,$j,8) # tp[j-1]
  572. mov %rdx,$N[1]
  573. mulq $m0 # ap[j]*bp[i]
  574. add %rax,$A[1]
  575. mov 8($np,$j,8),%rax
  576. adc \$0,%rdx
  577. add 8(%rsp,$j,8),$A[1]
  578. adc \$0,%rdx
  579. lea 4($j),$j # j++
  580. mov %rdx,$A[0]
  581. mulq $m1 # np[j]*m1
  582. add %rax,$N[1]
  583. mov -16($ap,$j,8),%rax
  584. adc \$0,%rdx
  585. add $A[1],$N[1]
  586. adc \$0,%rdx
  587. mov $N[1],-32(%rsp,$j,8) # tp[j-1]
  588. mov %rdx,$N[0]
  589. cmp $num,$j
  590. jb .Linner4x
  591. mulq $m0 # ap[j]*bp[i]
  592. add %rax,$A[0]
  593. mov -16($np,$j,8),%rax
  594. adc \$0,%rdx
  595. add -16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
  596. adc \$0,%rdx
  597. mov %rdx,$A[1]
  598. mulq $m1 # np[j]*m1
  599. add %rax,$N[0]
  600. mov -8($ap,$j,8),%rax
  601. adc \$0,%rdx
  602. add $A[0],$N[0]
  603. adc \$0,%rdx
  604. mov $N[0],-24(%rsp,$j,8) # tp[j-1]
  605. mov %rdx,$N[1]
  606. mulq $m0 # ap[j]*bp[i]
  607. add %rax,$A[1]
  608. mov -8($np,$j,8),%rax
  609. adc \$0,%rdx
  610. add -8(%rsp,$j,8),$A[1]
  611. adc \$0,%rdx
  612. lea 1($i),$i # i++
  613. mov %rdx,$A[0]
  614. mulq $m1 # np[j]*m1
  615. add %rax,$N[1]
  616. mov ($ap),%rax # ap[0]
  617. adc \$0,%rdx
  618. add $A[1],$N[1]
  619. adc \$0,%rdx
  620. mov $N[1],-16(%rsp,$j,8) # tp[j-1]
  621. mov %rdx,$N[0]
  622. xor $N[1],$N[1]
  623. add $A[0],$N[0]
  624. adc \$0,$N[1]
  625. add (%rsp,$num,8),$N[0] # pull upmost overflow bit
  626. adc \$0,$N[1]
  627. mov $N[0],-8(%rsp,$j,8)
  628. mov $N[1],(%rsp,$j,8) # store upmost overflow bit
  629. cmp $num,$i
  630. jb .Louter4x
  631. ___
  632. {
  633. my @ri=("%rax","%rdx",$m0,$m1);
  634. $code.=<<___;
  635. mov 16(%rsp,$num,8),$rp # restore $rp
  636. lea -4($num),$j
  637. mov 0(%rsp),@ri[0] # tp[0]
  638. mov 8(%rsp),@ri[1] # tp[1]
  639. shr \$2,$j # j=num/4-1
  640. lea (%rsp),$ap # borrow ap for tp
  641. xor $i,$i # i=0 and clear CF!
  642. sub 0($np),@ri[0]
  643. mov 16($ap),@ri[2] # tp[2]
  644. mov 24($ap),@ri[3] # tp[3]
  645. sbb 8($np),@ri[1]
  646. .Lsub4x:
  647. mov @ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
  648. mov @ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
  649. sbb 16($np,$i,8),@ri[2]
  650. mov 32($ap,$i,8),@ri[0] # tp[i+1]
  651. mov 40($ap,$i,8),@ri[1]
  652. sbb 24($np,$i,8),@ri[3]
  653. mov @ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
  654. mov @ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
  655. sbb 32($np,$i,8),@ri[0]
  656. mov 48($ap,$i,8),@ri[2]
  657. mov 56($ap,$i,8),@ri[3]
  658. sbb 40($np,$i,8),@ri[1]
  659. lea 4($i),$i # i++
  660. dec $j # doesn't affect CF!
  661. jnz .Lsub4x
  662. mov @ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
  663. mov 32($ap,$i,8),@ri[0] # load overflow bit
  664. sbb 16($np,$i,8),@ri[2]
  665. mov @ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
  666. sbb 24($np,$i,8),@ri[3]
  667. mov @ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
  668. sbb \$0,@ri[0] # handle upmost overflow bit
  669. mov @ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
  670. pxor %xmm0,%xmm0
  671. movq @ri[0],%xmm4
  672. pcmpeqd %xmm5,%xmm5
  673. pshufd \$0,%xmm4,%xmm4
  674. mov $num,$j
  675. pxor %xmm4,%xmm5
  676. shr \$2,$j # j=num/4
  677. xor %eax,%eax # i=0
  678. jmp .Lcopy4x
  679. .align 16
  680. .Lcopy4x: # conditional copy
  681. movdqa (%rsp,%rax),%xmm1
  682. movdqu ($rp,%rax),%xmm2
  683. pand %xmm4,%xmm1
  684. pand %xmm5,%xmm2
  685. movdqa 16(%rsp,%rax),%xmm3
  686. movdqa %xmm0,(%rsp,%rax)
  687. por %xmm2,%xmm1
  688. movdqu 16($rp,%rax),%xmm2
  689. movdqu %xmm1,($rp,%rax)
  690. pand %xmm4,%xmm3
  691. pand %xmm5,%xmm2
  692. movdqa %xmm0,16(%rsp,%rax)
  693. por %xmm2,%xmm3
  694. movdqu %xmm3,16($rp,%rax)
  695. lea 32(%rax),%rax
  696. dec $j
  697. jnz .Lcopy4x
  698. ___
  699. }
  700. $code.=<<___;
  701. mov 8(%rsp,$num,8),%rsi # restore %rsp
  702. .cfi_def_cfa %rsi, 8
  703. mov \$1,%rax
  704. mov -48(%rsi),%r15
  705. .cfi_restore %r15
  706. mov -40(%rsi),%r14
  707. .cfi_restore %r14
  708. mov -32(%rsi),%r13
  709. .cfi_restore %r13
  710. mov -24(%rsi),%r12
  711. .cfi_restore %r12
  712. mov -16(%rsi),%rbp
  713. .cfi_restore %rbp
  714. mov -8(%rsi),%rbx
  715. .cfi_restore %rbx
  716. lea (%rsi),%rsp
  717. .cfi_def_cfa_register %rsp
  718. .Lmul4x_epilogue:
  719. ret
  720. .cfi_endproc
  721. .size bn_mul4x_mont,.-bn_mul4x_mont
  722. ___
  723. }}}
  724. {{{
  725. ######################################################################
  726. # void bn_sqr8x_mont(
  727. my $rptr="%rdi"; # const BN_ULONG *rptr,
  728. my $aptr="%rsi"; # const BN_ULONG *aptr,
  729. my $bptr="%rdx"; # not used
  730. my $nptr="%rcx"; # const BN_ULONG *nptr,
  731. my $n0 ="%r8"; # const BN_ULONG *n0);
  732. my $num ="%r9"; # int num, has to be divisible by 8
  733. my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
  734. my @A0=("%r10","%r11");
  735. my @A1=("%r12","%r13");
  736. my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
  737. $code.=<<___ if ($addx);
  738. .extern bn_sqrx8x_internal # see x86_64-mont5 module
  739. ___
  740. $code.=<<___;
  741. .extern bn_sqr8x_internal # see x86_64-mont5 module
  742. .type bn_sqr8x_mont,\@function,6
  743. .align 32
  744. bn_sqr8x_mont:
  745. .cfi_startproc
  746. mov %rsp,%rax
  747. .cfi_def_cfa_register %rax
  748. .Lsqr8x_enter:
  749. push %rbx
  750. .cfi_push %rbx
  751. push %rbp
  752. .cfi_push %rbp
  753. push %r12
  754. .cfi_push %r12
  755. push %r13
  756. .cfi_push %r13
  757. push %r14
  758. .cfi_push %r14
  759. push %r15
  760. .cfi_push %r15
  761. .Lsqr8x_prologue:
  762. mov ${num}d,%r10d
  763. shl \$3,${num}d # convert $num to bytes
  764. shl \$3+2,%r10 # 4*$num
  765. neg $num
  766. ##############################################################
  767. # ensure that stack frame doesn't alias with $aptr modulo
  768. # 4096. this is done to allow memory disambiguation logic
  769. # do its job.
  770. #
  771. lea -64(%rsp,$num,2),%r11
  772. mov %rsp,%rbp
  773. mov ($n0),$n0 # *n0
  774. sub $aptr,%r11
  775. and \$4095,%r11
  776. cmp %r11,%r10
  777. jb .Lsqr8x_sp_alt
  778. sub %r11,%rbp # align with $aptr
  779. lea -64(%rbp,$num,2),%rbp # future alloca(frame+2*$num)
  780. jmp .Lsqr8x_sp_done
  781. .align 32
  782. .Lsqr8x_sp_alt:
  783. lea 4096-64(,$num,2),%r10 # 4096-frame-2*$num
  784. lea -64(%rbp,$num,2),%rbp # future alloca(frame+2*$num)
  785. sub %r10,%r11
  786. mov \$0,%r10
  787. cmovc %r10,%r11
  788. sub %r11,%rbp
  789. .Lsqr8x_sp_done:
  790. and \$-64,%rbp
  791. mov %rsp,%r11
  792. sub %rbp,%r11
  793. and \$-4096,%r11
  794. lea (%rbp,%r11),%rsp
  795. mov (%rsp),%r10
  796. cmp %rbp,%rsp
  797. ja .Lsqr8x_page_walk
  798. jmp .Lsqr8x_page_walk_done
  799. .align 16
  800. .Lsqr8x_page_walk:
  801. lea -4096(%rsp),%rsp
  802. mov (%rsp),%r10
  803. cmp %rbp,%rsp
  804. ja .Lsqr8x_page_walk
  805. .Lsqr8x_page_walk_done:
  806. mov $num,%r10
  807. neg $num
  808. mov $n0, 32(%rsp)
  809. mov %rax, 40(%rsp) # save original %rsp
  810. .cfi_cfa_expression %rsp+40,deref,+8
  811. .Lsqr8x_body:
  812. movq $nptr, %xmm2 # save pointer to modulus
  813. pxor %xmm0,%xmm0
  814. movq $rptr,%xmm1 # save $rptr
  815. movq %r10, %xmm3 # -$num
  816. ___
  817. $code.=<<___ if ($addx);
  818. mov OPENSSL_ia32cap_P+8(%rip),%eax
  819. and \$0x80100,%eax
  820. cmp \$0x80100,%eax
  821. jne .Lsqr8x_nox
  822. call bn_sqrx8x_internal # see x86_64-mont5 module
  823. # %rax top-most carry
  824. # %rbp nptr
  825. # %rcx -8*num
  826. # %r8 end of tp[2*num]
  827. lea (%r8,%rcx),%rbx
  828. mov %rcx,$num
  829. mov %rcx,%rdx
  830. movq %xmm1,$rptr
  831. sar \$3+2,%rcx # %cf=0
  832. jmp .Lsqr8x_sub
  833. .align 32
  834. .Lsqr8x_nox:
  835. ___
  836. $code.=<<___;
  837. call bn_sqr8x_internal # see x86_64-mont5 module
  838. # %rax top-most carry
  839. # %rbp nptr
  840. # %r8 -8*num
  841. # %rdi end of tp[2*num]
  842. lea (%rdi,$num),%rbx
  843. mov $num,%rcx
  844. mov $num,%rdx
  845. movq %xmm1,$rptr
  846. sar \$3+2,%rcx # %cf=0
  847. jmp .Lsqr8x_sub
  848. .align 32
  849. .Lsqr8x_sub:
  850. mov 8*0(%rbx),%r12
  851. mov 8*1(%rbx),%r13
  852. mov 8*2(%rbx),%r14
  853. mov 8*3(%rbx),%r15
  854. lea 8*4(%rbx),%rbx
  855. sbb 8*0(%rbp),%r12
  856. sbb 8*1(%rbp),%r13
  857. sbb 8*2(%rbp),%r14
  858. sbb 8*3(%rbp),%r15
  859. lea 8*4(%rbp),%rbp
  860. mov %r12,8*0($rptr)
  861. mov %r13,8*1($rptr)
  862. mov %r14,8*2($rptr)
  863. mov %r15,8*3($rptr)
  864. lea 8*4($rptr),$rptr
  865. inc %rcx # preserves %cf
  866. jnz .Lsqr8x_sub
  867. sbb \$0,%rax # top-most carry
  868. lea (%rbx,$num),%rbx # rewind
  869. lea ($rptr,$num),$rptr # rewind
  870. movq %rax,%xmm1
  871. pxor %xmm0,%xmm0
  872. pshufd \$0,%xmm1,%xmm1
  873. mov 40(%rsp),%rsi # restore %rsp
  874. .cfi_def_cfa %rsi,8
  875. jmp .Lsqr8x_cond_copy
  876. .align 32
  877. .Lsqr8x_cond_copy:
  878. movdqa 16*0(%rbx),%xmm2
  879. movdqa 16*1(%rbx),%xmm3
  880. lea 16*2(%rbx),%rbx
  881. movdqu 16*0($rptr),%xmm4
  882. movdqu 16*1($rptr),%xmm5
  883. lea 16*2($rptr),$rptr
  884. movdqa %xmm0,-16*2(%rbx) # zero tp
  885. movdqa %xmm0,-16*1(%rbx)
  886. movdqa %xmm0,-16*2(%rbx,%rdx)
  887. movdqa %xmm0,-16*1(%rbx,%rdx)
  888. pcmpeqd %xmm1,%xmm0
  889. pand %xmm1,%xmm2
  890. pand %xmm1,%xmm3
  891. pand %xmm0,%xmm4
  892. pand %xmm0,%xmm5
  893. pxor %xmm0,%xmm0
  894. por %xmm2,%xmm4
  895. por %xmm3,%xmm5
  896. movdqu %xmm4,-16*2($rptr)
  897. movdqu %xmm5,-16*1($rptr)
  898. add \$32,$num
  899. jnz .Lsqr8x_cond_copy
  900. mov \$1,%rax
  901. mov -48(%rsi),%r15
  902. .cfi_restore %r15
  903. mov -40(%rsi),%r14
  904. .cfi_restore %r14
  905. mov -32(%rsi),%r13
  906. .cfi_restore %r13
  907. mov -24(%rsi),%r12
  908. .cfi_restore %r12
  909. mov -16(%rsi),%rbp
  910. .cfi_restore %rbp
  911. mov -8(%rsi),%rbx
  912. .cfi_restore %rbx
  913. lea (%rsi),%rsp
  914. .cfi_def_cfa_register %rsp
  915. .Lsqr8x_epilogue:
  916. ret
  917. .cfi_endproc
  918. .size bn_sqr8x_mont,.-bn_sqr8x_mont
  919. ___
  920. }}}
  921. if ($addx) {{{
  922. my $bp="%rdx"; # original value
  923. $code.=<<___;
  924. .type bn_mulx4x_mont,\@function,6
  925. .align 32
  926. bn_mulx4x_mont:
  927. .cfi_startproc
  928. mov %rsp,%rax
  929. .cfi_def_cfa_register %rax
  930. .Lmulx4x_enter:
  931. push %rbx
  932. .cfi_push %rbx
  933. push %rbp
  934. .cfi_push %rbp
  935. push %r12
  936. .cfi_push %r12
  937. push %r13
  938. .cfi_push %r13
  939. push %r14
  940. .cfi_push %r14
  941. push %r15
  942. .cfi_push %r15
  943. .Lmulx4x_prologue:
  944. shl \$3,${num}d # convert $num to bytes
  945. xor %r10,%r10
  946. sub $num,%r10 # -$num
  947. mov ($n0),$n0 # *n0
  948. lea -72(%rsp,%r10),%rbp # future alloca(frame+$num+8)
  949. and \$-128,%rbp
  950. mov %rsp,%r11
  951. sub %rbp,%r11
  952. and \$-4096,%r11
  953. lea (%rbp,%r11),%rsp
  954. mov (%rsp),%r10
  955. cmp %rbp,%rsp
  956. ja .Lmulx4x_page_walk
  957. jmp .Lmulx4x_page_walk_done
  958. .align 16
  959. .Lmulx4x_page_walk:
  960. lea -4096(%rsp),%rsp
  961. mov (%rsp),%r10
  962. cmp %rbp,%rsp
  963. ja .Lmulx4x_page_walk
  964. .Lmulx4x_page_walk_done:
  965. lea ($bp,$num),%r10
  966. ##############################################################
  967. # Stack layout
  968. # +0 num
  969. # +8 off-loaded &b[i]
  970. # +16 end of b[num]
  971. # +24 saved n0
  972. # +32 saved rp
  973. # +40 saved %rsp
  974. # +48 inner counter
  975. # +56
  976. # +64 tmp[num+1]
  977. #
  978. mov $num,0(%rsp) # save $num
  979. shr \$5,$num
  980. mov %r10,16(%rsp) # end of b[num]
  981. sub \$1,$num
  982. mov $n0, 24(%rsp) # save *n0
  983. mov $rp, 32(%rsp) # save $rp
  984. mov %rax,40(%rsp) # save original %rsp
  985. .cfi_cfa_expression %rsp+40,deref,+8
  986. mov $num,48(%rsp) # inner counter
  987. jmp .Lmulx4x_body
  988. .align 32
  989. .Lmulx4x_body:
  990. ___
  991. my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
  992. ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
  993. my $rptr=$bptr;
  994. $code.=<<___;
  995. lea 8($bp),$bptr
  996. mov ($bp),%rdx # b[0], $bp==%rdx actually
  997. lea 64+32(%rsp),$tptr
  998. mov %rdx,$bi
  999. mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
  1000. mulx 1*8($aptr),%r11,%r14 # a[1]*b[0]
  1001. add %rax,%r11
  1002. mov $bptr,8(%rsp) # off-load &b[i]
  1003. mulx 2*8($aptr),%r12,%r13 # ...
  1004. adc %r14,%r12
  1005. adc \$0,%r13
  1006. mov $mi,$bptr # borrow $bptr
  1007. imulq 24(%rsp),$mi # "t[0]"*n0
  1008. xor $zero,$zero # cf=0, of=0
  1009. mulx 3*8($aptr),%rax,%r14
  1010. mov $mi,%rdx
  1011. lea 4*8($aptr),$aptr
  1012. adcx %rax,%r13
  1013. adcx $zero,%r14 # cf=0
  1014. mulx 0*8($nptr),%rax,%r10
  1015. adcx %rax,$bptr # discarded
  1016. adox %r11,%r10
  1017. mulx 1*8($nptr),%rax,%r11
  1018. adcx %rax,%r10
  1019. adox %r12,%r11
  1020. .byte 0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00 # mulx 2*8($nptr),%rax,%r12
  1021. mov 48(%rsp),$bptr # counter value
  1022. mov %r10,-4*8($tptr)
  1023. adcx %rax,%r11
  1024. adox %r13,%r12
  1025. mulx 3*8($nptr),%rax,%r15
  1026. mov $bi,%rdx
  1027. mov %r11,-3*8($tptr)
  1028. adcx %rax,%r12
  1029. adox $zero,%r15 # of=0
  1030. lea 4*8($nptr),$nptr
  1031. mov %r12,-2*8($tptr)
  1032. jmp .Lmulx4x_1st
  1033. .align 32
  1034. .Lmulx4x_1st:
  1035. adcx $zero,%r15 # cf=0, modulo-scheduled
  1036. mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
  1037. adcx %r14,%r10
  1038. mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
  1039. adcx %rax,%r11
  1040. mulx 2*8($aptr),%r12,%rax # ...
  1041. adcx %r14,%r12
  1042. mulx 3*8($aptr),%r13,%r14
  1043. .byte 0x67,0x67
  1044. mov $mi,%rdx
  1045. adcx %rax,%r13
  1046. adcx $zero,%r14 # cf=0
  1047. lea 4*8($aptr),$aptr
  1048. lea 4*8($tptr),$tptr
  1049. adox %r15,%r10
  1050. mulx 0*8($nptr),%rax,%r15
  1051. adcx %rax,%r10
  1052. adox %r15,%r11
  1053. mulx 1*8($nptr),%rax,%r15
  1054. adcx %rax,%r11
  1055. adox %r15,%r12
  1056. mulx 2*8($nptr),%rax,%r15
  1057. mov %r10,-5*8($tptr)
  1058. adcx %rax,%r12
  1059. mov %r11,-4*8($tptr)
  1060. adox %r15,%r13
  1061. mulx 3*8($nptr),%rax,%r15
  1062. mov $bi,%rdx
  1063. mov %r12,-3*8($tptr)
  1064. adcx %rax,%r13
  1065. adox $zero,%r15
  1066. lea 4*8($nptr),$nptr
  1067. mov %r13,-2*8($tptr)
  1068. dec $bptr # of=0, pass cf
  1069. jnz .Lmulx4x_1st
  1070. mov 0(%rsp),$num # load num
  1071. mov 8(%rsp),$bptr # re-load &b[i]
  1072. adc $zero,%r15 # modulo-scheduled
  1073. add %r15,%r14
  1074. sbb %r15,%r15 # top-most carry
  1075. mov %r14,-1*8($tptr)
  1076. jmp .Lmulx4x_outer
  1077. .align 32
  1078. .Lmulx4x_outer:
  1079. mov ($bptr),%rdx # b[i]
  1080. lea 8($bptr),$bptr # b++
  1081. sub $num,$aptr # rewind $aptr
  1082. mov %r15,($tptr) # save top-most carry
  1083. lea 64+4*8(%rsp),$tptr
  1084. sub $num,$nptr # rewind $nptr
  1085. mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
  1086. xor %ebp,%ebp # xor $zero,$zero # cf=0, of=0
  1087. mov %rdx,$bi
  1088. mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
  1089. adox -4*8($tptr),$mi
  1090. adcx %r14,%r11
  1091. mulx 2*8($aptr),%r15,%r13 # ...
  1092. adox -3*8($tptr),%r11
  1093. adcx %r15,%r12
  1094. adox -2*8($tptr),%r12
  1095. adcx $zero,%r13
  1096. adox $zero,%r13
  1097. mov $bptr,8(%rsp) # off-load &b[i]
  1098. mov $mi,%r15
  1099. imulq 24(%rsp),$mi # "t[0]"*n0
  1100. xor %ebp,%ebp # xor $zero,$zero # cf=0, of=0
  1101. mulx 3*8($aptr),%rax,%r14
  1102. mov $mi,%rdx
  1103. adcx %rax,%r13
  1104. adox -1*8($tptr),%r13
  1105. adcx $zero,%r14
  1106. lea 4*8($aptr),$aptr
  1107. adox $zero,%r14
  1108. mulx 0*8($nptr),%rax,%r10
  1109. adcx %rax,%r15 # discarded
  1110. adox %r11,%r10
  1111. mulx 1*8($nptr),%rax,%r11
  1112. adcx %rax,%r10
  1113. adox %r12,%r11
  1114. mulx 2*8($nptr),%rax,%r12
  1115. mov %r10,-4*8($tptr)
  1116. adcx %rax,%r11
  1117. adox %r13,%r12
  1118. mulx 3*8($nptr),%rax,%r15
  1119. mov $bi,%rdx
  1120. mov %r11,-3*8($tptr)
  1121. lea 4*8($nptr),$nptr
  1122. adcx %rax,%r12
  1123. adox $zero,%r15 # of=0
  1124. mov 48(%rsp),$bptr # counter value
  1125. mov %r12,-2*8($tptr)
  1126. jmp .Lmulx4x_inner
  1127. .align 32
  1128. .Lmulx4x_inner:
  1129. mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
  1130. adcx $zero,%r15 # cf=0, modulo-scheduled
  1131. adox %r14,%r10
  1132. mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
  1133. adcx 0*8($tptr),%r10
  1134. adox %rax,%r11
  1135. mulx 2*8($aptr),%r12,%rax # ...
  1136. adcx 1*8($tptr),%r11
  1137. adox %r14,%r12
  1138. mulx 3*8($aptr),%r13,%r14
  1139. mov $mi,%rdx
  1140. adcx 2*8($tptr),%r12
  1141. adox %rax,%r13
  1142. adcx 3*8($tptr),%r13
  1143. adox $zero,%r14 # of=0
  1144. lea 4*8($aptr),$aptr
  1145. lea 4*8($tptr),$tptr
  1146. adcx $zero,%r14 # cf=0
  1147. adox %r15,%r10
  1148. mulx 0*8($nptr),%rax,%r15
  1149. adcx %rax,%r10
  1150. adox %r15,%r11
  1151. mulx 1*8($nptr),%rax,%r15
  1152. adcx %rax,%r11
  1153. adox %r15,%r12
  1154. mulx 2*8($nptr),%rax,%r15
  1155. mov %r10,-5*8($tptr)
  1156. adcx %rax,%r12
  1157. adox %r15,%r13
  1158. mulx 3*8($nptr),%rax,%r15
  1159. mov $bi,%rdx
  1160. mov %r11,-4*8($tptr)
  1161. mov %r12,-3*8($tptr)
  1162. adcx %rax,%r13
  1163. adox $zero,%r15
  1164. lea 4*8($nptr),$nptr
  1165. mov %r13,-2*8($tptr)
  1166. dec $bptr # of=0, pass cf
  1167. jnz .Lmulx4x_inner
  1168. mov 0(%rsp),$num # load num
  1169. mov 8(%rsp),$bptr # re-load &b[i]
  1170. adc $zero,%r15 # modulo-scheduled
  1171. sub 0*8($tptr),$zero # pull top-most carry
  1172. adc %r15,%r14
  1173. sbb %r15,%r15 # top-most carry
  1174. mov %r14,-1*8($tptr)
  1175. cmp 16(%rsp),$bptr
  1176. jne .Lmulx4x_outer
  1177. lea 64(%rsp),$tptr
  1178. sub $num,$nptr # rewind $nptr
  1179. neg %r15
  1180. mov $num,%rdx
  1181. shr \$3+2,$num # %cf=0
  1182. mov 32(%rsp),$rptr # restore rp
  1183. jmp .Lmulx4x_sub
  1184. .align 32
  1185. .Lmulx4x_sub:
  1186. mov 8*0($tptr),%r11
  1187. mov 8*1($tptr),%r12
  1188. mov 8*2($tptr),%r13
  1189. mov 8*3($tptr),%r14
  1190. lea 8*4($tptr),$tptr
  1191. sbb 8*0($nptr),%r11
  1192. sbb 8*1($nptr),%r12
  1193. sbb 8*2($nptr),%r13
  1194. sbb 8*3($nptr),%r14
  1195. lea 8*4($nptr),$nptr
  1196. mov %r11,8*0($rptr)
  1197. mov %r12,8*1($rptr)
  1198. mov %r13,8*2($rptr)
  1199. mov %r14,8*3($rptr)
  1200. lea 8*4($rptr),$rptr
  1201. dec $num # preserves %cf
  1202. jnz .Lmulx4x_sub
  1203. sbb \$0,%r15 # top-most carry
  1204. lea 64(%rsp),$tptr
  1205. sub %rdx,$rptr # rewind
  1206. movq %r15,%xmm1
  1207. pxor %xmm0,%xmm0
  1208. pshufd \$0,%xmm1,%xmm1
  1209. mov 40(%rsp),%rsi # restore %rsp
  1210. .cfi_def_cfa %rsi,8
  1211. jmp .Lmulx4x_cond_copy
  1212. .align 32
  1213. .Lmulx4x_cond_copy:
  1214. movdqa 16*0($tptr),%xmm2
  1215. movdqa 16*1($tptr),%xmm3
  1216. lea 16*2($tptr),$tptr
  1217. movdqu 16*0($rptr),%xmm4
  1218. movdqu 16*1($rptr),%xmm5
  1219. lea 16*2($rptr),$rptr
  1220. movdqa %xmm0,-16*2($tptr) # zero tp
  1221. movdqa %xmm0,-16*1($tptr)
  1222. pcmpeqd %xmm1,%xmm0
  1223. pand %xmm1,%xmm2
  1224. pand %xmm1,%xmm3
  1225. pand %xmm0,%xmm4
  1226. pand %xmm0,%xmm5
  1227. pxor %xmm0,%xmm0
  1228. por %xmm2,%xmm4
  1229. por %xmm3,%xmm5
  1230. movdqu %xmm4,-16*2($rptr)
  1231. movdqu %xmm5,-16*1($rptr)
  1232. sub \$32,%rdx
  1233. jnz .Lmulx4x_cond_copy
  1234. mov %rdx,($tptr)
  1235. mov \$1,%rax
  1236. mov -48(%rsi),%r15
  1237. .cfi_restore %r15
  1238. mov -40(%rsi),%r14
  1239. .cfi_restore %r14
  1240. mov -32(%rsi),%r13
  1241. .cfi_restore %r13
  1242. mov -24(%rsi),%r12
  1243. .cfi_restore %r12
  1244. mov -16(%rsi),%rbp
  1245. .cfi_restore %rbp
  1246. mov -8(%rsi),%rbx
  1247. .cfi_restore %rbx
  1248. lea (%rsi),%rsp
  1249. .cfi_def_cfa_register %rsp
  1250. .Lmulx4x_epilogue:
  1251. ret
  1252. .cfi_endproc
  1253. .size bn_mulx4x_mont,.-bn_mulx4x_mont
  1254. ___
  1255. }}}
  1256. $code.=<<___;
  1257. .asciz "Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  1258. .align 16
  1259. ___
  1260. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1261. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1262. if ($win64) {
  1263. $rec="%rcx";
  1264. $frame="%rdx";
  1265. $context="%r8";
  1266. $disp="%r9";
  1267. $code.=<<___;
  1268. .extern __imp_RtlVirtualUnwind
  1269. .type mul_handler,\@abi-omnipotent
  1270. .align 16
  1271. mul_handler:
  1272. push %rsi
  1273. push %rdi
  1274. push %rbx
  1275. push %rbp
  1276. push %r12
  1277. push %r13
  1278. push %r14
  1279. push %r15
  1280. pushfq
  1281. sub \$64,%rsp
  1282. mov 120($context),%rax # pull context->Rax
  1283. mov 248($context),%rbx # pull context->Rip
  1284. mov 8($disp),%rsi # disp->ImageBase
  1285. mov 56($disp),%r11 # disp->HandlerData
  1286. mov 0(%r11),%r10d # HandlerData[0]
  1287. lea (%rsi,%r10),%r10 # end of prologue label
  1288. cmp %r10,%rbx # context->Rip<end of prologue label
  1289. jb .Lcommon_seh_tail
  1290. mov 152($context),%rax # pull context->Rsp
  1291. mov 4(%r11),%r10d # HandlerData[1]
  1292. lea (%rsi,%r10),%r10 # epilogue label
  1293. cmp %r10,%rbx # context->Rip>=epilogue label
  1294. jae .Lcommon_seh_tail
  1295. mov 192($context),%r10 # pull $num
  1296. mov 8(%rax,%r10,8),%rax # pull saved stack pointer
  1297. jmp .Lcommon_pop_regs
  1298. .size mul_handler,.-mul_handler
  1299. .type sqr_handler,\@abi-omnipotent
  1300. .align 16
  1301. sqr_handler:
  1302. push %rsi
  1303. push %rdi
  1304. push %rbx
  1305. push %rbp
  1306. push %r12
  1307. push %r13
  1308. push %r14
  1309. push %r15
  1310. pushfq
  1311. sub \$64,%rsp
  1312. mov 120($context),%rax # pull context->Rax
  1313. mov 248($context),%rbx # pull context->Rip
  1314. mov 8($disp),%rsi # disp->ImageBase
  1315. mov 56($disp),%r11 # disp->HandlerData
  1316. mov 0(%r11),%r10d # HandlerData[0]
  1317. lea (%rsi,%r10),%r10 # end of prologue label
  1318. cmp %r10,%rbx # context->Rip<.Lsqr_prologue
  1319. jb .Lcommon_seh_tail
  1320. mov 4(%r11),%r10d # HandlerData[1]
  1321. lea (%rsi,%r10),%r10 # body label
  1322. cmp %r10,%rbx # context->Rip<.Lsqr_body
  1323. jb .Lcommon_pop_regs
  1324. mov 152($context),%rax # pull context->Rsp
  1325. mov 8(%r11),%r10d # HandlerData[2]
  1326. lea (%rsi,%r10),%r10 # epilogue label
  1327. cmp %r10,%rbx # context->Rip>=.Lsqr_epilogue
  1328. jae .Lcommon_seh_tail
  1329. mov 40(%rax),%rax # pull saved stack pointer
  1330. .Lcommon_pop_regs:
  1331. mov -8(%rax),%rbx
  1332. mov -16(%rax),%rbp
  1333. mov -24(%rax),%r12
  1334. mov -32(%rax),%r13
  1335. mov -40(%rax),%r14
  1336. mov -48(%rax),%r15
  1337. mov %rbx,144($context) # restore context->Rbx
  1338. mov %rbp,160($context) # restore context->Rbp
  1339. mov %r12,216($context) # restore context->R12
  1340. mov %r13,224($context) # restore context->R13
  1341. mov %r14,232($context) # restore context->R14
  1342. mov %r15,240($context) # restore context->R15
  1343. .Lcommon_seh_tail:
  1344. mov 8(%rax),%rdi
  1345. mov 16(%rax),%rsi
  1346. mov %rax,152($context) # restore context->Rsp
  1347. mov %rsi,168($context) # restore context->Rsi
  1348. mov %rdi,176($context) # restore context->Rdi
  1349. mov 40($disp),%rdi # disp->ContextRecord
  1350. mov $context,%rsi # context
  1351. mov \$154,%ecx # sizeof(CONTEXT)
  1352. .long 0xa548f3fc # cld; rep movsq
  1353. mov $disp,%rsi
  1354. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1355. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1356. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1357. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1358. mov 40(%rsi),%r10 # disp->ContextRecord
  1359. lea 56(%rsi),%r11 # &disp->HandlerData
  1360. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1361. mov %r10,32(%rsp) # arg5
  1362. mov %r11,40(%rsp) # arg6
  1363. mov %r12,48(%rsp) # arg7
  1364. mov %rcx,56(%rsp) # arg8, (NULL)
  1365. call *__imp_RtlVirtualUnwind(%rip)
  1366. mov \$1,%eax # ExceptionContinueSearch
  1367. add \$64,%rsp
  1368. popfq
  1369. pop %r15
  1370. pop %r14
  1371. pop %r13
  1372. pop %r12
  1373. pop %rbp
  1374. pop %rbx
  1375. pop %rdi
  1376. pop %rsi
  1377. ret
  1378. .size sqr_handler,.-sqr_handler
  1379. .section .pdata
  1380. .align 4
  1381. .rva .LSEH_begin_bn_mul_mont
  1382. .rva .LSEH_end_bn_mul_mont
  1383. .rva .LSEH_info_bn_mul_mont
  1384. .rva .LSEH_begin_bn_mul4x_mont
  1385. .rva .LSEH_end_bn_mul4x_mont
  1386. .rva .LSEH_info_bn_mul4x_mont
  1387. .rva .LSEH_begin_bn_sqr8x_mont
  1388. .rva .LSEH_end_bn_sqr8x_mont
  1389. .rva .LSEH_info_bn_sqr8x_mont
  1390. ___
  1391. $code.=<<___ if ($addx);
  1392. .rva .LSEH_begin_bn_mulx4x_mont
  1393. .rva .LSEH_end_bn_mulx4x_mont
  1394. .rva .LSEH_info_bn_mulx4x_mont
  1395. ___
  1396. $code.=<<___;
  1397. .section .xdata
  1398. .align 8
  1399. .LSEH_info_bn_mul_mont:
  1400. .byte 9,0,0,0
  1401. .rva mul_handler
  1402. .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
  1403. .LSEH_info_bn_mul4x_mont:
  1404. .byte 9,0,0,0
  1405. .rva mul_handler
  1406. .rva .Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
  1407. .LSEH_info_bn_sqr8x_mont:
  1408. .byte 9,0,0,0
  1409. .rva sqr_handler
  1410. .rva .Lsqr8x_prologue,.Lsqr8x_body,.Lsqr8x_epilogue # HandlerData[]
  1411. .align 8
  1412. ___
  1413. $code.=<<___ if ($addx);
  1414. .LSEH_info_bn_mulx4x_mont:
  1415. .byte 9,0,0,0
  1416. .rva sqr_handler
  1417. .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
  1418. .align 8
  1419. ___
  1420. }
  1421. print $code;
  1422. close STDOUT or die "error closing STDOUT: $!";