sha1-mb-x86_64.pl 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. #! /usr/bin/env perl
  2. # Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # Multi-buffer SHA1 procedure processes n buffers in parallel by
  15. # placing buffer data to designated lane of SIMD register. n is
  16. # naturally limited to 4 on pre-AVX2 processors and to 8 on
  17. # AVX2-capable processors such as Haswell.
  18. #
  19. # this +aesni(i) sha1 aesni-sha1 gain(iv)
  20. # -------------------------------------------------------------------
  21. # Westmere(ii) 10.7/n +1.28=3.96(n=4) 5.30 6.66 +68%
  22. # Atom(ii) 18.1/n +3.93=8.46(n=4) 9.37 12.8 +51%
  23. # Sandy Bridge (8.16 +5.15=13.3)/n 4.99 5.98 +80%
  24. # Ivy Bridge (8.08 +5.14=13.2)/n 4.60 5.54 +68%
  25. # Haswell(iii) (8.96 +5.00=14.0)/n 3.57 4.55 +160%
  26. # Skylake (8.70 +5.00=13.7)/n 3.64 4.20 +145%
  27. # Bulldozer (9.76 +5.76=15.5)/n 5.95 6.37 +64%
  28. #
  29. # (i) multi-block CBC encrypt with 128-bit key;
  30. # (ii) (HASH+AES)/n does not apply to Westmere for n>3 and Atom,
  31. # because of lower AES-NI instruction throughput;
  32. # (iii) "this" is for n=8, when we gather twice as much data, result
  33. # for n=4 is 8.00+4.44=12.4;
  34. # (iv) presented improvement coefficients are asymptotic limits and
  35. # in real-life application are somewhat lower, e.g. for 2KB
  36. # fragments they range from 30% to 100% (on Haswell);
  37. # $output is the last argument if it looks like a file (it has an extension)
  38. # $flavour is the first argument if it doesn't look like a file
  39. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  40. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  41. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  42. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  43. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  44. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  45. die "can't locate x86_64-xlate.pl";
  46. $avx=0;
  47. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  48. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  49. $avx = ($1>=2.19) + ($1>=2.22);
  50. }
  51. if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  52. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  53. $avx = ($1>=2.09) + ($1>=2.10);
  54. }
  55. if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  56. `ml64 2>&1` =~ /Version ([0-9]+)\./) {
  57. $avx = ($1>=10) + ($1>=11);
  58. }
  59. if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/) {
  60. $avx = ($2>=3.0) + ($2>3.0);
  61. }
  62. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  63. or die "can't call $xlate: $!";
  64. *STDOUT=*OUT;
  65. # void sha1_multi_block (
  66. # struct { unsigned int A[8];
  67. # unsigned int B[8];
  68. # unsigned int C[8];
  69. # unsigned int D[8];
  70. # unsigned int E[8]; } *ctx,
  71. # struct { void *ptr; int blocks; } inp[8],
  72. # int num); /* 1 or 2 */
  73. #
  74. $ctx="%rdi"; # 1st arg
  75. $inp="%rsi"; # 2nd arg
  76. $num="%edx";
  77. @ptr=map("%r$_",(8..11));
  78. $Tbl="%rbp";
  79. @V=($A,$B,$C,$D,$E)=map("%xmm$_",(0..4));
  80. ($t0,$t1,$t2,$t3,$tx)=map("%xmm$_",(5..9));
  81. @Xi=map("%xmm$_",(10..14));
  82. $K="%xmm15";
  83. if (1) {
  84. # Atom-specific optimization aiming to eliminate pshufb with high
  85. # registers [and thus get rid of 48 cycles accumulated penalty]
  86. @Xi=map("%xmm$_",(0..4));
  87. ($tx,$t0,$t1,$t2,$t3)=map("%xmm$_",(5..9));
  88. @V=($A,$B,$C,$D,$E)=map("%xmm$_",(10..14));
  89. }
  90. $REG_SZ=16;
  91. sub Xi_off {
  92. my $off = shift;
  93. $off %= 16; $off *= $REG_SZ;
  94. $off<256 ? "$off-128(%rax)" : "$off-256-128(%rbx)";
  95. }
  96. sub BODY_00_19 {
  97. my ($i,$a,$b,$c,$d,$e)=@_;
  98. my $j=$i+1;
  99. my $k=$i+2;
  100. # Loads are performed 2+3/4 iterations in advance. 3/4 means that out
  101. # of 4 words you would expect to be loaded per given iteration one is
  102. # spilled to next iteration. In other words indices in four input
  103. # streams are distributed as following:
  104. #
  105. # $i==0: 0,0,0,0,1,1,1,1,2,2,2,
  106. # $i==1: 2,3,3,3,
  107. # $i==2: 3,4,4,4,
  108. # ...
  109. # $i==13: 14,15,15,15,
  110. # $i==14: 15
  111. #
  112. # Then at $i==15 Xupdate is applied one iteration in advance...
  113. $code.=<<___ if ($i==0);
  114. movd (@ptr[0]),@Xi[0]
  115. lea `16*4`(@ptr[0]),@ptr[0]
  116. movd (@ptr[1]),@Xi[2] # borrow @Xi[2]
  117. lea `16*4`(@ptr[1]),@ptr[1]
  118. movd (@ptr[2]),@Xi[3] # borrow @Xi[3]
  119. lea `16*4`(@ptr[2]),@ptr[2]
  120. movd (@ptr[3]),@Xi[4] # borrow @Xi[4]
  121. lea `16*4`(@ptr[3]),@ptr[3]
  122. punpckldq @Xi[3],@Xi[0]
  123. movd `4*$j-16*4`(@ptr[0]),@Xi[1]
  124. punpckldq @Xi[4],@Xi[2]
  125. movd `4*$j-16*4`(@ptr[1]),$t3
  126. punpckldq @Xi[2],@Xi[0]
  127. movd `4*$j-16*4`(@ptr[2]),$t2
  128. pshufb $tx,@Xi[0]
  129. ___
  130. $code.=<<___ if ($i<14); # just load input
  131. movd `4*$j-16*4`(@ptr[3]),$t1
  132. punpckldq $t2,@Xi[1]
  133. movdqa $a,$t2
  134. paddd $K,$e # e+=K_00_19
  135. punpckldq $t1,$t3
  136. movdqa $b,$t1
  137. movdqa $b,$t0
  138. pslld \$5,$t2
  139. pandn $d,$t1
  140. pand $c,$t0
  141. punpckldq $t3,@Xi[1]
  142. movdqa $a,$t3
  143. movdqa @Xi[0],`&Xi_off($i)`
  144. paddd @Xi[0],$e # e+=X[i]
  145. movd `4*$k-16*4`(@ptr[0]),@Xi[2]
  146. psrld \$27,$t3
  147. pxor $t1,$t0 # Ch(b,c,d)
  148. movdqa $b,$t1
  149. por $t3,$t2 # rol(a,5)
  150. movd `4*$k-16*4`(@ptr[1]),$t3
  151. pslld \$30,$t1
  152. paddd $t0,$e # e+=Ch(b,c,d)
  153. psrld \$2,$b
  154. paddd $t2,$e # e+=rol(a,5)
  155. pshufb $tx,@Xi[1]
  156. movd `4*$k-16*4`(@ptr[2]),$t2
  157. por $t1,$b # b=rol(b,30)
  158. ___
  159. $code.=<<___ if ($i==14); # just load input
  160. movd `4*$j-16*4`(@ptr[3]),$t1
  161. punpckldq $t2,@Xi[1]
  162. movdqa $a,$t2
  163. paddd $K,$e # e+=K_00_19
  164. punpckldq $t1,$t3
  165. movdqa $b,$t1
  166. movdqa $b,$t0
  167. pslld \$5,$t2
  168. prefetcht0 63(@ptr[0])
  169. pandn $d,$t1
  170. pand $c,$t0
  171. punpckldq $t3,@Xi[1]
  172. movdqa $a,$t3
  173. movdqa @Xi[0],`&Xi_off($i)`
  174. paddd @Xi[0],$e # e+=X[i]
  175. psrld \$27,$t3
  176. pxor $t1,$t0 # Ch(b,c,d)
  177. movdqa $b,$t1
  178. prefetcht0 63(@ptr[1])
  179. por $t3,$t2 # rol(a,5)
  180. pslld \$30,$t1
  181. paddd $t0,$e # e+=Ch(b,c,d)
  182. prefetcht0 63(@ptr[2])
  183. psrld \$2,$b
  184. paddd $t2,$e # e+=rol(a,5)
  185. pshufb $tx,@Xi[1]
  186. prefetcht0 63(@ptr[3])
  187. por $t1,$b # b=rol(b,30)
  188. ___
  189. $code.=<<___ if ($i>=13 && $i<15);
  190. movdqa `&Xi_off($j+2)`,@Xi[3] # preload "X[2]"
  191. ___
  192. $code.=<<___ if ($i>=15); # apply Xupdate
  193. pxor @Xi[-2],@Xi[1] # "X[13]"
  194. movdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  195. movdqa $a,$t2
  196. pxor `&Xi_off($j+8)`,@Xi[1]
  197. paddd $K,$e # e+=K_00_19
  198. movdqa $b,$t1
  199. pslld \$5,$t2
  200. pxor @Xi[3],@Xi[1]
  201. movdqa $b,$t0
  202. pandn $d,$t1
  203. movdqa @Xi[1],$tx
  204. pand $c,$t0
  205. movdqa $a,$t3
  206. psrld \$31,$tx
  207. paddd @Xi[1],@Xi[1]
  208. movdqa @Xi[0],`&Xi_off($i)`
  209. paddd @Xi[0],$e # e+=X[i]
  210. psrld \$27,$t3
  211. pxor $t1,$t0 # Ch(b,c,d)
  212. movdqa $b,$t1
  213. por $t3,$t2 # rol(a,5)
  214. pslld \$30,$t1
  215. paddd $t0,$e # e+=Ch(b,c,d)
  216. psrld \$2,$b
  217. paddd $t2,$e # e+=rol(a,5)
  218. por $tx,@Xi[1] # rol \$1,@Xi[1]
  219. por $t1,$b # b=rol(b,30)
  220. ___
  221. push(@Xi,shift(@Xi));
  222. }
  223. sub BODY_20_39 {
  224. my ($i,$a,$b,$c,$d,$e)=@_;
  225. my $j=$i+1;
  226. $code.=<<___ if ($i<79);
  227. pxor @Xi[-2],@Xi[1] # "X[13]"
  228. movdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  229. movdqa $a,$t2
  230. movdqa $d,$t0
  231. pxor `&Xi_off($j+8)`,@Xi[1]
  232. paddd $K,$e # e+=K_20_39
  233. pslld \$5,$t2
  234. pxor $b,$t0
  235. movdqa $a,$t3
  236. ___
  237. $code.=<<___ if ($i<72);
  238. movdqa @Xi[0],`&Xi_off($i)`
  239. ___
  240. $code.=<<___ if ($i<79);
  241. paddd @Xi[0],$e # e+=X[i]
  242. pxor @Xi[3],@Xi[1]
  243. psrld \$27,$t3
  244. pxor $c,$t0 # Parity(b,c,d)
  245. movdqa $b,$t1
  246. pslld \$30,$t1
  247. movdqa @Xi[1],$tx
  248. por $t3,$t2 # rol(a,5)
  249. psrld \$31,$tx
  250. paddd $t0,$e # e+=Parity(b,c,d)
  251. paddd @Xi[1],@Xi[1]
  252. psrld \$2,$b
  253. paddd $t2,$e # e+=rol(a,5)
  254. por $tx,@Xi[1] # rol(@Xi[1],1)
  255. por $t1,$b # b=rol(b,30)
  256. ___
  257. $code.=<<___ if ($i==79);
  258. movdqa $a,$t2
  259. paddd $K,$e # e+=K_20_39
  260. movdqa $d,$t0
  261. pslld \$5,$t2
  262. pxor $b,$t0
  263. movdqa $a,$t3
  264. paddd @Xi[0],$e # e+=X[i]
  265. psrld \$27,$t3
  266. movdqa $b,$t1
  267. pxor $c,$t0 # Parity(b,c,d)
  268. pslld \$30,$t1
  269. por $t3,$t2 # rol(a,5)
  270. paddd $t0,$e # e+=Parity(b,c,d)
  271. psrld \$2,$b
  272. paddd $t2,$e # e+=rol(a,5)
  273. por $t1,$b # b=rol(b,30)
  274. ___
  275. push(@Xi,shift(@Xi));
  276. }
  277. sub BODY_40_59 {
  278. my ($i,$a,$b,$c,$d,$e)=@_;
  279. my $j=$i+1;
  280. $code.=<<___;
  281. pxor @Xi[-2],@Xi[1] # "X[13]"
  282. movdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  283. movdqa $a,$t2
  284. movdqa $d,$t1
  285. pxor `&Xi_off($j+8)`,@Xi[1]
  286. pxor @Xi[3],@Xi[1]
  287. paddd $K,$e # e+=K_40_59
  288. pslld \$5,$t2
  289. movdqa $a,$t3
  290. pand $c,$t1
  291. movdqa $d,$t0
  292. movdqa @Xi[1],$tx
  293. psrld \$27,$t3
  294. paddd $t1,$e
  295. pxor $c,$t0
  296. movdqa @Xi[0],`&Xi_off($i)`
  297. paddd @Xi[0],$e # e+=X[i]
  298. por $t3,$t2 # rol(a,5)
  299. psrld \$31,$tx
  300. pand $b,$t0
  301. movdqa $b,$t1
  302. pslld \$30,$t1
  303. paddd @Xi[1],@Xi[1]
  304. paddd $t0,$e # e+=Maj(b,d,c)
  305. psrld \$2,$b
  306. paddd $t2,$e # e+=rol(a,5)
  307. por $tx,@Xi[1] # rol(@X[1],1)
  308. por $t1,$b # b=rol(b,30)
  309. ___
  310. push(@Xi,shift(@Xi));
  311. }
  312. $code.=<<___;
  313. .text
  314. .extern OPENSSL_ia32cap_P
  315. .globl sha1_multi_block
  316. .type sha1_multi_block,\@function,3
  317. .align 32
  318. sha1_multi_block:
  319. .cfi_startproc
  320. mov OPENSSL_ia32cap_P+4(%rip),%rcx
  321. bt \$61,%rcx # check SHA bit
  322. jc _shaext_shortcut
  323. ___
  324. $code.=<<___ if ($avx);
  325. test \$`1<<28`,%ecx
  326. jnz _avx_shortcut
  327. ___
  328. $code.=<<___;
  329. mov %rsp,%rax
  330. .cfi_def_cfa_register %rax
  331. push %rbx
  332. .cfi_push %rbx
  333. push %rbp
  334. .cfi_push %rbx
  335. ___
  336. $code.=<<___ if ($win64);
  337. lea -0xa8(%rsp),%rsp
  338. movaps %xmm6,(%rsp)
  339. movaps %xmm7,0x10(%rsp)
  340. movaps %xmm8,0x20(%rsp)
  341. movaps %xmm9,0x30(%rsp)
  342. movaps %xmm10,-0x78(%rax)
  343. movaps %xmm11,-0x68(%rax)
  344. movaps %xmm12,-0x58(%rax)
  345. movaps %xmm13,-0x48(%rax)
  346. movaps %xmm14,-0x38(%rax)
  347. movaps %xmm15,-0x28(%rax)
  348. ___
  349. $code.=<<___;
  350. sub \$`$REG_SZ*18`,%rsp
  351. and \$-256,%rsp
  352. mov %rax,`$REG_SZ*17`(%rsp) # original %rsp
  353. .cfi_cfa_expression %rsp+`$REG_SZ*17`,deref,+8
  354. .Lbody:
  355. lea K_XX_XX(%rip),$Tbl
  356. lea `$REG_SZ*16`(%rsp),%rbx
  357. .Loop_grande:
  358. mov $num,`$REG_SZ*17+8`(%rsp) # original $num
  359. xor $num,$num
  360. ___
  361. for($i=0;$i<4;$i++) {
  362. $code.=<<___;
  363. mov `16*$i+0`($inp),@ptr[$i] # input pointer
  364. mov `16*$i+8`($inp),%ecx # number of blocks
  365. cmp $num,%ecx
  366. cmovg %ecx,$num # find maximum
  367. test %ecx,%ecx
  368. mov %ecx,`4*$i`(%rbx) # initialize counters
  369. cmovle $Tbl,@ptr[$i] # cancel input
  370. ___
  371. }
  372. $code.=<<___;
  373. test $num,$num
  374. jz .Ldone
  375. movdqu 0x00($ctx),$A # load context
  376. lea 128(%rsp),%rax
  377. movdqu 0x20($ctx),$B
  378. movdqu 0x40($ctx),$C
  379. movdqu 0x60($ctx),$D
  380. movdqu 0x80($ctx),$E
  381. movdqa 0x60($Tbl),$tx # pbswap_mask
  382. movdqa -0x20($Tbl),$K # K_00_19
  383. jmp .Loop
  384. .align 32
  385. .Loop:
  386. ___
  387. for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
  388. $code.=" movdqa 0x00($Tbl),$K\n"; # K_20_39
  389. for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  390. $code.=" movdqa 0x20($Tbl),$K\n"; # K_40_59
  391. for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  392. $code.=" movdqa 0x40($Tbl),$K\n"; # K_60_79
  393. for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  394. $code.=<<___;
  395. movdqa (%rbx),@Xi[0] # pull counters
  396. mov \$1,%ecx
  397. cmp 4*0(%rbx),%ecx # examine counters
  398. pxor $t2,$t2
  399. cmovge $Tbl,@ptr[0] # cancel input
  400. cmp 4*1(%rbx),%ecx
  401. movdqa @Xi[0],@Xi[1]
  402. cmovge $Tbl,@ptr[1]
  403. cmp 4*2(%rbx),%ecx
  404. pcmpgtd $t2,@Xi[1] # mask value
  405. cmovge $Tbl,@ptr[2]
  406. cmp 4*3(%rbx),%ecx
  407. paddd @Xi[1],@Xi[0] # counters--
  408. cmovge $Tbl,@ptr[3]
  409. movdqu 0x00($ctx),$t0
  410. pand @Xi[1],$A
  411. movdqu 0x20($ctx),$t1
  412. pand @Xi[1],$B
  413. paddd $t0,$A
  414. movdqu 0x40($ctx),$t2
  415. pand @Xi[1],$C
  416. paddd $t1,$B
  417. movdqu 0x60($ctx),$t3
  418. pand @Xi[1],$D
  419. paddd $t2,$C
  420. movdqu 0x80($ctx),$tx
  421. pand @Xi[1],$E
  422. movdqu $A,0x00($ctx)
  423. paddd $t3,$D
  424. movdqu $B,0x20($ctx)
  425. paddd $tx,$E
  426. movdqu $C,0x40($ctx)
  427. movdqu $D,0x60($ctx)
  428. movdqu $E,0x80($ctx)
  429. movdqa @Xi[0],(%rbx) # save counters
  430. movdqa 0x60($Tbl),$tx # pbswap_mask
  431. movdqa -0x20($Tbl),$K # K_00_19
  432. dec $num
  433. jnz .Loop
  434. mov `$REG_SZ*17+8`(%rsp),$num
  435. lea $REG_SZ($ctx),$ctx
  436. lea `16*$REG_SZ/4`($inp),$inp
  437. dec $num
  438. jnz .Loop_grande
  439. .Ldone:
  440. mov `$REG_SZ*17`(%rsp),%rax # original %rsp
  441. .cfi_def_cfa %rax,8
  442. ___
  443. $code.=<<___ if ($win64);
  444. movaps -0xb8(%rax),%xmm6
  445. movaps -0xa8(%rax),%xmm7
  446. movaps -0x98(%rax),%xmm8
  447. movaps -0x88(%rax),%xmm9
  448. movaps -0x78(%rax),%xmm10
  449. movaps -0x68(%rax),%xmm11
  450. movaps -0x58(%rax),%xmm12
  451. movaps -0x48(%rax),%xmm13
  452. movaps -0x38(%rax),%xmm14
  453. movaps -0x28(%rax),%xmm15
  454. ___
  455. $code.=<<___;
  456. mov -16(%rax),%rbp
  457. .cfi_restore %rbp
  458. mov -8(%rax),%rbx
  459. .cfi_restore %rbx
  460. lea (%rax),%rsp
  461. .cfi_def_cfa_register %rsp
  462. .Lepilogue:
  463. ret
  464. .cfi_endproc
  465. .size sha1_multi_block,.-sha1_multi_block
  466. ___
  467. {{{
  468. my ($ABCD0,$E0,$E0_,$BSWAP,$ABCD1,$E1,$E1_)=map("%xmm$_",(0..3,8..10));
  469. my @MSG0=map("%xmm$_",(4..7));
  470. my @MSG1=map("%xmm$_",(11..14));
  471. $code.=<<___;
  472. .type sha1_multi_block_shaext,\@function,3
  473. .align 32
  474. sha1_multi_block_shaext:
  475. .cfi_startproc
  476. _shaext_shortcut:
  477. mov %rsp,%rax
  478. .cfi_def_cfa_register %rax
  479. push %rbx
  480. .cfi_push %rbx
  481. push %rbp
  482. .cfi_push %rbp
  483. ___
  484. $code.=<<___ if ($win64);
  485. lea -0xa8(%rsp),%rsp
  486. movaps %xmm6,(%rsp)
  487. movaps %xmm7,0x10(%rsp)
  488. movaps %xmm8,0x20(%rsp)
  489. movaps %xmm9,0x30(%rsp)
  490. movaps %xmm10,-0x78(%rax)
  491. movaps %xmm11,-0x68(%rax)
  492. movaps %xmm12,-0x58(%rax)
  493. movaps %xmm13,-0x48(%rax)
  494. movaps %xmm14,-0x38(%rax)
  495. movaps %xmm15,-0x28(%rax)
  496. ___
  497. $code.=<<___;
  498. sub \$`$REG_SZ*18`,%rsp
  499. shl \$1,$num # we process pair at a time
  500. and \$-256,%rsp
  501. lea 0x40($ctx),$ctx # size optimization
  502. mov %rax,`$REG_SZ*17`(%rsp) # original %rsp
  503. .Lbody_shaext:
  504. lea `$REG_SZ*16`(%rsp),%rbx
  505. movdqa K_XX_XX+0x80(%rip),$BSWAP # byte-n-word swap
  506. .Loop_grande_shaext:
  507. mov $num,`$REG_SZ*17+8`(%rsp) # original $num
  508. xor $num,$num
  509. ___
  510. for($i=0;$i<2;$i++) {
  511. $code.=<<___;
  512. mov `16*$i+0`($inp),@ptr[$i] # input pointer
  513. mov `16*$i+8`($inp),%ecx # number of blocks
  514. cmp $num,%ecx
  515. cmovg %ecx,$num # find maximum
  516. test %ecx,%ecx
  517. mov %ecx,`4*$i`(%rbx) # initialize counters
  518. cmovle %rsp,@ptr[$i] # cancel input
  519. ___
  520. }
  521. $code.=<<___;
  522. test $num,$num
  523. jz .Ldone_shaext
  524. movq 0x00-0x40($ctx),$ABCD0 # a1.a0
  525. movq 0x20-0x40($ctx),@MSG0[0]# b1.b0
  526. movq 0x40-0x40($ctx),@MSG0[1]# c1.c0
  527. movq 0x60-0x40($ctx),@MSG0[2]# d1.d0
  528. movq 0x80-0x40($ctx),@MSG0[3]# e1.e0
  529. punpckldq @MSG0[0],$ABCD0 # b1.a1.b0.a0
  530. punpckldq @MSG0[2],@MSG0[1] # d1.c1.d0.c0
  531. movdqa $ABCD0,$ABCD1
  532. punpcklqdq @MSG0[1],$ABCD0 # d0.c0.b0.a0
  533. punpckhqdq @MSG0[1],$ABCD1 # d1.c1.b1.a1
  534. pshufd \$0b00111111,@MSG0[3],$E0
  535. pshufd \$0b01111111,@MSG0[3],$E1
  536. pshufd \$0b00011011,$ABCD0,$ABCD0
  537. pshufd \$0b00011011,$ABCD1,$ABCD1
  538. jmp .Loop_shaext
  539. .align 32
  540. .Loop_shaext:
  541. movdqu 0x00(@ptr[0]),@MSG0[0]
  542. movdqu 0x00(@ptr[1]),@MSG1[0]
  543. movdqu 0x10(@ptr[0]),@MSG0[1]
  544. movdqu 0x10(@ptr[1]),@MSG1[1]
  545. movdqu 0x20(@ptr[0]),@MSG0[2]
  546. pshufb $BSWAP,@MSG0[0]
  547. movdqu 0x20(@ptr[1]),@MSG1[2]
  548. pshufb $BSWAP,@MSG1[0]
  549. movdqu 0x30(@ptr[0]),@MSG0[3]
  550. lea 0x40(@ptr[0]),@ptr[0]
  551. pshufb $BSWAP,@MSG0[1]
  552. movdqu 0x30(@ptr[1]),@MSG1[3]
  553. lea 0x40(@ptr[1]),@ptr[1]
  554. pshufb $BSWAP,@MSG1[1]
  555. movdqa $E0,0x50(%rsp) # offload
  556. paddd @MSG0[0],$E0
  557. movdqa $E1,0x70(%rsp)
  558. paddd @MSG1[0],$E1
  559. movdqa $ABCD0,0x40(%rsp) # offload
  560. movdqa $ABCD0,$E0_
  561. movdqa $ABCD1,0x60(%rsp)
  562. movdqa $ABCD1,$E1_
  563. sha1rnds4 \$0,$E0,$ABCD0 # 0-3
  564. sha1nexte @MSG0[1],$E0_
  565. sha1rnds4 \$0,$E1,$ABCD1 # 0-3
  566. sha1nexte @MSG1[1],$E1_
  567. pshufb $BSWAP,@MSG0[2]
  568. prefetcht0 127(@ptr[0])
  569. sha1msg1 @MSG0[1],@MSG0[0]
  570. pshufb $BSWAP,@MSG1[2]
  571. prefetcht0 127(@ptr[1])
  572. sha1msg1 @MSG1[1],@MSG1[0]
  573. pshufb $BSWAP,@MSG0[3]
  574. movdqa $ABCD0,$E0
  575. pshufb $BSWAP,@MSG1[3]
  576. movdqa $ABCD1,$E1
  577. sha1rnds4 \$0,$E0_,$ABCD0 # 4-7
  578. sha1nexte @MSG0[2],$E0
  579. sha1rnds4 \$0,$E1_,$ABCD1 # 4-7
  580. sha1nexte @MSG1[2],$E1
  581. pxor @MSG0[2],@MSG0[0]
  582. sha1msg1 @MSG0[2],@MSG0[1]
  583. pxor @MSG1[2],@MSG1[0]
  584. sha1msg1 @MSG1[2],@MSG1[1]
  585. ___
  586. for($i=2;$i<20-4;$i++) {
  587. $code.=<<___;
  588. movdqa $ABCD0,$E0_
  589. movdqa $ABCD1,$E1_
  590. sha1rnds4 \$`int($i/5)`,$E0,$ABCD0 # 8-11
  591. sha1nexte @MSG0[3],$E0_
  592. sha1rnds4 \$`int($i/5)`,$E1,$ABCD1 # 8-11
  593. sha1nexte @MSG1[3],$E1_
  594. sha1msg2 @MSG0[3],@MSG0[0]
  595. sha1msg2 @MSG1[3],@MSG1[0]
  596. pxor @MSG0[3],@MSG0[1]
  597. sha1msg1 @MSG0[3],@MSG0[2]
  598. pxor @MSG1[3],@MSG1[1]
  599. sha1msg1 @MSG1[3],@MSG1[2]
  600. ___
  601. ($E0,$E0_)=($E0_,$E0); ($E1,$E1_)=($E1_,$E1);
  602. push(@MSG0,shift(@MSG0)); push(@MSG1,shift(@MSG1));
  603. }
  604. $code.=<<___;
  605. movdqa $ABCD0,$E0_
  606. movdqa $ABCD1,$E1_
  607. sha1rnds4 \$3,$E0,$ABCD0 # 64-67
  608. sha1nexte @MSG0[3],$E0_
  609. sha1rnds4 \$3,$E1,$ABCD1 # 64-67
  610. sha1nexte @MSG1[3],$E1_
  611. sha1msg2 @MSG0[3],@MSG0[0]
  612. sha1msg2 @MSG1[3],@MSG1[0]
  613. pxor @MSG0[3],@MSG0[1]
  614. pxor @MSG1[3],@MSG1[1]
  615. mov \$1,%ecx
  616. pxor @MSG0[2],@MSG0[2] # zero
  617. cmp 4*0(%rbx),%ecx # examine counters
  618. cmovge %rsp,@ptr[0] # cancel input
  619. movdqa $ABCD0,$E0
  620. movdqa $ABCD1,$E1
  621. sha1rnds4 \$3,$E0_,$ABCD0 # 68-71
  622. sha1nexte @MSG0[0],$E0
  623. sha1rnds4 \$3,$E1_,$ABCD1 # 68-71
  624. sha1nexte @MSG1[0],$E1
  625. sha1msg2 @MSG0[0],@MSG0[1]
  626. sha1msg2 @MSG1[0],@MSG1[1]
  627. cmp 4*1(%rbx),%ecx
  628. cmovge %rsp,@ptr[1]
  629. movq (%rbx),@MSG0[0] # pull counters
  630. movdqa $ABCD0,$E0_
  631. movdqa $ABCD1,$E1_
  632. sha1rnds4 \$3,$E0,$ABCD0 # 72-75
  633. sha1nexte @MSG0[1],$E0_
  634. sha1rnds4 \$3,$E1,$ABCD1 # 72-75
  635. sha1nexte @MSG1[1],$E1_
  636. pshufd \$0x00,@MSG0[0],@MSG1[2]
  637. pshufd \$0x55,@MSG0[0],@MSG1[3]
  638. movdqa @MSG0[0],@MSG0[1]
  639. pcmpgtd @MSG0[2],@MSG1[2]
  640. pcmpgtd @MSG0[2],@MSG1[3]
  641. movdqa $ABCD0,$E0
  642. movdqa $ABCD1,$E1
  643. sha1rnds4 \$3,$E0_,$ABCD0 # 76-79
  644. sha1nexte $MSG0[2],$E0
  645. sha1rnds4 \$3,$E1_,$ABCD1 # 76-79
  646. sha1nexte $MSG0[2],$E1
  647. pcmpgtd @MSG0[2],@MSG0[1] # counter mask
  648. pand @MSG1[2],$ABCD0
  649. pand @MSG1[2],$E0
  650. pand @MSG1[3],$ABCD1
  651. pand @MSG1[3],$E1
  652. paddd @MSG0[1],@MSG0[0] # counters--
  653. paddd 0x40(%rsp),$ABCD0
  654. paddd 0x50(%rsp),$E0
  655. paddd 0x60(%rsp),$ABCD1
  656. paddd 0x70(%rsp),$E1
  657. movq @MSG0[0],(%rbx) # save counters
  658. dec $num
  659. jnz .Loop_shaext
  660. mov `$REG_SZ*17+8`(%rsp),$num
  661. pshufd \$0b00011011,$ABCD0,$ABCD0
  662. pshufd \$0b00011011,$ABCD1,$ABCD1
  663. movdqa $ABCD0,@MSG0[0]
  664. punpckldq $ABCD1,$ABCD0 # b1.b0.a1.a0
  665. punpckhdq $ABCD1,@MSG0[0] # d1.d0.c1.c0
  666. punpckhdq $E1,$E0 # e1.e0.xx.xx
  667. movq $ABCD0,0x00-0x40($ctx) # a1.a0
  668. psrldq \$8,$ABCD0
  669. movq @MSG0[0],0x40-0x40($ctx)# c1.c0
  670. psrldq \$8,@MSG0[0]
  671. movq $ABCD0,0x20-0x40($ctx) # b1.b0
  672. psrldq \$8,$E0
  673. movq @MSG0[0],0x60-0x40($ctx)# d1.d0
  674. movq $E0,0x80-0x40($ctx) # e1.e0
  675. lea `$REG_SZ/2`($ctx),$ctx
  676. lea `16*2`($inp),$inp
  677. dec $num
  678. jnz .Loop_grande_shaext
  679. .Ldone_shaext:
  680. #mov `$REG_SZ*17`(%rsp),%rax # original %rsp
  681. ___
  682. $code.=<<___ if ($win64);
  683. movaps -0xb8(%rax),%xmm6
  684. movaps -0xa8(%rax),%xmm7
  685. movaps -0x98(%rax),%xmm8
  686. movaps -0x88(%rax),%xmm9
  687. movaps -0x78(%rax),%xmm10
  688. movaps -0x68(%rax),%xmm11
  689. movaps -0x58(%rax),%xmm12
  690. movaps -0x48(%rax),%xmm13
  691. movaps -0x38(%rax),%xmm14
  692. movaps -0x28(%rax),%xmm15
  693. ___
  694. $code.=<<___;
  695. mov -16(%rax),%rbp
  696. .cfi_restore %rbp
  697. mov -8(%rax),%rbx
  698. .cfi_restore %rbx
  699. lea (%rax),%rsp
  700. .cfi_def_cfa_register %rsp
  701. .Lepilogue_shaext:
  702. ret
  703. .cfi_endproc
  704. .size sha1_multi_block_shaext,.-sha1_multi_block_shaext
  705. ___
  706. }}}
  707. if ($avx) {{{
  708. sub BODY_00_19_avx {
  709. my ($i,$a,$b,$c,$d,$e)=@_;
  710. my $j=$i+1;
  711. my $k=$i+2;
  712. my $vpack = $REG_SZ==16 ? "vpunpckldq" : "vinserti128";
  713. my $ptr_n = $REG_SZ==16 ? @ptr[1] : @ptr[4];
  714. $code.=<<___ if ($i==0 && $REG_SZ==16);
  715. vmovd (@ptr[0]),@Xi[0]
  716. lea `16*4`(@ptr[0]),@ptr[0]
  717. vmovd (@ptr[1]),@Xi[2] # borrow Xi[2]
  718. lea `16*4`(@ptr[1]),@ptr[1]
  719. vpinsrd \$1,(@ptr[2]),@Xi[0],@Xi[0]
  720. lea `16*4`(@ptr[2]),@ptr[2]
  721. vpinsrd \$1,(@ptr[3]),@Xi[2],@Xi[2]
  722. lea `16*4`(@ptr[3]),@ptr[3]
  723. vmovd `4*$j-16*4`(@ptr[0]),@Xi[1]
  724. vpunpckldq @Xi[2],@Xi[0],@Xi[0]
  725. vmovd `4*$j-16*4`($ptr_n),$t3
  726. vpshufb $tx,@Xi[0],@Xi[0]
  727. ___
  728. $code.=<<___ if ($i<15 && $REG_SZ==16); # just load input
  729. vpinsrd \$1,`4*$j-16*4`(@ptr[2]),@Xi[1],@Xi[1]
  730. vpinsrd \$1,`4*$j-16*4`(@ptr[3]),$t3,$t3
  731. ___
  732. $code.=<<___ if ($i==0 && $REG_SZ==32);
  733. vmovd (@ptr[0]),@Xi[0]
  734. lea `16*4`(@ptr[0]),@ptr[0]
  735. vmovd (@ptr[4]),@Xi[2] # borrow Xi[2]
  736. lea `16*4`(@ptr[4]),@ptr[4]
  737. vmovd (@ptr[1]),$t2
  738. lea `16*4`(@ptr[1]),@ptr[1]
  739. vmovd (@ptr[5]),$t1
  740. lea `16*4`(@ptr[5]),@ptr[5]
  741. vpinsrd \$1,(@ptr[2]),@Xi[0],@Xi[0]
  742. lea `16*4`(@ptr[2]),@ptr[2]
  743. vpinsrd \$1,(@ptr[6]),@Xi[2],@Xi[2]
  744. lea `16*4`(@ptr[6]),@ptr[6]
  745. vpinsrd \$1,(@ptr[3]),$t2,$t2
  746. lea `16*4`(@ptr[3]),@ptr[3]
  747. vpunpckldq $t2,@Xi[0],@Xi[0]
  748. vpinsrd \$1,(@ptr[7]),$t1,$t1
  749. lea `16*4`(@ptr[7]),@ptr[7]
  750. vpunpckldq $t1,@Xi[2],@Xi[2]
  751. vmovd `4*$j-16*4`(@ptr[0]),@Xi[1]
  752. vinserti128 @Xi[2],@Xi[0],@Xi[0]
  753. vmovd `4*$j-16*4`($ptr_n),$t3
  754. vpshufb $tx,@Xi[0],@Xi[0]
  755. ___
  756. $code.=<<___ if ($i<15 && $REG_SZ==32); # just load input
  757. vmovd `4*$j-16*4`(@ptr[1]),$t2
  758. vmovd `4*$j-16*4`(@ptr[5]),$t1
  759. vpinsrd \$1,`4*$j-16*4`(@ptr[2]),@Xi[1],@Xi[1]
  760. vpinsrd \$1,`4*$j-16*4`(@ptr[6]),$t3,$t3
  761. vpinsrd \$1,`4*$j-16*4`(@ptr[3]),$t2,$t2
  762. vpunpckldq $t2,@Xi[1],@Xi[1]
  763. vpinsrd \$1,`4*$j-16*4`(@ptr[7]),$t1,$t1
  764. vpunpckldq $t1,$t3,$t3
  765. ___
  766. $code.=<<___ if ($i<14);
  767. vpaddd $K,$e,$e # e+=K_00_19
  768. vpslld \$5,$a,$t2
  769. vpandn $d,$b,$t1
  770. vpand $c,$b,$t0
  771. vmovdqa @Xi[0],`&Xi_off($i)`
  772. vpaddd @Xi[0],$e,$e # e+=X[i]
  773. $vpack $t3,@Xi[1],@Xi[1]
  774. vpsrld \$27,$a,$t3
  775. vpxor $t1,$t0,$t0 # Ch(b,c,d)
  776. vmovd `4*$k-16*4`(@ptr[0]),@Xi[2]
  777. vpslld \$30,$b,$t1
  778. vpor $t3,$t2,$t2 # rol(a,5)
  779. vmovd `4*$k-16*4`($ptr_n),$t3
  780. vpaddd $t0,$e,$e # e+=Ch(b,c,d)
  781. vpsrld \$2,$b,$b
  782. vpaddd $t2,$e,$e # e+=rol(a,5)
  783. vpshufb $tx,@Xi[1],@Xi[1]
  784. vpor $t1,$b,$b # b=rol(b,30)
  785. ___
  786. $code.=<<___ if ($i==14);
  787. vpaddd $K,$e,$e # e+=K_00_19
  788. prefetcht0 63(@ptr[0])
  789. vpslld \$5,$a,$t2
  790. vpandn $d,$b,$t1
  791. vpand $c,$b,$t0
  792. vmovdqa @Xi[0],`&Xi_off($i)`
  793. vpaddd @Xi[0],$e,$e # e+=X[i]
  794. $vpack $t3,@Xi[1],@Xi[1]
  795. vpsrld \$27,$a,$t3
  796. prefetcht0 63(@ptr[1])
  797. vpxor $t1,$t0,$t0 # Ch(b,c,d)
  798. vpslld \$30,$b,$t1
  799. vpor $t3,$t2,$t2 # rol(a,5)
  800. prefetcht0 63(@ptr[2])
  801. vpaddd $t0,$e,$e # e+=Ch(b,c,d)
  802. vpsrld \$2,$b,$b
  803. vpaddd $t2,$e,$e # e+=rol(a,5)
  804. prefetcht0 63(@ptr[3])
  805. vpshufb $tx,@Xi[1],@Xi[1]
  806. vpor $t1,$b,$b # b=rol(b,30)
  807. ___
  808. $code.=<<___ if ($i>=13 && $i<15);
  809. vmovdqa `&Xi_off($j+2)`,@Xi[3] # preload "X[2]"
  810. ___
  811. $code.=<<___ if ($i>=15); # apply Xupdate
  812. vpxor @Xi[-2],@Xi[1],@Xi[1] # "X[13]"
  813. vmovdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  814. vpaddd $K,$e,$e # e+=K_00_19
  815. vpslld \$5,$a,$t2
  816. vpandn $d,$b,$t1
  817. `"prefetcht0 63(@ptr[4])" if ($i==15 && $REG_SZ==32)`
  818. vpand $c,$b,$t0
  819. vmovdqa @Xi[0],`&Xi_off($i)`
  820. vpaddd @Xi[0],$e,$e # e+=X[i]
  821. vpxor `&Xi_off($j+8)`,@Xi[1],@Xi[1]
  822. vpsrld \$27,$a,$t3
  823. vpxor $t1,$t0,$t0 # Ch(b,c,d)
  824. vpxor @Xi[3],@Xi[1],@Xi[1]
  825. `"prefetcht0 63(@ptr[5])" if ($i==15 && $REG_SZ==32)`
  826. vpslld \$30,$b,$t1
  827. vpor $t3,$t2,$t2 # rol(a,5)
  828. vpaddd $t0,$e,$e # e+=Ch(b,c,d)
  829. `"prefetcht0 63(@ptr[6])" if ($i==15 && $REG_SZ==32)`
  830. vpsrld \$31,@Xi[1],$tx
  831. vpaddd @Xi[1],@Xi[1],@Xi[1]
  832. vpsrld \$2,$b,$b
  833. `"prefetcht0 63(@ptr[7])" if ($i==15 && $REG_SZ==32)`
  834. vpaddd $t2,$e,$e # e+=rol(a,5)
  835. vpor $tx,@Xi[1],@Xi[1] # rol \$1,@Xi[1]
  836. vpor $t1,$b,$b # b=rol(b,30)
  837. ___
  838. push(@Xi,shift(@Xi));
  839. }
  840. sub BODY_20_39_avx {
  841. my ($i,$a,$b,$c,$d,$e)=@_;
  842. my $j=$i+1;
  843. $code.=<<___ if ($i<79);
  844. vpxor @Xi[-2],@Xi[1],@Xi[1] # "X[13]"
  845. vmovdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  846. vpslld \$5,$a,$t2
  847. vpaddd $K,$e,$e # e+=K_20_39
  848. vpxor $b,$d,$t0
  849. ___
  850. $code.=<<___ if ($i<72);
  851. vmovdqa @Xi[0],`&Xi_off($i)`
  852. ___
  853. $code.=<<___ if ($i<79);
  854. vpaddd @Xi[0],$e,$e # e+=X[i]
  855. vpxor `&Xi_off($j+8)`,@Xi[1],@Xi[1]
  856. vpsrld \$27,$a,$t3
  857. vpxor $c,$t0,$t0 # Parity(b,c,d)
  858. vpxor @Xi[3],@Xi[1],@Xi[1]
  859. vpslld \$30,$b,$t1
  860. vpor $t3,$t2,$t2 # rol(a,5)
  861. vpaddd $t0,$e,$e # e+=Parity(b,c,d)
  862. vpsrld \$31,@Xi[1],$tx
  863. vpaddd @Xi[1],@Xi[1],@Xi[1]
  864. vpsrld \$2,$b,$b
  865. vpaddd $t2,$e,$e # e+=rol(a,5)
  866. vpor $tx,@Xi[1],@Xi[1] # rol(@Xi[1],1)
  867. vpor $t1,$b,$b # b=rol(b,30)
  868. ___
  869. $code.=<<___ if ($i==79);
  870. vpslld \$5,$a,$t2
  871. vpaddd $K,$e,$e # e+=K_20_39
  872. vpxor $b,$d,$t0
  873. vpsrld \$27,$a,$t3
  874. vpaddd @Xi[0],$e,$e # e+=X[i]
  875. vpxor $c,$t0,$t0 # Parity(b,c,d)
  876. vpslld \$30,$b,$t1
  877. vpor $t3,$t2,$t2 # rol(a,5)
  878. vpaddd $t0,$e,$e # e+=Parity(b,c,d)
  879. vpsrld \$2,$b,$b
  880. vpaddd $t2,$e,$e # e+=rol(a,5)
  881. vpor $t1,$b,$b # b=rol(b,30)
  882. ___
  883. push(@Xi,shift(@Xi));
  884. }
  885. sub BODY_40_59_avx {
  886. my ($i,$a,$b,$c,$d,$e)=@_;
  887. my $j=$i+1;
  888. $code.=<<___;
  889. vpxor @Xi[-2],@Xi[1],@Xi[1] # "X[13]"
  890. vmovdqa `&Xi_off($j+2)`,@Xi[3] # "X[2]"
  891. vpaddd $K,$e,$e # e+=K_40_59
  892. vpslld \$5,$a,$t2
  893. vpand $c,$d,$t1
  894. vpxor `&Xi_off($j+8)`,@Xi[1],@Xi[1]
  895. vpaddd $t1,$e,$e
  896. vpsrld \$27,$a,$t3
  897. vpxor $c,$d,$t0
  898. vpxor @Xi[3],@Xi[1],@Xi[1]
  899. vmovdqu @Xi[0],`&Xi_off($i)`
  900. vpaddd @Xi[0],$e,$e # e+=X[i]
  901. vpor $t3,$t2,$t2 # rol(a,5)
  902. vpsrld \$31,@Xi[1],$tx
  903. vpand $b,$t0,$t0
  904. vpaddd @Xi[1],@Xi[1],@Xi[1]
  905. vpslld \$30,$b,$t1
  906. vpaddd $t0,$e,$e # e+=Maj(b,d,c)
  907. vpsrld \$2,$b,$b
  908. vpaddd $t2,$e,$e # e+=rol(a,5)
  909. vpor $tx,@Xi[1],@Xi[1] # rol(@X[1],1)
  910. vpor $t1,$b,$b # b=rol(b,30)
  911. ___
  912. push(@Xi,shift(@Xi));
  913. }
  914. $code.=<<___;
  915. .type sha1_multi_block_avx,\@function,3
  916. .align 32
  917. sha1_multi_block_avx:
  918. .cfi_startproc
  919. _avx_shortcut:
  920. ___
  921. $code.=<<___ if ($avx>1);
  922. shr \$32,%rcx
  923. cmp \$2,$num
  924. jb .Lavx
  925. test \$`1<<5`,%ecx
  926. jnz _avx2_shortcut
  927. jmp .Lavx
  928. .align 32
  929. .Lavx:
  930. ___
  931. $code.=<<___;
  932. mov %rsp,%rax
  933. .cfi_def_cfa_register %rax
  934. push %rbx
  935. .cfi_push %rbx
  936. push %rbp
  937. .cfi_push %rbp
  938. ___
  939. $code.=<<___ if ($win64);
  940. lea -0xa8(%rsp),%rsp
  941. movaps %xmm6,(%rsp)
  942. movaps %xmm7,0x10(%rsp)
  943. movaps %xmm8,0x20(%rsp)
  944. movaps %xmm9,0x30(%rsp)
  945. movaps %xmm10,-0x78(%rax)
  946. movaps %xmm11,-0x68(%rax)
  947. movaps %xmm12,-0x58(%rax)
  948. movaps %xmm13,-0x48(%rax)
  949. movaps %xmm14,-0x38(%rax)
  950. movaps %xmm15,-0x28(%rax)
  951. ___
  952. $code.=<<___;
  953. sub \$`$REG_SZ*18`, %rsp
  954. and \$-256,%rsp
  955. mov %rax,`$REG_SZ*17`(%rsp) # original %rsp
  956. .cfi_cfa_expression %rsp+`$REG_SZ*17`,deref,+8
  957. .Lbody_avx:
  958. lea K_XX_XX(%rip),$Tbl
  959. lea `$REG_SZ*16`(%rsp),%rbx
  960. vzeroupper
  961. .Loop_grande_avx:
  962. mov $num,`$REG_SZ*17+8`(%rsp) # original $num
  963. xor $num,$num
  964. ___
  965. for($i=0;$i<4;$i++) {
  966. $code.=<<___;
  967. mov `16*$i+0`($inp),@ptr[$i] # input pointer
  968. mov `16*$i+8`($inp),%ecx # number of blocks
  969. cmp $num,%ecx
  970. cmovg %ecx,$num # find maximum
  971. test %ecx,%ecx
  972. mov %ecx,`4*$i`(%rbx) # initialize counters
  973. cmovle $Tbl,@ptr[$i] # cancel input
  974. ___
  975. }
  976. $code.=<<___;
  977. test $num,$num
  978. jz .Ldone_avx
  979. vmovdqu 0x00($ctx),$A # load context
  980. lea 128(%rsp),%rax
  981. vmovdqu 0x20($ctx),$B
  982. vmovdqu 0x40($ctx),$C
  983. vmovdqu 0x60($ctx),$D
  984. vmovdqu 0x80($ctx),$E
  985. vmovdqu 0x60($Tbl),$tx # pbswap_mask
  986. jmp .Loop_avx
  987. .align 32
  988. .Loop_avx:
  989. ___
  990. $code.=" vmovdqa -0x20($Tbl),$K\n"; # K_00_19
  991. for($i=0;$i<20;$i++) { &BODY_00_19_avx($i,@V); unshift(@V,pop(@V)); }
  992. $code.=" vmovdqa 0x00($Tbl),$K\n"; # K_20_39
  993. for(;$i<40;$i++) { &BODY_20_39_avx($i,@V); unshift(@V,pop(@V)); }
  994. $code.=" vmovdqa 0x20($Tbl),$K\n"; # K_40_59
  995. for(;$i<60;$i++) { &BODY_40_59_avx($i,@V); unshift(@V,pop(@V)); }
  996. $code.=" vmovdqa 0x40($Tbl),$K\n"; # K_60_79
  997. for(;$i<80;$i++) { &BODY_20_39_avx($i,@V); unshift(@V,pop(@V)); }
  998. $code.=<<___;
  999. mov \$1,%ecx
  1000. ___
  1001. for($i=0;$i<4;$i++) {
  1002. $code.=<<___;
  1003. cmp `4*$i`(%rbx),%ecx # examine counters
  1004. cmovge $Tbl,@ptr[$i] # cancel input
  1005. ___
  1006. }
  1007. $code.=<<___;
  1008. vmovdqu (%rbx),$t0 # pull counters
  1009. vpxor $t2,$t2,$t2
  1010. vmovdqa $t0,$t1
  1011. vpcmpgtd $t2,$t1,$t1 # mask value
  1012. vpaddd $t1,$t0,$t0 # counters--
  1013. vpand $t1,$A,$A
  1014. vpand $t1,$B,$B
  1015. vpaddd 0x00($ctx),$A,$A
  1016. vpand $t1,$C,$C
  1017. vpaddd 0x20($ctx),$B,$B
  1018. vpand $t1,$D,$D
  1019. vpaddd 0x40($ctx),$C,$C
  1020. vpand $t1,$E,$E
  1021. vpaddd 0x60($ctx),$D,$D
  1022. vpaddd 0x80($ctx),$E,$E
  1023. vmovdqu $A,0x00($ctx)
  1024. vmovdqu $B,0x20($ctx)
  1025. vmovdqu $C,0x40($ctx)
  1026. vmovdqu $D,0x60($ctx)
  1027. vmovdqu $E,0x80($ctx)
  1028. vmovdqu $t0,(%rbx) # save counters
  1029. vmovdqu 0x60($Tbl),$tx # pbswap_mask
  1030. dec $num
  1031. jnz .Loop_avx
  1032. mov `$REG_SZ*17+8`(%rsp),$num
  1033. lea $REG_SZ($ctx),$ctx
  1034. lea `16*$REG_SZ/4`($inp),$inp
  1035. dec $num
  1036. jnz .Loop_grande_avx
  1037. .Ldone_avx:
  1038. mov `$REG_SZ*17`(%rsp),%rax # original %rsp
  1039. .cfi_def_cfa %rax,8
  1040. vzeroupper
  1041. ___
  1042. $code.=<<___ if ($win64);
  1043. movaps -0xb8(%rax),%xmm6
  1044. movaps -0xa8(%rax),%xmm7
  1045. movaps -0x98(%rax),%xmm8
  1046. movaps -0x88(%rax),%xmm9
  1047. movaps -0x78(%rax),%xmm10
  1048. movaps -0x68(%rax),%xmm11
  1049. movaps -0x58(%rax),%xmm12
  1050. movaps -0x48(%rax),%xmm13
  1051. movaps -0x38(%rax),%xmm14
  1052. movaps -0x28(%rax),%xmm15
  1053. ___
  1054. $code.=<<___;
  1055. mov -16(%rax),%rbp
  1056. .cfi_restore %rbp
  1057. mov -8(%rax),%rbx
  1058. .cfi_restore %rbx
  1059. lea (%rax),%rsp
  1060. .cfi_def_cfa_register %rsp
  1061. .Lepilogue_avx:
  1062. ret
  1063. .cfi_endproc
  1064. .size sha1_multi_block_avx,.-sha1_multi_block_avx
  1065. ___
  1066. if ($avx>1) {
  1067. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  1068. $REG_SZ=32;
  1069. @ptr=map("%r$_",(12..15,8..11));
  1070. @V=($A,$B,$C,$D,$E)=map("%ymm$_",(0..4));
  1071. ($t0,$t1,$t2,$t3,$tx)=map("%ymm$_",(5..9));
  1072. @Xi=map("%ymm$_",(10..14));
  1073. $K="%ymm15";
  1074. $code.=<<___;
  1075. .type sha1_multi_block_avx2,\@function,3
  1076. .align 32
  1077. sha1_multi_block_avx2:
  1078. .cfi_startproc
  1079. _avx2_shortcut:
  1080. mov %rsp,%rax
  1081. .cfi_def_cfa_register %rax
  1082. push %rbx
  1083. .cfi_push %rbx
  1084. push %rbp
  1085. .cfi_push %rbp
  1086. push %r12
  1087. .cfi_push %r12
  1088. push %r13
  1089. .cfi_push %r13
  1090. push %r14
  1091. .cfi_push %r14
  1092. push %r15
  1093. .cfi_push %r15
  1094. ___
  1095. $code.=<<___ if ($win64);
  1096. lea -0xa8(%rsp),%rsp
  1097. movaps %xmm6,(%rsp)
  1098. movaps %xmm7,0x10(%rsp)
  1099. movaps %xmm8,0x20(%rsp)
  1100. movaps %xmm9,0x30(%rsp)
  1101. movaps %xmm10,0x40(%rsp)
  1102. movaps %xmm11,0x50(%rsp)
  1103. movaps %xmm12,-0x78(%rax)
  1104. movaps %xmm13,-0x68(%rax)
  1105. movaps %xmm14,-0x58(%rax)
  1106. movaps %xmm15,-0x48(%rax)
  1107. ___
  1108. $code.=<<___;
  1109. sub \$`$REG_SZ*18`, %rsp
  1110. and \$-256,%rsp
  1111. mov %rax,`$REG_SZ*17`(%rsp) # original %rsp
  1112. .cfi_cfa_expression %rsp+`$REG_SZ*17`,deref,+8
  1113. .Lbody_avx2:
  1114. lea K_XX_XX(%rip),$Tbl
  1115. shr \$1,$num
  1116. vzeroupper
  1117. .Loop_grande_avx2:
  1118. mov $num,`$REG_SZ*17+8`(%rsp) # original $num
  1119. xor $num,$num
  1120. lea `$REG_SZ*16`(%rsp),%rbx
  1121. ___
  1122. for($i=0;$i<8;$i++) {
  1123. $code.=<<___;
  1124. mov `16*$i+0`($inp),@ptr[$i] # input pointer
  1125. mov `16*$i+8`($inp),%ecx # number of blocks
  1126. cmp $num,%ecx
  1127. cmovg %ecx,$num # find maximum
  1128. test %ecx,%ecx
  1129. mov %ecx,`4*$i`(%rbx) # initialize counters
  1130. cmovle $Tbl,@ptr[$i] # cancel input
  1131. ___
  1132. }
  1133. $code.=<<___;
  1134. vmovdqu 0x00($ctx),$A # load context
  1135. lea 128(%rsp),%rax
  1136. vmovdqu 0x20($ctx),$B
  1137. lea 256+128(%rsp),%rbx
  1138. vmovdqu 0x40($ctx),$C
  1139. vmovdqu 0x60($ctx),$D
  1140. vmovdqu 0x80($ctx),$E
  1141. vmovdqu 0x60($Tbl),$tx # pbswap_mask
  1142. jmp .Loop_avx2
  1143. .align 32
  1144. .Loop_avx2:
  1145. ___
  1146. $code.=" vmovdqa -0x20($Tbl),$K\n"; # K_00_19
  1147. for($i=0;$i<20;$i++) { &BODY_00_19_avx($i,@V); unshift(@V,pop(@V)); }
  1148. $code.=" vmovdqa 0x00($Tbl),$K\n"; # K_20_39
  1149. for(;$i<40;$i++) { &BODY_20_39_avx($i,@V); unshift(@V,pop(@V)); }
  1150. $code.=" vmovdqa 0x20($Tbl),$K\n"; # K_40_59
  1151. for(;$i<60;$i++) { &BODY_40_59_avx($i,@V); unshift(@V,pop(@V)); }
  1152. $code.=" vmovdqa 0x40($Tbl),$K\n"; # K_60_79
  1153. for(;$i<80;$i++) { &BODY_20_39_avx($i,@V); unshift(@V,pop(@V)); }
  1154. $code.=<<___;
  1155. mov \$1,%ecx
  1156. lea `$REG_SZ*16`(%rsp),%rbx
  1157. ___
  1158. for($i=0;$i<8;$i++) {
  1159. $code.=<<___;
  1160. cmp `4*$i`(%rbx),%ecx # examine counters
  1161. cmovge $Tbl,@ptr[$i] # cancel input
  1162. ___
  1163. }
  1164. $code.=<<___;
  1165. vmovdqu (%rbx),$t0 # pull counters
  1166. vpxor $t2,$t2,$t2
  1167. vmovdqa $t0,$t1
  1168. vpcmpgtd $t2,$t1,$t1 # mask value
  1169. vpaddd $t1,$t0,$t0 # counters--
  1170. vpand $t1,$A,$A
  1171. vpand $t1,$B,$B
  1172. vpaddd 0x00($ctx),$A,$A
  1173. vpand $t1,$C,$C
  1174. vpaddd 0x20($ctx),$B,$B
  1175. vpand $t1,$D,$D
  1176. vpaddd 0x40($ctx),$C,$C
  1177. vpand $t1,$E,$E
  1178. vpaddd 0x60($ctx),$D,$D
  1179. vpaddd 0x80($ctx),$E,$E
  1180. vmovdqu $A,0x00($ctx)
  1181. vmovdqu $B,0x20($ctx)
  1182. vmovdqu $C,0x40($ctx)
  1183. vmovdqu $D,0x60($ctx)
  1184. vmovdqu $E,0x80($ctx)
  1185. vmovdqu $t0,(%rbx) # save counters
  1186. lea 256+128(%rsp),%rbx
  1187. vmovdqu 0x60($Tbl),$tx # pbswap_mask
  1188. dec $num
  1189. jnz .Loop_avx2
  1190. #mov `$REG_SZ*17+8`(%rsp),$num
  1191. #lea $REG_SZ($ctx),$ctx
  1192. #lea `16*$REG_SZ/4`($inp),$inp
  1193. #dec $num
  1194. #jnz .Loop_grande_avx2
  1195. .Ldone_avx2:
  1196. mov `$REG_SZ*17`(%rsp),%rax # original %rsp
  1197. .cfi_def_cfa %rax,8
  1198. vzeroupper
  1199. ___
  1200. $code.=<<___ if ($win64);
  1201. movaps -0xd8(%rax),%xmm6
  1202. movaps -0xc8(%rax),%xmm7
  1203. movaps -0xb8(%rax),%xmm8
  1204. movaps -0xa8(%rax),%xmm9
  1205. movaps -0x98(%rax),%xmm10
  1206. movaps -0x88(%rax),%xmm11
  1207. movaps -0x78(%rax),%xmm12
  1208. movaps -0x68(%rax),%xmm13
  1209. movaps -0x58(%rax),%xmm14
  1210. movaps -0x48(%rax),%xmm15
  1211. ___
  1212. $code.=<<___;
  1213. mov -48(%rax),%r15
  1214. .cfi_restore %r15
  1215. mov -40(%rax),%r14
  1216. .cfi_restore %r14
  1217. mov -32(%rax),%r13
  1218. .cfi_restore %r13
  1219. mov -24(%rax),%r12
  1220. .cfi_restore %r12
  1221. mov -16(%rax),%rbp
  1222. .cfi_restore %rbp
  1223. mov -8(%rax),%rbx
  1224. .cfi_restore %rbx
  1225. lea (%rax),%rsp
  1226. .cfi_def_cfa_register %rsp
  1227. .Lepilogue_avx2:
  1228. ret
  1229. .cfi_endproc
  1230. .size sha1_multi_block_avx2,.-sha1_multi_block_avx2
  1231. ___
  1232. } }}}
  1233. $code.=<<___;
  1234. .align 256
  1235. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1236. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1237. K_XX_XX:
  1238. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1239. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1240. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1241. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1242. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1243. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1244. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap
  1245. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap
  1246. .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
  1247. .asciz "SHA1 multi-block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  1248. ___
  1249. if ($win64) {
  1250. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1251. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1252. $rec="%rcx";
  1253. $frame="%rdx";
  1254. $context="%r8";
  1255. $disp="%r9";
  1256. $code.=<<___;
  1257. .extern __imp_RtlVirtualUnwind
  1258. .type se_handler,\@abi-omnipotent
  1259. .align 16
  1260. se_handler:
  1261. push %rsi
  1262. push %rdi
  1263. push %rbx
  1264. push %rbp
  1265. push %r12
  1266. push %r13
  1267. push %r14
  1268. push %r15
  1269. pushfq
  1270. sub \$64,%rsp
  1271. mov 120($context),%rax # pull context->Rax
  1272. mov 248($context),%rbx # pull context->Rip
  1273. mov 8($disp),%rsi # disp->ImageBase
  1274. mov 56($disp),%r11 # disp->HandlerData
  1275. mov 0(%r11),%r10d # HandlerData[0]
  1276. lea (%rsi,%r10),%r10 # end of prologue label
  1277. cmp %r10,%rbx # context->Rip<.Lbody
  1278. jb .Lin_prologue
  1279. mov 152($context),%rax # pull context->Rsp
  1280. mov 4(%r11),%r10d # HandlerData[1]
  1281. lea (%rsi,%r10),%r10 # epilogue label
  1282. cmp %r10,%rbx # context->Rip>=.Lepilogue
  1283. jae .Lin_prologue
  1284. mov `16*17`(%rax),%rax # pull saved stack pointer
  1285. mov -8(%rax),%rbx
  1286. mov -16(%rax),%rbp
  1287. mov %rbx,144($context) # restore context->Rbx
  1288. mov %rbp,160($context) # restore context->Rbp
  1289. lea -24-10*16(%rax),%rsi
  1290. lea 512($context),%rdi # &context.Xmm6
  1291. mov \$20,%ecx
  1292. .long 0xa548f3fc # cld; rep movsq
  1293. .Lin_prologue:
  1294. mov 8(%rax),%rdi
  1295. mov 16(%rax),%rsi
  1296. mov %rax,152($context) # restore context->Rsp
  1297. mov %rsi,168($context) # restore context->Rsi
  1298. mov %rdi,176($context) # restore context->Rdi
  1299. mov 40($disp),%rdi # disp->ContextRecord
  1300. mov $context,%rsi # context
  1301. mov \$154,%ecx # sizeof(CONTEXT)
  1302. .long 0xa548f3fc # cld; rep movsq
  1303. mov $disp,%rsi
  1304. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1305. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1306. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1307. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1308. mov 40(%rsi),%r10 # disp->ContextRecord
  1309. lea 56(%rsi),%r11 # &disp->HandlerData
  1310. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1311. mov %r10,32(%rsp) # arg5
  1312. mov %r11,40(%rsp) # arg6
  1313. mov %r12,48(%rsp) # arg7
  1314. mov %rcx,56(%rsp) # arg8, (NULL)
  1315. call *__imp_RtlVirtualUnwind(%rip)
  1316. mov \$1,%eax # ExceptionContinueSearch
  1317. add \$64,%rsp
  1318. popfq
  1319. pop %r15
  1320. pop %r14
  1321. pop %r13
  1322. pop %r12
  1323. pop %rbp
  1324. pop %rbx
  1325. pop %rdi
  1326. pop %rsi
  1327. ret
  1328. .size se_handler,.-se_handler
  1329. ___
  1330. $code.=<<___ if ($avx>1);
  1331. .type avx2_handler,\@abi-omnipotent
  1332. .align 16
  1333. avx2_handler:
  1334. push %rsi
  1335. push %rdi
  1336. push %rbx
  1337. push %rbp
  1338. push %r12
  1339. push %r13
  1340. push %r14
  1341. push %r15
  1342. pushfq
  1343. sub \$64,%rsp
  1344. mov 120($context),%rax # pull context->Rax
  1345. mov 248($context),%rbx # pull context->Rip
  1346. mov 8($disp),%rsi # disp->ImageBase
  1347. mov 56($disp),%r11 # disp->HandlerData
  1348. mov 0(%r11),%r10d # HandlerData[0]
  1349. lea (%rsi,%r10),%r10 # end of prologue label
  1350. cmp %r10,%rbx # context->Rip<body label
  1351. jb .Lin_prologue
  1352. mov 152($context),%rax # pull context->Rsp
  1353. mov 4(%r11),%r10d # HandlerData[1]
  1354. lea (%rsi,%r10),%r10 # epilogue label
  1355. cmp %r10,%rbx # context->Rip>=epilogue label
  1356. jae .Lin_prologue
  1357. mov `32*17`($context),%rax # pull saved stack pointer
  1358. mov -8(%rax),%rbx
  1359. mov -16(%rax),%rbp
  1360. mov -24(%rax),%r12
  1361. mov -32(%rax),%r13
  1362. mov -40(%rax),%r14
  1363. mov -48(%rax),%r15
  1364. mov %rbx,144($context) # restore context->Rbx
  1365. mov %rbp,160($context) # restore context->Rbp
  1366. mov %r12,216($context) # restore context->R12
  1367. mov %r13,224($context) # restore context->R13
  1368. mov %r14,232($context) # restore context->R14
  1369. mov %r15,240($context) # restore context->R15
  1370. lea -56-10*16(%rax),%rsi
  1371. lea 512($context),%rdi # &context.Xmm6
  1372. mov \$20,%ecx
  1373. .long 0xa548f3fc # cld; rep movsq
  1374. jmp .Lin_prologue
  1375. .size avx2_handler,.-avx2_handler
  1376. ___
  1377. $code.=<<___;
  1378. .section .pdata
  1379. .align 4
  1380. .rva .LSEH_begin_sha1_multi_block
  1381. .rva .LSEH_end_sha1_multi_block
  1382. .rva .LSEH_info_sha1_multi_block
  1383. .rva .LSEH_begin_sha1_multi_block_shaext
  1384. .rva .LSEH_end_sha1_multi_block_shaext
  1385. .rva .LSEH_info_sha1_multi_block_shaext
  1386. ___
  1387. $code.=<<___ if ($avx);
  1388. .rva .LSEH_begin_sha1_multi_block_avx
  1389. .rva .LSEH_end_sha1_multi_block_avx
  1390. .rva .LSEH_info_sha1_multi_block_avx
  1391. ___
  1392. $code.=<<___ if ($avx>1);
  1393. .rva .LSEH_begin_sha1_multi_block_avx2
  1394. .rva .LSEH_end_sha1_multi_block_avx2
  1395. .rva .LSEH_info_sha1_multi_block_avx2
  1396. ___
  1397. $code.=<<___;
  1398. .section .xdata
  1399. .align 8
  1400. .LSEH_info_sha1_multi_block:
  1401. .byte 9,0,0,0
  1402. .rva se_handler
  1403. .rva .Lbody,.Lepilogue # HandlerData[]
  1404. .LSEH_info_sha1_multi_block_shaext:
  1405. .byte 9,0,0,0
  1406. .rva se_handler
  1407. .rva .Lbody_shaext,.Lepilogue_shaext # HandlerData[]
  1408. ___
  1409. $code.=<<___ if ($avx);
  1410. .LSEH_info_sha1_multi_block_avx:
  1411. .byte 9,0,0,0
  1412. .rva se_handler
  1413. .rva .Lbody_avx,.Lepilogue_avx # HandlerData[]
  1414. ___
  1415. $code.=<<___ if ($avx>1);
  1416. .LSEH_info_sha1_multi_block_avx2:
  1417. .byte 9,0,0,0
  1418. .rva avx2_handler
  1419. .rva .Lbody_avx2,.Lepilogue_avx2 # HandlerData[]
  1420. ___
  1421. }
  1422. ####################################################################
  1423. sub rex {
  1424. local *opcode=shift;
  1425. my ($dst,$src)=@_;
  1426. my $rex=0;
  1427. $rex|=0x04 if ($dst>=8);
  1428. $rex|=0x01 if ($src>=8);
  1429. unshift @opcode,$rex|0x40 if ($rex);
  1430. }
  1431. sub sha1rnds4 {
  1432. if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1433. my @opcode=(0x0f,0x3a,0xcc);
  1434. rex(\@opcode,$3,$2);
  1435. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1436. my $c=$1;
  1437. push @opcode,$c=~/^0/?oct($c):$c;
  1438. return ".byte\t".join(',',@opcode);
  1439. } else {
  1440. return "sha1rnds4\t".@_[0];
  1441. }
  1442. }
  1443. sub sha1op38 {
  1444. my $instr = shift;
  1445. my %opcodelet = (
  1446. "sha1nexte" => 0xc8,
  1447. "sha1msg1" => 0xc9,
  1448. "sha1msg2" => 0xca );
  1449. if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1450. my @opcode=(0x0f,0x38);
  1451. rex(\@opcode,$2,$1);
  1452. push @opcode,$opcodelet{$instr};
  1453. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  1454. return ".byte\t".join(',',@opcode);
  1455. } else {
  1456. return $instr."\t".@_[0];
  1457. }
  1458. }
  1459. foreach (split("\n",$code)) {
  1460. s/\`([^\`]*)\`/eval($1)/ge;
  1461. s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
  1462. s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
  1463. s/\b(vmov[dq])\b(.+)%ymm([0-9]+)/$1$2%xmm$3/go or
  1464. s/\b(vmovdqu)\b(.+)%x%ymm([0-9]+)/$1$2%xmm$3/go or
  1465. s/\b(vpinsr[qd])\b(.+)%ymm([0-9]+),%ymm([0-9]+)/$1$2%xmm$3,%xmm$4/go or
  1466. s/\b(vpextr[qd])\b(.+)%ymm([0-9]+)/$1$2%xmm$3/go or
  1467. s/\b(vinserti128)\b(\s+)%ymm/$1$2\$1,%xmm/go or
  1468. s/\b(vpbroadcast[qd]\s+)%ymm([0-9]+)/$1%xmm$2/go;
  1469. print $_,"\n";
  1470. }
  1471. close STDOUT;