x86_64-mont5.pl 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. # August 2011.
  15. #
  16. # Companion to x86_64-mont.pl that optimizes cache-timing attack
  17. # countermeasures. The subroutines are produced by replacing bp[i]
  18. # references in their x86_64-mont.pl counterparts with cache-neutral
  19. # references to powers table computed in BN_mod_exp_mont_consttime.
  20. # In addition subroutine that scatters elements of the powers table
  21. # is implemented, so that scatter-/gathering can be tuned without
  22. # bn_exp.c modifications.
  23. # August 2013.
  24. #
  25. # Add MULX/AD*X code paths and additional interfaces to optimize for
  26. # branch prediction unit. For input lengths that are multiples of 8
  27. # the np argument is not just modulus value, but one interleaved
  28. # with 0. This is to optimize post-condition...
  29. # $output is the last argument if it looks like a file (it has an extension)
  30. # $flavour is the first argument if it doesn't look like a file
  31. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  32. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  33. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  34. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  35. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  36. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  37. die "can't locate x86_64-xlate.pl";
  38. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  39. or die "can't call $xlate: $!";
  40. *STDOUT=*OUT;
  41. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  42. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  43. $addx = ($1>=2.23);
  44. }
  45. if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  46. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  47. $addx = ($1>=2.10);
  48. }
  49. if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  50. `ml64 2>&1` =~ /Version ([0-9]+)\./) {
  51. $addx = ($1>=12);
  52. }
  53. if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)/) {
  54. my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
  55. $addx = ($ver>=3.03);
  56. }
  57. # int bn_mul_mont_gather5(
  58. $rp="%rdi"; # BN_ULONG *rp,
  59. $ap="%rsi"; # const BN_ULONG *ap,
  60. $bp="%rdx"; # const BN_ULONG *bp,
  61. $np="%rcx"; # const BN_ULONG *np,
  62. $n0="%r8"; # const BN_ULONG *n0,
  63. $num="%r9"; # int num,
  64. # int idx); # 0 to 2^5-1, "index" in $bp holding
  65. # pre-computed powers of a', interlaced
  66. # in such manner that b[0] is $bp[idx],
  67. # b[1] is [2^5+idx], etc.
  68. $lo0="%r10";
  69. $hi0="%r11";
  70. $hi1="%r13";
  71. $i="%r14";
  72. $j="%r15";
  73. $m0="%rbx";
  74. $m1="%rbp";
  75. $code=<<___;
  76. .text
  77. .extern OPENSSL_ia32cap_P
  78. .globl bn_mul_mont_gather5
  79. .type bn_mul_mont_gather5,\@function,6
  80. .align 64
  81. bn_mul_mont_gather5:
  82. .cfi_startproc
  83. mov ${num}d,${num}d
  84. mov %rsp,%rax
  85. .cfi_def_cfa_register %rax
  86. test \$7,${num}d
  87. jnz .Lmul_enter
  88. ___
  89. $code.=<<___ if ($addx);
  90. mov OPENSSL_ia32cap_P+8(%rip),%r11d
  91. ___
  92. $code.=<<___;
  93. jmp .Lmul4x_enter
  94. .align 16
  95. .Lmul_enter:
  96. movd `($win64?56:8)`(%rsp),%xmm5 # load 7th argument
  97. push %rbx
  98. .cfi_push %rbx
  99. push %rbp
  100. .cfi_push %rbp
  101. push %r12
  102. .cfi_push %r12
  103. push %r13
  104. .cfi_push %r13
  105. push %r14
  106. .cfi_push %r14
  107. push %r15
  108. .cfi_push %r15
  109. neg $num
  110. mov %rsp,%r11
  111. lea -280(%rsp,$num,8),%r10 # future alloca(8*(num+2)+256+8)
  112. neg $num # restore $num
  113. and \$-1024,%r10 # minimize TLB usage
  114. # An OS-agnostic version of __chkstk.
  115. #
  116. # Some OSes (Windows) insist on stack being "wired" to
  117. # physical memory in strictly sequential manner, i.e. if stack
  118. # allocation spans two pages, then reference to farmost one can
  119. # be punishable by SEGV. But page walking can do good even on
  120. # other OSes, because it guarantees that villain thread hits
  121. # the guard page before it can make damage to innocent one...
  122. sub %r10,%r11
  123. and \$-4096,%r11
  124. lea (%r10,%r11),%rsp
  125. mov (%rsp),%r11
  126. cmp %r10,%rsp
  127. ja .Lmul_page_walk
  128. jmp .Lmul_page_walk_done
  129. .Lmul_page_walk:
  130. lea -4096(%rsp),%rsp
  131. mov (%rsp),%r11
  132. cmp %r10,%rsp
  133. ja .Lmul_page_walk
  134. .Lmul_page_walk_done:
  135. lea .Linc(%rip),%r10
  136. mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
  137. .cfi_cfa_expression %rsp+8,$num,8,mul,plus,deref,+8
  138. .Lmul_body:
  139. lea 128($bp),%r12 # reassign $bp (+size optimization)
  140. ___
  141. $bp="%r12";
  142. $STRIDE=2**5*8; # 5 is "window size"
  143. $N=$STRIDE/4; # should match cache line size
  144. $code.=<<___;
  145. movdqa 0(%r10),%xmm0 # 00000001000000010000000000000000
  146. movdqa 16(%r10),%xmm1 # 00000002000000020000000200000002
  147. lea 24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
  148. and \$-16,%r10
  149. pshufd \$0,%xmm5,%xmm5 # broadcast index
  150. movdqa %xmm1,%xmm4
  151. movdqa %xmm1,%xmm2
  152. ___
  153. ########################################################################
  154. # calculate mask by comparing 0..31 to index and save result to stack
  155. #
  156. $code.=<<___;
  157. paddd %xmm0,%xmm1
  158. pcmpeqd %xmm5,%xmm0 # compare to 1,0
  159. .byte 0x67
  160. movdqa %xmm4,%xmm3
  161. ___
  162. for($k=0;$k<$STRIDE/16-4;$k+=4) {
  163. $code.=<<___;
  164. paddd %xmm1,%xmm2
  165. pcmpeqd %xmm5,%xmm1 # compare to 3,2
  166. movdqa %xmm0,`16*($k+0)+112`(%r10)
  167. movdqa %xmm4,%xmm0
  168. paddd %xmm2,%xmm3
  169. pcmpeqd %xmm5,%xmm2 # compare to 5,4
  170. movdqa %xmm1,`16*($k+1)+112`(%r10)
  171. movdqa %xmm4,%xmm1
  172. paddd %xmm3,%xmm0
  173. pcmpeqd %xmm5,%xmm3 # compare to 7,6
  174. movdqa %xmm2,`16*($k+2)+112`(%r10)
  175. movdqa %xmm4,%xmm2
  176. paddd %xmm0,%xmm1
  177. pcmpeqd %xmm5,%xmm0
  178. movdqa %xmm3,`16*($k+3)+112`(%r10)
  179. movdqa %xmm4,%xmm3
  180. ___
  181. }
  182. $code.=<<___; # last iteration can be optimized
  183. paddd %xmm1,%xmm2
  184. pcmpeqd %xmm5,%xmm1
  185. movdqa %xmm0,`16*($k+0)+112`(%r10)
  186. paddd %xmm2,%xmm3
  187. .byte 0x67
  188. pcmpeqd %xmm5,%xmm2
  189. movdqa %xmm1,`16*($k+1)+112`(%r10)
  190. pcmpeqd %xmm5,%xmm3
  191. movdqa %xmm2,`16*($k+2)+112`(%r10)
  192. pand `16*($k+0)-128`($bp),%xmm0 # while it's still in register
  193. pand `16*($k+1)-128`($bp),%xmm1
  194. pand `16*($k+2)-128`($bp),%xmm2
  195. movdqa %xmm3,`16*($k+3)+112`(%r10)
  196. pand `16*($k+3)-128`($bp),%xmm3
  197. por %xmm2,%xmm0
  198. por %xmm3,%xmm1
  199. ___
  200. for($k=0;$k<$STRIDE/16-4;$k+=4) {
  201. $code.=<<___;
  202. movdqa `16*($k+0)-128`($bp),%xmm4
  203. movdqa `16*($k+1)-128`($bp),%xmm5
  204. movdqa `16*($k+2)-128`($bp),%xmm2
  205. pand `16*($k+0)+112`(%r10),%xmm4
  206. movdqa `16*($k+3)-128`($bp),%xmm3
  207. pand `16*($k+1)+112`(%r10),%xmm5
  208. por %xmm4,%xmm0
  209. pand `16*($k+2)+112`(%r10),%xmm2
  210. por %xmm5,%xmm1
  211. pand `16*($k+3)+112`(%r10),%xmm3
  212. por %xmm2,%xmm0
  213. por %xmm3,%xmm1
  214. ___
  215. }
  216. $code.=<<___;
  217. por %xmm1,%xmm0
  218. pshufd \$0x4e,%xmm0,%xmm1
  219. por %xmm1,%xmm0
  220. lea $STRIDE($bp),$bp
  221. movq %xmm0,$m0 # m0=bp[0]
  222. mov ($n0),$n0 # pull n0[0] value
  223. mov ($ap),%rax
  224. xor $i,$i # i=0
  225. xor $j,$j # j=0
  226. mov $n0,$m1
  227. mulq $m0 # ap[0]*bp[0]
  228. mov %rax,$lo0
  229. mov ($np),%rax
  230. imulq $lo0,$m1 # "tp[0]"*n0
  231. mov %rdx,$hi0
  232. mulq $m1 # np[0]*m1
  233. add %rax,$lo0 # discarded
  234. mov 8($ap),%rax
  235. adc \$0,%rdx
  236. mov %rdx,$hi1
  237. lea 1($j),$j # j++
  238. jmp .L1st_enter
  239. .align 16
  240. .L1st:
  241. add %rax,$hi1
  242. mov ($ap,$j,8),%rax
  243. adc \$0,%rdx
  244. add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
  245. mov $lo0,$hi0
  246. adc \$0,%rdx
  247. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  248. mov %rdx,$hi1
  249. .L1st_enter:
  250. mulq $m0 # ap[j]*bp[0]
  251. add %rax,$hi0
  252. mov ($np,$j,8),%rax
  253. adc \$0,%rdx
  254. lea 1($j),$j # j++
  255. mov %rdx,$lo0
  256. mulq $m1 # np[j]*m1
  257. cmp $num,$j
  258. jne .L1st # note that upon exit $j==$num, so
  259. # they can be used interchangeably
  260. add %rax,$hi1
  261. adc \$0,%rdx
  262. add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
  263. adc \$0,%rdx
  264. mov $hi1,-16(%rsp,$num,8) # tp[num-1]
  265. mov %rdx,$hi1
  266. mov $lo0,$hi0
  267. xor %rdx,%rdx
  268. add $hi0,$hi1
  269. adc \$0,%rdx
  270. mov $hi1,-8(%rsp,$num,8)
  271. mov %rdx,(%rsp,$num,8) # store upmost overflow bit
  272. lea 1($i),$i # i++
  273. jmp .Louter
  274. .align 16
  275. .Louter:
  276. lea 24+128(%rsp,$num,8),%rdx # where 256-byte mask is (+size optimization)
  277. and \$-16,%rdx
  278. pxor %xmm4,%xmm4
  279. pxor %xmm5,%xmm5
  280. ___
  281. for($k=0;$k<$STRIDE/16;$k+=4) {
  282. $code.=<<___;
  283. movdqa `16*($k+0)-128`($bp),%xmm0
  284. movdqa `16*($k+1)-128`($bp),%xmm1
  285. movdqa `16*($k+2)-128`($bp),%xmm2
  286. movdqa `16*($k+3)-128`($bp),%xmm3
  287. pand `16*($k+0)-128`(%rdx),%xmm0
  288. pand `16*($k+1)-128`(%rdx),%xmm1
  289. por %xmm0,%xmm4
  290. pand `16*($k+2)-128`(%rdx),%xmm2
  291. por %xmm1,%xmm5
  292. pand `16*($k+3)-128`(%rdx),%xmm3
  293. por %xmm2,%xmm4
  294. por %xmm3,%xmm5
  295. ___
  296. }
  297. $code.=<<___;
  298. por %xmm5,%xmm4
  299. pshufd \$0x4e,%xmm4,%xmm0
  300. por %xmm4,%xmm0
  301. lea $STRIDE($bp),$bp
  302. mov ($ap),%rax # ap[0]
  303. movq %xmm0,$m0 # m0=bp[i]
  304. xor $j,$j # j=0
  305. mov $n0,$m1
  306. mov (%rsp),$lo0
  307. mulq $m0 # ap[0]*bp[i]
  308. add %rax,$lo0 # ap[0]*bp[i]+tp[0]
  309. mov ($np),%rax
  310. adc \$0,%rdx
  311. imulq $lo0,$m1 # tp[0]*n0
  312. mov %rdx,$hi0
  313. mulq $m1 # np[0]*m1
  314. add %rax,$lo0 # discarded
  315. mov 8($ap),%rax
  316. adc \$0,%rdx
  317. mov 8(%rsp),$lo0 # tp[1]
  318. mov %rdx,$hi1
  319. lea 1($j),$j # j++
  320. jmp .Linner_enter
  321. .align 16
  322. .Linner:
  323. add %rax,$hi1
  324. mov ($ap,$j,8),%rax
  325. adc \$0,%rdx
  326. add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
  327. mov (%rsp,$j,8),$lo0
  328. adc \$0,%rdx
  329. mov $hi1,-16(%rsp,$j,8) # tp[j-1]
  330. mov %rdx,$hi1
  331. .Linner_enter:
  332. mulq $m0 # ap[j]*bp[i]
  333. add %rax,$hi0
  334. mov ($np,$j,8),%rax
  335. adc \$0,%rdx
  336. add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
  337. mov %rdx,$hi0
  338. adc \$0,$hi0
  339. lea 1($j),$j # j++
  340. mulq $m1 # np[j]*m1
  341. cmp $num,$j
  342. jne .Linner # note that upon exit $j==$num, so
  343. # they can be used interchangeably
  344. add %rax,$hi1
  345. adc \$0,%rdx
  346. add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
  347. mov (%rsp,$num,8),$lo0
  348. adc \$0,%rdx
  349. mov $hi1,-16(%rsp,$num,8) # tp[num-1]
  350. mov %rdx,$hi1
  351. xor %rdx,%rdx
  352. add $hi0,$hi1
  353. adc \$0,%rdx
  354. add $lo0,$hi1 # pull upmost overflow bit
  355. adc \$0,%rdx
  356. mov $hi1,-8(%rsp,$num,8)
  357. mov %rdx,(%rsp,$num,8) # store upmost overflow bit
  358. lea 1($i),$i # i++
  359. cmp $num,$i
  360. jb .Louter
  361. xor $i,$i # i=0 and clear CF!
  362. mov (%rsp),%rax # tp[0]
  363. lea (%rsp),$ap # borrow ap for tp
  364. mov $num,$j # j=num
  365. jmp .Lsub
  366. .align 16
  367. .Lsub: sbb ($np,$i,8),%rax
  368. mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
  369. mov 8($ap,$i,8),%rax # tp[i+1]
  370. lea 1($i),$i # i++
  371. dec $j # doesn't affect CF!
  372. jnz .Lsub
  373. sbb \$0,%rax # handle upmost overflow bit
  374. mov \$-1,%rbx
  375. xor %rax,%rbx
  376. xor $i,$i
  377. mov $num,$j # j=num
  378. .Lcopy: # conditional copy
  379. mov ($rp,$i,8),%rcx
  380. mov (%rsp,$i,8),%rdx
  381. and %rbx,%rcx
  382. and %rax,%rdx
  383. mov $i,(%rsp,$i,8) # zap temporary vector
  384. or %rcx,%rdx
  385. mov %rdx,($rp,$i,8) # rp[i]=tp[i]
  386. lea 1($i),$i
  387. sub \$1,$j
  388. jnz .Lcopy
  389. mov 8(%rsp,$num,8),%rsi # restore %rsp
  390. .cfi_def_cfa %rsi,8
  391. mov \$1,%rax
  392. mov -48(%rsi),%r15
  393. .cfi_restore %r15
  394. mov -40(%rsi),%r14
  395. .cfi_restore %r14
  396. mov -32(%rsi),%r13
  397. .cfi_restore %r13
  398. mov -24(%rsi),%r12
  399. .cfi_restore %r12
  400. mov -16(%rsi),%rbp
  401. .cfi_restore %rbp
  402. mov -8(%rsi),%rbx
  403. .cfi_restore %rbx
  404. lea (%rsi),%rsp
  405. .cfi_def_cfa_register %rsp
  406. .Lmul_epilogue:
  407. ret
  408. .cfi_endproc
  409. .size bn_mul_mont_gather5,.-bn_mul_mont_gather5
  410. ___
  411. {{{
  412. my @A=("%r10","%r11");
  413. my @N=("%r13","%rdi");
  414. $code.=<<___;
  415. .type bn_mul4x_mont_gather5,\@function,6
  416. .align 32
  417. bn_mul4x_mont_gather5:
  418. .cfi_startproc
  419. .byte 0x67
  420. mov %rsp,%rax
  421. .cfi_def_cfa_register %rax
  422. .Lmul4x_enter:
  423. ___
  424. $code.=<<___ if ($addx);
  425. and \$0x80108,%r11d
  426. cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
  427. je .Lmulx4x_enter
  428. ___
  429. $code.=<<___;
  430. push %rbx
  431. .cfi_push %rbx
  432. push %rbp
  433. .cfi_push %rbp
  434. push %r12
  435. .cfi_push %r12
  436. push %r13
  437. .cfi_push %r13
  438. push %r14
  439. .cfi_push %r14
  440. push %r15
  441. .cfi_push %r15
  442. .Lmul4x_prologue:
  443. .byte 0x67
  444. shl \$3,${num}d # convert $num to bytes
  445. lea ($num,$num,2),%r10 # 3*$num in bytes
  446. neg $num # -$num
  447. ##############################################################
  448. # Ensure that stack frame doesn't alias with $rptr+3*$num
  449. # modulo 4096, which covers ret[num], am[num] and n[num]
  450. # (see bn_exp.c). This is done to allow memory disambiguation
  451. # logic do its magic. [Extra [num] is allocated in order
  452. # to align with bn_power5's frame, which is cleansed after
  453. # completing exponentiation. Extra 256 bytes is for power mask
  454. # calculated from 7th argument, the index.]
  455. #
  456. lea -320(%rsp,$num,2),%r11
  457. mov %rsp,%rbp
  458. sub $rp,%r11
  459. and \$4095,%r11
  460. cmp %r11,%r10
  461. jb .Lmul4xsp_alt
  462. sub %r11,%rbp # align with $rp
  463. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
  464. jmp .Lmul4xsp_done
  465. .align 32
  466. .Lmul4xsp_alt:
  467. lea 4096-320(,$num,2),%r10
  468. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
  469. sub %r10,%r11
  470. mov \$0,%r10
  471. cmovc %r10,%r11
  472. sub %r11,%rbp
  473. .Lmul4xsp_done:
  474. and \$-64,%rbp
  475. mov %rsp,%r11
  476. sub %rbp,%r11
  477. and \$-4096,%r11
  478. lea (%rbp,%r11),%rsp
  479. mov (%rsp),%r10
  480. cmp %rbp,%rsp
  481. ja .Lmul4x_page_walk
  482. jmp .Lmul4x_page_walk_done
  483. .Lmul4x_page_walk:
  484. lea -4096(%rsp),%rsp
  485. mov (%rsp),%r10
  486. cmp %rbp,%rsp
  487. ja .Lmul4x_page_walk
  488. .Lmul4x_page_walk_done:
  489. neg $num
  490. mov %rax,40(%rsp)
  491. .cfi_cfa_expression %rsp+40,deref,+8
  492. .Lmul4x_body:
  493. call mul4x_internal
  494. mov 40(%rsp),%rsi # restore %rsp
  495. .cfi_def_cfa %rsi,8
  496. mov \$1,%rax
  497. mov -48(%rsi),%r15
  498. .cfi_restore %r15
  499. mov -40(%rsi),%r14
  500. .cfi_restore %r14
  501. mov -32(%rsi),%r13
  502. .cfi_restore %r13
  503. mov -24(%rsi),%r12
  504. .cfi_restore %r12
  505. mov -16(%rsi),%rbp
  506. .cfi_restore %rbp
  507. mov -8(%rsi),%rbx
  508. .cfi_restore %rbx
  509. lea (%rsi),%rsp
  510. .cfi_def_cfa_register %rsp
  511. .Lmul4x_epilogue:
  512. ret
  513. .cfi_endproc
  514. .size bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
  515. .type mul4x_internal,\@abi-omnipotent
  516. .align 32
  517. mul4x_internal:
  518. .cfi_startproc
  519. shl \$5,$num # $num was in bytes
  520. movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
  521. lea .Linc(%rip),%rax
  522. lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
  523. shr \$5,$num # restore $num
  524. ___
  525. $bp="%r12";
  526. $STRIDE=2**5*8; # 5 is "window size"
  527. $N=$STRIDE/4; # should match cache line size
  528. $tp=$i;
  529. $code.=<<___;
  530. movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
  531. movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
  532. lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
  533. lea 128(%rdx),$bp # size optimization
  534. pshufd \$0,%xmm5,%xmm5 # broadcast index
  535. movdqa %xmm1,%xmm4
  536. .byte 0x67,0x67
  537. movdqa %xmm1,%xmm2
  538. ___
  539. ########################################################################
  540. # calculate mask by comparing 0..31 to index and save result to stack
  541. #
  542. $code.=<<___;
  543. paddd %xmm0,%xmm1
  544. pcmpeqd %xmm5,%xmm0 # compare to 1,0
  545. .byte 0x67
  546. movdqa %xmm4,%xmm3
  547. ___
  548. for($i=0;$i<$STRIDE/16-4;$i+=4) {
  549. $code.=<<___;
  550. paddd %xmm1,%xmm2
  551. pcmpeqd %xmm5,%xmm1 # compare to 3,2
  552. movdqa %xmm0,`16*($i+0)+112`(%r10)
  553. movdqa %xmm4,%xmm0
  554. paddd %xmm2,%xmm3
  555. pcmpeqd %xmm5,%xmm2 # compare to 5,4
  556. movdqa %xmm1,`16*($i+1)+112`(%r10)
  557. movdqa %xmm4,%xmm1
  558. paddd %xmm3,%xmm0
  559. pcmpeqd %xmm5,%xmm3 # compare to 7,6
  560. movdqa %xmm2,`16*($i+2)+112`(%r10)
  561. movdqa %xmm4,%xmm2
  562. paddd %xmm0,%xmm1
  563. pcmpeqd %xmm5,%xmm0
  564. movdqa %xmm3,`16*($i+3)+112`(%r10)
  565. movdqa %xmm4,%xmm3
  566. ___
  567. }
  568. $code.=<<___; # last iteration can be optimized
  569. paddd %xmm1,%xmm2
  570. pcmpeqd %xmm5,%xmm1
  571. movdqa %xmm0,`16*($i+0)+112`(%r10)
  572. paddd %xmm2,%xmm3
  573. .byte 0x67
  574. pcmpeqd %xmm5,%xmm2
  575. movdqa %xmm1,`16*($i+1)+112`(%r10)
  576. pcmpeqd %xmm5,%xmm3
  577. movdqa %xmm2,`16*($i+2)+112`(%r10)
  578. pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
  579. pand `16*($i+1)-128`($bp),%xmm1
  580. pand `16*($i+2)-128`($bp),%xmm2
  581. movdqa %xmm3,`16*($i+3)+112`(%r10)
  582. pand `16*($i+3)-128`($bp),%xmm3
  583. por %xmm2,%xmm0
  584. por %xmm3,%xmm1
  585. ___
  586. for($i=0;$i<$STRIDE/16-4;$i+=4) {
  587. $code.=<<___;
  588. movdqa `16*($i+0)-128`($bp),%xmm4
  589. movdqa `16*($i+1)-128`($bp),%xmm5
  590. movdqa `16*($i+2)-128`($bp),%xmm2
  591. pand `16*($i+0)+112`(%r10),%xmm4
  592. movdqa `16*($i+3)-128`($bp),%xmm3
  593. pand `16*($i+1)+112`(%r10),%xmm5
  594. por %xmm4,%xmm0
  595. pand `16*($i+2)+112`(%r10),%xmm2
  596. por %xmm5,%xmm1
  597. pand `16*($i+3)+112`(%r10),%xmm3
  598. por %xmm2,%xmm0
  599. por %xmm3,%xmm1
  600. ___
  601. }
  602. $code.=<<___;
  603. por %xmm1,%xmm0
  604. pshufd \$0x4e,%xmm0,%xmm1
  605. por %xmm1,%xmm0
  606. lea $STRIDE($bp),$bp
  607. movq %xmm0,$m0 # m0=bp[0]
  608. mov %r13,16+8(%rsp) # save end of b[num]
  609. mov $rp, 56+8(%rsp) # save $rp
  610. mov ($n0),$n0 # pull n0[0] value
  611. mov ($ap),%rax
  612. lea ($ap,$num),$ap # end of a[num]
  613. neg $num
  614. mov $n0,$m1
  615. mulq $m0 # ap[0]*bp[0]
  616. mov %rax,$A[0]
  617. mov ($np),%rax
  618. imulq $A[0],$m1 # "tp[0]"*n0
  619. lea 64+8(%rsp),$tp
  620. mov %rdx,$A[1]
  621. mulq $m1 # np[0]*m1
  622. add %rax,$A[0] # discarded
  623. mov 8($ap,$num),%rax
  624. adc \$0,%rdx
  625. mov %rdx,$N[1]
  626. mulq $m0
  627. add %rax,$A[1]
  628. mov 8*1($np),%rax
  629. adc \$0,%rdx
  630. mov %rdx,$A[0]
  631. mulq $m1
  632. add %rax,$N[1]
  633. mov 16($ap,$num),%rax
  634. adc \$0,%rdx
  635. add $A[1],$N[1]
  636. lea 4*8($num),$j # j=4
  637. lea 8*4($np),$np
  638. adc \$0,%rdx
  639. mov $N[1],($tp)
  640. mov %rdx,$N[0]
  641. jmp .L1st4x
  642. .align 32
  643. .L1st4x:
  644. mulq $m0 # ap[j]*bp[0]
  645. add %rax,$A[0]
  646. mov -8*2($np),%rax
  647. lea 32($tp),$tp
  648. adc \$0,%rdx
  649. mov %rdx,$A[1]
  650. mulq $m1 # np[j]*m1
  651. add %rax,$N[0]
  652. mov -8($ap,$j),%rax
  653. adc \$0,%rdx
  654. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  655. adc \$0,%rdx
  656. mov $N[0],-24($tp) # tp[j-1]
  657. mov %rdx,$N[1]
  658. mulq $m0 # ap[j]*bp[0]
  659. add %rax,$A[1]
  660. mov -8*1($np),%rax
  661. adc \$0,%rdx
  662. mov %rdx,$A[0]
  663. mulq $m1 # np[j]*m1
  664. add %rax,$N[1]
  665. mov ($ap,$j),%rax
  666. adc \$0,%rdx
  667. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  668. adc \$0,%rdx
  669. mov $N[1],-16($tp) # tp[j-1]
  670. mov %rdx,$N[0]
  671. mulq $m0 # ap[j]*bp[0]
  672. add %rax,$A[0]
  673. mov 8*0($np),%rax
  674. adc \$0,%rdx
  675. mov %rdx,$A[1]
  676. mulq $m1 # np[j]*m1
  677. add %rax,$N[0]
  678. mov 8($ap,$j),%rax
  679. adc \$0,%rdx
  680. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  681. adc \$0,%rdx
  682. mov $N[0],-8($tp) # tp[j-1]
  683. mov %rdx,$N[1]
  684. mulq $m0 # ap[j]*bp[0]
  685. add %rax,$A[1]
  686. mov 8*1($np),%rax
  687. adc \$0,%rdx
  688. mov %rdx,$A[0]
  689. mulq $m1 # np[j]*m1
  690. add %rax,$N[1]
  691. mov 16($ap,$j),%rax
  692. adc \$0,%rdx
  693. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  694. lea 8*4($np),$np
  695. adc \$0,%rdx
  696. mov $N[1],($tp) # tp[j-1]
  697. mov %rdx,$N[0]
  698. add \$32,$j # j+=4
  699. jnz .L1st4x
  700. mulq $m0 # ap[j]*bp[0]
  701. add %rax,$A[0]
  702. mov -8*2($np),%rax
  703. lea 32($tp),$tp
  704. adc \$0,%rdx
  705. mov %rdx,$A[1]
  706. mulq $m1 # np[j]*m1
  707. add %rax,$N[0]
  708. mov -8($ap),%rax
  709. adc \$0,%rdx
  710. add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
  711. adc \$0,%rdx
  712. mov $N[0],-24($tp) # tp[j-1]
  713. mov %rdx,$N[1]
  714. mulq $m0 # ap[j]*bp[0]
  715. add %rax,$A[1]
  716. mov -8*1($np),%rax
  717. adc \$0,%rdx
  718. mov %rdx,$A[0]
  719. mulq $m1 # np[j]*m1
  720. add %rax,$N[1]
  721. mov ($ap,$num),%rax # ap[0]
  722. adc \$0,%rdx
  723. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
  724. adc \$0,%rdx
  725. mov $N[1],-16($tp) # tp[j-1]
  726. mov %rdx,$N[0]
  727. lea ($np,$num),$np # rewind $np
  728. xor $N[1],$N[1]
  729. add $A[0],$N[0]
  730. adc \$0,$N[1]
  731. mov $N[0],-8($tp)
  732. jmp .Louter4x
  733. .align 32
  734. .Louter4x:
  735. lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
  736. pxor %xmm4,%xmm4
  737. pxor %xmm5,%xmm5
  738. ___
  739. for($i=0;$i<$STRIDE/16;$i+=4) {
  740. $code.=<<___;
  741. movdqa `16*($i+0)-128`($bp),%xmm0
  742. movdqa `16*($i+1)-128`($bp),%xmm1
  743. movdqa `16*($i+2)-128`($bp),%xmm2
  744. movdqa `16*($i+3)-128`($bp),%xmm3
  745. pand `16*($i+0)-128`(%rdx),%xmm0
  746. pand `16*($i+1)-128`(%rdx),%xmm1
  747. por %xmm0,%xmm4
  748. pand `16*($i+2)-128`(%rdx),%xmm2
  749. por %xmm1,%xmm5
  750. pand `16*($i+3)-128`(%rdx),%xmm3
  751. por %xmm2,%xmm4
  752. por %xmm3,%xmm5
  753. ___
  754. }
  755. $code.=<<___;
  756. por %xmm5,%xmm4
  757. pshufd \$0x4e,%xmm4,%xmm0
  758. por %xmm4,%xmm0
  759. lea $STRIDE($bp),$bp
  760. movq %xmm0,$m0 # m0=bp[i]
  761. mov ($tp,$num),$A[0]
  762. mov $n0,$m1
  763. mulq $m0 # ap[0]*bp[i]
  764. add %rax,$A[0] # ap[0]*bp[i]+tp[0]
  765. mov ($np),%rax
  766. adc \$0,%rdx
  767. imulq $A[0],$m1 # tp[0]*n0
  768. mov %rdx,$A[1]
  769. mov $N[1],($tp) # store upmost overflow bit
  770. lea ($tp,$num),$tp # rewind $tp
  771. mulq $m1 # np[0]*m1
  772. add %rax,$A[0] # "$N[0]", discarded
  773. mov 8($ap,$num),%rax
  774. adc \$0,%rdx
  775. mov %rdx,$N[1]
  776. mulq $m0 # ap[j]*bp[i]
  777. add %rax,$A[1]
  778. mov 8*1($np),%rax
  779. adc \$0,%rdx
  780. add 8($tp),$A[1] # +tp[1]
  781. adc \$0,%rdx
  782. mov %rdx,$A[0]
  783. mulq $m1 # np[j]*m1
  784. add %rax,$N[1]
  785. mov 16($ap,$num),%rax
  786. adc \$0,%rdx
  787. add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
  788. lea 4*8($num),$j # j=4
  789. lea 8*4($np),$np
  790. adc \$0,%rdx
  791. mov %rdx,$N[0]
  792. jmp .Linner4x
  793. .align 32
  794. .Linner4x:
  795. mulq $m0 # ap[j]*bp[i]
  796. add %rax,$A[0]
  797. mov -8*2($np),%rax
  798. adc \$0,%rdx
  799. add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
  800. lea 32($tp),$tp
  801. adc \$0,%rdx
  802. mov %rdx,$A[1]
  803. mulq $m1 # np[j]*m1
  804. add %rax,$N[0]
  805. mov -8($ap,$j),%rax
  806. adc \$0,%rdx
  807. add $A[0],$N[0]
  808. adc \$0,%rdx
  809. mov $N[1],-32($tp) # tp[j-1]
  810. mov %rdx,$N[1]
  811. mulq $m0 # ap[j]*bp[i]
  812. add %rax,$A[1]
  813. mov -8*1($np),%rax
  814. adc \$0,%rdx
  815. add -8($tp),$A[1]
  816. adc \$0,%rdx
  817. mov %rdx,$A[0]
  818. mulq $m1 # np[j]*m1
  819. add %rax,$N[1]
  820. mov ($ap,$j),%rax
  821. adc \$0,%rdx
  822. add $A[1],$N[1]
  823. adc \$0,%rdx
  824. mov $N[0],-24($tp) # tp[j-1]
  825. mov %rdx,$N[0]
  826. mulq $m0 # ap[j]*bp[i]
  827. add %rax,$A[0]
  828. mov 8*0($np),%rax
  829. adc \$0,%rdx
  830. add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
  831. adc \$0,%rdx
  832. mov %rdx,$A[1]
  833. mulq $m1 # np[j]*m1
  834. add %rax,$N[0]
  835. mov 8($ap,$j),%rax
  836. adc \$0,%rdx
  837. add $A[0],$N[0]
  838. adc \$0,%rdx
  839. mov $N[1],-16($tp) # tp[j-1]
  840. mov %rdx,$N[1]
  841. mulq $m0 # ap[j]*bp[i]
  842. add %rax,$A[1]
  843. mov 8*1($np),%rax
  844. adc \$0,%rdx
  845. add 8($tp),$A[1]
  846. adc \$0,%rdx
  847. mov %rdx,$A[0]
  848. mulq $m1 # np[j]*m1
  849. add %rax,$N[1]
  850. mov 16($ap,$j),%rax
  851. adc \$0,%rdx
  852. add $A[1],$N[1]
  853. lea 8*4($np),$np
  854. adc \$0,%rdx
  855. mov $N[0],-8($tp) # tp[j-1]
  856. mov %rdx,$N[0]
  857. add \$32,$j # j+=4
  858. jnz .Linner4x
  859. mulq $m0 # ap[j]*bp[i]
  860. add %rax,$A[0]
  861. mov -8*2($np),%rax
  862. adc \$0,%rdx
  863. add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
  864. lea 32($tp),$tp
  865. adc \$0,%rdx
  866. mov %rdx,$A[1]
  867. mulq $m1 # np[j]*m1
  868. add %rax,$N[0]
  869. mov -8($ap),%rax
  870. adc \$0,%rdx
  871. add $A[0],$N[0]
  872. adc \$0,%rdx
  873. mov $N[1],-32($tp) # tp[j-1]
  874. mov %rdx,$N[1]
  875. mulq $m0 # ap[j]*bp[i]
  876. add %rax,$A[1]
  877. mov $m1,%rax
  878. mov -8*1($np),$m1
  879. adc \$0,%rdx
  880. add -8($tp),$A[1]
  881. adc \$0,%rdx
  882. mov %rdx,$A[0]
  883. mulq $m1 # np[j]*m1
  884. add %rax,$N[1]
  885. mov ($ap,$num),%rax # ap[0]
  886. adc \$0,%rdx
  887. add $A[1],$N[1]
  888. adc \$0,%rdx
  889. mov $N[0],-24($tp) # tp[j-1]
  890. mov %rdx,$N[0]
  891. mov $N[1],-16($tp) # tp[j-1]
  892. lea ($np,$num),$np # rewind $np
  893. xor $N[1],$N[1]
  894. add $A[0],$N[0]
  895. adc \$0,$N[1]
  896. add ($tp),$N[0] # pull upmost overflow bit
  897. adc \$0,$N[1] # upmost overflow bit
  898. mov $N[0],-8($tp)
  899. cmp 16+8(%rsp),$bp
  900. jb .Louter4x
  901. ___
  902. if (1) {
  903. $code.=<<___;
  904. xor %rax,%rax
  905. sub $N[0],$m1 # compare top-most words
  906. adc $j,$j # $j is zero
  907. or $j,$N[1]
  908. sub $N[1],%rax # %rax=-$N[1]
  909. lea ($tp,$num),%rbx # tptr in .sqr4x_sub
  910. mov ($np),%r12
  911. lea ($np),%rbp # nptr in .sqr4x_sub
  912. mov %r9,%rcx
  913. sar \$3+2,%rcx
  914. mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
  915. dec %r12 # so that after 'not' we get -n[0]
  916. xor %r10,%r10
  917. mov 8*1(%rbp),%r13
  918. mov 8*2(%rbp),%r14
  919. mov 8*3(%rbp),%r15
  920. jmp .Lsqr4x_sub_entry
  921. ___
  922. } else {
  923. my @ri=("%rax",$bp,$m0,$m1);
  924. my $rp="%rdx";
  925. $code.=<<___
  926. xor \$1,$N[1]
  927. lea ($tp,$num),$tp # rewind $tp
  928. sar \$5,$num # cf=0
  929. lea ($np,$N[1],8),$np
  930. mov 56+8(%rsp),$rp # restore $rp
  931. jmp .Lsub4x
  932. .align 32
  933. .Lsub4x:
  934. .byte 0x66
  935. mov 8*0($tp),@ri[0]
  936. mov 8*1($tp),@ri[1]
  937. .byte 0x66
  938. sbb 16*0($np),@ri[0]
  939. mov 8*2($tp),@ri[2]
  940. sbb 16*1($np),@ri[1]
  941. mov 3*8($tp),@ri[3]
  942. lea 4*8($tp),$tp
  943. sbb 16*2($np),@ri[2]
  944. mov @ri[0],8*0($rp)
  945. sbb 16*3($np),@ri[3]
  946. lea 16*4($np),$np
  947. mov @ri[1],8*1($rp)
  948. mov @ri[2],8*2($rp)
  949. mov @ri[3],8*3($rp)
  950. lea 8*4($rp),$rp
  951. inc $num
  952. jnz .Lsub4x
  953. ret
  954. ___
  955. }
  956. $code.=<<___;
  957. .cfi_endproc
  958. .size mul4x_internal,.-mul4x_internal
  959. ___
  960. }}}
  961. {{{
  962. ######################################################################
  963. # void bn_power5(
  964. my $rptr="%rdi"; # BN_ULONG *rptr,
  965. my $aptr="%rsi"; # const BN_ULONG *aptr,
  966. my $bptr="%rdx"; # const void *table,
  967. my $nptr="%rcx"; # const BN_ULONG *nptr,
  968. my $n0 ="%r8"; # const BN_ULONG *n0);
  969. my $num ="%r9"; # int num, has to be divisible by 8
  970. # int pwr
  971. my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
  972. my @A0=("%r10","%r11");
  973. my @A1=("%r12","%r13");
  974. my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
  975. $code.=<<___;
  976. .globl bn_power5
  977. .type bn_power5,\@function,6
  978. .align 32
  979. bn_power5:
  980. .cfi_startproc
  981. mov %rsp,%rax
  982. .cfi_def_cfa_register %rax
  983. ___
  984. $code.=<<___ if ($addx);
  985. mov OPENSSL_ia32cap_P+8(%rip),%r11d
  986. and \$0x80108,%r11d
  987. cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
  988. je .Lpowerx5_enter
  989. ___
  990. $code.=<<___;
  991. push %rbx
  992. .cfi_push %rbx
  993. push %rbp
  994. .cfi_push %rbp
  995. push %r12
  996. .cfi_push %r12
  997. push %r13
  998. .cfi_push %r13
  999. push %r14
  1000. .cfi_push %r14
  1001. push %r15
  1002. .cfi_push %r15
  1003. .Lpower5_prologue:
  1004. shl \$3,${num}d # convert $num to bytes
  1005. lea ($num,$num,2),%r10d # 3*$num
  1006. neg $num
  1007. mov ($n0),$n0 # *n0
  1008. ##############################################################
  1009. # Ensure that stack frame doesn't alias with $rptr+3*$num
  1010. # modulo 4096, which covers ret[num], am[num] and n[num]
  1011. # (see bn_exp.c). This is done to allow memory disambiguation
  1012. # logic do its magic. [Extra 256 bytes is for power mask
  1013. # calculated from 7th argument, the index.]
  1014. #
  1015. lea -320(%rsp,$num,2),%r11
  1016. mov %rsp,%rbp
  1017. sub $rptr,%r11
  1018. and \$4095,%r11
  1019. cmp %r11,%r10
  1020. jb .Lpwr_sp_alt
  1021. sub %r11,%rbp # align with $aptr
  1022. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
  1023. jmp .Lpwr_sp_done
  1024. .align 32
  1025. .Lpwr_sp_alt:
  1026. lea 4096-320(,$num,2),%r10
  1027. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
  1028. sub %r10,%r11
  1029. mov \$0,%r10
  1030. cmovc %r10,%r11
  1031. sub %r11,%rbp
  1032. .Lpwr_sp_done:
  1033. and \$-64,%rbp
  1034. mov %rsp,%r11
  1035. sub %rbp,%r11
  1036. and \$-4096,%r11
  1037. lea (%rbp,%r11),%rsp
  1038. mov (%rsp),%r10
  1039. cmp %rbp,%rsp
  1040. ja .Lpwr_page_walk
  1041. jmp .Lpwr_page_walk_done
  1042. .Lpwr_page_walk:
  1043. lea -4096(%rsp),%rsp
  1044. mov (%rsp),%r10
  1045. cmp %rbp,%rsp
  1046. ja .Lpwr_page_walk
  1047. .Lpwr_page_walk_done:
  1048. mov $num,%r10
  1049. neg $num
  1050. ##############################################################
  1051. # Stack layout
  1052. #
  1053. # +0 saved $num, used in reduction section
  1054. # +8 &t[2*$num], used in reduction section
  1055. # +32 saved *n0
  1056. # +40 saved %rsp
  1057. # +48 t[2*$num]
  1058. #
  1059. mov $n0, 32(%rsp)
  1060. mov %rax, 40(%rsp) # save original %rsp
  1061. .cfi_cfa_expression %rsp+40,deref,+8
  1062. .Lpower5_body:
  1063. movq $rptr,%xmm1 # save $rptr, used in sqr8x
  1064. movq $nptr,%xmm2 # save $nptr
  1065. movq %r10, %xmm3 # -$num, used in sqr8x
  1066. movq $bptr,%xmm4
  1067. call __bn_sqr8x_internal
  1068. call __bn_post4x_internal
  1069. call __bn_sqr8x_internal
  1070. call __bn_post4x_internal
  1071. call __bn_sqr8x_internal
  1072. call __bn_post4x_internal
  1073. call __bn_sqr8x_internal
  1074. call __bn_post4x_internal
  1075. call __bn_sqr8x_internal
  1076. call __bn_post4x_internal
  1077. movq %xmm2,$nptr
  1078. movq %xmm4,$bptr
  1079. mov $aptr,$rptr
  1080. mov 40(%rsp),%rax
  1081. lea 32(%rsp),$n0
  1082. call mul4x_internal
  1083. mov 40(%rsp),%rsi # restore %rsp
  1084. .cfi_def_cfa %rsi,8
  1085. mov \$1,%rax
  1086. mov -48(%rsi),%r15
  1087. .cfi_restore %r15
  1088. mov -40(%rsi),%r14
  1089. .cfi_restore %r14
  1090. mov -32(%rsi),%r13
  1091. .cfi_restore %r13
  1092. mov -24(%rsi),%r12
  1093. .cfi_restore %r12
  1094. mov -16(%rsi),%rbp
  1095. .cfi_restore %rbp
  1096. mov -8(%rsi),%rbx
  1097. .cfi_restore %rbx
  1098. lea (%rsi),%rsp
  1099. .cfi_def_cfa_register %rsp
  1100. .Lpower5_epilogue:
  1101. ret
  1102. .cfi_endproc
  1103. .size bn_power5,.-bn_power5
  1104. .globl bn_sqr8x_internal
  1105. .hidden bn_sqr8x_internal
  1106. .type bn_sqr8x_internal,\@abi-omnipotent
  1107. .align 32
  1108. bn_sqr8x_internal:
  1109. __bn_sqr8x_internal:
  1110. .cfi_startproc
  1111. ##############################################################
  1112. # Squaring part:
  1113. #
  1114. # a) multiply-n-add everything but a[i]*a[i];
  1115. # b) shift result of a) by 1 to the left and accumulate
  1116. # a[i]*a[i] products;
  1117. #
  1118. ##############################################################
  1119. # a[1]a[0]
  1120. # a[2]a[0]
  1121. # a[3]a[0]
  1122. # a[2]a[1]
  1123. # a[4]a[0]
  1124. # a[3]a[1]
  1125. # a[5]a[0]
  1126. # a[4]a[1]
  1127. # a[3]a[2]
  1128. # a[6]a[0]
  1129. # a[5]a[1]
  1130. # a[4]a[2]
  1131. # a[7]a[0]
  1132. # a[6]a[1]
  1133. # a[5]a[2]
  1134. # a[4]a[3]
  1135. # a[7]a[1]
  1136. # a[6]a[2]
  1137. # a[5]a[3]
  1138. # a[7]a[2]
  1139. # a[6]a[3]
  1140. # a[5]a[4]
  1141. # a[7]a[3]
  1142. # a[6]a[4]
  1143. # a[7]a[4]
  1144. # a[6]a[5]
  1145. # a[7]a[5]
  1146. # a[7]a[6]
  1147. # a[1]a[0]
  1148. # a[2]a[0]
  1149. # a[3]a[0]
  1150. # a[4]a[0]
  1151. # a[5]a[0]
  1152. # a[6]a[0]
  1153. # a[7]a[0]
  1154. # a[2]a[1]
  1155. # a[3]a[1]
  1156. # a[4]a[1]
  1157. # a[5]a[1]
  1158. # a[6]a[1]
  1159. # a[7]a[1]
  1160. # a[3]a[2]
  1161. # a[4]a[2]
  1162. # a[5]a[2]
  1163. # a[6]a[2]
  1164. # a[7]a[2]
  1165. # a[4]a[3]
  1166. # a[5]a[3]
  1167. # a[6]a[3]
  1168. # a[7]a[3]
  1169. # a[5]a[4]
  1170. # a[6]a[4]
  1171. # a[7]a[4]
  1172. # a[6]a[5]
  1173. # a[7]a[5]
  1174. # a[7]a[6]
  1175. # a[0]a[0]
  1176. # a[1]a[1]
  1177. # a[2]a[2]
  1178. # a[3]a[3]
  1179. # a[4]a[4]
  1180. # a[5]a[5]
  1181. # a[6]a[6]
  1182. # a[7]a[7]
  1183. lea 32(%r10),$i # $i=-($num-32)
  1184. lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
  1185. mov $num,$j # $j=$num
  1186. # comments apply to $num==8 case
  1187. mov -32($aptr,$i),$a0 # a[0]
  1188. lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
  1189. mov -24($aptr,$i),%rax # a[1]
  1190. lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
  1191. mov -16($aptr,$i),$ai # a[2]
  1192. mov %rax,$a1
  1193. mul $a0 # a[1]*a[0]
  1194. mov %rax,$A0[0] # a[1]*a[0]
  1195. mov $ai,%rax # a[2]
  1196. mov %rdx,$A0[1]
  1197. mov $A0[0],-24($tptr,$i) # t[1]
  1198. mul $a0 # a[2]*a[0]
  1199. add %rax,$A0[1]
  1200. mov $ai,%rax
  1201. adc \$0,%rdx
  1202. mov $A0[1],-16($tptr,$i) # t[2]
  1203. mov %rdx,$A0[0]
  1204. mov -8($aptr,$i),$ai # a[3]
  1205. mul $a1 # a[2]*a[1]
  1206. mov %rax,$A1[0] # a[2]*a[1]+t[3]
  1207. mov $ai,%rax
  1208. mov %rdx,$A1[1]
  1209. lea ($i),$j
  1210. mul $a0 # a[3]*a[0]
  1211. add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
  1212. mov $ai,%rax
  1213. mov %rdx,$A0[1]
  1214. adc \$0,$A0[1]
  1215. add $A1[0],$A0[0]
  1216. adc \$0,$A0[1]
  1217. mov $A0[0],-8($tptr,$j) # t[3]
  1218. jmp .Lsqr4x_1st
  1219. .align 32
  1220. .Lsqr4x_1st:
  1221. mov ($aptr,$j),$ai # a[4]
  1222. mul $a1 # a[3]*a[1]
  1223. add %rax,$A1[1] # a[3]*a[1]+t[4]
  1224. mov $ai,%rax
  1225. mov %rdx,$A1[0]
  1226. adc \$0,$A1[0]
  1227. mul $a0 # a[4]*a[0]
  1228. add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
  1229. mov $ai,%rax # a[3]
  1230. mov 8($aptr,$j),$ai # a[5]
  1231. mov %rdx,$A0[0]
  1232. adc \$0,$A0[0]
  1233. add $A1[1],$A0[1]
  1234. adc \$0,$A0[0]
  1235. mul $a1 # a[4]*a[3]
  1236. add %rax,$A1[0] # a[4]*a[3]+t[5]
  1237. mov $ai,%rax
  1238. mov $A0[1],($tptr,$j) # t[4]
  1239. mov %rdx,$A1[1]
  1240. adc \$0,$A1[1]
  1241. mul $a0 # a[5]*a[2]
  1242. add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
  1243. mov $ai,%rax
  1244. mov 16($aptr,$j),$ai # a[6]
  1245. mov %rdx,$A0[1]
  1246. adc \$0,$A0[1]
  1247. add $A1[0],$A0[0]
  1248. adc \$0,$A0[1]
  1249. mul $a1 # a[5]*a[3]
  1250. add %rax,$A1[1] # a[5]*a[3]+t[6]
  1251. mov $ai,%rax
  1252. mov $A0[0],8($tptr,$j) # t[5]
  1253. mov %rdx,$A1[0]
  1254. adc \$0,$A1[0]
  1255. mul $a0 # a[6]*a[2]
  1256. add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
  1257. mov $ai,%rax # a[3]
  1258. mov 24($aptr,$j),$ai # a[7]
  1259. mov %rdx,$A0[0]
  1260. adc \$0,$A0[0]
  1261. add $A1[1],$A0[1]
  1262. adc \$0,$A0[0]
  1263. mul $a1 # a[6]*a[5]
  1264. add %rax,$A1[0] # a[6]*a[5]+t[7]
  1265. mov $ai,%rax
  1266. mov $A0[1],16($tptr,$j) # t[6]
  1267. mov %rdx,$A1[1]
  1268. adc \$0,$A1[1]
  1269. lea 32($j),$j
  1270. mul $a0 # a[7]*a[4]
  1271. add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
  1272. mov $ai,%rax
  1273. mov %rdx,$A0[1]
  1274. adc \$0,$A0[1]
  1275. add $A1[0],$A0[0]
  1276. adc \$0,$A0[1]
  1277. mov $A0[0],-8($tptr,$j) # t[7]
  1278. cmp \$0,$j
  1279. jne .Lsqr4x_1st
  1280. mul $a1 # a[7]*a[5]
  1281. add %rax,$A1[1]
  1282. lea 16($i),$i
  1283. adc \$0,%rdx
  1284. add $A0[1],$A1[1]
  1285. adc \$0,%rdx
  1286. mov $A1[1],($tptr) # t[8]
  1287. mov %rdx,$A1[0]
  1288. mov %rdx,8($tptr) # t[9]
  1289. jmp .Lsqr4x_outer
  1290. .align 32
  1291. .Lsqr4x_outer: # comments apply to $num==6 case
  1292. mov -32($aptr,$i),$a0 # a[0]
  1293. lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
  1294. mov -24($aptr,$i),%rax # a[1]
  1295. lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
  1296. mov -16($aptr,$i),$ai # a[2]
  1297. mov %rax,$a1
  1298. mul $a0 # a[1]*a[0]
  1299. mov -24($tptr,$i),$A0[0] # t[1]
  1300. add %rax,$A0[0] # a[1]*a[0]+t[1]
  1301. mov $ai,%rax # a[2]
  1302. adc \$0,%rdx
  1303. mov $A0[0],-24($tptr,$i) # t[1]
  1304. mov %rdx,$A0[1]
  1305. mul $a0 # a[2]*a[0]
  1306. add %rax,$A0[1]
  1307. mov $ai,%rax
  1308. adc \$0,%rdx
  1309. add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
  1310. mov %rdx,$A0[0]
  1311. adc \$0,$A0[0]
  1312. mov $A0[1],-16($tptr,$i) # t[2]
  1313. xor $A1[0],$A1[0]
  1314. mov -8($aptr,$i),$ai # a[3]
  1315. mul $a1 # a[2]*a[1]
  1316. add %rax,$A1[0] # a[2]*a[1]+t[3]
  1317. mov $ai,%rax
  1318. adc \$0,%rdx
  1319. add -8($tptr,$i),$A1[0]
  1320. mov %rdx,$A1[1]
  1321. adc \$0,$A1[1]
  1322. mul $a0 # a[3]*a[0]
  1323. add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
  1324. mov $ai,%rax
  1325. adc \$0,%rdx
  1326. add $A1[0],$A0[0]
  1327. mov %rdx,$A0[1]
  1328. adc \$0,$A0[1]
  1329. mov $A0[0],-8($tptr,$i) # t[3]
  1330. lea ($i),$j
  1331. jmp .Lsqr4x_inner
  1332. .align 32
  1333. .Lsqr4x_inner:
  1334. mov ($aptr,$j),$ai # a[4]
  1335. mul $a1 # a[3]*a[1]
  1336. add %rax,$A1[1] # a[3]*a[1]+t[4]
  1337. mov $ai,%rax
  1338. mov %rdx,$A1[0]
  1339. adc \$0,$A1[0]
  1340. add ($tptr,$j),$A1[1]
  1341. adc \$0,$A1[0]
  1342. .byte 0x67
  1343. mul $a0 # a[4]*a[0]
  1344. add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
  1345. mov $ai,%rax # a[3]
  1346. mov 8($aptr,$j),$ai # a[5]
  1347. mov %rdx,$A0[0]
  1348. adc \$0,$A0[0]
  1349. add $A1[1],$A0[1]
  1350. adc \$0,$A0[0]
  1351. mul $a1 # a[4]*a[3]
  1352. add %rax,$A1[0] # a[4]*a[3]+t[5]
  1353. mov $A0[1],($tptr,$j) # t[4]
  1354. mov $ai,%rax
  1355. mov %rdx,$A1[1]
  1356. adc \$0,$A1[1]
  1357. add 8($tptr,$j),$A1[0]
  1358. lea 16($j),$j # j++
  1359. adc \$0,$A1[1]
  1360. mul $a0 # a[5]*a[2]
  1361. add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
  1362. mov $ai,%rax
  1363. adc \$0,%rdx
  1364. add $A1[0],$A0[0]
  1365. mov %rdx,$A0[1]
  1366. adc \$0,$A0[1]
  1367. mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
  1368. cmp \$0,$j
  1369. jne .Lsqr4x_inner
  1370. .byte 0x67
  1371. mul $a1 # a[5]*a[3]
  1372. add %rax,$A1[1]
  1373. adc \$0,%rdx
  1374. add $A0[1],$A1[1]
  1375. adc \$0,%rdx
  1376. mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
  1377. mov %rdx,$A1[0]
  1378. mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
  1379. add \$16,$i
  1380. jnz .Lsqr4x_outer
  1381. # comments apply to $num==4 case
  1382. mov -32($aptr),$a0 # a[0]
  1383. lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
  1384. mov -24($aptr),%rax # a[1]
  1385. lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
  1386. mov -16($aptr),$ai # a[2]
  1387. mov %rax,$a1
  1388. mul $a0 # a[1]*a[0]
  1389. add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
  1390. mov $ai,%rax # a[2]
  1391. mov %rdx,$A0[1]
  1392. adc \$0,$A0[1]
  1393. mul $a0 # a[2]*a[0]
  1394. add %rax,$A0[1]
  1395. mov $ai,%rax
  1396. mov $A0[0],-24($tptr) # t[1]
  1397. mov %rdx,$A0[0]
  1398. adc \$0,$A0[0]
  1399. add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
  1400. mov -8($aptr),$ai # a[3]
  1401. adc \$0,$A0[0]
  1402. mul $a1 # a[2]*a[1]
  1403. add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
  1404. mov $ai,%rax
  1405. mov $A0[1],-16($tptr) # t[2]
  1406. mov %rdx,$A1[1]
  1407. adc \$0,$A1[1]
  1408. mul $a0 # a[3]*a[0]
  1409. add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
  1410. mov $ai,%rax
  1411. mov %rdx,$A0[1]
  1412. adc \$0,$A0[1]
  1413. add $A1[0],$A0[0]
  1414. adc \$0,$A0[1]
  1415. mov $A0[0],-8($tptr) # t[3]
  1416. mul $a1 # a[3]*a[1]
  1417. add %rax,$A1[1]
  1418. mov -16($aptr),%rax # a[2]
  1419. adc \$0,%rdx
  1420. add $A0[1],$A1[1]
  1421. adc \$0,%rdx
  1422. mov $A1[1],($tptr) # t[4]
  1423. mov %rdx,$A1[0]
  1424. mov %rdx,8($tptr) # t[5]
  1425. mul $ai # a[2]*a[3]
  1426. ___
  1427. {
  1428. my ($shift,$carry)=($a0,$a1);
  1429. my @S=(@A1,$ai,$n0);
  1430. $code.=<<___;
  1431. add \$16,$i
  1432. xor $shift,$shift
  1433. sub $num,$i # $i=16-$num
  1434. xor $carry,$carry
  1435. add $A1[0],%rax # t[5]
  1436. adc \$0,%rdx
  1437. mov %rax,8($tptr) # t[5]
  1438. mov %rdx,16($tptr) # t[6]
  1439. mov $carry,24($tptr) # t[7]
  1440. mov -16($aptr,$i),%rax # a[0]
  1441. lea 48+8(%rsp),$tptr
  1442. xor $A0[0],$A0[0] # t[0]
  1443. mov 8($tptr),$A0[1] # t[1]
  1444. lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
  1445. shr \$63,$A0[0]
  1446. lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
  1447. shr \$63,$A0[1]
  1448. or $A0[0],$S[1] # | t[2*i]>>63
  1449. mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
  1450. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1451. mul %rax # a[i]*a[i]
  1452. neg $carry # mov $carry,cf
  1453. mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1454. adc %rax,$S[0]
  1455. mov -8($aptr,$i),%rax # a[i+1] # prefetch
  1456. mov $S[0],($tptr)
  1457. adc %rdx,$S[1]
  1458. lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
  1459. mov $S[1],8($tptr)
  1460. sbb $carry,$carry # mov cf,$carry
  1461. shr \$63,$A0[0]
  1462. lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
  1463. shr \$63,$A0[1]
  1464. or $A0[0],$S[3] # | t[2*i]>>63
  1465. mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
  1466. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1467. mul %rax # a[i]*a[i]
  1468. neg $carry # mov $carry,cf
  1469. mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1470. adc %rax,$S[2]
  1471. mov 0($aptr,$i),%rax # a[i+1] # prefetch
  1472. mov $S[2],16($tptr)
  1473. adc %rdx,$S[3]
  1474. lea 16($i),$i
  1475. mov $S[3],24($tptr)
  1476. sbb $carry,$carry # mov cf,$carry
  1477. lea 64($tptr),$tptr
  1478. jmp .Lsqr4x_shift_n_add
  1479. .align 32
  1480. .Lsqr4x_shift_n_add:
  1481. lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
  1482. shr \$63,$A0[0]
  1483. lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
  1484. shr \$63,$A0[1]
  1485. or $A0[0],$S[1] # | t[2*i]>>63
  1486. mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
  1487. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1488. mul %rax # a[i]*a[i]
  1489. neg $carry # mov $carry,cf
  1490. mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1491. adc %rax,$S[0]
  1492. mov -8($aptr,$i),%rax # a[i+1] # prefetch
  1493. mov $S[0],-32($tptr)
  1494. adc %rdx,$S[1]
  1495. lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
  1496. mov $S[1],-24($tptr)
  1497. sbb $carry,$carry # mov cf,$carry
  1498. shr \$63,$A0[0]
  1499. lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
  1500. shr \$63,$A0[1]
  1501. or $A0[0],$S[3] # | t[2*i]>>63
  1502. mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
  1503. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1504. mul %rax # a[i]*a[i]
  1505. neg $carry # mov $carry,cf
  1506. mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1507. adc %rax,$S[2]
  1508. mov 0($aptr,$i),%rax # a[i+1] # prefetch
  1509. mov $S[2],-16($tptr)
  1510. adc %rdx,$S[3]
  1511. lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
  1512. mov $S[3],-8($tptr)
  1513. sbb $carry,$carry # mov cf,$carry
  1514. shr \$63,$A0[0]
  1515. lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
  1516. shr \$63,$A0[1]
  1517. or $A0[0],$S[1] # | t[2*i]>>63
  1518. mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
  1519. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1520. mul %rax # a[i]*a[i]
  1521. neg $carry # mov $carry,cf
  1522. mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1523. adc %rax,$S[0]
  1524. mov 8($aptr,$i),%rax # a[i+1] # prefetch
  1525. mov $S[0],0($tptr)
  1526. adc %rdx,$S[1]
  1527. lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
  1528. mov $S[1],8($tptr)
  1529. sbb $carry,$carry # mov cf,$carry
  1530. shr \$63,$A0[0]
  1531. lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
  1532. shr \$63,$A0[1]
  1533. or $A0[0],$S[3] # | t[2*i]>>63
  1534. mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
  1535. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1536. mul %rax # a[i]*a[i]
  1537. neg $carry # mov $carry,cf
  1538. mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1539. adc %rax,$S[2]
  1540. mov 16($aptr,$i),%rax # a[i+1] # prefetch
  1541. mov $S[2],16($tptr)
  1542. adc %rdx,$S[3]
  1543. mov $S[3],24($tptr)
  1544. sbb $carry,$carry # mov cf,$carry
  1545. lea 64($tptr),$tptr
  1546. add \$32,$i
  1547. jnz .Lsqr4x_shift_n_add
  1548. lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
  1549. .byte 0x67
  1550. shr \$63,$A0[0]
  1551. lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
  1552. shr \$63,$A0[1]
  1553. or $A0[0],$S[1] # | t[2*i]>>63
  1554. mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
  1555. mov $A0[1],$shift # shift=t[2*i+1]>>63
  1556. mul %rax # a[i]*a[i]
  1557. neg $carry # mov $carry,cf
  1558. mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
  1559. adc %rax,$S[0]
  1560. mov -8($aptr),%rax # a[i+1] # prefetch
  1561. mov $S[0],-32($tptr)
  1562. adc %rdx,$S[1]
  1563. lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
  1564. mov $S[1],-24($tptr)
  1565. sbb $carry,$carry # mov cf,$carry
  1566. shr \$63,$A0[0]
  1567. lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
  1568. shr \$63,$A0[1]
  1569. or $A0[0],$S[3] # | t[2*i]>>63
  1570. mul %rax # a[i]*a[i]
  1571. neg $carry # mov $carry,cf
  1572. adc %rax,$S[2]
  1573. adc %rdx,$S[3]
  1574. mov $S[2],-16($tptr)
  1575. mov $S[3],-8($tptr)
  1576. ___
  1577. }
  1578. ######################################################################
  1579. # Montgomery reduction part, "word-by-word" algorithm.
  1580. #
  1581. # This new path is inspired by multiple submissions from Intel, by
  1582. # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
  1583. # Vinodh Gopal...
  1584. {
  1585. my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
  1586. $code.=<<___;
  1587. movq %xmm2,$nptr
  1588. __bn_sqr8x_reduction:
  1589. xor %rax,%rax
  1590. lea ($nptr,$num),%rcx # end of n[]
  1591. lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
  1592. mov %rcx,0+8(%rsp)
  1593. lea 48+8(%rsp,$num),$tptr # end of initial t[] window
  1594. mov %rdx,8+8(%rsp)
  1595. neg $num
  1596. jmp .L8x_reduction_loop
  1597. .align 32
  1598. .L8x_reduction_loop:
  1599. lea ($tptr,$num),$tptr # start of current t[] window
  1600. .byte 0x66
  1601. mov 8*0($tptr),$m0
  1602. mov 8*1($tptr),%r9
  1603. mov 8*2($tptr),%r10
  1604. mov 8*3($tptr),%r11
  1605. mov 8*4($tptr),%r12
  1606. mov 8*5($tptr),%r13
  1607. mov 8*6($tptr),%r14
  1608. mov 8*7($tptr),%r15
  1609. mov %rax,(%rdx) # store top-most carry bit
  1610. lea 8*8($tptr),$tptr
  1611. .byte 0x67
  1612. mov $m0,%r8
  1613. imulq 32+8(%rsp),$m0 # n0*a[0]
  1614. mov 8*0($nptr),%rax # n[0]
  1615. mov \$8,%ecx
  1616. jmp .L8x_reduce
  1617. .align 32
  1618. .L8x_reduce:
  1619. mulq $m0
  1620. mov 8*1($nptr),%rax # n[1]
  1621. neg %r8
  1622. mov %rdx,%r8
  1623. adc \$0,%r8
  1624. mulq $m0
  1625. add %rax,%r9
  1626. mov 8*2($nptr),%rax
  1627. adc \$0,%rdx
  1628. add %r9,%r8
  1629. mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
  1630. mov %rdx,%r9
  1631. adc \$0,%r9
  1632. mulq $m0
  1633. add %rax,%r10
  1634. mov 8*3($nptr),%rax
  1635. adc \$0,%rdx
  1636. add %r10,%r9
  1637. mov 32+8(%rsp),$carry # pull n0, borrow $carry
  1638. mov %rdx,%r10
  1639. adc \$0,%r10
  1640. mulq $m0
  1641. add %rax,%r11
  1642. mov 8*4($nptr),%rax
  1643. adc \$0,%rdx
  1644. imulq %r8,$carry # modulo-scheduled
  1645. add %r11,%r10
  1646. mov %rdx,%r11
  1647. adc \$0,%r11
  1648. mulq $m0
  1649. add %rax,%r12
  1650. mov 8*5($nptr),%rax
  1651. adc \$0,%rdx
  1652. add %r12,%r11
  1653. mov %rdx,%r12
  1654. adc \$0,%r12
  1655. mulq $m0
  1656. add %rax,%r13
  1657. mov 8*6($nptr),%rax
  1658. adc \$0,%rdx
  1659. add %r13,%r12
  1660. mov %rdx,%r13
  1661. adc \$0,%r13
  1662. mulq $m0
  1663. add %rax,%r14
  1664. mov 8*7($nptr),%rax
  1665. adc \$0,%rdx
  1666. add %r14,%r13
  1667. mov %rdx,%r14
  1668. adc \$0,%r14
  1669. mulq $m0
  1670. mov $carry,$m0 # n0*a[i]
  1671. add %rax,%r15
  1672. mov 8*0($nptr),%rax # n[0]
  1673. adc \$0,%rdx
  1674. add %r15,%r14
  1675. mov %rdx,%r15
  1676. adc \$0,%r15
  1677. dec %ecx
  1678. jnz .L8x_reduce
  1679. lea 8*8($nptr),$nptr
  1680. xor %rax,%rax
  1681. mov 8+8(%rsp),%rdx # pull end of t[]
  1682. cmp 0+8(%rsp),$nptr # end of n[]?
  1683. jae .L8x_no_tail
  1684. .byte 0x66
  1685. add 8*0($tptr),%r8
  1686. adc 8*1($tptr),%r9
  1687. adc 8*2($tptr),%r10
  1688. adc 8*3($tptr),%r11
  1689. adc 8*4($tptr),%r12
  1690. adc 8*5($tptr),%r13
  1691. adc 8*6($tptr),%r14
  1692. adc 8*7($tptr),%r15
  1693. sbb $carry,$carry # top carry
  1694. mov 48+56+8(%rsp),$m0 # pull n0*a[0]
  1695. mov \$8,%ecx
  1696. mov 8*0($nptr),%rax
  1697. jmp .L8x_tail
  1698. .align 32
  1699. .L8x_tail:
  1700. mulq $m0
  1701. add %rax,%r8
  1702. mov 8*1($nptr),%rax
  1703. mov %r8,($tptr) # save result
  1704. mov %rdx,%r8
  1705. adc \$0,%r8
  1706. mulq $m0
  1707. add %rax,%r9
  1708. mov 8*2($nptr),%rax
  1709. adc \$0,%rdx
  1710. add %r9,%r8
  1711. lea 8($tptr),$tptr # $tptr++
  1712. mov %rdx,%r9
  1713. adc \$0,%r9
  1714. mulq $m0
  1715. add %rax,%r10
  1716. mov 8*3($nptr),%rax
  1717. adc \$0,%rdx
  1718. add %r10,%r9
  1719. mov %rdx,%r10
  1720. adc \$0,%r10
  1721. mulq $m0
  1722. add %rax,%r11
  1723. mov 8*4($nptr),%rax
  1724. adc \$0,%rdx
  1725. add %r11,%r10
  1726. mov %rdx,%r11
  1727. adc \$0,%r11
  1728. mulq $m0
  1729. add %rax,%r12
  1730. mov 8*5($nptr),%rax
  1731. adc \$0,%rdx
  1732. add %r12,%r11
  1733. mov %rdx,%r12
  1734. adc \$0,%r12
  1735. mulq $m0
  1736. add %rax,%r13
  1737. mov 8*6($nptr),%rax
  1738. adc \$0,%rdx
  1739. add %r13,%r12
  1740. mov %rdx,%r13
  1741. adc \$0,%r13
  1742. mulq $m0
  1743. add %rax,%r14
  1744. mov 8*7($nptr),%rax
  1745. adc \$0,%rdx
  1746. add %r14,%r13
  1747. mov %rdx,%r14
  1748. adc \$0,%r14
  1749. mulq $m0
  1750. mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
  1751. add %rax,%r15
  1752. adc \$0,%rdx
  1753. add %r15,%r14
  1754. mov 8*0($nptr),%rax # pull n[0]
  1755. mov %rdx,%r15
  1756. adc \$0,%r15
  1757. dec %ecx
  1758. jnz .L8x_tail
  1759. lea 8*8($nptr),$nptr
  1760. mov 8+8(%rsp),%rdx # pull end of t[]
  1761. cmp 0+8(%rsp),$nptr # end of n[]?
  1762. jae .L8x_tail_done # break out of loop
  1763. mov 48+56+8(%rsp),$m0 # pull n0*a[0]
  1764. neg $carry
  1765. mov 8*0($nptr),%rax # pull n[0]
  1766. adc 8*0($tptr),%r8
  1767. adc 8*1($tptr),%r9
  1768. adc 8*2($tptr),%r10
  1769. adc 8*3($tptr),%r11
  1770. adc 8*4($tptr),%r12
  1771. adc 8*5($tptr),%r13
  1772. adc 8*6($tptr),%r14
  1773. adc 8*7($tptr),%r15
  1774. sbb $carry,$carry # top carry
  1775. mov \$8,%ecx
  1776. jmp .L8x_tail
  1777. .align 32
  1778. .L8x_tail_done:
  1779. xor %rax,%rax
  1780. add (%rdx),%r8 # can this overflow?
  1781. adc \$0,%r9
  1782. adc \$0,%r10
  1783. adc \$0,%r11
  1784. adc \$0,%r12
  1785. adc \$0,%r13
  1786. adc \$0,%r14
  1787. adc \$0,%r15
  1788. adc \$0,%rax
  1789. neg $carry
  1790. .L8x_no_tail:
  1791. adc 8*0($tptr),%r8
  1792. adc 8*1($tptr),%r9
  1793. adc 8*2($tptr),%r10
  1794. adc 8*3($tptr),%r11
  1795. adc 8*4($tptr),%r12
  1796. adc 8*5($tptr),%r13
  1797. adc 8*6($tptr),%r14
  1798. adc 8*7($tptr),%r15
  1799. adc \$0,%rax # top-most carry
  1800. mov -8($nptr),%rcx # np[num-1]
  1801. xor $carry,$carry
  1802. movq %xmm2,$nptr # restore $nptr
  1803. mov %r8,8*0($tptr) # store top 512 bits
  1804. mov %r9,8*1($tptr)
  1805. movq %xmm3,$num # $num is %r9, can't be moved upwards
  1806. mov %r10,8*2($tptr)
  1807. mov %r11,8*3($tptr)
  1808. mov %r12,8*4($tptr)
  1809. mov %r13,8*5($tptr)
  1810. mov %r14,8*6($tptr)
  1811. mov %r15,8*7($tptr)
  1812. lea 8*8($tptr),$tptr
  1813. cmp %rdx,$tptr # end of t[]?
  1814. jb .L8x_reduction_loop
  1815. ret
  1816. .cfi_endproc
  1817. .size bn_sqr8x_internal,.-bn_sqr8x_internal
  1818. ___
  1819. }
  1820. ##############################################################
  1821. # Post-condition, 4x unrolled
  1822. #
  1823. {
  1824. my ($tptr,$nptr)=("%rbx","%rbp");
  1825. $code.=<<___;
  1826. .type __bn_post4x_internal,\@abi-omnipotent
  1827. .align 32
  1828. __bn_post4x_internal:
  1829. .cfi_startproc
  1830. mov 8*0($nptr),%r12
  1831. lea (%rdi,$num),$tptr # %rdi was $tptr above
  1832. mov $num,%rcx
  1833. movq %xmm1,$rptr # restore $rptr
  1834. neg %rax
  1835. movq %xmm1,$aptr # prepare for back-to-back call
  1836. sar \$3+2,%rcx
  1837. dec %r12 # so that after 'not' we get -n[0]
  1838. xor %r10,%r10
  1839. mov 8*1($nptr),%r13
  1840. mov 8*2($nptr),%r14
  1841. mov 8*3($nptr),%r15
  1842. jmp .Lsqr4x_sub_entry
  1843. .align 16
  1844. .Lsqr4x_sub:
  1845. mov 8*0($nptr),%r12
  1846. mov 8*1($nptr),%r13
  1847. mov 8*2($nptr),%r14
  1848. mov 8*3($nptr),%r15
  1849. .Lsqr4x_sub_entry:
  1850. lea 8*4($nptr),$nptr
  1851. not %r12
  1852. not %r13
  1853. not %r14
  1854. not %r15
  1855. and %rax,%r12
  1856. and %rax,%r13
  1857. and %rax,%r14
  1858. and %rax,%r15
  1859. neg %r10 # mov %r10,%cf
  1860. adc 8*0($tptr),%r12
  1861. adc 8*1($tptr),%r13
  1862. adc 8*2($tptr),%r14
  1863. adc 8*3($tptr),%r15
  1864. mov %r12,8*0($rptr)
  1865. lea 8*4($tptr),$tptr
  1866. mov %r13,8*1($rptr)
  1867. sbb %r10,%r10 # mov %cf,%r10
  1868. mov %r14,8*2($rptr)
  1869. mov %r15,8*3($rptr)
  1870. lea 8*4($rptr),$rptr
  1871. inc %rcx # pass %cf
  1872. jnz .Lsqr4x_sub
  1873. mov $num,%r10 # prepare for back-to-back call
  1874. neg $num # restore $num
  1875. ret
  1876. .cfi_endproc
  1877. .size __bn_post4x_internal,.-__bn_post4x_internal
  1878. ___
  1879. }
  1880. {
  1881. $code.=<<___;
  1882. .globl bn_from_montgomery
  1883. .type bn_from_montgomery,\@abi-omnipotent
  1884. .align 32
  1885. bn_from_montgomery:
  1886. .cfi_startproc
  1887. testl \$7,`($win64?"48(%rsp)":"%r9d")`
  1888. jz bn_from_mont8x
  1889. xor %eax,%eax
  1890. ret
  1891. .cfi_endproc
  1892. .size bn_from_montgomery,.-bn_from_montgomery
  1893. .type bn_from_mont8x,\@function,6
  1894. .align 32
  1895. bn_from_mont8x:
  1896. .cfi_startproc
  1897. .byte 0x67
  1898. mov %rsp,%rax
  1899. .cfi_def_cfa_register %rax
  1900. push %rbx
  1901. .cfi_push %rbx
  1902. push %rbp
  1903. .cfi_push %rbp
  1904. push %r12
  1905. .cfi_push %r12
  1906. push %r13
  1907. .cfi_push %r13
  1908. push %r14
  1909. .cfi_push %r14
  1910. push %r15
  1911. .cfi_push %r15
  1912. .Lfrom_prologue:
  1913. shl \$3,${num}d # convert $num to bytes
  1914. lea ($num,$num,2),%r10 # 3*$num in bytes
  1915. neg $num
  1916. mov ($n0),$n0 # *n0
  1917. ##############################################################
  1918. # Ensure that stack frame doesn't alias with $rptr+3*$num
  1919. # modulo 4096, which covers ret[num], am[num] and n[num]
  1920. # (see bn_exp.c). The stack is allocated to aligned with
  1921. # bn_power5's frame, and as bn_from_montgomery happens to be
  1922. # last operation, we use the opportunity to cleanse it.
  1923. #
  1924. lea -320(%rsp,$num,2),%r11
  1925. mov %rsp,%rbp
  1926. sub $rptr,%r11
  1927. and \$4095,%r11
  1928. cmp %r11,%r10
  1929. jb .Lfrom_sp_alt
  1930. sub %r11,%rbp # align with $aptr
  1931. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
  1932. jmp .Lfrom_sp_done
  1933. .align 32
  1934. .Lfrom_sp_alt:
  1935. lea 4096-320(,$num,2),%r10
  1936. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
  1937. sub %r10,%r11
  1938. mov \$0,%r10
  1939. cmovc %r10,%r11
  1940. sub %r11,%rbp
  1941. .Lfrom_sp_done:
  1942. and \$-64,%rbp
  1943. mov %rsp,%r11
  1944. sub %rbp,%r11
  1945. and \$-4096,%r11
  1946. lea (%rbp,%r11),%rsp
  1947. mov (%rsp),%r10
  1948. cmp %rbp,%rsp
  1949. ja .Lfrom_page_walk
  1950. jmp .Lfrom_page_walk_done
  1951. .Lfrom_page_walk:
  1952. lea -4096(%rsp),%rsp
  1953. mov (%rsp),%r10
  1954. cmp %rbp,%rsp
  1955. ja .Lfrom_page_walk
  1956. .Lfrom_page_walk_done:
  1957. mov $num,%r10
  1958. neg $num
  1959. ##############################################################
  1960. # Stack layout
  1961. #
  1962. # +0 saved $num, used in reduction section
  1963. # +8 &t[2*$num], used in reduction section
  1964. # +32 saved *n0
  1965. # +40 saved %rsp
  1966. # +48 t[2*$num]
  1967. #
  1968. mov $n0, 32(%rsp)
  1969. mov %rax, 40(%rsp) # save original %rsp
  1970. .cfi_cfa_expression %rsp+40,deref,+8
  1971. .Lfrom_body:
  1972. mov $num,%r11
  1973. lea 48(%rsp),%rax
  1974. pxor %xmm0,%xmm0
  1975. jmp .Lmul_by_1
  1976. .align 32
  1977. .Lmul_by_1:
  1978. movdqu ($aptr),%xmm1
  1979. movdqu 16($aptr),%xmm2
  1980. movdqu 32($aptr),%xmm3
  1981. movdqa %xmm0,(%rax,$num)
  1982. movdqu 48($aptr),%xmm4
  1983. movdqa %xmm0,16(%rax,$num)
  1984. .byte 0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
  1985. movdqa %xmm1,(%rax)
  1986. movdqa %xmm0,32(%rax,$num)
  1987. movdqa %xmm2,16(%rax)
  1988. movdqa %xmm0,48(%rax,$num)
  1989. movdqa %xmm3,32(%rax)
  1990. movdqa %xmm4,48(%rax)
  1991. lea 64(%rax),%rax
  1992. sub \$64,%r11
  1993. jnz .Lmul_by_1
  1994. movq $rptr,%xmm1
  1995. movq $nptr,%xmm2
  1996. .byte 0x67
  1997. mov $nptr,%rbp
  1998. movq %r10, %xmm3 # -num
  1999. ___
  2000. $code.=<<___ if ($addx);
  2001. mov OPENSSL_ia32cap_P+8(%rip),%r11d
  2002. and \$0x80108,%r11d
  2003. cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
  2004. jne .Lfrom_mont_nox
  2005. lea (%rax,$num),$rptr
  2006. call __bn_sqrx8x_reduction
  2007. call __bn_postx4x_internal
  2008. pxor %xmm0,%xmm0
  2009. lea 48(%rsp),%rax
  2010. jmp .Lfrom_mont_zero
  2011. .align 32
  2012. .Lfrom_mont_nox:
  2013. ___
  2014. $code.=<<___;
  2015. call __bn_sqr8x_reduction
  2016. call __bn_post4x_internal
  2017. pxor %xmm0,%xmm0
  2018. lea 48(%rsp),%rax
  2019. jmp .Lfrom_mont_zero
  2020. .align 32
  2021. .Lfrom_mont_zero:
  2022. mov 40(%rsp),%rsi # restore %rsp
  2023. .cfi_def_cfa %rsi,8
  2024. movdqa %xmm0,16*0(%rax)
  2025. movdqa %xmm0,16*1(%rax)
  2026. movdqa %xmm0,16*2(%rax)
  2027. movdqa %xmm0,16*3(%rax)
  2028. lea 16*4(%rax),%rax
  2029. sub \$32,$num
  2030. jnz .Lfrom_mont_zero
  2031. mov \$1,%rax
  2032. mov -48(%rsi),%r15
  2033. .cfi_restore %r15
  2034. mov -40(%rsi),%r14
  2035. .cfi_restore %r14
  2036. mov -32(%rsi),%r13
  2037. .cfi_restore %r13
  2038. mov -24(%rsi),%r12
  2039. .cfi_restore %r12
  2040. mov -16(%rsi),%rbp
  2041. .cfi_restore %rbp
  2042. mov -8(%rsi),%rbx
  2043. .cfi_restore %rbx
  2044. lea (%rsi),%rsp
  2045. .cfi_def_cfa_register %rsp
  2046. .Lfrom_epilogue:
  2047. ret
  2048. .cfi_endproc
  2049. .size bn_from_mont8x,.-bn_from_mont8x
  2050. ___
  2051. }
  2052. }}}
  2053. if ($addx) {{{
  2054. my $bp="%rdx"; # restore original value
  2055. $code.=<<___;
  2056. .type bn_mulx4x_mont_gather5,\@function,6
  2057. .align 32
  2058. bn_mulx4x_mont_gather5:
  2059. .cfi_startproc
  2060. mov %rsp,%rax
  2061. .cfi_def_cfa_register %rax
  2062. .Lmulx4x_enter:
  2063. push %rbx
  2064. .cfi_push %rbx
  2065. push %rbp
  2066. .cfi_push %rbp
  2067. push %r12
  2068. .cfi_push %r12
  2069. push %r13
  2070. .cfi_push %r13
  2071. push %r14
  2072. .cfi_push %r14
  2073. push %r15
  2074. .cfi_push %r15
  2075. .Lmulx4x_prologue:
  2076. shl \$3,${num}d # convert $num to bytes
  2077. lea ($num,$num,2),%r10 # 3*$num in bytes
  2078. neg $num # -$num
  2079. mov ($n0),$n0 # *n0
  2080. ##############################################################
  2081. # Ensure that stack frame doesn't alias with $rptr+3*$num
  2082. # modulo 4096, which covers ret[num], am[num] and n[num]
  2083. # (see bn_exp.c). This is done to allow memory disambiguation
  2084. # logic do its magic. [Extra [num] is allocated in order
  2085. # to align with bn_power5's frame, which is cleansed after
  2086. # completing exponentiation. Extra 256 bytes is for power mask
  2087. # calculated from 7th argument, the index.]
  2088. #
  2089. lea -320(%rsp,$num,2),%r11
  2090. mov %rsp,%rbp
  2091. sub $rp,%r11
  2092. and \$4095,%r11
  2093. cmp %r11,%r10
  2094. jb .Lmulx4xsp_alt
  2095. sub %r11,%rbp # align with $aptr
  2096. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
  2097. jmp .Lmulx4xsp_done
  2098. .Lmulx4xsp_alt:
  2099. lea 4096-320(,$num,2),%r10
  2100. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
  2101. sub %r10,%r11
  2102. mov \$0,%r10
  2103. cmovc %r10,%r11
  2104. sub %r11,%rbp
  2105. .Lmulx4xsp_done:
  2106. and \$-64,%rbp # ensure alignment
  2107. mov %rsp,%r11
  2108. sub %rbp,%r11
  2109. and \$-4096,%r11
  2110. lea (%rbp,%r11),%rsp
  2111. mov (%rsp),%r10
  2112. cmp %rbp,%rsp
  2113. ja .Lmulx4x_page_walk
  2114. jmp .Lmulx4x_page_walk_done
  2115. .Lmulx4x_page_walk:
  2116. lea -4096(%rsp),%rsp
  2117. mov (%rsp),%r10
  2118. cmp %rbp,%rsp
  2119. ja .Lmulx4x_page_walk
  2120. .Lmulx4x_page_walk_done:
  2121. ##############################################################
  2122. # Stack layout
  2123. # +0 -num
  2124. # +8 off-loaded &b[i]
  2125. # +16 end of b[num]
  2126. # +24 inner counter
  2127. # +32 saved n0
  2128. # +40 saved %rsp
  2129. # +48
  2130. # +56 saved rp
  2131. # +64 tmp[num+1]
  2132. #
  2133. mov $n0, 32(%rsp) # save *n0
  2134. mov %rax,40(%rsp) # save original %rsp
  2135. .cfi_cfa_expression %rsp+40,deref,+8
  2136. .Lmulx4x_body:
  2137. call mulx4x_internal
  2138. mov 40(%rsp),%rsi # restore %rsp
  2139. .cfi_def_cfa %rsi,8
  2140. mov \$1,%rax
  2141. mov -48(%rsi),%r15
  2142. .cfi_restore %r15
  2143. mov -40(%rsi),%r14
  2144. .cfi_restore %r14
  2145. mov -32(%rsi),%r13
  2146. .cfi_restore %r13
  2147. mov -24(%rsi),%r12
  2148. .cfi_restore %r12
  2149. mov -16(%rsi),%rbp
  2150. .cfi_restore %rbp
  2151. mov -8(%rsi),%rbx
  2152. .cfi_restore %rbx
  2153. lea (%rsi),%rsp
  2154. .cfi_def_cfa_register %rsp
  2155. .Lmulx4x_epilogue:
  2156. ret
  2157. .cfi_endproc
  2158. .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
  2159. .type mulx4x_internal,\@abi-omnipotent
  2160. .align 32
  2161. mulx4x_internal:
  2162. .cfi_startproc
  2163. mov $num,8(%rsp) # save -$num (it was in bytes)
  2164. mov $num,%r10
  2165. neg $num # restore $num
  2166. shl \$5,$num
  2167. neg %r10 # restore $num
  2168. lea 128($bp,$num),%r13 # end of powers table (+size optimization)
  2169. shr \$5+5,$num
  2170. movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
  2171. sub \$1,$num
  2172. lea .Linc(%rip),%rax
  2173. mov %r13,16+8(%rsp) # end of b[num]
  2174. mov $num,24+8(%rsp) # inner counter
  2175. mov $rp, 56+8(%rsp) # save $rp
  2176. ___
  2177. my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
  2178. ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
  2179. my $rptr=$bptr;
  2180. my $STRIDE=2**5*8; # 5 is "window size"
  2181. my $N=$STRIDE/4; # should match cache line size
  2182. $code.=<<___;
  2183. movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
  2184. movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
  2185. lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimization)
  2186. lea 128($bp),$bptr # size optimization
  2187. pshufd \$0,%xmm5,%xmm5 # broadcast index
  2188. movdqa %xmm1,%xmm4
  2189. .byte 0x67
  2190. movdqa %xmm1,%xmm2
  2191. ___
  2192. ########################################################################
  2193. # calculate mask by comparing 0..31 to index and save result to stack
  2194. #
  2195. $code.=<<___;
  2196. .byte 0x67
  2197. paddd %xmm0,%xmm1
  2198. pcmpeqd %xmm5,%xmm0 # compare to 1,0
  2199. movdqa %xmm4,%xmm3
  2200. ___
  2201. for($i=0;$i<$STRIDE/16-4;$i+=4) {
  2202. $code.=<<___;
  2203. paddd %xmm1,%xmm2
  2204. pcmpeqd %xmm5,%xmm1 # compare to 3,2
  2205. movdqa %xmm0,`16*($i+0)+112`(%r10)
  2206. movdqa %xmm4,%xmm0
  2207. paddd %xmm2,%xmm3
  2208. pcmpeqd %xmm5,%xmm2 # compare to 5,4
  2209. movdqa %xmm1,`16*($i+1)+112`(%r10)
  2210. movdqa %xmm4,%xmm1
  2211. paddd %xmm3,%xmm0
  2212. pcmpeqd %xmm5,%xmm3 # compare to 7,6
  2213. movdqa %xmm2,`16*($i+2)+112`(%r10)
  2214. movdqa %xmm4,%xmm2
  2215. paddd %xmm0,%xmm1
  2216. pcmpeqd %xmm5,%xmm0
  2217. movdqa %xmm3,`16*($i+3)+112`(%r10)
  2218. movdqa %xmm4,%xmm3
  2219. ___
  2220. }
  2221. $code.=<<___; # last iteration can be optimized
  2222. .byte 0x67
  2223. paddd %xmm1,%xmm2
  2224. pcmpeqd %xmm5,%xmm1
  2225. movdqa %xmm0,`16*($i+0)+112`(%r10)
  2226. paddd %xmm2,%xmm3
  2227. pcmpeqd %xmm5,%xmm2
  2228. movdqa %xmm1,`16*($i+1)+112`(%r10)
  2229. pcmpeqd %xmm5,%xmm3
  2230. movdqa %xmm2,`16*($i+2)+112`(%r10)
  2231. pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
  2232. pand `16*($i+1)-128`($bptr),%xmm1
  2233. pand `16*($i+2)-128`($bptr),%xmm2
  2234. movdqa %xmm3,`16*($i+3)+112`(%r10)
  2235. pand `16*($i+3)-128`($bptr),%xmm3
  2236. por %xmm2,%xmm0
  2237. por %xmm3,%xmm1
  2238. ___
  2239. for($i=0;$i<$STRIDE/16-4;$i+=4) {
  2240. $code.=<<___;
  2241. movdqa `16*($i+0)-128`($bptr),%xmm4
  2242. movdqa `16*($i+1)-128`($bptr),%xmm5
  2243. movdqa `16*($i+2)-128`($bptr),%xmm2
  2244. pand `16*($i+0)+112`(%r10),%xmm4
  2245. movdqa `16*($i+3)-128`($bptr),%xmm3
  2246. pand `16*($i+1)+112`(%r10),%xmm5
  2247. por %xmm4,%xmm0
  2248. pand `16*($i+2)+112`(%r10),%xmm2
  2249. por %xmm5,%xmm1
  2250. pand `16*($i+3)+112`(%r10),%xmm3
  2251. por %xmm2,%xmm0
  2252. por %xmm3,%xmm1
  2253. ___
  2254. }
  2255. $code.=<<___;
  2256. pxor %xmm1,%xmm0
  2257. pshufd \$0x4e,%xmm0,%xmm1
  2258. por %xmm1,%xmm0
  2259. lea $STRIDE($bptr),$bptr
  2260. movq %xmm0,%rdx # bp[0]
  2261. lea 64+8*4+8(%rsp),$tptr
  2262. mov %rdx,$bi
  2263. mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
  2264. mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
  2265. add %rax,%r11
  2266. mulx 2*8($aptr),%rax,%r13 # ...
  2267. adc %rax,%r12
  2268. adc \$0,%r13
  2269. mulx 3*8($aptr),%rax,%r14
  2270. mov $mi,%r15
  2271. imulq 32+8(%rsp),$mi # "t[0]"*n0
  2272. xor $zero,$zero # cf=0, of=0
  2273. mov $mi,%rdx
  2274. mov $bptr,8+8(%rsp) # off-load &b[i]
  2275. lea 4*8($aptr),$aptr
  2276. adcx %rax,%r13
  2277. adcx $zero,%r14 # cf=0
  2278. mulx 0*8($nptr),%rax,%r10
  2279. adcx %rax,%r15 # discarded
  2280. adox %r11,%r10
  2281. mulx 1*8($nptr),%rax,%r11
  2282. adcx %rax,%r10
  2283. adox %r12,%r11
  2284. mulx 2*8($nptr),%rax,%r12
  2285. mov 24+8(%rsp),$bptr # counter value
  2286. mov %r10,-8*4($tptr)
  2287. adcx %rax,%r11
  2288. adox %r13,%r12
  2289. mulx 3*8($nptr),%rax,%r15
  2290. mov $bi,%rdx
  2291. mov %r11,-8*3($tptr)
  2292. adcx %rax,%r12
  2293. adox $zero,%r15 # of=0
  2294. lea 4*8($nptr),$nptr
  2295. mov %r12,-8*2($tptr)
  2296. jmp .Lmulx4x_1st
  2297. .align 32
  2298. .Lmulx4x_1st:
  2299. adcx $zero,%r15 # cf=0, modulo-scheduled
  2300. mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
  2301. adcx %r14,%r10
  2302. mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
  2303. adcx %rax,%r11
  2304. mulx 2*8($aptr),%r12,%rax # ...
  2305. adcx %r14,%r12
  2306. mulx 3*8($aptr),%r13,%r14
  2307. .byte 0x67,0x67
  2308. mov $mi,%rdx
  2309. adcx %rax,%r13
  2310. adcx $zero,%r14 # cf=0
  2311. lea 4*8($aptr),$aptr
  2312. lea 4*8($tptr),$tptr
  2313. adox %r15,%r10
  2314. mulx 0*8($nptr),%rax,%r15
  2315. adcx %rax,%r10
  2316. adox %r15,%r11
  2317. mulx 1*8($nptr),%rax,%r15
  2318. adcx %rax,%r11
  2319. adox %r15,%r12
  2320. mulx 2*8($nptr),%rax,%r15
  2321. mov %r10,-5*8($tptr)
  2322. adcx %rax,%r12
  2323. mov %r11,-4*8($tptr)
  2324. adox %r15,%r13
  2325. mulx 3*8($nptr),%rax,%r15
  2326. mov $bi,%rdx
  2327. mov %r12,-3*8($tptr)
  2328. adcx %rax,%r13
  2329. adox $zero,%r15
  2330. lea 4*8($nptr),$nptr
  2331. mov %r13,-2*8($tptr)
  2332. dec $bptr # of=0, pass cf
  2333. jnz .Lmulx4x_1st
  2334. mov 8(%rsp),$num # load -num
  2335. adc $zero,%r15 # modulo-scheduled
  2336. lea ($aptr,$num),$aptr # rewind $aptr
  2337. add %r15,%r14
  2338. mov 8+8(%rsp),$bptr # re-load &b[i]
  2339. adc $zero,$zero # top-most carry
  2340. mov %r14,-1*8($tptr)
  2341. jmp .Lmulx4x_outer
  2342. .align 32
  2343. .Lmulx4x_outer:
  2344. lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
  2345. pxor %xmm4,%xmm4
  2346. .byte 0x67,0x67
  2347. pxor %xmm5,%xmm5
  2348. ___
  2349. for($i=0;$i<$STRIDE/16;$i+=4) {
  2350. $code.=<<___;
  2351. movdqa `16*($i+0)-128`($bptr),%xmm0
  2352. movdqa `16*($i+1)-128`($bptr),%xmm1
  2353. movdqa `16*($i+2)-128`($bptr),%xmm2
  2354. pand `16*($i+0)+256`(%r10),%xmm0
  2355. movdqa `16*($i+3)-128`($bptr),%xmm3
  2356. pand `16*($i+1)+256`(%r10),%xmm1
  2357. por %xmm0,%xmm4
  2358. pand `16*($i+2)+256`(%r10),%xmm2
  2359. por %xmm1,%xmm5
  2360. pand `16*($i+3)+256`(%r10),%xmm3
  2361. por %xmm2,%xmm4
  2362. por %xmm3,%xmm5
  2363. ___
  2364. }
  2365. $code.=<<___;
  2366. por %xmm5,%xmm4
  2367. pshufd \$0x4e,%xmm4,%xmm0
  2368. por %xmm4,%xmm0
  2369. lea $STRIDE($bptr),$bptr
  2370. movq %xmm0,%rdx # m0=bp[i]
  2371. mov $zero,($tptr) # save top-most carry
  2372. lea 4*8($tptr,$num),$tptr # rewind $tptr
  2373. mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
  2374. xor $zero,$zero # cf=0, of=0
  2375. mov %rdx,$bi
  2376. mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
  2377. adox -4*8($tptr),$mi # +t[0]
  2378. adcx %r14,%r11
  2379. mulx 2*8($aptr),%r15,%r13 # ...
  2380. adox -3*8($tptr),%r11
  2381. adcx %r15,%r12
  2382. mulx 3*8($aptr),%rdx,%r14
  2383. adox -2*8($tptr),%r12
  2384. adcx %rdx,%r13
  2385. lea ($nptr,$num),$nptr # rewind $nptr
  2386. lea 4*8($aptr),$aptr
  2387. adox -1*8($tptr),%r13
  2388. adcx $zero,%r14
  2389. adox $zero,%r14
  2390. mov $mi,%r15
  2391. imulq 32+8(%rsp),$mi # "t[0]"*n0
  2392. mov $mi,%rdx
  2393. xor $zero,$zero # cf=0, of=0
  2394. mov $bptr,8+8(%rsp) # off-load &b[i]
  2395. mulx 0*8($nptr),%rax,%r10
  2396. adcx %rax,%r15 # discarded
  2397. adox %r11,%r10
  2398. mulx 1*8($nptr),%rax,%r11
  2399. adcx %rax,%r10
  2400. adox %r12,%r11
  2401. mulx 2*8($nptr),%rax,%r12
  2402. adcx %rax,%r11
  2403. adox %r13,%r12
  2404. mulx 3*8($nptr),%rax,%r15
  2405. mov $bi,%rdx
  2406. mov 24+8(%rsp),$bptr # counter value
  2407. mov %r10,-8*4($tptr)
  2408. adcx %rax,%r12
  2409. mov %r11,-8*3($tptr)
  2410. adox $zero,%r15 # of=0
  2411. mov %r12,-8*2($tptr)
  2412. lea 4*8($nptr),$nptr
  2413. jmp .Lmulx4x_inner
  2414. .align 32
  2415. .Lmulx4x_inner:
  2416. mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
  2417. adcx $zero,%r15 # cf=0, modulo-scheduled
  2418. adox %r14,%r10
  2419. mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
  2420. adcx 0*8($tptr),%r10
  2421. adox %rax,%r11
  2422. mulx 2*8($aptr),%r12,%rax # ...
  2423. adcx 1*8($tptr),%r11
  2424. adox %r14,%r12
  2425. mulx 3*8($aptr),%r13,%r14
  2426. mov $mi,%rdx
  2427. adcx 2*8($tptr),%r12
  2428. adox %rax,%r13
  2429. adcx 3*8($tptr),%r13
  2430. adox $zero,%r14 # of=0
  2431. lea 4*8($aptr),$aptr
  2432. lea 4*8($tptr),$tptr
  2433. adcx $zero,%r14 # cf=0
  2434. adox %r15,%r10
  2435. mulx 0*8($nptr),%rax,%r15
  2436. adcx %rax,%r10
  2437. adox %r15,%r11
  2438. mulx 1*8($nptr),%rax,%r15
  2439. adcx %rax,%r11
  2440. adox %r15,%r12
  2441. mulx 2*8($nptr),%rax,%r15
  2442. mov %r10,-5*8($tptr)
  2443. adcx %rax,%r12
  2444. adox %r15,%r13
  2445. mov %r11,-4*8($tptr)
  2446. mulx 3*8($nptr),%rax,%r15
  2447. mov $bi,%rdx
  2448. lea 4*8($nptr),$nptr
  2449. mov %r12,-3*8($tptr)
  2450. adcx %rax,%r13
  2451. adox $zero,%r15
  2452. mov %r13,-2*8($tptr)
  2453. dec $bptr # of=0, pass cf
  2454. jnz .Lmulx4x_inner
  2455. mov 0+8(%rsp),$num # load -num
  2456. adc $zero,%r15 # modulo-scheduled
  2457. sub 0*8($tptr),$bptr # pull top-most carry to %cf
  2458. mov 8+8(%rsp),$bptr # re-load &b[i]
  2459. mov 16+8(%rsp),%r10
  2460. adc %r15,%r14
  2461. lea ($aptr,$num),$aptr # rewind $aptr
  2462. adc $zero,$zero # top-most carry
  2463. mov %r14,-1*8($tptr)
  2464. cmp %r10,$bptr
  2465. jb .Lmulx4x_outer
  2466. mov -8($nptr),%r10
  2467. mov $zero,%r8
  2468. mov ($nptr,$num),%r12
  2469. lea ($nptr,$num),%rbp # rewind $nptr
  2470. mov $num,%rcx
  2471. lea ($tptr,$num),%rdi # rewind $tptr
  2472. xor %eax,%eax
  2473. xor %r15,%r15
  2474. sub %r14,%r10 # compare top-most words
  2475. adc %r15,%r15
  2476. or %r15,%r8
  2477. sar \$3+2,%rcx
  2478. sub %r8,%rax # %rax=-%r8
  2479. mov 56+8(%rsp),%rdx # restore rp
  2480. dec %r12 # so that after 'not' we get -n[0]
  2481. mov 8*1(%rbp),%r13
  2482. xor %r8,%r8
  2483. mov 8*2(%rbp),%r14
  2484. mov 8*3(%rbp),%r15
  2485. jmp .Lsqrx4x_sub_entry # common post-condition
  2486. .cfi_endproc
  2487. .size mulx4x_internal,.-mulx4x_internal
  2488. ___
  2489. } {
  2490. ######################################################################
  2491. # void bn_power5(
  2492. my $rptr="%rdi"; # BN_ULONG *rptr,
  2493. my $aptr="%rsi"; # const BN_ULONG *aptr,
  2494. my $bptr="%rdx"; # const void *table,
  2495. my $nptr="%rcx"; # const BN_ULONG *nptr,
  2496. my $n0 ="%r8"; # const BN_ULONG *n0);
  2497. my $num ="%r9"; # int num, has to be divisible by 8
  2498. # int pwr);
  2499. my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
  2500. my @A0=("%r10","%r11");
  2501. my @A1=("%r12","%r13");
  2502. my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
  2503. $code.=<<___;
  2504. .type bn_powerx5,\@function,6
  2505. .align 32
  2506. bn_powerx5:
  2507. .cfi_startproc
  2508. mov %rsp,%rax
  2509. .cfi_def_cfa_register %rax
  2510. .Lpowerx5_enter:
  2511. push %rbx
  2512. .cfi_push %rbx
  2513. push %rbp
  2514. .cfi_push %rbp
  2515. push %r12
  2516. .cfi_push %r12
  2517. push %r13
  2518. .cfi_push %r13
  2519. push %r14
  2520. .cfi_push %r14
  2521. push %r15
  2522. .cfi_push %r15
  2523. .Lpowerx5_prologue:
  2524. shl \$3,${num}d # convert $num to bytes
  2525. lea ($num,$num,2),%r10 # 3*$num in bytes
  2526. neg $num
  2527. mov ($n0),$n0 # *n0
  2528. ##############################################################
  2529. # Ensure that stack frame doesn't alias with $rptr+3*$num
  2530. # modulo 4096, which covers ret[num], am[num] and n[num]
  2531. # (see bn_exp.c). This is done to allow memory disambiguation
  2532. # logic do its magic. [Extra 256 bytes is for power mask
  2533. # calculated from 7th argument, the index.]
  2534. #
  2535. lea -320(%rsp,$num,2),%r11
  2536. mov %rsp,%rbp
  2537. sub $rptr,%r11
  2538. and \$4095,%r11
  2539. cmp %r11,%r10
  2540. jb .Lpwrx_sp_alt
  2541. sub %r11,%rbp # align with $aptr
  2542. lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
  2543. jmp .Lpwrx_sp_done
  2544. .align 32
  2545. .Lpwrx_sp_alt:
  2546. lea 4096-320(,$num,2),%r10
  2547. lea -320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
  2548. sub %r10,%r11
  2549. mov \$0,%r10
  2550. cmovc %r10,%r11
  2551. sub %r11,%rbp
  2552. .Lpwrx_sp_done:
  2553. and \$-64,%rbp
  2554. mov %rsp,%r11
  2555. sub %rbp,%r11
  2556. and \$-4096,%r11
  2557. lea (%rbp,%r11),%rsp
  2558. mov (%rsp),%r10
  2559. cmp %rbp,%rsp
  2560. ja .Lpwrx_page_walk
  2561. jmp .Lpwrx_page_walk_done
  2562. .Lpwrx_page_walk:
  2563. lea -4096(%rsp),%rsp
  2564. mov (%rsp),%r10
  2565. cmp %rbp,%rsp
  2566. ja .Lpwrx_page_walk
  2567. .Lpwrx_page_walk_done:
  2568. mov $num,%r10
  2569. neg $num
  2570. ##############################################################
  2571. # Stack layout
  2572. #
  2573. # +0 saved $num, used in reduction section
  2574. # +8 &t[2*$num], used in reduction section
  2575. # +16 intermediate carry bit
  2576. # +24 top-most carry bit, used in reduction section
  2577. # +32 saved *n0
  2578. # +40 saved %rsp
  2579. # +48 t[2*$num]
  2580. #
  2581. pxor %xmm0,%xmm0
  2582. movq $rptr,%xmm1 # save $rptr
  2583. movq $nptr,%xmm2 # save $nptr
  2584. movq %r10, %xmm3 # -$num
  2585. movq $bptr,%xmm4
  2586. mov $n0, 32(%rsp)
  2587. mov %rax, 40(%rsp) # save original %rsp
  2588. .cfi_cfa_expression %rsp+40,deref,+8
  2589. .Lpowerx5_body:
  2590. call __bn_sqrx8x_internal
  2591. call __bn_postx4x_internal
  2592. call __bn_sqrx8x_internal
  2593. call __bn_postx4x_internal
  2594. call __bn_sqrx8x_internal
  2595. call __bn_postx4x_internal
  2596. call __bn_sqrx8x_internal
  2597. call __bn_postx4x_internal
  2598. call __bn_sqrx8x_internal
  2599. call __bn_postx4x_internal
  2600. mov %r10,$num # -num
  2601. mov $aptr,$rptr
  2602. movq %xmm2,$nptr
  2603. movq %xmm4,$bptr
  2604. mov 40(%rsp),%rax
  2605. call mulx4x_internal
  2606. mov 40(%rsp),%rsi # restore %rsp
  2607. .cfi_def_cfa %rsi,8
  2608. mov \$1,%rax
  2609. mov -48(%rsi),%r15
  2610. .cfi_restore %r15
  2611. mov -40(%rsi),%r14
  2612. .cfi_restore %r14
  2613. mov -32(%rsi),%r13
  2614. .cfi_restore %r13
  2615. mov -24(%rsi),%r12
  2616. .cfi_restore %r12
  2617. mov -16(%rsi),%rbp
  2618. .cfi_restore %rbp
  2619. mov -8(%rsi),%rbx
  2620. .cfi_restore %rbx
  2621. lea (%rsi),%rsp
  2622. .cfi_def_cfa_register %rsp
  2623. .Lpowerx5_epilogue:
  2624. ret
  2625. .cfi_endproc
  2626. .size bn_powerx5,.-bn_powerx5
  2627. .globl bn_sqrx8x_internal
  2628. .hidden bn_sqrx8x_internal
  2629. .type bn_sqrx8x_internal,\@abi-omnipotent
  2630. .align 32
  2631. bn_sqrx8x_internal:
  2632. __bn_sqrx8x_internal:
  2633. .cfi_startproc
  2634. ##################################################################
  2635. # Squaring part:
  2636. #
  2637. # a) multiply-n-add everything but a[i]*a[i];
  2638. # b) shift result of a) by 1 to the left and accumulate
  2639. # a[i]*a[i] products;
  2640. #
  2641. ##################################################################
  2642. # a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
  2643. # a[1]a[0]
  2644. # a[2]a[0]
  2645. # a[3]a[0]
  2646. # a[2]a[1]
  2647. # a[3]a[1]
  2648. # a[3]a[2]
  2649. #
  2650. # a[4]a[0]
  2651. # a[5]a[0]
  2652. # a[6]a[0]
  2653. # a[7]a[0]
  2654. # a[4]a[1]
  2655. # a[5]a[1]
  2656. # a[6]a[1]
  2657. # a[7]a[1]
  2658. # a[4]a[2]
  2659. # a[5]a[2]
  2660. # a[6]a[2]
  2661. # a[7]a[2]
  2662. # a[4]a[3]
  2663. # a[5]a[3]
  2664. # a[6]a[3]
  2665. # a[7]a[3]
  2666. #
  2667. # a[5]a[4]
  2668. # a[6]a[4]
  2669. # a[7]a[4]
  2670. # a[6]a[5]
  2671. # a[7]a[5]
  2672. # a[7]a[6]
  2673. # a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
  2674. ___
  2675. {
  2676. my ($zero,$carry)=("%rbp","%rcx");
  2677. my $aaptr=$zero;
  2678. $code.=<<___;
  2679. lea 48+8(%rsp),$tptr
  2680. lea ($aptr,$num),$aaptr
  2681. mov $num,0+8(%rsp) # save $num
  2682. mov $aaptr,8+8(%rsp) # save end of $aptr
  2683. jmp .Lsqr8x_zero_start
  2684. .align 32
  2685. .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
  2686. .Lsqrx8x_zero:
  2687. .byte 0x3e
  2688. movdqa %xmm0,0*8($tptr)
  2689. movdqa %xmm0,2*8($tptr)
  2690. movdqa %xmm0,4*8($tptr)
  2691. movdqa %xmm0,6*8($tptr)
  2692. .Lsqr8x_zero_start: # aligned at 32
  2693. movdqa %xmm0,8*8($tptr)
  2694. movdqa %xmm0,10*8($tptr)
  2695. movdqa %xmm0,12*8($tptr)
  2696. movdqa %xmm0,14*8($tptr)
  2697. lea 16*8($tptr),$tptr
  2698. sub \$64,$num
  2699. jnz .Lsqrx8x_zero
  2700. mov 0*8($aptr),%rdx # a[0], modulo-scheduled
  2701. #xor %r9,%r9 # t[1], ex-$num, zero already
  2702. xor %r10,%r10
  2703. xor %r11,%r11
  2704. xor %r12,%r12
  2705. xor %r13,%r13
  2706. xor %r14,%r14
  2707. xor %r15,%r15
  2708. lea 48+8(%rsp),$tptr
  2709. xor $zero,$zero # cf=0, cf=0
  2710. jmp .Lsqrx8x_outer_loop
  2711. .align 32
  2712. .Lsqrx8x_outer_loop:
  2713. mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
  2714. adcx %r9,%r8 # a[1]*a[0]+=t[1]
  2715. adox %rax,%r10
  2716. mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
  2717. adcx %r10,%r9
  2718. adox %rax,%r11
  2719. .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
  2720. adcx %r11,%r10
  2721. adox %rax,%r12
  2722. .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
  2723. adcx %r12,%r11
  2724. adox %rax,%r13
  2725. mulx 5*8($aptr),%r12,%rax
  2726. adcx %r13,%r12
  2727. adox %rax,%r14
  2728. mulx 6*8($aptr),%r13,%rax
  2729. adcx %r14,%r13
  2730. adox %r15,%rax
  2731. mulx 7*8($aptr),%r14,%r15
  2732. mov 1*8($aptr),%rdx # a[1]
  2733. adcx %rax,%r14
  2734. adox $zero,%r15
  2735. adc 8*8($tptr),%r15
  2736. mov %r8,1*8($tptr) # t[1]
  2737. mov %r9,2*8($tptr) # t[2]
  2738. sbb $carry,$carry # mov %cf,$carry
  2739. xor $zero,$zero # cf=0, of=0
  2740. mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
  2741. mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
  2742. adcx %r10,%r8
  2743. adox %rbx,%r9
  2744. mulx 4*8($aptr),%r10,%rbx # ...
  2745. adcx %r11,%r9
  2746. adox %rax,%r10
  2747. .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
  2748. adcx %r12,%r10
  2749. adox %rbx,%r11
  2750. .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
  2751. adcx %r13,%r11
  2752. adox %r14,%r12
  2753. .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
  2754. mov 2*8($aptr),%rdx # a[2]
  2755. adcx %rax,%r12
  2756. adox %rbx,%r13
  2757. adcx %r15,%r13
  2758. adox $zero,%r14 # of=0
  2759. adcx $zero,%r14 # cf=0
  2760. mov %r8,3*8($tptr) # t[3]
  2761. mov %r9,4*8($tptr) # t[4]
  2762. mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
  2763. mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
  2764. adcx %r10,%r8
  2765. adox %rbx,%r9
  2766. mulx 5*8($aptr),%r10,%rbx # ...
  2767. adcx %r11,%r9
  2768. adox %rax,%r10
  2769. .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
  2770. adcx %r12,%r10
  2771. adox %r13,%r11
  2772. .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
  2773. .byte 0x3e
  2774. mov 3*8($aptr),%rdx # a[3]
  2775. adcx %rbx,%r11
  2776. adox %rax,%r12
  2777. adcx %r14,%r12
  2778. mov %r8,5*8($tptr) # t[5]
  2779. mov %r9,6*8($tptr) # t[6]
  2780. mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
  2781. adox $zero,%r13 # of=0
  2782. adcx $zero,%r13 # cf=0
  2783. mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
  2784. adcx %r10,%r8
  2785. adox %rax,%r9
  2786. mulx 6*8($aptr),%r10,%rax # ...
  2787. adcx %r11,%r9
  2788. adox %r12,%r10
  2789. mulx 7*8($aptr),%r11,%r12
  2790. mov 4*8($aptr),%rdx # a[4]
  2791. mov 5*8($aptr),%r14 # a[5]
  2792. adcx %rbx,%r10
  2793. adox %rax,%r11
  2794. mov 6*8($aptr),%r15 # a[6]
  2795. adcx %r13,%r11
  2796. adox $zero,%r12 # of=0
  2797. adcx $zero,%r12 # cf=0
  2798. mov %r8,7*8($tptr) # t[7]
  2799. mov %r9,8*8($tptr) # t[8]
  2800. mulx %r14,%r9,%rax # a[5]*a[4]
  2801. mov 7*8($aptr),%r8 # a[7]
  2802. adcx %r10,%r9
  2803. mulx %r15,%r10,%rbx # a[6]*a[4]
  2804. adox %rax,%r10
  2805. adcx %r11,%r10
  2806. mulx %r8,%r11,%rax # a[7]*a[4]
  2807. mov %r14,%rdx # a[5]
  2808. adox %rbx,%r11
  2809. adcx %r12,%r11
  2810. #adox $zero,%rax # of=0
  2811. adcx $zero,%rax # cf=0
  2812. mulx %r15,%r14,%rbx # a[6]*a[5]
  2813. mulx %r8,%r12,%r13 # a[7]*a[5]
  2814. mov %r15,%rdx # a[6]
  2815. lea 8*8($aptr),$aptr
  2816. adcx %r14,%r11
  2817. adox %rbx,%r12
  2818. adcx %rax,%r12
  2819. adox $zero,%r13
  2820. .byte 0x67,0x67
  2821. mulx %r8,%r8,%r14 # a[7]*a[6]
  2822. adcx %r8,%r13
  2823. adcx $zero,%r14
  2824. cmp 8+8(%rsp),$aptr
  2825. je .Lsqrx8x_outer_break
  2826. neg $carry # mov $carry,%cf
  2827. mov \$-8,%rcx
  2828. mov $zero,%r15
  2829. mov 8*8($tptr),%r8
  2830. adcx 9*8($tptr),%r9 # +=t[9]
  2831. adcx 10*8($tptr),%r10 # ...
  2832. adcx 11*8($tptr),%r11
  2833. adc 12*8($tptr),%r12
  2834. adc 13*8($tptr),%r13
  2835. adc 14*8($tptr),%r14
  2836. adc 15*8($tptr),%r15
  2837. lea ($aptr),$aaptr
  2838. lea 2*64($tptr),$tptr
  2839. sbb %rax,%rax # mov %cf,$carry
  2840. mov -64($aptr),%rdx # a[0]
  2841. mov %rax,16+8(%rsp) # offload $carry
  2842. mov $tptr,24+8(%rsp)
  2843. #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
  2844. xor %eax,%eax # cf=0, of=0
  2845. jmp .Lsqrx8x_loop
  2846. .align 32
  2847. .Lsqrx8x_loop:
  2848. mov %r8,%rbx
  2849. mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
  2850. adcx %rax,%rbx # +=t[8]
  2851. adox %r9,%r8
  2852. mulx 1*8($aaptr),%rax,%r9 # ...
  2853. adcx %rax,%r8
  2854. adox %r10,%r9
  2855. mulx 2*8($aaptr),%rax,%r10
  2856. adcx %rax,%r9
  2857. adox %r11,%r10
  2858. mulx 3*8($aaptr),%rax,%r11
  2859. adcx %rax,%r10
  2860. adox %r12,%r11
  2861. .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
  2862. adcx %rax,%r11
  2863. adox %r13,%r12
  2864. mulx 5*8($aaptr),%rax,%r13
  2865. adcx %rax,%r12
  2866. adox %r14,%r13
  2867. mulx 6*8($aaptr),%rax,%r14
  2868. mov %rbx,($tptr,%rcx,8) # store t[8+i]
  2869. mov \$0,%ebx
  2870. adcx %rax,%r13
  2871. adox %r15,%r14
  2872. .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
  2873. mov 8($aptr,%rcx,8),%rdx # a[i]
  2874. adcx %rax,%r14
  2875. adox %rbx,%r15 # %rbx is 0, of=0
  2876. adcx %rbx,%r15 # cf=0
  2877. .byte 0x67
  2878. inc %rcx # of=0
  2879. jnz .Lsqrx8x_loop
  2880. lea 8*8($aaptr),$aaptr
  2881. mov \$-8,%rcx
  2882. cmp 8+8(%rsp),$aaptr # done?
  2883. je .Lsqrx8x_break
  2884. sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
  2885. .byte 0x66
  2886. mov -64($aptr),%rdx
  2887. adcx 0*8($tptr),%r8
  2888. adcx 1*8($tptr),%r9
  2889. adc 2*8($tptr),%r10
  2890. adc 3*8($tptr),%r11
  2891. adc 4*8($tptr),%r12
  2892. adc 5*8($tptr),%r13
  2893. adc 6*8($tptr),%r14
  2894. adc 7*8($tptr),%r15
  2895. lea 8*8($tptr),$tptr
  2896. .byte 0x67
  2897. sbb %rax,%rax # mov %cf,%rax
  2898. xor %ebx,%ebx # cf=0, of=0
  2899. mov %rax,16+8(%rsp) # offload carry
  2900. jmp .Lsqrx8x_loop
  2901. .align 32
  2902. .Lsqrx8x_break:
  2903. xor $zero,$zero
  2904. sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
  2905. adcx $zero,%r8
  2906. mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
  2907. adcx $zero,%r9
  2908. mov 0*8($aptr),%rdx # a[8], modulo-scheduled
  2909. adc \$0,%r10
  2910. mov %r8,0*8($tptr)
  2911. adc \$0,%r11
  2912. adc \$0,%r12
  2913. adc \$0,%r13
  2914. adc \$0,%r14
  2915. adc \$0,%r15
  2916. cmp $carry,$tptr # cf=0, of=0
  2917. je .Lsqrx8x_outer_loop
  2918. mov %r9,1*8($tptr)
  2919. mov 1*8($carry),%r9
  2920. mov %r10,2*8($tptr)
  2921. mov 2*8($carry),%r10
  2922. mov %r11,3*8($tptr)
  2923. mov 3*8($carry),%r11
  2924. mov %r12,4*8($tptr)
  2925. mov 4*8($carry),%r12
  2926. mov %r13,5*8($tptr)
  2927. mov 5*8($carry),%r13
  2928. mov %r14,6*8($tptr)
  2929. mov 6*8($carry),%r14
  2930. mov %r15,7*8($tptr)
  2931. mov 7*8($carry),%r15
  2932. mov $carry,$tptr
  2933. jmp .Lsqrx8x_outer_loop
  2934. .align 32
  2935. .Lsqrx8x_outer_break:
  2936. mov %r9,9*8($tptr) # t[9]
  2937. movq %xmm3,%rcx # -$num
  2938. mov %r10,10*8($tptr) # ...
  2939. mov %r11,11*8($tptr)
  2940. mov %r12,12*8($tptr)
  2941. mov %r13,13*8($tptr)
  2942. mov %r14,14*8($tptr)
  2943. ___
  2944. } {
  2945. my $i="%rcx";
  2946. $code.=<<___;
  2947. lea 48+8(%rsp),$tptr
  2948. mov ($aptr,$i),%rdx # a[0]
  2949. mov 8($tptr),$A0[1] # t[1]
  2950. xor $A0[0],$A0[0] # t[0], of=0, cf=0
  2951. mov 0+8(%rsp),$num # restore $num
  2952. adox $A0[1],$A0[1]
  2953. mov 16($tptr),$A1[0] # t[2] # prefetch
  2954. mov 24($tptr),$A1[1] # t[3] # prefetch
  2955. #jmp .Lsqrx4x_shift_n_add # happens to be aligned
  2956. .align 32
  2957. .Lsqrx4x_shift_n_add:
  2958. mulx %rdx,%rax,%rbx
  2959. adox $A1[0],$A1[0]
  2960. adcx $A0[0],%rax
  2961. .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
  2962. .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
  2963. adox $A1[1],$A1[1]
  2964. adcx $A0[1],%rbx
  2965. mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
  2966. mov %rax,0($tptr)
  2967. mov %rbx,8($tptr)
  2968. mulx %rdx,%rax,%rbx
  2969. adox $A0[0],$A0[0]
  2970. adcx $A1[0],%rax
  2971. mov 16($aptr,$i),%rdx # a[i+2] # prefetch
  2972. mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
  2973. adox $A0[1],$A0[1]
  2974. adcx $A1[1],%rbx
  2975. mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
  2976. mov %rax,16($tptr)
  2977. mov %rbx,24($tptr)
  2978. mulx %rdx,%rax,%rbx
  2979. adox $A1[0],$A1[0]
  2980. adcx $A0[0],%rax
  2981. mov 24($aptr,$i),%rdx # a[i+3] # prefetch
  2982. lea 32($i),$i
  2983. mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
  2984. adox $A1[1],$A1[1]
  2985. adcx $A0[1],%rbx
  2986. mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
  2987. mov %rax,32($tptr)
  2988. mov %rbx,40($tptr)
  2989. mulx %rdx,%rax,%rbx
  2990. adox $A0[0],$A0[0]
  2991. adcx $A1[0],%rax
  2992. jrcxz .Lsqrx4x_shift_n_add_break
  2993. .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
  2994. adox $A0[1],$A0[1]
  2995. adcx $A1[1],%rbx
  2996. mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
  2997. mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
  2998. mov %rax,48($tptr)
  2999. mov %rbx,56($tptr)
  3000. lea 64($tptr),$tptr
  3001. nop
  3002. jmp .Lsqrx4x_shift_n_add
  3003. .align 32
  3004. .Lsqrx4x_shift_n_add_break:
  3005. adcx $A1[1],%rbx
  3006. mov %rax,48($tptr)
  3007. mov %rbx,56($tptr)
  3008. lea 64($tptr),$tptr # end of t[] buffer
  3009. ___
  3010. }
  3011. ######################################################################
  3012. # Montgomery reduction part, "word-by-word" algorithm.
  3013. #
  3014. # This new path is inspired by multiple submissions from Intel, by
  3015. # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
  3016. # Vinodh Gopal...
  3017. {
  3018. my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
  3019. $code.=<<___;
  3020. movq %xmm2,$nptr
  3021. __bn_sqrx8x_reduction:
  3022. xor %eax,%eax # initial top-most carry bit
  3023. mov 32+8(%rsp),%rbx # n0
  3024. mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
  3025. lea -8*8($nptr,$num),%rcx # end of n[]
  3026. #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
  3027. mov %rcx, 0+8(%rsp) # save end of n[]
  3028. mov $tptr,8+8(%rsp) # save end of t[]
  3029. lea 48+8(%rsp),$tptr # initial t[] window
  3030. jmp .Lsqrx8x_reduction_loop
  3031. .align 32
  3032. .Lsqrx8x_reduction_loop:
  3033. mov 8*1($tptr),%r9
  3034. mov 8*2($tptr),%r10
  3035. mov 8*3($tptr),%r11
  3036. mov 8*4($tptr),%r12
  3037. mov %rdx,%r8
  3038. imulq %rbx,%rdx # n0*a[i]
  3039. mov 8*5($tptr),%r13
  3040. mov 8*6($tptr),%r14
  3041. mov 8*7($tptr),%r15
  3042. mov %rax,24+8(%rsp) # store top-most carry bit
  3043. lea 8*8($tptr),$tptr
  3044. xor $carry,$carry # cf=0,of=0
  3045. mov \$-8,%rcx
  3046. jmp .Lsqrx8x_reduce
  3047. .align 32
  3048. .Lsqrx8x_reduce:
  3049. mov %r8, %rbx
  3050. mulx 8*0($nptr),%rax,%r8 # n[0]
  3051. adcx %rbx,%rax # discarded
  3052. adox %r9,%r8
  3053. mulx 8*1($nptr),%rbx,%r9 # n[1]
  3054. adcx %rbx,%r8
  3055. adox %r10,%r9
  3056. mulx 8*2($nptr),%rbx,%r10
  3057. adcx %rbx,%r9
  3058. adox %r11,%r10
  3059. mulx 8*3($nptr),%rbx,%r11
  3060. adcx %rbx,%r10
  3061. adox %r12,%r11
  3062. .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
  3063. mov %rdx,%rax
  3064. mov %r8,%rdx
  3065. adcx %rbx,%r11
  3066. adox %r13,%r12
  3067. mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
  3068. mov %rax,%rdx
  3069. mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
  3070. mulx 8*5($nptr),%rax,%r13
  3071. adcx %rax,%r12
  3072. adox %r14,%r13
  3073. mulx 8*6($nptr),%rax,%r14
  3074. adcx %rax,%r13
  3075. adox %r15,%r14
  3076. mulx 8*7($nptr),%rax,%r15
  3077. mov %rbx,%rdx
  3078. adcx %rax,%r14
  3079. adox $carry,%r15 # $carry is 0
  3080. adcx $carry,%r15 # cf=0
  3081. .byte 0x67,0x67,0x67
  3082. inc %rcx # of=0
  3083. jnz .Lsqrx8x_reduce
  3084. mov $carry,%rax # xor %rax,%rax
  3085. cmp 0+8(%rsp),$nptr # end of n[]?
  3086. jae .Lsqrx8x_no_tail
  3087. mov 48+8(%rsp),%rdx # pull n0*a[0]
  3088. add 8*0($tptr),%r8
  3089. lea 8*8($nptr),$nptr
  3090. mov \$-8,%rcx
  3091. adcx 8*1($tptr),%r9
  3092. adcx 8*2($tptr),%r10
  3093. adc 8*3($tptr),%r11
  3094. adc 8*4($tptr),%r12
  3095. adc 8*5($tptr),%r13
  3096. adc 8*6($tptr),%r14
  3097. adc 8*7($tptr),%r15
  3098. lea 8*8($tptr),$tptr
  3099. sbb %rax,%rax # top carry
  3100. xor $carry,$carry # of=0, cf=0
  3101. mov %rax,16+8(%rsp)
  3102. jmp .Lsqrx8x_tail
  3103. .align 32
  3104. .Lsqrx8x_tail:
  3105. mov %r8,%rbx
  3106. mulx 8*0($nptr),%rax,%r8
  3107. adcx %rax,%rbx
  3108. adox %r9,%r8
  3109. mulx 8*1($nptr),%rax,%r9
  3110. adcx %rax,%r8
  3111. adox %r10,%r9
  3112. mulx 8*2($nptr),%rax,%r10
  3113. adcx %rax,%r9
  3114. adox %r11,%r10
  3115. mulx 8*3($nptr),%rax,%r11
  3116. adcx %rax,%r10
  3117. adox %r12,%r11
  3118. .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
  3119. adcx %rax,%r11
  3120. adox %r13,%r12
  3121. mulx 8*5($nptr),%rax,%r13
  3122. adcx %rax,%r12
  3123. adox %r14,%r13
  3124. mulx 8*6($nptr),%rax,%r14
  3125. adcx %rax,%r13
  3126. adox %r15,%r14
  3127. mulx 8*7($nptr),%rax,%r15
  3128. mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
  3129. adcx %rax,%r14
  3130. adox $carry,%r15
  3131. mov %rbx,($tptr,%rcx,8) # save result
  3132. mov %r8,%rbx
  3133. adcx $carry,%r15 # cf=0
  3134. inc %rcx # of=0
  3135. jnz .Lsqrx8x_tail
  3136. cmp 0+8(%rsp),$nptr # end of n[]?
  3137. jae .Lsqrx8x_tail_done # break out of loop
  3138. sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
  3139. mov 48+8(%rsp),%rdx # pull n0*a[0]
  3140. lea 8*8($nptr),$nptr
  3141. adc 8*0($tptr),%r8
  3142. adc 8*1($tptr),%r9
  3143. adc 8*2($tptr),%r10
  3144. adc 8*3($tptr),%r11
  3145. adc 8*4($tptr),%r12
  3146. adc 8*5($tptr),%r13
  3147. adc 8*6($tptr),%r14
  3148. adc 8*7($tptr),%r15
  3149. lea 8*8($tptr),$tptr
  3150. sbb %rax,%rax
  3151. sub \$8,%rcx # mov \$-8,%rcx
  3152. xor $carry,$carry # of=0, cf=0
  3153. mov %rax,16+8(%rsp)
  3154. jmp .Lsqrx8x_tail
  3155. .align 32
  3156. .Lsqrx8x_tail_done:
  3157. xor %rax,%rax
  3158. add 24+8(%rsp),%r8 # can this overflow?
  3159. adc \$0,%r9
  3160. adc \$0,%r10
  3161. adc \$0,%r11
  3162. adc \$0,%r12
  3163. adc \$0,%r13
  3164. adc \$0,%r14
  3165. adc \$0,%r15
  3166. adc \$0,%rax
  3167. sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
  3168. .Lsqrx8x_no_tail: # %cf is 0 if jumped here
  3169. adc 8*0($tptr),%r8
  3170. movq %xmm3,%rcx
  3171. adc 8*1($tptr),%r9
  3172. mov 8*7($nptr),$carry
  3173. movq %xmm2,$nptr # restore $nptr
  3174. adc 8*2($tptr),%r10
  3175. adc 8*3($tptr),%r11
  3176. adc 8*4($tptr),%r12
  3177. adc 8*5($tptr),%r13
  3178. adc 8*6($tptr),%r14
  3179. adc 8*7($tptr),%r15
  3180. adc \$0,%rax # top-most carry
  3181. mov 32+8(%rsp),%rbx # n0
  3182. mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
  3183. mov %r8,8*0($tptr) # store top 512 bits
  3184. lea 8*8($tptr),%r8 # borrow %r8
  3185. mov %r9,8*1($tptr)
  3186. mov %r10,8*2($tptr)
  3187. mov %r11,8*3($tptr)
  3188. mov %r12,8*4($tptr)
  3189. mov %r13,8*5($tptr)
  3190. mov %r14,8*6($tptr)
  3191. mov %r15,8*7($tptr)
  3192. lea 8*8($tptr,%rcx),$tptr # start of current t[] window
  3193. cmp 8+8(%rsp),%r8 # end of t[]?
  3194. jb .Lsqrx8x_reduction_loop
  3195. ret
  3196. .cfi_endproc
  3197. .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
  3198. ___
  3199. }
  3200. ##############################################################
  3201. # Post-condition, 4x unrolled
  3202. #
  3203. {
  3204. my ($rptr,$nptr)=("%rdx","%rbp");
  3205. $code.=<<___;
  3206. .align 32
  3207. __bn_postx4x_internal:
  3208. .cfi_startproc
  3209. mov 8*0($nptr),%r12
  3210. mov %rcx,%r10 # -$num
  3211. mov %rcx,%r9 # -$num
  3212. neg %rax
  3213. sar \$3+2,%rcx
  3214. #lea 48+8(%rsp,%r9),$tptr
  3215. movq %xmm1,$rptr # restore $rptr
  3216. movq %xmm1,$aptr # prepare for back-to-back call
  3217. dec %r12 # so that after 'not' we get -n[0]
  3218. mov 8*1($nptr),%r13
  3219. xor %r8,%r8
  3220. mov 8*2($nptr),%r14
  3221. mov 8*3($nptr),%r15
  3222. jmp .Lsqrx4x_sub_entry
  3223. .align 16
  3224. .Lsqrx4x_sub:
  3225. mov 8*0($nptr),%r12
  3226. mov 8*1($nptr),%r13
  3227. mov 8*2($nptr),%r14
  3228. mov 8*3($nptr),%r15
  3229. .Lsqrx4x_sub_entry:
  3230. andn %rax,%r12,%r12
  3231. lea 8*4($nptr),$nptr
  3232. andn %rax,%r13,%r13
  3233. andn %rax,%r14,%r14
  3234. andn %rax,%r15,%r15
  3235. neg %r8 # mov %r8,%cf
  3236. adc 8*0($tptr),%r12
  3237. adc 8*1($tptr),%r13
  3238. adc 8*2($tptr),%r14
  3239. adc 8*3($tptr),%r15
  3240. mov %r12,8*0($rptr)
  3241. lea 8*4($tptr),$tptr
  3242. mov %r13,8*1($rptr)
  3243. sbb %r8,%r8 # mov %cf,%r8
  3244. mov %r14,8*2($rptr)
  3245. mov %r15,8*3($rptr)
  3246. lea 8*4($rptr),$rptr
  3247. inc %rcx
  3248. jnz .Lsqrx4x_sub
  3249. neg %r9 # restore $num
  3250. ret
  3251. .cfi_endproc
  3252. .size __bn_postx4x_internal,.-__bn_postx4x_internal
  3253. ___
  3254. }
  3255. }}}
  3256. {
  3257. my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
  3258. ("%rdi","%esi","%rdx","%ecx"); # Unix order
  3259. my $out=$inp;
  3260. my $STRIDE=2**5*8;
  3261. my $N=$STRIDE/4;
  3262. $code.=<<___;
  3263. .globl bn_get_bits5
  3264. .type bn_get_bits5,\@abi-omnipotent
  3265. .align 16
  3266. bn_get_bits5:
  3267. .cfi_startproc
  3268. lea 0($inp),%r10
  3269. lea 1($inp),%r11
  3270. mov $num,%ecx
  3271. shr \$4,$num
  3272. and \$15,%ecx
  3273. lea -8(%ecx),%eax
  3274. cmp \$11,%ecx
  3275. cmova %r11,%r10
  3276. cmova %eax,%ecx
  3277. movzw (%r10,$num,2),%eax
  3278. shrl %cl,%eax
  3279. and \$31,%eax
  3280. ret
  3281. .cfi_endproc
  3282. .size bn_get_bits5,.-bn_get_bits5
  3283. .globl bn_scatter5
  3284. .type bn_scatter5,\@abi-omnipotent
  3285. .align 16
  3286. bn_scatter5:
  3287. .cfi_startproc
  3288. cmp \$0, $num
  3289. jz .Lscatter_epilogue
  3290. lea ($tbl,$idx,8),$tbl
  3291. .Lscatter:
  3292. mov ($inp),%rax
  3293. lea 8($inp),$inp
  3294. mov %rax,($tbl)
  3295. lea 32*8($tbl),$tbl
  3296. sub \$1,$num
  3297. jnz .Lscatter
  3298. .Lscatter_epilogue:
  3299. ret
  3300. .cfi_endproc
  3301. .size bn_scatter5,.-bn_scatter5
  3302. .globl bn_gather5
  3303. .type bn_gather5,\@abi-omnipotent
  3304. .align 32
  3305. bn_gather5:
  3306. .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
  3307. .cfi_startproc
  3308. # I can't trust assembler to use specific encoding:-(
  3309. .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
  3310. .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
  3311. lea .Linc(%rip),%rax
  3312. and \$-16,%rsp # shouldn't be formally required
  3313. movd $idx,%xmm5
  3314. movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
  3315. movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
  3316. lea 128($tbl),%r11 # size optimization
  3317. lea 128(%rsp),%rax # size optimization
  3318. pshufd \$0,%xmm5,%xmm5 # broadcast $idx
  3319. movdqa %xmm1,%xmm4
  3320. movdqa %xmm1,%xmm2
  3321. ___
  3322. ########################################################################
  3323. # calculate mask by comparing 0..31 to $idx and save result to stack
  3324. #
  3325. for($i=0;$i<$STRIDE/16;$i+=4) {
  3326. $code.=<<___;
  3327. paddd %xmm0,%xmm1
  3328. pcmpeqd %xmm5,%xmm0 # compare to 1,0
  3329. ___
  3330. $code.=<<___ if ($i);
  3331. movdqa %xmm3,`16*($i-1)-128`(%rax)
  3332. ___
  3333. $code.=<<___;
  3334. movdqa %xmm4,%xmm3
  3335. paddd %xmm1,%xmm2
  3336. pcmpeqd %xmm5,%xmm1 # compare to 3,2
  3337. movdqa %xmm0,`16*($i+0)-128`(%rax)
  3338. movdqa %xmm4,%xmm0
  3339. paddd %xmm2,%xmm3
  3340. pcmpeqd %xmm5,%xmm2 # compare to 5,4
  3341. movdqa %xmm1,`16*($i+1)-128`(%rax)
  3342. movdqa %xmm4,%xmm1
  3343. paddd %xmm3,%xmm0
  3344. pcmpeqd %xmm5,%xmm3 # compare to 7,6
  3345. movdqa %xmm2,`16*($i+2)-128`(%rax)
  3346. movdqa %xmm4,%xmm2
  3347. ___
  3348. }
  3349. $code.=<<___;
  3350. movdqa %xmm3,`16*($i-1)-128`(%rax)
  3351. jmp .Lgather
  3352. .align 32
  3353. .Lgather:
  3354. pxor %xmm4,%xmm4
  3355. pxor %xmm5,%xmm5
  3356. ___
  3357. for($i=0;$i<$STRIDE/16;$i+=4) {
  3358. $code.=<<___;
  3359. movdqa `16*($i+0)-128`(%r11),%xmm0
  3360. movdqa `16*($i+1)-128`(%r11),%xmm1
  3361. movdqa `16*($i+2)-128`(%r11),%xmm2
  3362. pand `16*($i+0)-128`(%rax),%xmm0
  3363. movdqa `16*($i+3)-128`(%r11),%xmm3
  3364. pand `16*($i+1)-128`(%rax),%xmm1
  3365. por %xmm0,%xmm4
  3366. pand `16*($i+2)-128`(%rax),%xmm2
  3367. por %xmm1,%xmm5
  3368. pand `16*($i+3)-128`(%rax),%xmm3
  3369. por %xmm2,%xmm4
  3370. por %xmm3,%xmm5
  3371. ___
  3372. }
  3373. $code.=<<___;
  3374. por %xmm5,%xmm4
  3375. lea $STRIDE(%r11),%r11
  3376. pshufd \$0x4e,%xmm4,%xmm0
  3377. por %xmm4,%xmm0
  3378. movq %xmm0,($out) # m0=bp[0]
  3379. lea 8($out),$out
  3380. sub \$1,$num
  3381. jnz .Lgather
  3382. lea (%r10),%rsp
  3383. ret
  3384. .LSEH_end_bn_gather5:
  3385. .cfi_endproc
  3386. .size bn_gather5,.-bn_gather5
  3387. ___
  3388. }
  3389. $code.=<<___;
  3390. .align 64
  3391. .Linc:
  3392. .long 0,0, 1,1
  3393. .long 2,2, 2,2
  3394. .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  3395. ___
  3396. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  3397. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  3398. if ($win64) {
  3399. $rec="%rcx";
  3400. $frame="%rdx";
  3401. $context="%r8";
  3402. $disp="%r9";
  3403. $code.=<<___;
  3404. .extern __imp_RtlVirtualUnwind
  3405. .type mul_handler,\@abi-omnipotent
  3406. .align 16
  3407. mul_handler:
  3408. push %rsi
  3409. push %rdi
  3410. push %rbx
  3411. push %rbp
  3412. push %r12
  3413. push %r13
  3414. push %r14
  3415. push %r15
  3416. pushfq
  3417. sub \$64,%rsp
  3418. mov 120($context),%rax # pull context->Rax
  3419. mov 248($context),%rbx # pull context->Rip
  3420. mov 8($disp),%rsi # disp->ImageBase
  3421. mov 56($disp),%r11 # disp->HandlerData
  3422. mov 0(%r11),%r10d # HandlerData[0]
  3423. lea (%rsi,%r10),%r10 # end of prologue label
  3424. cmp %r10,%rbx # context->Rip<end of prologue label
  3425. jb .Lcommon_seh_tail
  3426. mov 4(%r11),%r10d # HandlerData[1]
  3427. lea (%rsi,%r10),%r10 # beginning of body label
  3428. cmp %r10,%rbx # context->Rip<body label
  3429. jb .Lcommon_pop_regs
  3430. mov 152($context),%rax # pull context->Rsp
  3431. mov 8(%r11),%r10d # HandlerData[2]
  3432. lea (%rsi,%r10),%r10 # epilogue label
  3433. cmp %r10,%rbx # context->Rip>=epilogue label
  3434. jae .Lcommon_seh_tail
  3435. lea .Lmul_epilogue(%rip),%r10
  3436. cmp %r10,%rbx
  3437. ja .Lbody_40
  3438. mov 192($context),%r10 # pull $num
  3439. mov 8(%rax,%r10,8),%rax # pull saved stack pointer
  3440. jmp .Lcommon_pop_regs
  3441. .Lbody_40:
  3442. mov 40(%rax),%rax # pull saved stack pointer
  3443. .Lcommon_pop_regs:
  3444. mov -8(%rax),%rbx
  3445. mov -16(%rax),%rbp
  3446. mov -24(%rax),%r12
  3447. mov -32(%rax),%r13
  3448. mov -40(%rax),%r14
  3449. mov -48(%rax),%r15
  3450. mov %rbx,144($context) # restore context->Rbx
  3451. mov %rbp,160($context) # restore context->Rbp
  3452. mov %r12,216($context) # restore context->R12
  3453. mov %r13,224($context) # restore context->R13
  3454. mov %r14,232($context) # restore context->R14
  3455. mov %r15,240($context) # restore context->R15
  3456. .Lcommon_seh_tail:
  3457. mov 8(%rax),%rdi
  3458. mov 16(%rax),%rsi
  3459. mov %rax,152($context) # restore context->Rsp
  3460. mov %rsi,168($context) # restore context->Rsi
  3461. mov %rdi,176($context) # restore context->Rdi
  3462. mov 40($disp),%rdi # disp->ContextRecord
  3463. mov $context,%rsi # context
  3464. mov \$154,%ecx # sizeof(CONTEXT)
  3465. .long 0xa548f3fc # cld; rep movsq
  3466. mov $disp,%rsi
  3467. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  3468. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  3469. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  3470. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  3471. mov 40(%rsi),%r10 # disp->ContextRecord
  3472. lea 56(%rsi),%r11 # &disp->HandlerData
  3473. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  3474. mov %r10,32(%rsp) # arg5
  3475. mov %r11,40(%rsp) # arg6
  3476. mov %r12,48(%rsp) # arg7
  3477. mov %rcx,56(%rsp) # arg8, (NULL)
  3478. call *__imp_RtlVirtualUnwind(%rip)
  3479. mov \$1,%eax # ExceptionContinueSearch
  3480. add \$64,%rsp
  3481. popfq
  3482. pop %r15
  3483. pop %r14
  3484. pop %r13
  3485. pop %r12
  3486. pop %rbp
  3487. pop %rbx
  3488. pop %rdi
  3489. pop %rsi
  3490. ret
  3491. .size mul_handler,.-mul_handler
  3492. .section .pdata
  3493. .align 4
  3494. .rva .LSEH_begin_bn_mul_mont_gather5
  3495. .rva .LSEH_end_bn_mul_mont_gather5
  3496. .rva .LSEH_info_bn_mul_mont_gather5
  3497. .rva .LSEH_begin_bn_mul4x_mont_gather5
  3498. .rva .LSEH_end_bn_mul4x_mont_gather5
  3499. .rva .LSEH_info_bn_mul4x_mont_gather5
  3500. .rva .LSEH_begin_bn_power5
  3501. .rva .LSEH_end_bn_power5
  3502. .rva .LSEH_info_bn_power5
  3503. .rva .LSEH_begin_bn_from_mont8x
  3504. .rva .LSEH_end_bn_from_mont8x
  3505. .rva .LSEH_info_bn_from_mont8x
  3506. ___
  3507. $code.=<<___ if ($addx);
  3508. .rva .LSEH_begin_bn_mulx4x_mont_gather5
  3509. .rva .LSEH_end_bn_mulx4x_mont_gather5
  3510. .rva .LSEH_info_bn_mulx4x_mont_gather5
  3511. .rva .LSEH_begin_bn_powerx5
  3512. .rva .LSEH_end_bn_powerx5
  3513. .rva .LSEH_info_bn_powerx5
  3514. ___
  3515. $code.=<<___;
  3516. .rva .LSEH_begin_bn_gather5
  3517. .rva .LSEH_end_bn_gather5
  3518. .rva .LSEH_info_bn_gather5
  3519. .section .xdata
  3520. .align 8
  3521. .LSEH_info_bn_mul_mont_gather5:
  3522. .byte 9,0,0,0
  3523. .rva mul_handler
  3524. .rva .Lmul_body,.Lmul_body,.Lmul_epilogue # HandlerData[]
  3525. .align 8
  3526. .LSEH_info_bn_mul4x_mont_gather5:
  3527. .byte 9,0,0,0
  3528. .rva mul_handler
  3529. .rva .Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
  3530. .align 8
  3531. .LSEH_info_bn_power5:
  3532. .byte 9,0,0,0
  3533. .rva mul_handler
  3534. .rva .Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue # HandlerData[]
  3535. .align 8
  3536. .LSEH_info_bn_from_mont8x:
  3537. .byte 9,0,0,0
  3538. .rva mul_handler
  3539. .rva .Lfrom_prologue,.Lfrom_body,.Lfrom_epilogue # HandlerData[]
  3540. ___
  3541. $code.=<<___ if ($addx);
  3542. .align 8
  3543. .LSEH_info_bn_mulx4x_mont_gather5:
  3544. .byte 9,0,0,0
  3545. .rva mul_handler
  3546. .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
  3547. .align 8
  3548. .LSEH_info_bn_powerx5:
  3549. .byte 9,0,0,0
  3550. .rva mul_handler
  3551. .rva .Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
  3552. ___
  3553. $code.=<<___;
  3554. .align 8
  3555. .LSEH_info_bn_gather5:
  3556. .byte 0x01,0x0b,0x03,0x0a
  3557. .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
  3558. .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
  3559. .align 8
  3560. ___
  3561. }
  3562. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  3563. print $code;
  3564. close STDOUT or die "error closing STDOUT: $!";