sha1-586.pl 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. #! /usr/bin/env perl
  2. # Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # [Re]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. # "[Re]written" was achieved in two major overhauls. In 2004 BODY_*
  15. # functions were re-implemented to address P4 performance issue [see
  16. # commentary below], and in 2006 the rest was rewritten in order to
  17. # gain freedom to liberate licensing terms.
  18. # January, September 2004.
  19. #
  20. # It was noted that Intel IA-32 C compiler generates code which
  21. # performs ~30% *faster* on P4 CPU than original *hand-coded*
  22. # SHA1 assembler implementation. To address this problem (and
  23. # prove that humans are still better than machines:-), the
  24. # original code was overhauled, which resulted in following
  25. # performance changes:
  26. #
  27. # compared with original compared with Intel cc
  28. # assembler impl. generated code
  29. # Pentium -16% +48%
  30. # PIII/AMD +8% +16%
  31. # P4 +85%(!) +45%
  32. #
  33. # As you can see Pentium came out as looser:-( Yet I reckoned that
  34. # improvement on P4 outweighs the loss and incorporate this
  35. # re-tuned code to 0.9.7 and later.
  36. # ----------------------------------------------------------------
  37. # August 2009.
  38. #
  39. # George Spelvin has tipped that F_40_59(b,c,d) can be rewritten as
  40. # '(c&d) + (b&(c^d))', which allows to accumulate partial results
  41. # and lighten "pressure" on scratch registers. This resulted in
  42. # >12% performance improvement on contemporary AMD cores (with no
  43. # degradation on other CPUs:-). Also, the code was revised to maximize
  44. # "distance" between instructions producing input to 'lea' instruction
  45. # and the 'lea' instruction itself, which is essential for Intel Atom
  46. # core and resulted in ~15% improvement.
  47. # October 2010.
  48. #
  49. # Add SSSE3, Supplemental[!] SSE3, implementation. The idea behind it
  50. # is to offload message schedule denoted by Wt in NIST specification,
  51. # or Xupdate in OpenSSL source, to SIMD unit. The idea is not novel,
  52. # and in SSE2 context was first explored by Dean Gaudet in 2004, see
  53. # http://arctic.org/~dean/crypto/sha1.html. Since then several things
  54. # have changed that made it interesting again:
  55. #
  56. # a) XMM units became faster and wider;
  57. # b) instruction set became more versatile;
  58. # c) an important observation was made by Max Locktykhin, which made
  59. # it possible to reduce amount of instructions required to perform
  60. # the operation in question, for further details see
  61. # http://software.intel.com/en-us/articles/improving-the-performance-of-the-secure-hash-algorithm-1/.
  62. # April 2011.
  63. #
  64. # Add AVX code path, probably most controversial... The thing is that
  65. # switch to AVX alone improves performance by as little as 4% in
  66. # comparison to SSSE3 code path. But below result doesn't look like
  67. # 4% improvement... Trouble is that Sandy Bridge decodes 'ro[rl]' as
  68. # pair of µ-ops, and it's the additional µ-ops, two per round, that
  69. # make it run slower than Core2 and Westmere. But 'sh[rl]d' is decoded
  70. # as single µ-op by Sandy Bridge and it's replacing 'ro[rl]' with
  71. # equivalent 'sh[rl]d' that is responsible for the impressive 5.1
  72. # cycles per processed byte. But 'sh[rl]d' is not something that used
  73. # to be fast, nor does it appear to be fast in upcoming Bulldozer
  74. # [according to its optimization manual]. Which is why AVX code path
  75. # is guarded by *both* AVX and synthetic bit denoting Intel CPUs.
  76. # One can argue that it's unfair to AMD, but without 'sh[rl]d' it
  77. # makes no sense to keep the AVX code path. If somebody feels that
  78. # strongly, it's probably more appropriate to discuss possibility of
  79. # using vector rotate XOP on AMD...
  80. # March 2014.
  81. #
  82. # Add support for Intel SHA Extensions.
  83. ######################################################################
  84. # Current performance is summarized in following table. Numbers are
  85. # CPU clock cycles spent to process single byte (less is better).
  86. #
  87. # x86 SSSE3 AVX
  88. # Pentium 15.7 -
  89. # PIII 11.5 -
  90. # P4 10.6 -
  91. # AMD K8 7.1 -
  92. # Core2 7.3 6.0/+22% -
  93. # Westmere 7.3 5.5/+33% -
  94. # Sandy Bridge 8.8 6.2/+40% 5.1(**)/+73%
  95. # Ivy Bridge 7.2 4.8/+51% 4.7(**)/+53%
  96. # Haswell 6.5 4.3/+51% 4.1(**)/+58%
  97. # Skylake 6.4 4.1/+55% 4.1(**)/+55%
  98. # Bulldozer 11.6 6.0/+92%
  99. # VIA Nano 10.6 7.5/+41%
  100. # Atom 12.5 9.3(*)/+35%
  101. # Silvermont 14.5 9.9(*)/+46%
  102. # Goldmont 8.8 6.7/+30% 1.7(***)/+415%
  103. #
  104. # (*) Loop is 1056 instructions long and expected result is ~8.25.
  105. # The discrepancy is because of front-end limitations, so
  106. # called MS-ROM penalties, and on Silvermont even rotate's
  107. # limited parallelism.
  108. #
  109. # (**) As per above comment, the result is for AVX *plus* sh[rl]d.
  110. #
  111. # (***) SHAEXT result
  112. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  113. push(@INC,"${dir}","${dir}../../perlasm");
  114. require "x86asm.pl";
  115. $output=pop and open STDOUT,">$output";
  116. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  117. $xmm=$ymm=0;
  118. for (@ARGV) { $xmm=1 if (/-DOPENSSL_IA32_SSE2/); }
  119. $ymm=1 if ($xmm &&
  120. `$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  121. =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
  122. $1>=2.19); # first version supporting AVX
  123. $ymm=1 if ($xmm && !$ymm && $ARGV[0] eq "win32n" &&
  124. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
  125. $1>=2.03); # first version supporting AVX
  126. $ymm=1 if ($xmm && !$ymm && $ARGV[0] eq "win32" &&
  127. `ml 2>&1` =~ /Version ([0-9]+)\./ &&
  128. $1>=10); # first version supporting AVX
  129. $ymm=1 if ($xmm && !$ymm && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|based on LLVM) ([0-9]+\.[0-9]+)/ &&
  130. $2>=3.0); # first version supporting AVX
  131. $shaext=$xmm; ### set to zero if compiling for 1.0.1
  132. &external_label("OPENSSL_ia32cap_P") if ($xmm);
  133. $A="eax";
  134. $B="ebx";
  135. $C="ecx";
  136. $D="edx";
  137. $E="edi";
  138. $T="esi";
  139. $tmp1="ebp";
  140. @V=($A,$B,$C,$D,$E,$T);
  141. $alt=0; # 1 denotes alternative IALU implementation, which performs
  142. # 8% *worse* on P4, same on Westmere and Atom, 2% better on
  143. # Sandy Bridge...
  144. sub BODY_00_15
  145. {
  146. local($n,$a,$b,$c,$d,$e,$f)=@_;
  147. &comment("00_15 $n");
  148. &mov($f,$c); # f to hold F_00_19(b,c,d)
  149. if ($n==0) { &mov($tmp1,$a); }
  150. else { &mov($a,$tmp1); }
  151. &rotl($tmp1,5); # tmp1=ROTATE(a,5)
  152. &xor($f,$d);
  153. &add($tmp1,$e); # tmp1+=e;
  154. &mov($e,&swtmp($n%16)); # e becomes volatile and is loaded
  155. # with xi, also note that e becomes
  156. # f in next round...
  157. &and($f,$b);
  158. &rotr($b,2); # b=ROTATE(b,30)
  159. &xor($f,$d); # f holds F_00_19(b,c,d)
  160. &lea($tmp1,&DWP(0x5a827999,$tmp1,$e)); # tmp1+=K_00_19+xi
  161. if ($n==15) { &mov($e,&swtmp(($n+1)%16));# pre-fetch f for next round
  162. &add($f,$tmp1); } # f+=tmp1
  163. else { &add($tmp1,$f); } # f becomes a in next round
  164. &mov($tmp1,$a) if ($alt && $n==15);
  165. }
  166. sub BODY_16_19
  167. {
  168. local($n,$a,$b,$c,$d,$e,$f)=@_;
  169. &comment("16_19 $n");
  170. if ($alt) {
  171. &xor($c,$d);
  172. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  173. &and($tmp1,$c); # tmp1 to hold F_00_19(b,c,d), b&=c^d
  174. &xor($f,&swtmp(($n+8)%16));
  175. &xor($tmp1,$d); # tmp1=F_00_19(b,c,d)
  176. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  177. &rotl($f,1); # f=ROTATE(f,1)
  178. &add($e,$tmp1); # e+=F_00_19(b,c,d)
  179. &xor($c,$d); # restore $c
  180. &mov($tmp1,$a); # b in next round
  181. &rotr($b,$n==16?2:7); # b=ROTATE(b,30)
  182. &mov(&swtmp($n%16),$f); # xi=f
  183. &rotl($a,5); # ROTATE(a,5)
  184. &lea($f,&DWP(0x5a827999,$f,$e));# f+=F_00_19(b,c,d)+e
  185. &mov($e,&swtmp(($n+1)%16)); # pre-fetch f for next round
  186. &add($f,$a); # f+=ROTATE(a,5)
  187. } else {
  188. &mov($tmp1,$c); # tmp1 to hold F_00_19(b,c,d)
  189. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  190. &xor($tmp1,$d);
  191. &xor($f,&swtmp(($n+8)%16));
  192. &and($tmp1,$b);
  193. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  194. &rotl($f,1); # f=ROTATE(f,1)
  195. &xor($tmp1,$d); # tmp1=F_00_19(b,c,d)
  196. &add($e,$tmp1); # e+=F_00_19(b,c,d)
  197. &mov($tmp1,$a);
  198. &rotr($b,2); # b=ROTATE(b,30)
  199. &mov(&swtmp($n%16),$f); # xi=f
  200. &rotl($tmp1,5); # ROTATE(a,5)
  201. &lea($f,&DWP(0x5a827999,$f,$e));# f+=F_00_19(b,c,d)+e
  202. &mov($e,&swtmp(($n+1)%16)); # pre-fetch f for next round
  203. &add($f,$tmp1); # f+=ROTATE(a,5)
  204. }
  205. }
  206. sub BODY_20_39
  207. {
  208. local($n,$a,$b,$c,$d,$e,$f)=@_;
  209. local $K=($n<40)?0x6ed9eba1:0xca62c1d6;
  210. &comment("20_39 $n");
  211. if ($alt) {
  212. &xor($tmp1,$c); # tmp1 to hold F_20_39(b,c,d), b^=c
  213. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  214. &xor($tmp1,$d); # tmp1 holds F_20_39(b,c,d)
  215. &xor($f,&swtmp(($n+8)%16));
  216. &add($e,$tmp1); # e+=F_20_39(b,c,d)
  217. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  218. &rotl($f,1); # f=ROTATE(f,1)
  219. &mov($tmp1,$a); # b in next round
  220. &rotr($b,7); # b=ROTATE(b,30)
  221. &mov(&swtmp($n%16),$f) if($n<77);# xi=f
  222. &rotl($a,5); # ROTATE(a,5)
  223. &xor($b,$c) if($n==39);# warm up for BODY_40_59
  224. &and($tmp1,$b) if($n==39);
  225. &lea($f,&DWP($K,$f,$e)); # f+=e+K_XX_YY
  226. &mov($e,&swtmp(($n+1)%16)) if($n<79);# pre-fetch f for next round
  227. &add($f,$a); # f+=ROTATE(a,5)
  228. &rotr($a,5) if ($n==79);
  229. } else {
  230. &mov($tmp1,$b); # tmp1 to hold F_20_39(b,c,d)
  231. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  232. &xor($tmp1,$c);
  233. &xor($f,&swtmp(($n+8)%16));
  234. &xor($tmp1,$d); # tmp1 holds F_20_39(b,c,d)
  235. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  236. &rotl($f,1); # f=ROTATE(f,1)
  237. &add($e,$tmp1); # e+=F_20_39(b,c,d)
  238. &rotr($b,2); # b=ROTATE(b,30)
  239. &mov($tmp1,$a);
  240. &rotl($tmp1,5); # ROTATE(a,5)
  241. &mov(&swtmp($n%16),$f) if($n<77);# xi=f
  242. &lea($f,&DWP($K,$f,$e)); # f+=e+K_XX_YY
  243. &mov($e,&swtmp(($n+1)%16)) if($n<79);# pre-fetch f for next round
  244. &add($f,$tmp1); # f+=ROTATE(a,5)
  245. }
  246. }
  247. sub BODY_40_59
  248. {
  249. local($n,$a,$b,$c,$d,$e,$f)=@_;
  250. &comment("40_59 $n");
  251. if ($alt) {
  252. &add($e,$tmp1); # e+=b&(c^d)
  253. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  254. &mov($tmp1,$d);
  255. &xor($f,&swtmp(($n+8)%16));
  256. &xor($c,$d); # restore $c
  257. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  258. &rotl($f,1); # f=ROTATE(f,1)
  259. &and($tmp1,$c);
  260. &rotr($b,7); # b=ROTATE(b,30)
  261. &add($e,$tmp1); # e+=c&d
  262. &mov($tmp1,$a); # b in next round
  263. &mov(&swtmp($n%16),$f); # xi=f
  264. &rotl($a,5); # ROTATE(a,5)
  265. &xor($b,$c) if ($n<59);
  266. &and($tmp1,$b) if ($n<59);# tmp1 to hold F_40_59(b,c,d)
  267. &lea($f,&DWP(0x8f1bbcdc,$f,$e));# f+=K_40_59+e+(b&(c^d))
  268. &mov($e,&swtmp(($n+1)%16)); # pre-fetch f for next round
  269. &add($f,$a); # f+=ROTATE(a,5)
  270. } else {
  271. &mov($tmp1,$c); # tmp1 to hold F_40_59(b,c,d)
  272. &xor($f,&swtmp(($n+2)%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  273. &xor($tmp1,$d);
  274. &xor($f,&swtmp(($n+8)%16));
  275. &and($tmp1,$b);
  276. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  277. &rotl($f,1); # f=ROTATE(f,1)
  278. &add($tmp1,$e); # b&(c^d)+=e
  279. &rotr($b,2); # b=ROTATE(b,30)
  280. &mov($e,$a); # e becomes volatile
  281. &rotl($e,5); # ROTATE(a,5)
  282. &mov(&swtmp($n%16),$f); # xi=f
  283. &lea($f,&DWP(0x8f1bbcdc,$f,$tmp1));# f+=K_40_59+e+(b&(c^d))
  284. &mov($tmp1,$c);
  285. &add($f,$e); # f+=ROTATE(a,5)
  286. &and($tmp1,$d);
  287. &mov($e,&swtmp(($n+1)%16)); # pre-fetch f for next round
  288. &add($f,$tmp1); # f+=c&d
  289. }
  290. }
  291. &function_begin("sha1_block_data_order");
  292. if ($xmm) {
  293. &static_label("shaext_shortcut") if ($shaext);
  294. &static_label("ssse3_shortcut");
  295. &static_label("avx_shortcut") if ($ymm);
  296. &static_label("K_XX_XX");
  297. &call (&label("pic_point")); # make it PIC!
  298. &set_label("pic_point");
  299. &blindpop($tmp1);
  300. &picmeup($T,"OPENSSL_ia32cap_P",$tmp1,&label("pic_point"));
  301. &lea ($tmp1,&DWP(&label("K_XX_XX")."-".&label("pic_point"),$tmp1));
  302. &mov ($A,&DWP(0,$T));
  303. &mov ($D,&DWP(4,$T));
  304. &test ($D,1<<9); # check SSSE3 bit
  305. &jz (&label("x86"));
  306. &mov ($C,&DWP(8,$T));
  307. &test ($A,1<<24); # check FXSR bit
  308. &jz (&label("x86"));
  309. if ($shaext) {
  310. &test ($C,1<<29); # check SHA bit
  311. &jnz (&label("shaext_shortcut"));
  312. }
  313. if ($ymm) {
  314. &and ($D,1<<28); # mask AVX bit
  315. &and ($A,1<<30); # mask "Intel CPU" bit
  316. &or ($A,$D);
  317. &cmp ($A,1<<28|1<<30);
  318. &je (&label("avx_shortcut"));
  319. }
  320. &jmp (&label("ssse3_shortcut"));
  321. &set_label("x86",16);
  322. }
  323. &mov($tmp1,&wparam(0)); # SHA_CTX *c
  324. &mov($T,&wparam(1)); # const void *input
  325. &mov($A,&wparam(2)); # size_t num
  326. &stack_push(16+3); # allocate X[16]
  327. &shl($A,6);
  328. &add($A,$T);
  329. &mov(&wparam(2),$A); # pointer beyond the end of input
  330. &mov($E,&DWP(16,$tmp1));# pre-load E
  331. &jmp(&label("loop"));
  332. &set_label("loop",16);
  333. # copy input chunk to X, but reversing byte order!
  334. for ($i=0; $i<16; $i+=4)
  335. {
  336. &mov($A,&DWP(4*($i+0),$T));
  337. &mov($B,&DWP(4*($i+1),$T));
  338. &mov($C,&DWP(4*($i+2),$T));
  339. &mov($D,&DWP(4*($i+3),$T));
  340. &bswap($A);
  341. &bswap($B);
  342. &bswap($C);
  343. &bswap($D);
  344. &mov(&swtmp($i+0),$A);
  345. &mov(&swtmp($i+1),$B);
  346. &mov(&swtmp($i+2),$C);
  347. &mov(&swtmp($i+3),$D);
  348. }
  349. &mov(&wparam(1),$T); # redundant in 1st spin
  350. &mov($A,&DWP(0,$tmp1)); # load SHA_CTX
  351. &mov($B,&DWP(4,$tmp1));
  352. &mov($C,&DWP(8,$tmp1));
  353. &mov($D,&DWP(12,$tmp1));
  354. # E is pre-loaded
  355. for($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
  356. for(;$i<20;$i++) { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
  357. for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  358. for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  359. for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  360. (($V[5] eq $D) and ($V[0] eq $E)) or die; # double-check
  361. &mov($tmp1,&wparam(0)); # re-load SHA_CTX*
  362. &mov($D,&wparam(1)); # D is last "T" and is discarded
  363. &add($E,&DWP(0,$tmp1)); # E is last "A"...
  364. &add($T,&DWP(4,$tmp1));
  365. &add($A,&DWP(8,$tmp1));
  366. &add($B,&DWP(12,$tmp1));
  367. &add($C,&DWP(16,$tmp1));
  368. &mov(&DWP(0,$tmp1),$E); # update SHA_CTX
  369. &add($D,64); # advance input pointer
  370. &mov(&DWP(4,$tmp1),$T);
  371. &cmp($D,&wparam(2)); # have we reached the end yet?
  372. &mov(&DWP(8,$tmp1),$A);
  373. &mov($E,$C); # C is last "E" which needs to be "pre-loaded"
  374. &mov(&DWP(12,$tmp1),$B);
  375. &mov($T,$D); # input pointer
  376. &mov(&DWP(16,$tmp1),$C);
  377. &jb(&label("loop"));
  378. &stack_pop(16+3);
  379. &function_end("sha1_block_data_order");
  380. if ($xmm) {
  381. if ($shaext) {
  382. ######################################################################
  383. # Intel SHA Extensions implementation of SHA1 update function.
  384. #
  385. my ($ctx,$inp,$num)=("edi","esi","ecx");
  386. my ($ABCD,$E,$E_,$BSWAP)=map("xmm$_",(0..3));
  387. my @MSG=map("xmm$_",(4..7));
  388. sub sha1rnds4 {
  389. my ($dst,$src,$imm)=@_;
  390. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  391. { &data_byte(0x0f,0x3a,0xcc,0xc0|($1<<3)|$2,$imm); }
  392. }
  393. sub sha1op38 {
  394. my ($opcodelet,$dst,$src)=@_;
  395. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  396. { &data_byte(0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2); }
  397. }
  398. sub sha1nexte { sha1op38(0xc8,@_); }
  399. sub sha1msg1 { sha1op38(0xc9,@_); }
  400. sub sha1msg2 { sha1op38(0xca,@_); }
  401. &function_begin("_sha1_block_data_order_shaext");
  402. &call (&label("pic_point")); # make it PIC!
  403. &set_label("pic_point");
  404. &blindpop($tmp1);
  405. &lea ($tmp1,&DWP(&label("K_XX_XX")."-".&label("pic_point"),$tmp1));
  406. &set_label("shaext_shortcut");
  407. &mov ($ctx,&wparam(0));
  408. &mov ("ebx","esp");
  409. &mov ($inp,&wparam(1));
  410. &mov ($num,&wparam(2));
  411. &sub ("esp",32);
  412. &movdqu ($ABCD,&QWP(0,$ctx));
  413. &movd ($E,&DWP(16,$ctx));
  414. &and ("esp",-32);
  415. &movdqa ($BSWAP,&QWP(0x50,$tmp1)); # byte-n-word swap
  416. &movdqu (@MSG[0],&QWP(0,$inp));
  417. &pshufd ($ABCD,$ABCD,0b00011011); # flip word order
  418. &movdqu (@MSG[1],&QWP(0x10,$inp));
  419. &pshufd ($E,$E,0b00011011); # flip word order
  420. &movdqu (@MSG[2],&QWP(0x20,$inp));
  421. &pshufb (@MSG[0],$BSWAP);
  422. &movdqu (@MSG[3],&QWP(0x30,$inp));
  423. &pshufb (@MSG[1],$BSWAP);
  424. &pshufb (@MSG[2],$BSWAP);
  425. &pshufb (@MSG[3],$BSWAP);
  426. &jmp (&label("loop_shaext"));
  427. &set_label("loop_shaext",16);
  428. &dec ($num);
  429. &lea ("eax",&DWP(0x40,$inp));
  430. &movdqa (&QWP(0,"esp"),$E); # offload $E
  431. &paddd ($E,@MSG[0]);
  432. &cmovne ($inp,"eax");
  433. &movdqa (&QWP(16,"esp"),$ABCD); # offload $ABCD
  434. for($i=0;$i<20-4;$i+=2) {
  435. &sha1msg1 (@MSG[0],@MSG[1]);
  436. &movdqa ($E_,$ABCD);
  437. &sha1rnds4 ($ABCD,$E,int($i/5)); # 0-3...
  438. &sha1nexte ($E_,@MSG[1]);
  439. &pxor (@MSG[0],@MSG[2]);
  440. &sha1msg1 (@MSG[1],@MSG[2]);
  441. &sha1msg2 (@MSG[0],@MSG[3]);
  442. &movdqa ($E,$ABCD);
  443. &sha1rnds4 ($ABCD,$E_,int(($i+1)/5));
  444. &sha1nexte ($E,@MSG[2]);
  445. &pxor (@MSG[1],@MSG[3]);
  446. &sha1msg2 (@MSG[1],@MSG[0]);
  447. push(@MSG,shift(@MSG)); push(@MSG,shift(@MSG));
  448. }
  449. &movdqu (@MSG[0],&QWP(0,$inp));
  450. &movdqa ($E_,$ABCD);
  451. &sha1rnds4 ($ABCD,$E,3); # 64-67
  452. &sha1nexte ($E_,@MSG[1]);
  453. &movdqu (@MSG[1],&QWP(0x10,$inp));
  454. &pshufb (@MSG[0],$BSWAP);
  455. &movdqa ($E,$ABCD);
  456. &sha1rnds4 ($ABCD,$E_,3); # 68-71
  457. &sha1nexte ($E,@MSG[2]);
  458. &movdqu (@MSG[2],&QWP(0x20,$inp));
  459. &pshufb (@MSG[1],$BSWAP);
  460. &movdqa ($E_,$ABCD);
  461. &sha1rnds4 ($ABCD,$E,3); # 72-75
  462. &sha1nexte ($E_,@MSG[3]);
  463. &movdqu (@MSG[3],&QWP(0x30,$inp));
  464. &pshufb (@MSG[2],$BSWAP);
  465. &movdqa ($E,$ABCD);
  466. &sha1rnds4 ($ABCD,$E_,3); # 76-79
  467. &movdqa ($E_,&QWP(0,"esp"));
  468. &pshufb (@MSG[3],$BSWAP);
  469. &sha1nexte ($E,$E_);
  470. &paddd ($ABCD,&QWP(16,"esp"));
  471. &jnz (&label("loop_shaext"));
  472. &pshufd ($ABCD,$ABCD,0b00011011);
  473. &pshufd ($E,$E,0b00011011);
  474. &movdqu (&QWP(0,$ctx),$ABCD)
  475. &movd (&DWP(16,$ctx),$E);
  476. &mov ("esp","ebx");
  477. &function_end("_sha1_block_data_order_shaext");
  478. }
  479. ######################################################################
  480. # The SSSE3 implementation.
  481. #
  482. # %xmm[0-7] are used as ring @X[] buffer containing quadruples of last
  483. # 32 elements of the message schedule or Xupdate outputs. First 4
  484. # quadruples are simply byte-swapped input, next 4 are calculated
  485. # according to method originally suggested by Dean Gaudet (modulo
  486. # being implemented in SSSE3). Once 8 quadruples or 32 elements are
  487. # collected, it switches to routine proposed by Max Locktyukhin.
  488. #
  489. # Calculations inevitably require temporary registers, and there are
  490. # no %xmm registers left to spare. For this reason part of the ring
  491. # buffer, X[2..4] to be specific, is offloaded to 3 quadriples ring
  492. # buffer on the stack. Keep in mind that X[2] is alias X[-6], X[3] -
  493. # X[-5], and X[4] - X[-4]...
  494. #
  495. # Another notable optimization is aggressive stack frame compression
  496. # aiming to minimize amount of 9-byte instructions...
  497. #
  498. # Yet another notable optimization is "jumping" $B variable. It means
  499. # that there is no register permanently allocated for $B value. This
  500. # allowed to eliminate one instruction from body_20_39...
  501. #
  502. my $Xi=4; # 4xSIMD Xupdate round, start pre-seeded
  503. my @X=map("xmm$_",(4..7,0..3)); # pre-seeded for $Xi=4
  504. my @V=($A,$B,$C,$D,$E);
  505. my $j=0; # hash round
  506. my $rx=0;
  507. my @T=($T,$tmp1);
  508. my $inp;
  509. my $_rol=sub { &rol(@_) };
  510. my $_ror=sub { &ror(@_) };
  511. &function_begin("_sha1_block_data_order_ssse3");
  512. &call (&label("pic_point")); # make it PIC!
  513. &set_label("pic_point");
  514. &blindpop($tmp1);
  515. &lea ($tmp1,&DWP(&label("K_XX_XX")."-".&label("pic_point"),$tmp1));
  516. &set_label("ssse3_shortcut");
  517. &movdqa (@X[3],&QWP(0,$tmp1)); # K_00_19
  518. &movdqa (@X[4],&QWP(16,$tmp1)); # K_20_39
  519. &movdqa (@X[5],&QWP(32,$tmp1)); # K_40_59
  520. &movdqa (@X[6],&QWP(48,$tmp1)); # K_60_79
  521. &movdqa (@X[2],&QWP(64,$tmp1)); # pbswap mask
  522. &mov ($E,&wparam(0)); # load argument block
  523. &mov ($inp=@T[1],&wparam(1));
  524. &mov ($D,&wparam(2));
  525. &mov (@T[0],"esp");
  526. # stack frame layout
  527. #
  528. # +0 X[0]+K X[1]+K X[2]+K X[3]+K # XMM->IALU xfer area
  529. # X[4]+K X[5]+K X[6]+K X[7]+K
  530. # X[8]+K X[9]+K X[10]+K X[11]+K
  531. # X[12]+K X[13]+K X[14]+K X[15]+K
  532. #
  533. # +64 X[0] X[1] X[2] X[3] # XMM->XMM backtrace area
  534. # X[4] X[5] X[6] X[7]
  535. # X[8] X[9] X[10] X[11] # even borrowed for K_00_19
  536. #
  537. # +112 K_20_39 K_20_39 K_20_39 K_20_39 # constants
  538. # K_40_59 K_40_59 K_40_59 K_40_59
  539. # K_60_79 K_60_79 K_60_79 K_60_79
  540. # K_00_19 K_00_19 K_00_19 K_00_19
  541. # pbswap mask
  542. #
  543. # +192 ctx # argument block
  544. # +196 inp
  545. # +200 end
  546. # +204 esp
  547. &sub ("esp",208);
  548. &and ("esp",-64);
  549. &movdqa (&QWP(112+0,"esp"),@X[4]); # copy constants
  550. &movdqa (&QWP(112+16,"esp"),@X[5]);
  551. &movdqa (&QWP(112+32,"esp"),@X[6]);
  552. &shl ($D,6); # len*64
  553. &movdqa (&QWP(112+48,"esp"),@X[3]);
  554. &add ($D,$inp); # end of input
  555. &movdqa (&QWP(112+64,"esp"),@X[2]);
  556. &add ($inp,64);
  557. &mov (&DWP(192+0,"esp"),$E); # save argument block
  558. &mov (&DWP(192+4,"esp"),$inp);
  559. &mov (&DWP(192+8,"esp"),$D);
  560. &mov (&DWP(192+12,"esp"),@T[0]); # save original %esp
  561. &mov ($A,&DWP(0,$E)); # load context
  562. &mov ($B,&DWP(4,$E));
  563. &mov ($C,&DWP(8,$E));
  564. &mov ($D,&DWP(12,$E));
  565. &mov ($E,&DWP(16,$E));
  566. &mov (@T[0],$B); # magic seed
  567. &movdqu (@X[-4&7],&QWP(-64,$inp)); # load input to %xmm[0-3]
  568. &movdqu (@X[-3&7],&QWP(-48,$inp));
  569. &movdqu (@X[-2&7],&QWP(-32,$inp));
  570. &movdqu (@X[-1&7],&QWP(-16,$inp));
  571. &pshufb (@X[-4&7],@X[2]); # byte swap
  572. &pshufb (@X[-3&7],@X[2]);
  573. &pshufb (@X[-2&7],@X[2]);
  574. &movdqa (&QWP(112-16,"esp"),@X[3]); # borrow last backtrace slot
  575. &pshufb (@X[-1&7],@X[2]);
  576. &paddd (@X[-4&7],@X[3]); # add K_00_19
  577. &paddd (@X[-3&7],@X[3]);
  578. &paddd (@X[-2&7],@X[3]);
  579. &movdqa (&QWP(0,"esp"),@X[-4&7]); # X[]+K xfer to IALU
  580. &psubd (@X[-4&7],@X[3]); # restore X[]
  581. &movdqa (&QWP(0+16,"esp"),@X[-3&7]);
  582. &psubd (@X[-3&7],@X[3]);
  583. &movdqa (&QWP(0+32,"esp"),@X[-2&7]);
  584. &mov (@T[1],$C);
  585. &psubd (@X[-2&7],@X[3]);
  586. &xor (@T[1],$D);
  587. &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
  588. &and (@T[0],@T[1]);
  589. &jmp (&label("loop"));
  590. ######################################################################
  591. # SSE instruction sequence is first broken to groups of independent
  592. # instructions, independent in respect to their inputs and shifter
  593. # (not all architectures have more than one). Then IALU instructions
  594. # are "knitted in" between the SSE groups. Distance is maintained for
  595. # SSE latency of 2 in hope that it fits better upcoming AMD Bulldozer
  596. # [which allegedly also implements SSSE3]...
  597. #
  598. # Temporary registers usage. X[2] is volatile at the entry and at the
  599. # end is restored from backtrace ring buffer. X[3] is expected to
  600. # contain current K_XX_XX constant and is used to calculate X[-1]+K
  601. # from previous round, it becomes volatile the moment the value is
  602. # saved to stack for transfer to IALU. X[4] becomes volatile whenever
  603. # X[-4] is accumulated and offloaded to backtrace ring buffer, at the
  604. # end it is loaded with next K_XX_XX [which becomes X[3] in next
  605. # round]...
  606. #
  607. sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
  608. { use integer;
  609. my $body = shift;
  610. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  611. my ($a,$b,$c,$d,$e);
  612. eval(shift(@insns)); # ror
  613. eval(shift(@insns));
  614. eval(shift(@insns));
  615. &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
  616. &movdqa (@X[2],@X[-1&7]);
  617. eval(shift(@insns));
  618. eval(shift(@insns));
  619. &paddd (@X[3],@X[-1&7]);
  620. &movdqa (&QWP(64+16*(($Xi-4)%3),"esp"),@X[-4&7]);# save X[] to backtrace buffer
  621. eval(shift(@insns)); # rol
  622. eval(shift(@insns));
  623. &psrldq (@X[2],4); # "X[-3]", 3 dwords
  624. eval(shift(@insns));
  625. eval(shift(@insns));
  626. &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  627. eval(shift(@insns));
  628. eval(shift(@insns)); # ror
  629. &pxor (@X[2],@X[-2&7]); # "X[-3]"^"X[-8]"
  630. eval(shift(@insns));
  631. eval(shift(@insns));
  632. eval(shift(@insns));
  633. &pxor (@X[0],@X[2]); # "X[0]"^="X[-3]"^"X[-8]"
  634. eval(shift(@insns));
  635. eval(shift(@insns)); # rol
  636. &movdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer to IALU
  637. eval(shift(@insns));
  638. eval(shift(@insns));
  639. &movdqa (@X[4],@X[0]);
  640. eval(shift(@insns));
  641. eval(shift(@insns));
  642. eval(shift(@insns)); # ror
  643. &movdqa (@X[2],@X[0]);
  644. eval(shift(@insns));
  645. &pslldq (@X[4],12); # "X[0]"<<96, extract one dword
  646. &paddd (@X[0],@X[0]);
  647. eval(shift(@insns));
  648. eval(shift(@insns));
  649. &psrld (@X[2],31);
  650. eval(shift(@insns));
  651. eval(shift(@insns)); # rol
  652. &movdqa (@X[3],@X[4]);
  653. eval(shift(@insns));
  654. eval(shift(@insns));
  655. eval(shift(@insns));
  656. &psrld (@X[4],30);
  657. eval(shift(@insns));
  658. eval(shift(@insns)); # ror
  659. &por (@X[0],@X[2]); # "X[0]"<<<=1
  660. eval(shift(@insns));
  661. &movdqa (@X[2],&QWP(64+16*(($Xi-6)%3),"esp")) if ($Xi>5); # restore X[] from backtrace buffer
  662. eval(shift(@insns));
  663. eval(shift(@insns));
  664. &pslld (@X[3],2);
  665. eval(shift(@insns));
  666. eval(shift(@insns)); # rol
  667. &pxor (@X[0],@X[4]);
  668. &movdqa (@X[4],&QWP(112-16+16*(($Xi)/5),"esp")); # K_XX_XX
  669. eval(shift(@insns));
  670. eval(shift(@insns));
  671. &pxor (@X[0],@X[3]); # "X[0]"^=("X[0]"<<96)<<<2
  672. &pshufd (@X[1],@X[-3&7],0xee) if ($Xi<7); # was &movdqa (@X[1],@X[-2&7])
  673. &pshufd (@X[3],@X[-1&7],0xee) if ($Xi==7);
  674. eval(shift(@insns));
  675. eval(shift(@insns));
  676. foreach (@insns) { eval; } # remaining instructions [if any]
  677. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  678. }
  679. sub Xupdate_ssse3_32_79()
  680. { use integer;
  681. my $body = shift;
  682. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  683. my ($a,$b,$c,$d,$e);
  684. eval(shift(@insns)); # body_20_39
  685. &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  686. &punpcklqdq(@X[2],@X[-1&7]); # compose "X[-6]", was &palignr(@X[2],@X[-2&7],8)
  687. eval(shift(@insns));
  688. eval(shift(@insns));
  689. eval(shift(@insns)); # rol
  690. &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  691. &movdqa (&QWP(64+16*(($Xi-4)%3),"esp"),@X[-4&7]); # save X[] to backtrace buffer
  692. eval(shift(@insns));
  693. eval(shift(@insns));
  694. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  695. if ($Xi%5) {
  696. &movdqa (@X[4],@X[3]); # "perpetuate" K_XX_XX...
  697. } else { # ... or load next one
  698. &movdqa (@X[4],&QWP(112-16+16*($Xi/5),"esp"));
  699. }
  700. eval(shift(@insns)); # ror
  701. &paddd (@X[3],@X[-1&7]);
  702. eval(shift(@insns));
  703. &pxor (@X[0],@X[2]); # "X[0]"^="X[-6]"
  704. eval(shift(@insns)); # body_20_39
  705. eval(shift(@insns));
  706. eval(shift(@insns));
  707. eval(shift(@insns)); # rol
  708. &movdqa (@X[2],@X[0]);
  709. &movdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer to IALU
  710. eval(shift(@insns));
  711. eval(shift(@insns));
  712. eval(shift(@insns)); # ror
  713. eval(shift(@insns));
  714. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  715. &pslld (@X[0],2);
  716. eval(shift(@insns)); # body_20_39
  717. eval(shift(@insns));
  718. &psrld (@X[2],30);
  719. eval(shift(@insns));
  720. eval(shift(@insns)); # rol
  721. eval(shift(@insns));
  722. eval(shift(@insns));
  723. eval(shift(@insns)); # ror
  724. eval(shift(@insns));
  725. eval(shift(@insns)) if (@insns[1] =~ /_rol/);
  726. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  727. &por (@X[0],@X[2]); # "X[0]"<<<=2
  728. eval(shift(@insns)); # body_20_39
  729. eval(shift(@insns));
  730. &movdqa (@X[2],&QWP(64+16*(($Xi-6)%3),"esp")) if($Xi<19); # restore X[] from backtrace buffer
  731. eval(shift(@insns));
  732. eval(shift(@insns)); # rol
  733. eval(shift(@insns));
  734. eval(shift(@insns));
  735. eval(shift(@insns)); # ror
  736. &pshufd (@X[3],@X[-1],0xee) if ($Xi<19); # was &movdqa (@X[3],@X[0])
  737. eval(shift(@insns));
  738. foreach (@insns) { eval; } # remaining instructions
  739. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  740. }
  741. sub Xuplast_ssse3_80()
  742. { use integer;
  743. my $body = shift;
  744. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  745. my ($a,$b,$c,$d,$e);
  746. eval(shift(@insns));
  747. eval(shift(@insns));
  748. eval(shift(@insns));
  749. eval(shift(@insns));
  750. eval(shift(@insns));
  751. eval(shift(@insns));
  752. eval(shift(@insns));
  753. &paddd (@X[3],@X[-1&7]);
  754. eval(shift(@insns));
  755. eval(shift(@insns));
  756. eval(shift(@insns));
  757. eval(shift(@insns));
  758. &movdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer IALU
  759. foreach (@insns) { eval; } # remaining instructions
  760. &mov ($inp=@T[1],&DWP(192+4,"esp"));
  761. &cmp ($inp,&DWP(192+8,"esp"));
  762. &je (&label("done"));
  763. &movdqa (@X[3],&QWP(112+48,"esp")); # K_00_19
  764. &movdqa (@X[2],&QWP(112+64,"esp")); # pbswap mask
  765. &movdqu (@X[-4&7],&QWP(0,$inp)); # load input
  766. &movdqu (@X[-3&7],&QWP(16,$inp));
  767. &movdqu (@X[-2&7],&QWP(32,$inp));
  768. &movdqu (@X[-1&7],&QWP(48,$inp));
  769. &add ($inp,64);
  770. &pshufb (@X[-4&7],@X[2]); # byte swap
  771. &mov (&DWP(192+4,"esp"),$inp);
  772. &movdqa (&QWP(112-16,"esp"),@X[3]); # borrow last backtrace slot
  773. $Xi=0;
  774. }
  775. sub Xloop_ssse3()
  776. { use integer;
  777. my $body = shift;
  778. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  779. my ($a,$b,$c,$d,$e);
  780. eval(shift(@insns));
  781. eval(shift(@insns));
  782. eval(shift(@insns));
  783. eval(shift(@insns));
  784. eval(shift(@insns));
  785. eval(shift(@insns));
  786. eval(shift(@insns));
  787. &pshufb (@X[($Xi-3)&7],@X[2]);
  788. eval(shift(@insns));
  789. eval(shift(@insns));
  790. eval(shift(@insns));
  791. eval(shift(@insns));
  792. &paddd (@X[($Xi-4)&7],@X[3]);
  793. eval(shift(@insns));
  794. eval(shift(@insns));
  795. eval(shift(@insns));
  796. eval(shift(@insns));
  797. &movdqa (&QWP(0+16*$Xi,"esp"),@X[($Xi-4)&7]); # X[]+K xfer to IALU
  798. eval(shift(@insns));
  799. eval(shift(@insns));
  800. eval(shift(@insns));
  801. eval(shift(@insns));
  802. &psubd (@X[($Xi-4)&7],@X[3]);
  803. foreach (@insns) { eval; }
  804. $Xi++;
  805. }
  806. sub Xtail_ssse3()
  807. { use integer;
  808. my $body = shift;
  809. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  810. my ($a,$b,$c,$d,$e);
  811. foreach (@insns) { eval; }
  812. }
  813. sub body_00_19 () { # ((c^d)&b)^d
  814. # on start @T[0]=(c^d)&b
  815. return &body_20_39() if ($rx==19); $rx++;
  816. (
  817. '($a,$b,$c,$d,$e)=@V;'.
  818. '&$_ror ($b,$j?7:2);', # $b>>>2
  819. '&xor (@T[0],$d);',
  820. '&mov (@T[1],$a);', # $b in next round
  821. '&add ($e,&DWP(4*($j&15),"esp"));', # X[]+K xfer
  822. '&xor ($b,$c);', # $c^$d for next round
  823. '&$_rol ($a,5);',
  824. '&add ($e,@T[0]);',
  825. '&and (@T[1],$b);', # ($b&($c^$d)) for next round
  826. '&xor ($b,$c);', # restore $b
  827. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  828. );
  829. }
  830. sub body_20_39 () { # b^d^c
  831. # on entry @T[0]=b^d
  832. return &body_40_59() if ($rx==39); $rx++;
  833. (
  834. '($a,$b,$c,$d,$e)=@V;'.
  835. '&add ($e,&DWP(4*($j&15),"esp"));', # X[]+K xfer
  836. '&xor (@T[0],$d) if($j==19);'.
  837. '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
  838. '&mov (@T[1],$a);', # $b in next round
  839. '&$_rol ($a,5);',
  840. '&add ($e,@T[0]);',
  841. '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
  842. '&$_ror ($b,7);', # $b>>>2
  843. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  844. );
  845. }
  846. sub body_40_59 () { # ((b^c)&(c^d))^c
  847. # on entry @T[0]=(b^c), (c^=d)
  848. $rx++;
  849. (
  850. '($a,$b,$c,$d,$e)=@V;'.
  851. '&add ($e,&DWP(4*($j&15),"esp"));', # X[]+K xfer
  852. '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
  853. '&xor ($c,$d) if ($j>=40);', # restore $c
  854. '&$_ror ($b,7);', # $b>>>2
  855. '&mov (@T[1],$a);', # $b for next round
  856. '&xor (@T[0],$c);',
  857. '&$_rol ($a,5);',
  858. '&add ($e,@T[0]);',
  859. '&xor (@T[1],$c) if ($j==59);'.
  860. '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
  861. '&xor ($b,$c) if ($j< 59);', # c^d for next round
  862. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  863. );
  864. }
  865. ######
  866. sub bodyx_00_19 () { # ((c^d)&b)^d
  867. # on start @T[0]=(b&c)^(~b&d), $e+=X[]+K
  868. return &bodyx_20_39() if ($rx==19); $rx++;
  869. (
  870. '($a,$b,$c,$d,$e)=@V;'.
  871. '&rorx ($b,$b,2) if ($j==0);'. # $b>>>2
  872. '&rorx ($b,@T[1],7) if ($j!=0);', # $b>>>2
  873. '&lea ($e,&DWP(0,$e,@T[0]));',
  874. '&rorx (@T[0],$a,5);',
  875. '&andn (@T[1],$a,$c);',
  876. '&and ($a,$b)',
  877. '&add ($d,&DWP(4*(($j+1)&15),"esp"));', # X[]+K xfer
  878. '&xor (@T[1],$a)',
  879. '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  880. );
  881. }
  882. sub bodyx_20_39 () { # b^d^c
  883. # on start $b=b^c^d
  884. return &bodyx_40_59() if ($rx==39); $rx++;
  885. (
  886. '($a,$b,$c,$d,$e)=@V;'.
  887. '&add ($e,($j==19?@T[0]:$b))',
  888. '&rorx ($b,@T[1],7);', # $b>>>2
  889. '&rorx (@T[0],$a,5);',
  890. '&xor ($a,$b) if ($j<79);',
  891. '&add ($d,&DWP(4*(($j+1)&15),"esp")) if ($j<79);', # X[]+K xfer
  892. '&xor ($a,$c) if ($j<79);',
  893. '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  894. );
  895. }
  896. sub bodyx_40_59 () { # ((b^c)&(c^d))^c
  897. # on start $b=((b^c)&(c^d))^c
  898. return &bodyx_20_39() if ($rx==59); $rx++;
  899. (
  900. '($a,$b,$c,$d,$e)=@V;'.
  901. '&rorx (@T[0],$a,5)',
  902. '&lea ($e,&DWP(0,$e,$b))',
  903. '&rorx ($b,@T[1],7)', # $b>>>2
  904. '&add ($d,&DWP(4*(($j+1)&15),"esp"))', # X[]+K xfer
  905. '&mov (@T[1],$c)',
  906. '&xor ($a,$b)', # b^c for next round
  907. '&xor (@T[1],$b)', # c^d for next round
  908. '&and ($a,@T[1])',
  909. '&add ($e,@T[0])',
  910. '&xor ($a,$b)' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  911. );
  912. }
  913. &set_label("loop",16);
  914. &Xupdate_ssse3_16_31(\&body_00_19);
  915. &Xupdate_ssse3_16_31(\&body_00_19);
  916. &Xupdate_ssse3_16_31(\&body_00_19);
  917. &Xupdate_ssse3_16_31(\&body_00_19);
  918. &Xupdate_ssse3_32_79(\&body_00_19);
  919. &Xupdate_ssse3_32_79(\&body_20_39);
  920. &Xupdate_ssse3_32_79(\&body_20_39);
  921. &Xupdate_ssse3_32_79(\&body_20_39);
  922. &Xupdate_ssse3_32_79(\&body_20_39);
  923. &Xupdate_ssse3_32_79(\&body_20_39);
  924. &Xupdate_ssse3_32_79(\&body_40_59);
  925. &Xupdate_ssse3_32_79(\&body_40_59);
  926. &Xupdate_ssse3_32_79(\&body_40_59);
  927. &Xupdate_ssse3_32_79(\&body_40_59);
  928. &Xupdate_ssse3_32_79(\&body_40_59);
  929. &Xupdate_ssse3_32_79(\&body_20_39);
  930. &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
  931. $saved_j=$j; @saved_V=@V;
  932. &Xloop_ssse3(\&body_20_39);
  933. &Xloop_ssse3(\&body_20_39);
  934. &Xloop_ssse3(\&body_20_39);
  935. &mov (@T[1],&DWP(192,"esp")); # update context
  936. &add ($A,&DWP(0,@T[1]));
  937. &add (@T[0],&DWP(4,@T[1])); # $b
  938. &add ($C,&DWP(8,@T[1]));
  939. &mov (&DWP(0,@T[1]),$A);
  940. &add ($D,&DWP(12,@T[1]));
  941. &mov (&DWP(4,@T[1]),@T[0]);
  942. &add ($E,&DWP(16,@T[1]));
  943. &mov (&DWP(8,@T[1]),$C);
  944. &mov ($B,$C);
  945. &mov (&DWP(12,@T[1]),$D);
  946. &xor ($B,$D);
  947. &mov (&DWP(16,@T[1]),$E);
  948. &mov (@T[1],@T[0]);
  949. &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
  950. &and (@T[0],$B);
  951. &mov ($B,$T[1]);
  952. &jmp (&label("loop"));
  953. &set_label("done",16); $j=$saved_j; @V=@saved_V;
  954. &Xtail_ssse3(\&body_20_39);
  955. &Xtail_ssse3(\&body_20_39);
  956. &Xtail_ssse3(\&body_20_39);
  957. &mov (@T[1],&DWP(192,"esp")); # update context
  958. &add ($A,&DWP(0,@T[1]));
  959. &mov ("esp",&DWP(192+12,"esp")); # restore %esp
  960. &add (@T[0],&DWP(4,@T[1])); # $b
  961. &add ($C,&DWP(8,@T[1]));
  962. &mov (&DWP(0,@T[1]),$A);
  963. &add ($D,&DWP(12,@T[1]));
  964. &mov (&DWP(4,@T[1]),@T[0]);
  965. &add ($E,&DWP(16,@T[1]));
  966. &mov (&DWP(8,@T[1]),$C);
  967. &mov (&DWP(12,@T[1]),$D);
  968. &mov (&DWP(16,@T[1]),$E);
  969. &function_end("_sha1_block_data_order_ssse3");
  970. $rx=0; # reset
  971. if ($ymm) {
  972. my $Xi=4; # 4xSIMD Xupdate round, start pre-seeded
  973. my @X=map("xmm$_",(4..7,0..3)); # pre-seeded for $Xi=4
  974. my @V=($A,$B,$C,$D,$E);
  975. my $j=0; # hash round
  976. my @T=($T,$tmp1);
  977. my $inp;
  978. my $_rol=sub { &shld(@_[0],@_) };
  979. my $_ror=sub { &shrd(@_[0],@_) };
  980. &function_begin("_sha1_block_data_order_avx");
  981. &call (&label("pic_point")); # make it PIC!
  982. &set_label("pic_point");
  983. &blindpop($tmp1);
  984. &lea ($tmp1,&DWP(&label("K_XX_XX")."-".&label("pic_point"),$tmp1));
  985. &set_label("avx_shortcut");
  986. &vzeroall();
  987. &vmovdqa(@X[3],&QWP(0,$tmp1)); # K_00_19
  988. &vmovdqa(@X[4],&QWP(16,$tmp1)); # K_20_39
  989. &vmovdqa(@X[5],&QWP(32,$tmp1)); # K_40_59
  990. &vmovdqa(@X[6],&QWP(48,$tmp1)); # K_60_79
  991. &vmovdqa(@X[2],&QWP(64,$tmp1)); # pbswap mask
  992. &mov ($E,&wparam(0)); # load argument block
  993. &mov ($inp=@T[1],&wparam(1));
  994. &mov ($D,&wparam(2));
  995. &mov (@T[0],"esp");
  996. # stack frame layout
  997. #
  998. # +0 X[0]+K X[1]+K X[2]+K X[3]+K # XMM->IALU xfer area
  999. # X[4]+K X[5]+K X[6]+K X[7]+K
  1000. # X[8]+K X[9]+K X[10]+K X[11]+K
  1001. # X[12]+K X[13]+K X[14]+K X[15]+K
  1002. #
  1003. # +64 X[0] X[1] X[2] X[3] # XMM->XMM backtrace area
  1004. # X[4] X[5] X[6] X[7]
  1005. # X[8] X[9] X[10] X[11] # even borrowed for K_00_19
  1006. #
  1007. # +112 K_20_39 K_20_39 K_20_39 K_20_39 # constants
  1008. # K_40_59 K_40_59 K_40_59 K_40_59
  1009. # K_60_79 K_60_79 K_60_79 K_60_79
  1010. # K_00_19 K_00_19 K_00_19 K_00_19
  1011. # pbswap mask
  1012. #
  1013. # +192 ctx # argument block
  1014. # +196 inp
  1015. # +200 end
  1016. # +204 esp
  1017. &sub ("esp",208);
  1018. &and ("esp",-64);
  1019. &vmovdqa(&QWP(112+0,"esp"),@X[4]); # copy constants
  1020. &vmovdqa(&QWP(112+16,"esp"),@X[5]);
  1021. &vmovdqa(&QWP(112+32,"esp"),@X[6]);
  1022. &shl ($D,6); # len*64
  1023. &vmovdqa(&QWP(112+48,"esp"),@X[3]);
  1024. &add ($D,$inp); # end of input
  1025. &vmovdqa(&QWP(112+64,"esp"),@X[2]);
  1026. &add ($inp,64);
  1027. &mov (&DWP(192+0,"esp"),$E); # save argument block
  1028. &mov (&DWP(192+4,"esp"),$inp);
  1029. &mov (&DWP(192+8,"esp"),$D);
  1030. &mov (&DWP(192+12,"esp"),@T[0]); # save original %esp
  1031. &mov ($A,&DWP(0,$E)); # load context
  1032. &mov ($B,&DWP(4,$E));
  1033. &mov ($C,&DWP(8,$E));
  1034. &mov ($D,&DWP(12,$E));
  1035. &mov ($E,&DWP(16,$E));
  1036. &mov (@T[0],$B); # magic seed
  1037. &vmovdqu(@X[-4&7],&QWP(-64,$inp)); # load input to %xmm[0-3]
  1038. &vmovdqu(@X[-3&7],&QWP(-48,$inp));
  1039. &vmovdqu(@X[-2&7],&QWP(-32,$inp));
  1040. &vmovdqu(@X[-1&7],&QWP(-16,$inp));
  1041. &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
  1042. &vpshufb(@X[-3&7],@X[-3&7],@X[2]);
  1043. &vpshufb(@X[-2&7],@X[-2&7],@X[2]);
  1044. &vmovdqa(&QWP(112-16,"esp"),@X[3]); # borrow last backtrace slot
  1045. &vpshufb(@X[-1&7],@X[-1&7],@X[2]);
  1046. &vpaddd (@X[0],@X[-4&7],@X[3]); # add K_00_19
  1047. &vpaddd (@X[1],@X[-3&7],@X[3]);
  1048. &vpaddd (@X[2],@X[-2&7],@X[3]);
  1049. &vmovdqa(&QWP(0,"esp"),@X[0]); # X[]+K xfer to IALU
  1050. &mov (@T[1],$C);
  1051. &vmovdqa(&QWP(0+16,"esp"),@X[1]);
  1052. &xor (@T[1],$D);
  1053. &vmovdqa(&QWP(0+32,"esp"),@X[2]);
  1054. &and (@T[0],@T[1]);
  1055. &jmp (&label("loop"));
  1056. sub Xupdate_avx_16_31() # recall that $Xi starts with 4
  1057. { use integer;
  1058. my $body = shift;
  1059. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  1060. my ($a,$b,$c,$d,$e);
  1061. eval(shift(@insns));
  1062. eval(shift(@insns));
  1063. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  1064. eval(shift(@insns));
  1065. eval(shift(@insns));
  1066. &vpaddd (@X[3],@X[3],@X[-1&7]);
  1067. &vmovdqa (&QWP(64+16*(($Xi-4)%3),"esp"),@X[-4&7]);# save X[] to backtrace buffer
  1068. eval(shift(@insns));
  1069. eval(shift(@insns));
  1070. &vpsrldq(@X[2],@X[-1&7],4); # "X[-3]", 3 dwords
  1071. eval(shift(@insns));
  1072. eval(shift(@insns));
  1073. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  1074. eval(shift(@insns));
  1075. eval(shift(@insns));
  1076. &vpxor (@X[2],@X[2],@X[-2&7]); # "X[-3]"^"X[-8]"
  1077. eval(shift(@insns));
  1078. eval(shift(@insns));
  1079. &vmovdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer to IALU
  1080. eval(shift(@insns));
  1081. eval(shift(@insns));
  1082. &vpxor (@X[0],@X[0],@X[2]); # "X[0]"^="X[-3]"^"X[-8]"
  1083. eval(shift(@insns));
  1084. eval(shift(@insns));
  1085. eval(shift(@insns));
  1086. eval(shift(@insns));
  1087. &vpsrld (@X[2],@X[0],31);
  1088. eval(shift(@insns));
  1089. eval(shift(@insns));
  1090. eval(shift(@insns));
  1091. eval(shift(@insns));
  1092. &vpslldq(@X[4],@X[0],12); # "X[0]"<<96, extract one dword
  1093. &vpaddd (@X[0],@X[0],@X[0]);
  1094. eval(shift(@insns));
  1095. eval(shift(@insns));
  1096. eval(shift(@insns));
  1097. eval(shift(@insns));
  1098. &vpsrld (@X[3],@X[4],30);
  1099. &vpor (@X[0],@X[0],@X[2]); # "X[0]"<<<=1
  1100. eval(shift(@insns));
  1101. eval(shift(@insns));
  1102. eval(shift(@insns));
  1103. eval(shift(@insns));
  1104. &vpslld (@X[4],@X[4],2);
  1105. &vmovdqa (@X[2],&QWP(64+16*(($Xi-6)%3),"esp")) if ($Xi>5); # restore X[] from backtrace buffer
  1106. eval(shift(@insns));
  1107. eval(shift(@insns));
  1108. &vpxor (@X[0],@X[0],@X[3]);
  1109. eval(shift(@insns));
  1110. eval(shift(@insns));
  1111. eval(shift(@insns));
  1112. eval(shift(@insns));
  1113. &vpxor (@X[0],@X[0],@X[4]); # "X[0]"^=("X[0]"<<96)<<<2
  1114. eval(shift(@insns));
  1115. eval(shift(@insns));
  1116. &vmovdqa (@X[4],&QWP(112-16+16*(($Xi)/5),"esp")); # K_XX_XX
  1117. eval(shift(@insns));
  1118. eval(shift(@insns));
  1119. foreach (@insns) { eval; } # remaining instructions [if any]
  1120. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1121. }
  1122. sub Xupdate_avx_32_79()
  1123. { use integer;
  1124. my $body = shift;
  1125. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  1126. my ($a,$b,$c,$d,$e);
  1127. &vpalignr(@X[2],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
  1128. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  1129. eval(shift(@insns)); # body_20_39
  1130. eval(shift(@insns));
  1131. eval(shift(@insns));
  1132. eval(shift(@insns)); # rol
  1133. &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  1134. &vmovdqa (&QWP(64+16*(($Xi-4)%3),"esp"),@X[-4&7]); # save X[] to backtrace buffer
  1135. eval(shift(@insns));
  1136. eval(shift(@insns));
  1137. if ($Xi%5) {
  1138. &vmovdqa (@X[4],@X[3]); # "perpetuate" K_XX_XX...
  1139. } else { # ... or load next one
  1140. &vmovdqa (@X[4],&QWP(112-16+16*($Xi/5),"esp"));
  1141. }
  1142. &vpaddd (@X[3],@X[3],@X[-1&7]);
  1143. eval(shift(@insns)); # ror
  1144. eval(shift(@insns));
  1145. &vpxor (@X[0],@X[0],@X[2]); # "X[0]"^="X[-6]"
  1146. eval(shift(@insns)); # body_20_39
  1147. eval(shift(@insns));
  1148. eval(shift(@insns));
  1149. eval(shift(@insns)); # rol
  1150. &vpsrld (@X[2],@X[0],30);
  1151. &vmovdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer to IALU
  1152. eval(shift(@insns));
  1153. eval(shift(@insns));
  1154. eval(shift(@insns)); # ror
  1155. eval(shift(@insns));
  1156. &vpslld (@X[0],@X[0],2);
  1157. eval(shift(@insns)); # body_20_39
  1158. eval(shift(@insns));
  1159. eval(shift(@insns));
  1160. eval(shift(@insns)); # rol
  1161. eval(shift(@insns));
  1162. eval(shift(@insns));
  1163. eval(shift(@insns)); # ror
  1164. eval(shift(@insns));
  1165. &vpor (@X[0],@X[0],@X[2]); # "X[0]"<<<=2
  1166. eval(shift(@insns)); # body_20_39
  1167. eval(shift(@insns));
  1168. &vmovdqa (@X[2],&QWP(64+16*(($Xi-6)%3),"esp")) if($Xi<19); # restore X[] from backtrace buffer
  1169. eval(shift(@insns));
  1170. eval(shift(@insns)); # rol
  1171. eval(shift(@insns));
  1172. eval(shift(@insns));
  1173. eval(shift(@insns)); # ror
  1174. eval(shift(@insns));
  1175. foreach (@insns) { eval; } # remaining instructions
  1176. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1177. }
  1178. sub Xuplast_avx_80()
  1179. { use integer;
  1180. my $body = shift;
  1181. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1182. my ($a,$b,$c,$d,$e);
  1183. eval(shift(@insns));
  1184. &vpaddd (@X[3],@X[3],@X[-1&7]);
  1185. eval(shift(@insns));
  1186. eval(shift(@insns));
  1187. eval(shift(@insns));
  1188. eval(shift(@insns));
  1189. &vmovdqa (&QWP(0+16*(($Xi-1)&3),"esp"),@X[3]); # X[]+K xfer IALU
  1190. foreach (@insns) { eval; } # remaining instructions
  1191. &mov ($inp=@T[1],&DWP(192+4,"esp"));
  1192. &cmp ($inp,&DWP(192+8,"esp"));
  1193. &je (&label("done"));
  1194. &vmovdqa(@X[3],&QWP(112+48,"esp")); # K_00_19
  1195. &vmovdqa(@X[2],&QWP(112+64,"esp")); # pbswap mask
  1196. &vmovdqu(@X[-4&7],&QWP(0,$inp)); # load input
  1197. &vmovdqu(@X[-3&7],&QWP(16,$inp));
  1198. &vmovdqu(@X[-2&7],&QWP(32,$inp));
  1199. &vmovdqu(@X[-1&7],&QWP(48,$inp));
  1200. &add ($inp,64);
  1201. &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
  1202. &mov (&DWP(192+4,"esp"),$inp);
  1203. &vmovdqa(&QWP(112-16,"esp"),@X[3]); # borrow last backtrace slot
  1204. $Xi=0;
  1205. }
  1206. sub Xloop_avx()
  1207. { use integer;
  1208. my $body = shift;
  1209. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1210. my ($a,$b,$c,$d,$e);
  1211. eval(shift(@insns));
  1212. eval(shift(@insns));
  1213. &vpshufb (@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
  1214. eval(shift(@insns));
  1215. eval(shift(@insns));
  1216. &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@X[3]);
  1217. eval(shift(@insns));
  1218. eval(shift(@insns));
  1219. eval(shift(@insns));
  1220. eval(shift(@insns));
  1221. &vmovdqa (&QWP(0+16*$Xi,"esp"),@X[$Xi&7]); # X[]+K xfer to IALU
  1222. eval(shift(@insns));
  1223. eval(shift(@insns));
  1224. foreach (@insns) { eval; }
  1225. $Xi++;
  1226. }
  1227. sub Xtail_avx()
  1228. { use integer;
  1229. my $body = shift;
  1230. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1231. my ($a,$b,$c,$d,$e);
  1232. foreach (@insns) { eval; }
  1233. }
  1234. &set_label("loop",16);
  1235. &Xupdate_avx_16_31(\&body_00_19);
  1236. &Xupdate_avx_16_31(\&body_00_19);
  1237. &Xupdate_avx_16_31(\&body_00_19);
  1238. &Xupdate_avx_16_31(\&body_00_19);
  1239. &Xupdate_avx_32_79(\&body_00_19);
  1240. &Xupdate_avx_32_79(\&body_20_39);
  1241. &Xupdate_avx_32_79(\&body_20_39);
  1242. &Xupdate_avx_32_79(\&body_20_39);
  1243. &Xupdate_avx_32_79(\&body_20_39);
  1244. &Xupdate_avx_32_79(\&body_20_39);
  1245. &Xupdate_avx_32_79(\&body_40_59);
  1246. &Xupdate_avx_32_79(\&body_40_59);
  1247. &Xupdate_avx_32_79(\&body_40_59);
  1248. &Xupdate_avx_32_79(\&body_40_59);
  1249. &Xupdate_avx_32_79(\&body_40_59);
  1250. &Xupdate_avx_32_79(\&body_20_39);
  1251. &Xuplast_avx_80(\&body_20_39); # can jump to "done"
  1252. $saved_j=$j; @saved_V=@V;
  1253. &Xloop_avx(\&body_20_39);
  1254. &Xloop_avx(\&body_20_39);
  1255. &Xloop_avx(\&body_20_39);
  1256. &mov (@T[1],&DWP(192,"esp")); # update context
  1257. &add ($A,&DWP(0,@T[1]));
  1258. &add (@T[0],&DWP(4,@T[1])); # $b
  1259. &add ($C,&DWP(8,@T[1]));
  1260. &mov (&DWP(0,@T[1]),$A);
  1261. &add ($D,&DWP(12,@T[1]));
  1262. &mov (&DWP(4,@T[1]),@T[0]);
  1263. &add ($E,&DWP(16,@T[1]));
  1264. &mov ($B,$C);
  1265. &mov (&DWP(8,@T[1]),$C);
  1266. &xor ($B,$D);
  1267. &mov (&DWP(12,@T[1]),$D);
  1268. &mov (&DWP(16,@T[1]),$E);
  1269. &mov (@T[1],@T[0]);
  1270. &and (@T[0],$B);
  1271. &mov ($B,@T[1]);
  1272. &jmp (&label("loop"));
  1273. &set_label("done",16); $j=$saved_j; @V=@saved_V;
  1274. &Xtail_avx(\&body_20_39);
  1275. &Xtail_avx(\&body_20_39);
  1276. &Xtail_avx(\&body_20_39);
  1277. &vzeroall();
  1278. &mov (@T[1],&DWP(192,"esp")); # update context
  1279. &add ($A,&DWP(0,@T[1]));
  1280. &mov ("esp",&DWP(192+12,"esp")); # restore %esp
  1281. &add (@T[0],&DWP(4,@T[1])); # $b
  1282. &add ($C,&DWP(8,@T[1]));
  1283. &mov (&DWP(0,@T[1]),$A);
  1284. &add ($D,&DWP(12,@T[1]));
  1285. &mov (&DWP(4,@T[1]),@T[0]);
  1286. &add ($E,&DWP(16,@T[1]));
  1287. &mov (&DWP(8,@T[1]),$C);
  1288. &mov (&DWP(12,@T[1]),$D);
  1289. &mov (&DWP(16,@T[1]),$E);
  1290. &function_end("_sha1_block_data_order_avx");
  1291. }
  1292. &set_label("K_XX_XX",64);
  1293. &data_word(0x5a827999,0x5a827999,0x5a827999,0x5a827999); # K_00_19
  1294. &data_word(0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1); # K_20_39
  1295. &data_word(0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc); # K_40_59
  1296. &data_word(0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6); # K_60_79
  1297. &data_word(0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f); # pbswap mask
  1298. &data_byte(0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0);
  1299. }
  1300. &asciz("SHA1 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
  1301. &asm_finish();
  1302. close STDOUT or die "error closing STDOUT: $!";