x86_64-mont5.pl 86 KB

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