sha1-586.pl 43 KB

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