sha1-x86_64.pl 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. #! /usr/bin/env perl
  2. # Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # sha1_block procedure for x86_64.
  17. #
  18. # It was brought to my attention that on EM64T compiler-generated code
  19. # was far behind 32-bit assembler implementation. This is unlike on
  20. # Opteron where compiler-generated code was only 15% behind 32-bit
  21. # assembler, which originally made it hard to motivate the effort.
  22. # There was suggestion to mechanically translate 32-bit code, but I
  23. # dismissed it, reasoning that x86_64 offers enough register bank
  24. # capacity to fully utilize SHA-1 parallelism. Therefore this fresh
  25. # implementation:-) However! While 64-bit code does perform better
  26. # on Opteron, I failed to beat 32-bit assembler on EM64T core. Well,
  27. # x86_64 does offer larger *addressable* bank, but out-of-order core
  28. # reaches for even more registers through dynamic aliasing, and EM64T
  29. # core must have managed to run-time optimize even 32-bit code just as
  30. # good as 64-bit one. Performance improvement is summarized in the
  31. # following table:
  32. #
  33. # gcc 3.4 32-bit asm cycles/byte
  34. # Opteron +45% +20% 6.8
  35. # Xeon P4 +65% +0% 9.9
  36. # Core2 +60% +10% 7.0
  37. # August 2009.
  38. #
  39. # The code was revised to minimize code size and to maximize
  40. # "distance" between instructions producing input to 'lea'
  41. # instruction and the 'lea' instruction itself, which is essential
  42. # for Intel Atom core.
  43. # October 2010.
  44. #
  45. # Add SSSE3, Supplemental[!] SSE3, implementation. The idea behind it
  46. # is to offload message schedule denoted by Wt in NIST specification,
  47. # or Xupdate in OpenSSL source, to SIMD unit. See sha1-586.pl module
  48. # for background and implementation details. The only difference from
  49. # 32-bit code is that 64-bit code doesn't have to spill @X[] elements
  50. # to free temporary registers.
  51. # April 2011.
  52. #
  53. # Add AVX code path. See sha1-586.pl for further information.
  54. # May 2013.
  55. #
  56. # Add AVX2+BMI code path. Initial attempt (utilizing BMI instructions
  57. # and loading pair of consecutive blocks to 256-bit %ymm registers)
  58. # did not provide impressive performance improvement till a crucial
  59. # hint regarding the number of Xupdate iterations to pre-compute in
  60. # advance was provided by Ilya Albrekht of Intel Corp.
  61. # March 2014.
  62. #
  63. # Add support for Intel SHA Extensions.
  64. ######################################################################
  65. # Current performance is summarized in following table. Numbers are
  66. # CPU clock cycles spent to process single byte (less is better).
  67. #
  68. # x86_64 SSSE3 AVX[2]
  69. # P4 9.05 -
  70. # Opteron 6.26 -
  71. # Core2 6.55 6.05/+8% -
  72. # Westmere 6.73 5.30/+27% -
  73. # Sandy Bridge 7.70 6.10/+26% 4.99/+54%
  74. # Ivy Bridge 6.06 4.67/+30% 4.60/+32%
  75. # Haswell 5.45 4.15/+31% 3.57/+53%
  76. # Skylake 5.18 4.06/+28% 3.54/+46%
  77. # Bulldozer 9.11 5.95/+53%
  78. # Ryzen 4.75 3.80/+24% 1.93/+150%(**)
  79. # VIA Nano 9.32 7.15/+30%
  80. # Atom 10.3 9.17/+12%
  81. # Silvermont 13.1(*) 9.37/+40%
  82. # Knights L 13.2(*) 9.68/+36% 8.30/+59%
  83. # Goldmont 8.13 6.42/+27% 1.70/+380%(**)
  84. #
  85. # (*) obviously suboptimal result, nothing was done about it,
  86. # because SSSE3 code is compiled unconditionally;
  87. # (**) SHAEXT result
  88. # $output is the last argument if it looks like a file (it has an extension)
  89. # $flavour is the first argument if it doesn't look like a file
  90. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  91. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  92. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  93. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  94. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  95. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  96. die "can't locate x86_64-xlate.pl";
  97. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  98. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  99. $avx = ($1>=2.19) + ($1>=2.22);
  100. }
  101. if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  102. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  103. $avx = ($1>=2.09) + ($1>=2.10);
  104. }
  105. if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  106. `ml64 2>&1` =~ /Version ([0-9]+)\./) {
  107. $avx = ($1>=10) + ($1>=11);
  108. }
  109. if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+\.[0-9]+)/) {
  110. $avx = ($2>=3.0) + ($2>3.0);
  111. }
  112. $shaext=1; ### set to zero if compiling for 1.0.1
  113. $avx=1 if (!$shaext && $avx);
  114. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
  115. or die "can't call $xlate: $!";
  116. *STDOUT=*OUT;
  117. $ctx="%rdi"; # 1st arg
  118. $inp="%rsi"; # 2nd arg
  119. $num="%rdx"; # 3rd arg
  120. # reassign arguments in order to produce more compact code
  121. $ctx="%r8";
  122. $inp="%r9";
  123. $num="%r10";
  124. $t0="%eax";
  125. $t1="%ebx";
  126. $t2="%ecx";
  127. @xi=("%edx","%ebp","%r14d");
  128. $A="%esi";
  129. $B="%edi";
  130. $C="%r11d";
  131. $D="%r12d";
  132. $E="%r13d";
  133. @V=($A,$B,$C,$D,$E);
  134. sub BODY_00_19 {
  135. my ($i,$a,$b,$c,$d,$e)=@_;
  136. my $j=$i+1;
  137. $code.=<<___ if ($i==0);
  138. mov `4*$i`($inp),$xi[0]
  139. bswap $xi[0]
  140. ___
  141. $code.=<<___ if ($i<15);
  142. mov `4*$j`($inp),$xi[1]
  143. mov $d,$t0
  144. mov $xi[0],`4*$i`(%rsp)
  145. mov $a,$t2
  146. bswap $xi[1]
  147. xor $c,$t0
  148. rol \$5,$t2
  149. and $b,$t0
  150. lea 0x5a827999($xi[0],$e),$e
  151. add $t2,$e
  152. xor $d,$t0
  153. rol \$30,$b
  154. add $t0,$e
  155. ___
  156. $code.=<<___ if ($i>=15);
  157. xor `4*($j%16)`(%rsp),$xi[1]
  158. mov $d,$t0
  159. mov $xi[0],`4*($i%16)`(%rsp)
  160. mov $a,$t2
  161. xor `4*(($j+2)%16)`(%rsp),$xi[1]
  162. xor $c,$t0
  163. rol \$5,$t2
  164. xor `4*(($j+8)%16)`(%rsp),$xi[1]
  165. and $b,$t0
  166. lea 0x5a827999($xi[0],$e),$e
  167. rol \$30,$b
  168. xor $d,$t0
  169. add $t2,$e
  170. rol \$1,$xi[1]
  171. add $t0,$e
  172. ___
  173. push(@xi,shift(@xi));
  174. }
  175. sub BODY_20_39 {
  176. my ($i,$a,$b,$c,$d,$e)=@_;
  177. my $j=$i+1;
  178. my $K=($i<40)?0x6ed9eba1:0xca62c1d6;
  179. $code.=<<___ if ($i<79);
  180. xor `4*($j%16)`(%rsp),$xi[1]
  181. mov $b,$t0
  182. `"mov $xi[0],".4*($i%16)."(%rsp)" if ($i<72)`
  183. mov $a,$t2
  184. xor `4*(($j+2)%16)`(%rsp),$xi[1]
  185. xor $d,$t0
  186. rol \$5,$t2
  187. xor `4*(($j+8)%16)`(%rsp),$xi[1]
  188. lea $K($xi[0],$e),$e
  189. xor $c,$t0
  190. add $t2,$e
  191. rol \$30,$b
  192. add $t0,$e
  193. rol \$1,$xi[1]
  194. ___
  195. $code.=<<___ if ($i==79);
  196. mov $b,$t0
  197. mov $a,$t2
  198. xor $d,$t0
  199. lea $K($xi[0],$e),$e
  200. rol \$5,$t2
  201. xor $c,$t0
  202. add $t2,$e
  203. rol \$30,$b
  204. add $t0,$e
  205. ___
  206. push(@xi,shift(@xi));
  207. }
  208. sub BODY_40_59 {
  209. my ($i,$a,$b,$c,$d,$e)=@_;
  210. my $j=$i+1;
  211. $code.=<<___;
  212. xor `4*($j%16)`(%rsp),$xi[1]
  213. mov $d,$t0
  214. mov $xi[0],`4*($i%16)`(%rsp)
  215. mov $d,$t1
  216. xor `4*(($j+2)%16)`(%rsp),$xi[1]
  217. and $c,$t0
  218. mov $a,$t2
  219. xor `4*(($j+8)%16)`(%rsp),$xi[1]
  220. lea 0x8f1bbcdc($xi[0],$e),$e
  221. xor $c,$t1
  222. rol \$5,$t2
  223. add $t0,$e
  224. rol \$1,$xi[1]
  225. and $b,$t1
  226. add $t2,$e
  227. rol \$30,$b
  228. add $t1,$e
  229. ___
  230. push(@xi,shift(@xi));
  231. }
  232. $code.=<<___;
  233. .text
  234. .extern OPENSSL_ia32cap_P
  235. .globl sha1_block_data_order
  236. .type sha1_block_data_order,\@function,3
  237. .align 16
  238. sha1_block_data_order:
  239. .cfi_startproc
  240. mov OPENSSL_ia32cap_P+0(%rip),%r9d
  241. mov OPENSSL_ia32cap_P+4(%rip),%r8d
  242. mov OPENSSL_ia32cap_P+8(%rip),%r10d
  243. test \$`1<<9`,%r8d # check SSSE3 bit
  244. jz .Lialu
  245. ___
  246. $code.=<<___ if ($shaext);
  247. test \$`1<<29`,%r10d # check SHA bit
  248. jnz _shaext_shortcut
  249. ___
  250. $code.=<<___ if ($avx>1);
  251. and \$`1<<3|1<<5|1<<8`,%r10d # check AVX2+BMI1+BMI2
  252. cmp \$`1<<3|1<<5|1<<8`,%r10d
  253. je _avx2_shortcut
  254. ___
  255. $code.=<<___ if ($avx);
  256. and \$`1<<28`,%r8d # mask AVX bit
  257. and \$`1<<30`,%r9d # mask "Intel CPU" bit
  258. or %r9d,%r8d
  259. cmp \$`1<<28|1<<30`,%r8d
  260. je _avx_shortcut
  261. ___
  262. $code.=<<___;
  263. jmp _ssse3_shortcut
  264. .align 16
  265. .Lialu:
  266. mov %rsp,%rax
  267. .cfi_def_cfa_register %rax
  268. push %rbx
  269. .cfi_push %rbx
  270. push %rbp
  271. .cfi_push %rbp
  272. push %r12
  273. .cfi_push %r12
  274. push %r13
  275. .cfi_push %r13
  276. push %r14
  277. .cfi_push %r14
  278. mov %rdi,$ctx # reassigned argument
  279. sub \$`8+16*4`,%rsp
  280. mov %rsi,$inp # reassigned argument
  281. and \$-64,%rsp
  282. mov %rdx,$num # reassigned argument
  283. mov %rax,`16*4`(%rsp)
  284. .cfi_cfa_expression %rsp+64,deref,+8
  285. .Lprologue:
  286. mov 0($ctx),$A
  287. mov 4($ctx),$B
  288. mov 8($ctx),$C
  289. mov 12($ctx),$D
  290. mov 16($ctx),$E
  291. jmp .Lloop
  292. .align 16
  293. .Lloop:
  294. ___
  295. for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
  296. for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  297. for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  298. for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  299. $code.=<<___;
  300. add 0($ctx),$A
  301. add 4($ctx),$B
  302. add 8($ctx),$C
  303. add 12($ctx),$D
  304. add 16($ctx),$E
  305. mov $A,0($ctx)
  306. mov $B,4($ctx)
  307. mov $C,8($ctx)
  308. mov $D,12($ctx)
  309. mov $E,16($ctx)
  310. sub \$1,$num
  311. lea `16*4`($inp),$inp
  312. jnz .Lloop
  313. mov `16*4`(%rsp),%rsi
  314. .cfi_def_cfa %rsi,8
  315. mov -40(%rsi),%r14
  316. .cfi_restore %r14
  317. mov -32(%rsi),%r13
  318. .cfi_restore %r13
  319. mov -24(%rsi),%r12
  320. .cfi_restore %r12
  321. mov -16(%rsi),%rbp
  322. .cfi_restore %rbp
  323. mov -8(%rsi),%rbx
  324. .cfi_restore %rbx
  325. lea (%rsi),%rsp
  326. .cfi_def_cfa_register %rsp
  327. .Lepilogue:
  328. ret
  329. .cfi_endproc
  330. .size sha1_block_data_order,.-sha1_block_data_order
  331. ___
  332. if ($shaext) {{{
  333. ######################################################################
  334. # Intel SHA Extensions implementation of SHA1 update function.
  335. #
  336. my ($ctx,$inp,$num)=("%rdi","%rsi","%rdx");
  337. my ($ABCD,$E,$E_,$BSWAP,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(0..3,8,9));
  338. my @MSG=map("%xmm$_",(4..7));
  339. $code.=<<___;
  340. .type sha1_block_data_order_shaext,\@function,3
  341. .align 32
  342. sha1_block_data_order_shaext:
  343. _shaext_shortcut:
  344. .cfi_startproc
  345. ___
  346. $code.=<<___ if ($win64);
  347. lea `-8-4*16`(%rsp),%rsp
  348. movaps %xmm6,-8-4*16(%rax)
  349. movaps %xmm7,-8-3*16(%rax)
  350. movaps %xmm8,-8-2*16(%rax)
  351. movaps %xmm9,-8-1*16(%rax)
  352. .Lprologue_shaext:
  353. ___
  354. $code.=<<___;
  355. movdqu ($ctx),$ABCD
  356. movd 16($ctx),$E
  357. movdqa K_XX_XX+0xa0(%rip),$BSWAP # byte-n-word swap
  358. movdqu ($inp),@MSG[0]
  359. pshufd \$0b00011011,$ABCD,$ABCD # flip word order
  360. movdqu 0x10($inp),@MSG[1]
  361. pshufd \$0b00011011,$E,$E # flip word order
  362. movdqu 0x20($inp),@MSG[2]
  363. pshufb $BSWAP,@MSG[0]
  364. movdqu 0x30($inp),@MSG[3]
  365. pshufb $BSWAP,@MSG[1]
  366. pshufb $BSWAP,@MSG[2]
  367. movdqa $E,$E_SAVE # offload $E
  368. pshufb $BSWAP,@MSG[3]
  369. jmp .Loop_shaext
  370. .align 16
  371. .Loop_shaext:
  372. dec $num
  373. lea 0x40($inp),%r8 # next input block
  374. paddd @MSG[0],$E
  375. cmovne %r8,$inp
  376. movdqa $ABCD,$ABCD_SAVE # offload $ABCD
  377. ___
  378. for($i=0;$i<20-4;$i+=2) {
  379. $code.=<<___;
  380. sha1msg1 @MSG[1],@MSG[0]
  381. movdqa $ABCD,$E_
  382. sha1rnds4 \$`int($i/5)`,$E,$ABCD # 0-3...
  383. sha1nexte @MSG[1],$E_
  384. pxor @MSG[2],@MSG[0]
  385. sha1msg1 @MSG[2],@MSG[1]
  386. sha1msg2 @MSG[3],@MSG[0]
  387. movdqa $ABCD,$E
  388. sha1rnds4 \$`int(($i+1)/5)`,$E_,$ABCD
  389. sha1nexte @MSG[2],$E
  390. pxor @MSG[3],@MSG[1]
  391. sha1msg2 @MSG[0],@MSG[1]
  392. ___
  393. push(@MSG,shift(@MSG)); push(@MSG,shift(@MSG));
  394. }
  395. $code.=<<___;
  396. movdqu ($inp),@MSG[0]
  397. movdqa $ABCD,$E_
  398. sha1rnds4 \$3,$E,$ABCD # 64-67
  399. sha1nexte @MSG[1],$E_
  400. movdqu 0x10($inp),@MSG[1]
  401. pshufb $BSWAP,@MSG[0]
  402. movdqa $ABCD,$E
  403. sha1rnds4 \$3,$E_,$ABCD # 68-71
  404. sha1nexte @MSG[2],$E
  405. movdqu 0x20($inp),@MSG[2]
  406. pshufb $BSWAP,@MSG[1]
  407. movdqa $ABCD,$E_
  408. sha1rnds4 \$3,$E,$ABCD # 72-75
  409. sha1nexte @MSG[3],$E_
  410. movdqu 0x30($inp),@MSG[3]
  411. pshufb $BSWAP,@MSG[2]
  412. movdqa $ABCD,$E
  413. sha1rnds4 \$3,$E_,$ABCD # 76-79
  414. sha1nexte $E_SAVE,$E
  415. pshufb $BSWAP,@MSG[3]
  416. paddd $ABCD_SAVE,$ABCD
  417. movdqa $E,$E_SAVE # offload $E
  418. jnz .Loop_shaext
  419. pshufd \$0b00011011,$ABCD,$ABCD
  420. pshufd \$0b00011011,$E,$E
  421. movdqu $ABCD,($ctx)
  422. movd $E,16($ctx)
  423. ___
  424. $code.=<<___ if ($win64);
  425. movaps -8-4*16(%rax),%xmm6
  426. movaps -8-3*16(%rax),%xmm7
  427. movaps -8-2*16(%rax),%xmm8
  428. movaps -8-1*16(%rax),%xmm9
  429. mov %rax,%rsp
  430. .Lepilogue_shaext:
  431. ___
  432. $code.=<<___;
  433. ret
  434. .cfi_endproc
  435. .size sha1_block_data_order_shaext,.-sha1_block_data_order_shaext
  436. ___
  437. }}}
  438. {{{
  439. my $Xi=4;
  440. my @X=map("%xmm$_",(4..7,0..3));
  441. my @Tx=map("%xmm$_",(8..10));
  442. my $Kx="%xmm11";
  443. my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
  444. my @T=("%esi","%edi");
  445. my $j=0;
  446. my $rx=0;
  447. my $K_XX_XX="%r14";
  448. my $fp="%r11";
  449. my $_rol=sub { &rol(@_) };
  450. my $_ror=sub { &ror(@_) };
  451. { my $sn;
  452. sub align32() {
  453. ++$sn;
  454. $code.=<<___;
  455. jmp .Lalign32_$sn # see "Decoded ICache" in manual
  456. .align 32
  457. .Lalign32_$sn:
  458. ___
  459. }
  460. }
  461. $code.=<<___;
  462. .type sha1_block_data_order_ssse3,\@function,3
  463. .align 16
  464. sha1_block_data_order_ssse3:
  465. _ssse3_shortcut:
  466. .cfi_startproc
  467. mov %rsp,$fp # frame pointer
  468. .cfi_def_cfa_register $fp
  469. push %rbx
  470. .cfi_push %rbx
  471. push %rbp
  472. .cfi_push %rbp
  473. push %r12
  474. .cfi_push %r12
  475. push %r13 # redundant, done to share Win64 SE handler
  476. .cfi_push %r13
  477. push %r14
  478. .cfi_push %r14
  479. lea `-64-($win64?6*16:0)`(%rsp),%rsp
  480. ___
  481. $code.=<<___ if ($win64);
  482. movaps %xmm6,-40-6*16($fp)
  483. movaps %xmm7,-40-5*16($fp)
  484. movaps %xmm8,-40-4*16($fp)
  485. movaps %xmm9,-40-3*16($fp)
  486. movaps %xmm10,-40-2*16($fp)
  487. movaps %xmm11,-40-1*16($fp)
  488. .Lprologue_ssse3:
  489. ___
  490. $code.=<<___;
  491. and \$-64,%rsp
  492. mov %rdi,$ctx # reassigned argument
  493. mov %rsi,$inp # reassigned argument
  494. mov %rdx,$num # reassigned argument
  495. shl \$6,$num
  496. add $inp,$num
  497. lea K_XX_XX+64(%rip),$K_XX_XX
  498. mov 0($ctx),$A # load context
  499. mov 4($ctx),$B
  500. mov 8($ctx),$C
  501. mov 12($ctx),$D
  502. mov $B,@T[0] # magic seed
  503. mov 16($ctx),$E
  504. mov $C,@T[1]
  505. xor $D,@T[1]
  506. and @T[1],@T[0]
  507. movdqa 64($K_XX_XX),@X[2] # pbswap mask
  508. movdqa -64($K_XX_XX),@Tx[1] # K_00_19
  509. movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  510. movdqu 16($inp),@X[-3&7]
  511. movdqu 32($inp),@X[-2&7]
  512. movdqu 48($inp),@X[-1&7]
  513. pshufb @X[2],@X[-4&7] # byte swap
  514. pshufb @X[2],@X[-3&7]
  515. pshufb @X[2],@X[-2&7]
  516. add \$64,$inp
  517. paddd @Tx[1],@X[-4&7] # add K_00_19
  518. pshufb @X[2],@X[-1&7]
  519. paddd @Tx[1],@X[-3&7]
  520. paddd @Tx[1],@X[-2&7]
  521. movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
  522. psubd @Tx[1],@X[-4&7] # restore X[]
  523. movdqa @X[-3&7],16(%rsp)
  524. psubd @Tx[1],@X[-3&7]
  525. movdqa @X[-2&7],32(%rsp)
  526. psubd @Tx[1],@X[-2&7]
  527. jmp .Loop_ssse3
  528. ___
  529. sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
  530. { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
  531. my $arg = pop;
  532. $arg = "\$$arg" if ($arg*1 eq $arg);
  533. $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
  534. }
  535. sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
  536. { use integer;
  537. my $body = shift;
  538. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  539. my ($a,$b,$c,$d,$e);
  540. eval(shift(@insns)); # ror
  541. &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
  542. eval(shift(@insns));
  543. &movdqa (@Tx[0],@X[-1&7]);
  544. &paddd (@Tx[1],@X[-1&7]);
  545. eval(shift(@insns));
  546. eval(shift(@insns));
  547. &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
  548. eval(shift(@insns));
  549. eval(shift(@insns)); # rol
  550. eval(shift(@insns));
  551. &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
  552. eval(shift(@insns));
  553. eval(shift(@insns));
  554. &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  555. eval(shift(@insns));
  556. eval(shift(@insns)); # ror
  557. &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  558. eval(shift(@insns));
  559. eval(shift(@insns));
  560. eval(shift(@insns));
  561. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  562. eval(shift(@insns));
  563. eval(shift(@insns)); # rol
  564. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  565. eval(shift(@insns));
  566. eval(shift(@insns));
  567. &movdqa (@Tx[2],@X[0]);
  568. eval(shift(@insns));
  569. eval(shift(@insns));
  570. eval(shift(@insns)); # ror
  571. &movdqa (@Tx[0],@X[0]);
  572. eval(shift(@insns));
  573. &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
  574. &paddd (@X[0],@X[0]);
  575. eval(shift(@insns));
  576. eval(shift(@insns));
  577. &psrld (@Tx[0],31);
  578. eval(shift(@insns));
  579. eval(shift(@insns)); # rol
  580. eval(shift(@insns));
  581. &movdqa (@Tx[1],@Tx[2]);
  582. eval(shift(@insns));
  583. eval(shift(@insns));
  584. &psrld (@Tx[2],30);
  585. eval(shift(@insns));
  586. eval(shift(@insns)); # ror
  587. &por (@X[0],@Tx[0]); # "X[0]"<<<=1
  588. eval(shift(@insns));
  589. eval(shift(@insns));
  590. eval(shift(@insns));
  591. &pslld (@Tx[1],2);
  592. &pxor (@X[0],@Tx[2]);
  593. eval(shift(@insns));
  594. &movdqa (@Tx[2],eval(2*16*(($Xi)/5)-64)."($K_XX_XX)"); # K_XX_XX
  595. eval(shift(@insns)); # rol
  596. eval(shift(@insns));
  597. eval(shift(@insns));
  598. &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
  599. &pshufd (@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
  600. foreach (@insns) { eval; } # remaining instructions [if any]
  601. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  602. push(@Tx,shift(@Tx));
  603. }
  604. sub Xupdate_ssse3_32_79()
  605. { use integer;
  606. my $body = shift;
  607. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  608. my ($a,$b,$c,$d,$e);
  609. eval(shift(@insns)) if ($Xi==8);
  610. &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  611. eval(shift(@insns)) if ($Xi==8);
  612. eval(shift(@insns)); # body_20_39
  613. eval(shift(@insns));
  614. eval(shift(@insns)) if (@insns[1] =~ /_ror/);
  615. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  616. &punpcklqdq(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
  617. eval(shift(@insns));
  618. eval(shift(@insns)); # rol
  619. &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  620. eval(shift(@insns));
  621. eval(shift(@insns));
  622. if ($Xi%5) {
  623. &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
  624. } else { # ... or load next one
  625. &movdqa (@Tx[2],eval(2*16*($Xi/5)-64)."($K_XX_XX)");
  626. }
  627. eval(shift(@insns)); # ror
  628. &paddd (@Tx[1],@X[-1&7]);
  629. eval(shift(@insns));
  630. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  631. eval(shift(@insns)); # body_20_39
  632. eval(shift(@insns));
  633. eval(shift(@insns));
  634. eval(shift(@insns)); # rol
  635. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  636. &movdqa (@Tx[0],@X[0]);
  637. eval(shift(@insns));
  638. eval(shift(@insns));
  639. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  640. eval(shift(@insns)); # ror
  641. eval(shift(@insns));
  642. eval(shift(@insns)); # body_20_39
  643. &pslld (@X[0],2);
  644. eval(shift(@insns));
  645. eval(shift(@insns));
  646. &psrld (@Tx[0],30);
  647. eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
  648. eval(shift(@insns));
  649. eval(shift(@insns));
  650. eval(shift(@insns)); # ror
  651. &por (@X[0],@Tx[0]); # "X[0]"<<<=2
  652. eval(shift(@insns));
  653. eval(shift(@insns)); # body_20_39
  654. eval(shift(@insns)) if (@insns[1] =~ /_rol/);
  655. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  656. &pshufd(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
  657. eval(shift(@insns));
  658. eval(shift(@insns)); # rol
  659. eval(shift(@insns));
  660. eval(shift(@insns));
  661. eval(shift(@insns)); # rol
  662. eval(shift(@insns));
  663. foreach (@insns) { eval; } # remaining instructions
  664. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  665. push(@Tx,shift(@Tx));
  666. }
  667. sub Xuplast_ssse3_80()
  668. { use integer;
  669. my $body = shift;
  670. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  671. my ($a,$b,$c,$d,$e);
  672. eval(shift(@insns));
  673. eval(shift(@insns));
  674. eval(shift(@insns));
  675. eval(shift(@insns));
  676. &paddd (@Tx[1],@X[-1&7]);
  677. eval(shift(@insns));
  678. eval(shift(@insns));
  679. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  680. foreach (@insns) { eval; } # remaining instructions
  681. &cmp ($inp,$num);
  682. &je (".Ldone_ssse3");
  683. unshift(@Tx,pop(@Tx));
  684. &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
  685. &movdqa (@Tx[1],"-64($K_XX_XX)"); # K_00_19
  686. &movdqu (@X[-4&7],"0($inp)"); # load input
  687. &movdqu (@X[-3&7],"16($inp)");
  688. &movdqu (@X[-2&7],"32($inp)");
  689. &movdqu (@X[-1&7],"48($inp)");
  690. &pshufb (@X[-4&7],@X[2]); # byte swap
  691. &add ($inp,64);
  692. $Xi=0;
  693. }
  694. sub Xloop_ssse3()
  695. { use integer;
  696. my $body = shift;
  697. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  698. my ($a,$b,$c,$d,$e);
  699. eval(shift(@insns));
  700. eval(shift(@insns));
  701. eval(shift(@insns));
  702. &pshufb (@X[($Xi-3)&7],@X[2]);
  703. eval(shift(@insns));
  704. eval(shift(@insns));
  705. eval(shift(@insns));
  706. eval(shift(@insns));
  707. &paddd (@X[($Xi-4)&7],@Tx[1]);
  708. eval(shift(@insns));
  709. eval(shift(@insns));
  710. eval(shift(@insns));
  711. eval(shift(@insns));
  712. &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
  713. eval(shift(@insns));
  714. eval(shift(@insns));
  715. eval(shift(@insns));
  716. eval(shift(@insns));
  717. &psubd (@X[($Xi-4)&7],@Tx[1]);
  718. foreach (@insns) { eval; }
  719. $Xi++;
  720. }
  721. sub Xtail_ssse3()
  722. { use integer;
  723. my $body = shift;
  724. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  725. my ($a,$b,$c,$d,$e);
  726. foreach (@insns) { eval; }
  727. }
  728. sub body_00_19 () { # ((c^d)&b)^d
  729. # on start @T[0]=(c^d)&b
  730. return &body_20_39() if ($rx==19); $rx++;
  731. (
  732. '($a,$b,$c,$d,$e)=@V;'.
  733. '&$_ror ($b,$j?7:2)', # $b>>>2
  734. '&xor (@T[0],$d)',
  735. '&mov (@T[1],$a)', # $b for next round
  736. '&add ($e,eval(4*($j&15))."(%rsp)")', # X[]+K xfer
  737. '&xor ($b,$c)', # $c^$d for next round
  738. '&$_rol ($a,5)',
  739. '&add ($e,@T[0])',
  740. '&and (@T[1],$b)', # ($b&($c^$d)) for next round
  741. '&xor ($b,$c)', # restore $b
  742. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  743. );
  744. }
  745. sub body_20_39 () { # b^d^c
  746. # on entry @T[0]=b^d
  747. return &body_40_59() if ($rx==39); $rx++;
  748. (
  749. '($a,$b,$c,$d,$e)=@V;'.
  750. '&add ($e,eval(4*($j&15))."(%rsp)")', # X[]+K xfer
  751. '&xor (@T[0],$d) if($j==19);'.
  752. '&xor (@T[0],$c) if($j> 19)', # ($b^$d^$c)
  753. '&mov (@T[1],$a)', # $b for next round
  754. '&$_rol ($a,5)',
  755. '&add ($e,@T[0])',
  756. '&xor (@T[1],$c) if ($j< 79)', # $b^$d for next round
  757. '&$_ror ($b,7)', # $b>>>2
  758. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  759. );
  760. }
  761. sub body_40_59 () { # ((b^c)&(c^d))^c
  762. # on entry @T[0]=(b^c), (c^=d)
  763. $rx++;
  764. (
  765. '($a,$b,$c,$d,$e)=@V;'.
  766. '&add ($e,eval(4*($j&15))."(%rsp)")', # X[]+K xfer
  767. '&and (@T[0],$c) if ($j>=40)', # (b^c)&(c^d)
  768. '&xor ($c,$d) if ($j>=40)', # restore $c
  769. '&$_ror ($b,7)', # $b>>>2
  770. '&mov (@T[1],$a)', # $b for next round
  771. '&xor (@T[0],$c)',
  772. '&$_rol ($a,5)',
  773. '&add ($e,@T[0])',
  774. '&xor (@T[1],$c) if ($j==59);'.
  775. '&xor (@T[1],$b) if ($j< 59)', # b^c for next round
  776. '&xor ($b,$c) if ($j< 59)', # c^d for next round
  777. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  778. );
  779. }
  780. $code.=<<___;
  781. .align 16
  782. .Loop_ssse3:
  783. ___
  784. &Xupdate_ssse3_16_31(\&body_00_19);
  785. &Xupdate_ssse3_16_31(\&body_00_19);
  786. &Xupdate_ssse3_16_31(\&body_00_19);
  787. &Xupdate_ssse3_16_31(\&body_00_19);
  788. &Xupdate_ssse3_32_79(\&body_00_19);
  789. &Xupdate_ssse3_32_79(\&body_20_39);
  790. &Xupdate_ssse3_32_79(\&body_20_39);
  791. &Xupdate_ssse3_32_79(\&body_20_39);
  792. &Xupdate_ssse3_32_79(\&body_20_39);
  793. &Xupdate_ssse3_32_79(\&body_20_39);
  794. &Xupdate_ssse3_32_79(\&body_40_59);
  795. &Xupdate_ssse3_32_79(\&body_40_59);
  796. &Xupdate_ssse3_32_79(\&body_40_59);
  797. &Xupdate_ssse3_32_79(\&body_40_59);
  798. &Xupdate_ssse3_32_79(\&body_40_59);
  799. &Xupdate_ssse3_32_79(\&body_20_39);
  800. &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
  801. $saved_j=$j; @saved_V=@V;
  802. &Xloop_ssse3(\&body_20_39);
  803. &Xloop_ssse3(\&body_20_39);
  804. &Xloop_ssse3(\&body_20_39);
  805. $code.=<<___;
  806. add 0($ctx),$A # update context
  807. add 4($ctx),@T[0]
  808. add 8($ctx),$C
  809. add 12($ctx),$D
  810. mov $A,0($ctx)
  811. add 16($ctx),$E
  812. mov @T[0],4($ctx)
  813. mov @T[0],$B # magic seed
  814. mov $C,8($ctx)
  815. mov $C,@T[1]
  816. mov $D,12($ctx)
  817. xor $D,@T[1]
  818. mov $E,16($ctx)
  819. and @T[1],@T[0]
  820. jmp .Loop_ssse3
  821. .align 16
  822. .Ldone_ssse3:
  823. ___
  824. $j=$saved_j; @V=@saved_V;
  825. &Xtail_ssse3(\&body_20_39);
  826. &Xtail_ssse3(\&body_20_39);
  827. &Xtail_ssse3(\&body_20_39);
  828. $code.=<<___;
  829. add 0($ctx),$A # update context
  830. add 4($ctx),@T[0]
  831. add 8($ctx),$C
  832. mov $A,0($ctx)
  833. add 12($ctx),$D
  834. mov @T[0],4($ctx)
  835. add 16($ctx),$E
  836. mov $C,8($ctx)
  837. mov $D,12($ctx)
  838. mov $E,16($ctx)
  839. ___
  840. $code.=<<___ if ($win64);
  841. movaps -40-6*16($fp),%xmm6
  842. movaps -40-5*16($fp),%xmm7
  843. movaps -40-4*16($fp),%xmm8
  844. movaps -40-3*16($fp),%xmm9
  845. movaps -40-2*16($fp),%xmm10
  846. movaps -40-1*16($fp),%xmm11
  847. ___
  848. $code.=<<___;
  849. mov -40($fp),%r14
  850. .cfi_restore %r14
  851. mov -32($fp),%r13
  852. .cfi_restore %r13
  853. mov -24($fp),%r12
  854. .cfi_restore %r12
  855. mov -16($fp),%rbp
  856. .cfi_restore %rbp
  857. mov -8($fp),%rbx
  858. .cfi_restore %rbx
  859. lea ($fp),%rsp
  860. .cfi_def_cfa_register %rsp
  861. .Lepilogue_ssse3:
  862. ret
  863. .cfi_endproc
  864. .size sha1_block_data_order_ssse3,.-sha1_block_data_order_ssse3
  865. ___
  866. if ($avx) {
  867. $Xi=4; # reset variables
  868. @X=map("%xmm$_",(4..7,0..3));
  869. @Tx=map("%xmm$_",(8..10));
  870. $j=0;
  871. $rx=0;
  872. my $done_avx_label=".Ldone_avx";
  873. my $_rol=sub { &shld(@_[0],@_) };
  874. my $_ror=sub { &shrd(@_[0],@_) };
  875. $code.=<<___;
  876. .type sha1_block_data_order_avx,\@function,3
  877. .align 16
  878. sha1_block_data_order_avx:
  879. _avx_shortcut:
  880. .cfi_startproc
  881. mov %rsp,$fp
  882. .cfi_def_cfa_register $fp
  883. push %rbx
  884. .cfi_push %rbx
  885. push %rbp
  886. .cfi_push %rbp
  887. push %r12
  888. .cfi_push %r12
  889. push %r13 # redundant, done to share Win64 SE handler
  890. .cfi_push %r13
  891. push %r14
  892. .cfi_push %r14
  893. lea `-64-($win64?6*16:0)`(%rsp),%rsp
  894. vzeroupper
  895. ___
  896. $code.=<<___ if ($win64);
  897. vmovaps %xmm6,-40-6*16($fp)
  898. vmovaps %xmm7,-40-5*16($fp)
  899. vmovaps %xmm8,-40-4*16($fp)
  900. vmovaps %xmm9,-40-3*16($fp)
  901. vmovaps %xmm10,-40-2*16($fp)
  902. vmovaps %xmm11,-40-1*16($fp)
  903. .Lprologue_avx:
  904. ___
  905. $code.=<<___;
  906. and \$-64,%rsp
  907. mov %rdi,$ctx # reassigned argument
  908. mov %rsi,$inp # reassigned argument
  909. mov %rdx,$num # reassigned argument
  910. shl \$6,$num
  911. add $inp,$num
  912. lea K_XX_XX+64(%rip),$K_XX_XX
  913. mov 0($ctx),$A # load context
  914. mov 4($ctx),$B
  915. mov 8($ctx),$C
  916. mov 12($ctx),$D
  917. mov $B,@T[0] # magic seed
  918. mov 16($ctx),$E
  919. mov $C,@T[1]
  920. xor $D,@T[1]
  921. and @T[1],@T[0]
  922. vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
  923. vmovdqa -64($K_XX_XX),$Kx # K_00_19
  924. vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  925. vmovdqu 16($inp),@X[-3&7]
  926. vmovdqu 32($inp),@X[-2&7]
  927. vmovdqu 48($inp),@X[-1&7]
  928. vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
  929. add \$64,$inp
  930. vpshufb @X[2],@X[-3&7],@X[-3&7]
  931. vpshufb @X[2],@X[-2&7],@X[-2&7]
  932. vpshufb @X[2],@X[-1&7],@X[-1&7]
  933. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  934. vpaddd $Kx,@X[-3&7],@X[1]
  935. vpaddd $Kx,@X[-2&7],@X[2]
  936. vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
  937. vmovdqa @X[1],16(%rsp)
  938. vmovdqa @X[2],32(%rsp)
  939. jmp .Loop_avx
  940. ___
  941. sub Xupdate_avx_16_31() # recall that $Xi starts with 4
  942. { use integer;
  943. my $body = shift;
  944. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  945. my ($a,$b,$c,$d,$e);
  946. eval(shift(@insns));
  947. eval(shift(@insns));
  948. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  949. eval(shift(@insns));
  950. eval(shift(@insns));
  951. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  952. eval(shift(@insns));
  953. eval(shift(@insns));
  954. &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
  955. eval(shift(@insns));
  956. eval(shift(@insns));
  957. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  958. eval(shift(@insns));
  959. eval(shift(@insns));
  960. &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  961. eval(shift(@insns));
  962. eval(shift(@insns));
  963. eval(shift(@insns));
  964. eval(shift(@insns));
  965. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  966. eval(shift(@insns));
  967. eval(shift(@insns));
  968. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  969. eval(shift(@insns));
  970. eval(shift(@insns));
  971. &vpsrld (@Tx[0],@X[0],31);
  972. eval(shift(@insns));
  973. eval(shift(@insns));
  974. eval(shift(@insns));
  975. eval(shift(@insns));
  976. &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
  977. &vpaddd (@X[0],@X[0],@X[0]);
  978. eval(shift(@insns));
  979. eval(shift(@insns));
  980. eval(shift(@insns));
  981. eval(shift(@insns));
  982. &vpsrld (@Tx[1],@Tx[2],30);
  983. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
  984. eval(shift(@insns));
  985. eval(shift(@insns));
  986. eval(shift(@insns));
  987. eval(shift(@insns));
  988. &vpslld (@Tx[2],@Tx[2],2);
  989. &vpxor (@X[0],@X[0],@Tx[1]);
  990. eval(shift(@insns));
  991. eval(shift(@insns));
  992. eval(shift(@insns));
  993. eval(shift(@insns));
  994. &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
  995. eval(shift(@insns));
  996. eval(shift(@insns));
  997. &vmovdqa ($Kx,eval(2*16*(($Xi)/5)-64)."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
  998. eval(shift(@insns));
  999. eval(shift(@insns));
  1000. foreach (@insns) { eval; } # remaining instructions [if any]
  1001. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1002. }
  1003. sub Xupdate_avx_32_79()
  1004. { use integer;
  1005. my $body = shift;
  1006. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  1007. my ($a,$b,$c,$d,$e);
  1008. &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
  1009. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  1010. eval(shift(@insns)); # body_20_39
  1011. eval(shift(@insns));
  1012. eval(shift(@insns));
  1013. eval(shift(@insns)); # rol
  1014. &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  1015. eval(shift(@insns));
  1016. eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
  1017. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1018. &vmovdqa ($Kx,eval(2*16*($Xi/5)-64)."($K_XX_XX)") if ($Xi%5==0);
  1019. eval(shift(@insns)); # ror
  1020. eval(shift(@insns));
  1021. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  1022. eval(shift(@insns)); # body_20_39
  1023. eval(shift(@insns));
  1024. eval(shift(@insns));
  1025. eval(shift(@insns)); # rol
  1026. &vpsrld (@Tx[0],@X[0],30);
  1027. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1028. eval(shift(@insns));
  1029. eval(shift(@insns));
  1030. eval(shift(@insns)); # ror
  1031. eval(shift(@insns));
  1032. &vpslld (@X[0],@X[0],2);
  1033. eval(shift(@insns)); # body_20_39
  1034. eval(shift(@insns));
  1035. eval(shift(@insns));
  1036. eval(shift(@insns)); # rol
  1037. eval(shift(@insns));
  1038. eval(shift(@insns));
  1039. eval(shift(@insns)); # ror
  1040. eval(shift(@insns));
  1041. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
  1042. eval(shift(@insns)); # body_20_39
  1043. eval(shift(@insns));
  1044. eval(shift(@insns));
  1045. eval(shift(@insns)); # rol
  1046. eval(shift(@insns));
  1047. eval(shift(@insns));
  1048. eval(shift(@insns)); # rol
  1049. eval(shift(@insns));
  1050. foreach (@insns) { eval; } # remaining instructions
  1051. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1052. }
  1053. sub Xuplast_avx_80()
  1054. { use integer;
  1055. my $body = shift;
  1056. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1057. my ($a,$b,$c,$d,$e);
  1058. eval(shift(@insns));
  1059. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1060. eval(shift(@insns));
  1061. eval(shift(@insns));
  1062. eval(shift(@insns));
  1063. eval(shift(@insns));
  1064. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  1065. foreach (@insns) { eval; } # remaining instructions
  1066. &cmp ($inp,$num);
  1067. &je ($done_avx_label);
  1068. &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
  1069. &vmovdqa($Kx,"-64($K_XX_XX)"); # K_00_19
  1070. &vmovdqu(@X[-4&7],"0($inp)"); # load input
  1071. &vmovdqu(@X[-3&7],"16($inp)");
  1072. &vmovdqu(@X[-2&7],"32($inp)");
  1073. &vmovdqu(@X[-1&7],"48($inp)");
  1074. &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
  1075. &add ($inp,64);
  1076. $Xi=0;
  1077. }
  1078. sub Xloop_avx()
  1079. { use integer;
  1080. my $body = shift;
  1081. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1082. my ($a,$b,$c,$d,$e);
  1083. eval(shift(@insns));
  1084. eval(shift(@insns));
  1085. &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
  1086. eval(shift(@insns));
  1087. eval(shift(@insns));
  1088. &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],$Kx);
  1089. eval(shift(@insns));
  1090. eval(shift(@insns));
  1091. eval(shift(@insns));
  1092. eval(shift(@insns));
  1093. &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
  1094. eval(shift(@insns));
  1095. eval(shift(@insns));
  1096. foreach (@insns) { eval; }
  1097. $Xi++;
  1098. }
  1099. sub Xtail_avx()
  1100. { use integer;
  1101. my $body = shift;
  1102. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1103. my ($a,$b,$c,$d,$e);
  1104. foreach (@insns) { eval; }
  1105. }
  1106. $code.=<<___;
  1107. .align 16
  1108. .Loop_avx:
  1109. ___
  1110. &Xupdate_avx_16_31(\&body_00_19);
  1111. &Xupdate_avx_16_31(\&body_00_19);
  1112. &Xupdate_avx_16_31(\&body_00_19);
  1113. &Xupdate_avx_16_31(\&body_00_19);
  1114. &Xupdate_avx_32_79(\&body_00_19);
  1115. &Xupdate_avx_32_79(\&body_20_39);
  1116. &Xupdate_avx_32_79(\&body_20_39);
  1117. &Xupdate_avx_32_79(\&body_20_39);
  1118. &Xupdate_avx_32_79(\&body_20_39);
  1119. &Xupdate_avx_32_79(\&body_20_39);
  1120. &Xupdate_avx_32_79(\&body_40_59);
  1121. &Xupdate_avx_32_79(\&body_40_59);
  1122. &Xupdate_avx_32_79(\&body_40_59);
  1123. &Xupdate_avx_32_79(\&body_40_59);
  1124. &Xupdate_avx_32_79(\&body_40_59);
  1125. &Xupdate_avx_32_79(\&body_20_39);
  1126. &Xuplast_avx_80(\&body_20_39); # can jump to "done"
  1127. $saved_j=$j; @saved_V=@V;
  1128. &Xloop_avx(\&body_20_39);
  1129. &Xloop_avx(\&body_20_39);
  1130. &Xloop_avx(\&body_20_39);
  1131. $code.=<<___;
  1132. add 0($ctx),$A # update context
  1133. add 4($ctx),@T[0]
  1134. add 8($ctx),$C
  1135. add 12($ctx),$D
  1136. mov $A,0($ctx)
  1137. add 16($ctx),$E
  1138. mov @T[0],4($ctx)
  1139. mov @T[0],$B # magic seed
  1140. mov $C,8($ctx)
  1141. mov $C,@T[1]
  1142. mov $D,12($ctx)
  1143. xor $D,@T[1]
  1144. mov $E,16($ctx)
  1145. and @T[1],@T[0]
  1146. jmp .Loop_avx
  1147. .align 16
  1148. $done_avx_label:
  1149. ___
  1150. $j=$saved_j; @V=@saved_V;
  1151. &Xtail_avx(\&body_20_39);
  1152. &Xtail_avx(\&body_20_39);
  1153. &Xtail_avx(\&body_20_39);
  1154. $code.=<<___;
  1155. vzeroupper
  1156. add 0($ctx),$A # update context
  1157. add 4($ctx),@T[0]
  1158. add 8($ctx),$C
  1159. mov $A,0($ctx)
  1160. add 12($ctx),$D
  1161. mov @T[0],4($ctx)
  1162. add 16($ctx),$E
  1163. mov $C,8($ctx)
  1164. mov $D,12($ctx)
  1165. mov $E,16($ctx)
  1166. ___
  1167. $code.=<<___ if ($win64);
  1168. movaps -40-6*16($fp),%xmm6
  1169. movaps -40-5*16($fp),%xmm7
  1170. movaps -40-4*16($fp),%xmm8
  1171. movaps -40-3*16($fp),%xmm9
  1172. movaps -40-2*16($fp),%xmm10
  1173. movaps -40-1*16($fp),%xmm11
  1174. ___
  1175. $code.=<<___;
  1176. mov -40($fp),%r14
  1177. .cfi_restore %r14
  1178. mov -32($fp),%r13
  1179. .cfi_restore %r13
  1180. mov -24($fp),%r12
  1181. .cfi_restore %r12
  1182. mov -16($fp),%rbp
  1183. .cfi_restore %rbp
  1184. mov -8($fp),%rbx
  1185. .cfi_restore %rbx
  1186. lea ($fp),%rsp
  1187. .cfi_def_cfa_register %rsp
  1188. .Lepilogue_avx:
  1189. ret
  1190. .cfi_endproc
  1191. .size sha1_block_data_order_avx,.-sha1_block_data_order_avx
  1192. ___
  1193. if ($avx>1) {
  1194. use integer;
  1195. $Xi=4; # reset variables
  1196. @X=map("%ymm$_",(4..7,0..3));
  1197. @Tx=map("%ymm$_",(8..10));
  1198. $Kx="%ymm11";
  1199. $j=0;
  1200. my @ROTX=("%eax","%ebp","%ebx","%ecx","%edx","%esi");
  1201. my ($a5,$t0)=("%r12d","%edi");
  1202. my ($A,$F,$B,$C,$D,$E)=@ROTX;
  1203. my $rx=0;
  1204. my $frame="%r13";
  1205. $code.=<<___;
  1206. .type sha1_block_data_order_avx2,\@function,3
  1207. .align 16
  1208. sha1_block_data_order_avx2:
  1209. _avx2_shortcut:
  1210. .cfi_startproc
  1211. mov %rsp,$fp
  1212. .cfi_def_cfa_register $fp
  1213. push %rbx
  1214. .cfi_push %rbx
  1215. push %rbp
  1216. .cfi_push %rbp
  1217. push %r12
  1218. .cfi_push %r12
  1219. push %r13
  1220. .cfi_push %r13
  1221. push %r14
  1222. .cfi_push %r14
  1223. vzeroupper
  1224. ___
  1225. $code.=<<___ if ($win64);
  1226. lea -6*16(%rsp),%rsp
  1227. vmovaps %xmm6,-40-6*16($fp)
  1228. vmovaps %xmm7,-40-5*16($fp)
  1229. vmovaps %xmm8,-40-4*16($fp)
  1230. vmovaps %xmm9,-40-3*16($fp)
  1231. vmovaps %xmm10,-40-2*16($fp)
  1232. vmovaps %xmm11,-40-1*16($fp)
  1233. .Lprologue_avx2:
  1234. ___
  1235. $code.=<<___;
  1236. mov %rdi,$ctx # reassigned argument
  1237. mov %rsi,$inp # reassigned argument
  1238. mov %rdx,$num # reassigned argument
  1239. lea -640(%rsp),%rsp
  1240. shl \$6,$num
  1241. lea 64($inp),$frame
  1242. and \$-128,%rsp
  1243. add $inp,$num
  1244. lea K_XX_XX+64(%rip),$K_XX_XX
  1245. mov 0($ctx),$A # load context
  1246. cmp $num,$frame
  1247. cmovae $inp,$frame # next or same block
  1248. mov 4($ctx),$F
  1249. mov 8($ctx),$C
  1250. mov 12($ctx),$D
  1251. mov 16($ctx),$E
  1252. vmovdqu 64($K_XX_XX),@X[2] # pbswap mask
  1253. vmovdqu ($inp),%xmm0
  1254. vmovdqu 16($inp),%xmm1
  1255. vmovdqu 32($inp),%xmm2
  1256. vmovdqu 48($inp),%xmm3
  1257. lea 64($inp),$inp
  1258. vinserti128 \$1,($frame),@X[-4&7],@X[-4&7]
  1259. vinserti128 \$1,16($frame),@X[-3&7],@X[-3&7]
  1260. vpshufb @X[2],@X[-4&7],@X[-4&7]
  1261. vinserti128 \$1,32($frame),@X[-2&7],@X[-2&7]
  1262. vpshufb @X[2],@X[-3&7],@X[-3&7]
  1263. vinserti128 \$1,48($frame),@X[-1&7],@X[-1&7]
  1264. vpshufb @X[2],@X[-2&7],@X[-2&7]
  1265. vmovdqu -64($K_XX_XX),$Kx # K_00_19
  1266. vpshufb @X[2],@X[-1&7],@X[-1&7]
  1267. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  1268. vpaddd $Kx,@X[-3&7],@X[1]
  1269. vmovdqu @X[0],0(%rsp) # X[]+K xfer to IALU
  1270. vpaddd $Kx,@X[-2&7],@X[2]
  1271. vmovdqu @X[1],32(%rsp)
  1272. vpaddd $Kx,@X[-1&7],@X[3]
  1273. vmovdqu @X[2],64(%rsp)
  1274. vmovdqu @X[3],96(%rsp)
  1275. ___
  1276. for (;$Xi<8;$Xi++) { # Xupdate_avx2_16_31
  1277. use integer;
  1278. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  1279. &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
  1280. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  1281. &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  1282. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  1283. &vpsrld (@Tx[0],@X[0],31);
  1284. &vmovdqu($Kx,eval(2*16*(($Xi)/5)-64)."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
  1285. &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
  1286. &vpaddd (@X[0],@X[0],@X[0]);
  1287. &vpsrld (@Tx[1],@Tx[2],30);
  1288. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
  1289. &vpslld (@Tx[2],@Tx[2],2);
  1290. &vpxor (@X[0],@X[0],@Tx[1]);
  1291. &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
  1292. &vpaddd (@Tx[1],@X[0],$Kx);
  1293. &vmovdqu("32*$Xi(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1294. push(@X,shift(@X)); # "rotate" X[]
  1295. }
  1296. $code.=<<___;
  1297. lea 128(%rsp),$frame
  1298. jmp .Loop_avx2
  1299. .align 32
  1300. .Loop_avx2:
  1301. rorx \$2,$F,$B
  1302. andn $D,$F,$t0
  1303. and $C,$F
  1304. xor $t0,$F
  1305. ___
  1306. sub bodyx_00_19 () { # 8 instructions, 3 cycles critical path
  1307. # at start $f=(b&c)^(~b&d), $b>>>=2
  1308. return &bodyx_20_39() if ($rx==19); $rx++;
  1309. (
  1310. '($a,$f,$b,$c,$d,$e)=@ROTX;'.
  1311. '&add ($e,((32*($j/4)+4*($j%4))%256-128)."($frame)");'. # e+=X[i]+K
  1312. '&lea ($frame,"256($frame)") if ($j%32==31);',
  1313. '&andn ($t0,$a,$c)', # ~b&d for next round
  1314. '&add ($e,$f)', # e+=(b&c)^(~b&d)
  1315. '&rorx ($a5,$a,27)', # a<<<5
  1316. '&rorx ($f,$a,2)', # b>>>2 for next round
  1317. '&and ($a,$b)', # b&c for next round
  1318. '&add ($e,$a5)', # e+=a<<<5
  1319. '&xor ($a,$t0);'. # f=(b&c)^(~b&d) for next round
  1320. 'unshift(@ROTX,pop(@ROTX)); $j++;'
  1321. )
  1322. }
  1323. sub bodyx_20_39 () { # 7 instructions, 2 cycles critical path
  1324. # on entry $f=b^c^d, $b>>>=2
  1325. return &bodyx_40_59() if ($rx==39); $rx++;
  1326. (
  1327. '($a,$f,$b,$c,$d,$e)=@ROTX;'.
  1328. '&add ($e,((32*($j/4)+4*($j%4))%256-128)."($frame)");'. # e+=X[i]+K
  1329. '&lea ($frame,"256($frame)") if ($j%32==31);',
  1330. '&lea ($e,"($e,$f)")', # e+=b^c^d
  1331. '&rorx ($a5,$a,27)', # a<<<5
  1332. '&rorx ($f,$a,2) if ($j<79)', # b>>>2 in next round
  1333. '&xor ($a,$b) if ($j<79)', # b^c for next round
  1334. '&add ($e,$a5)', # e+=a<<<5
  1335. '&xor ($a,$c) if ($j<79);'. # f=b^c^d for next round
  1336. 'unshift(@ROTX,pop(@ROTX)); $j++;'
  1337. )
  1338. }
  1339. sub bodyx_40_59 () { # 10 instructions, 3 cycles critical path
  1340. # on entry $f=((b^c)&(c^d)), $b>>>=2
  1341. $rx++;
  1342. (
  1343. '($a,$f,$b,$c,$d,$e)=@ROTX;'.
  1344. '&add ($e,((32*($j/4)+4*($j%4))%256-128)."($frame)");'. # e+=X[i]+K
  1345. '&lea ($frame,"256($frame)") if ($j%32==31);',
  1346. '&xor ($f,$c) if ($j>39)', # (b^c)&(c^d)^c
  1347. '&mov ($t0,$b) if ($j<59)', # count on zero latency
  1348. '&xor ($t0,$c) if ($j<59)', # c^d for next round
  1349. '&lea ($e,"($e,$f)")', # e+=(b^c)&(c^d)^c
  1350. '&rorx ($a5,$a,27)', # a<<<5
  1351. '&rorx ($f,$a,2)', # b>>>2 in next round
  1352. '&xor ($a,$b)', # b^c for next round
  1353. '&add ($e,$a5)', # e+=a<<<5
  1354. '&and ($a,$t0) if ($j< 59);'. # f=(b^c)&(c^d) for next round
  1355. '&xor ($a,$c) if ($j==59);'. # f=b^c^d for next round
  1356. 'unshift(@ROTX,pop(@ROTX)); $j++;'
  1357. )
  1358. }
  1359. sub Xupdate_avx2_16_31() # recall that $Xi starts with 4
  1360. { use integer;
  1361. my $body = shift;
  1362. my @insns = (&$body,&$body,&$body,&$body,&$body); # 35 instructions
  1363. my ($a,$b,$c,$d,$e);
  1364. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  1365. eval(shift(@insns));
  1366. eval(shift(@insns));
  1367. eval(shift(@insns));
  1368. eval(shift(@insns));
  1369. &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
  1370. eval(shift(@insns));
  1371. eval(shift(@insns));
  1372. eval(shift(@insns));
  1373. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  1374. &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  1375. eval(shift(@insns));
  1376. eval(shift(@insns));
  1377. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  1378. eval(shift(@insns));
  1379. eval(shift(@insns));
  1380. eval(shift(@insns));
  1381. eval(shift(@insns));
  1382. &vpsrld (@Tx[0],@X[0],31);
  1383. &vmovdqu($Kx,eval(2*16*(($Xi)/5)-64)."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
  1384. eval(shift(@insns));
  1385. eval(shift(@insns));
  1386. eval(shift(@insns));
  1387. &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
  1388. &vpaddd (@X[0],@X[0],@X[0]);
  1389. eval(shift(@insns));
  1390. eval(shift(@insns));
  1391. &vpsrld (@Tx[1],@Tx[2],30);
  1392. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
  1393. eval(shift(@insns));
  1394. eval(shift(@insns));
  1395. &vpslld (@Tx[2],@Tx[2],2);
  1396. &vpxor (@X[0],@X[0],@Tx[1]);
  1397. eval(shift(@insns));
  1398. eval(shift(@insns));
  1399. &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
  1400. eval(shift(@insns));
  1401. eval(shift(@insns));
  1402. eval(shift(@insns));
  1403. &vpaddd (@Tx[1],@X[0],$Kx);
  1404. eval(shift(@insns));
  1405. eval(shift(@insns));
  1406. eval(shift(@insns));
  1407. &vmovdqu(eval(32*($Xi))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1408. foreach (@insns) { eval; } # remaining instructions [if any]
  1409. $Xi++;
  1410. push(@X,shift(@X)); # "rotate" X[]
  1411. }
  1412. sub Xupdate_avx2_32_79()
  1413. { use integer;
  1414. my $body = shift;
  1415. my @insns = (&$body,&$body,&$body,&$body,&$body); # 35 to 50 instructions
  1416. my ($a,$b,$c,$d,$e);
  1417. &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
  1418. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  1419. eval(shift(@insns));
  1420. eval(shift(@insns));
  1421. &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  1422. &vmovdqu($Kx,eval(2*16*($Xi/5)-64)."($K_XX_XX)") if ($Xi%5==0);
  1423. eval(shift(@insns));
  1424. eval(shift(@insns));
  1425. eval(shift(@insns));
  1426. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  1427. eval(shift(@insns));
  1428. eval(shift(@insns));
  1429. eval(shift(@insns));
  1430. &vpsrld (@Tx[0],@X[0],30);
  1431. &vpslld (@X[0],@X[0],2);
  1432. eval(shift(@insns));
  1433. eval(shift(@insns));
  1434. eval(shift(@insns));
  1435. #&vpslld (@X[0],@X[0],2);
  1436. eval(shift(@insns));
  1437. eval(shift(@insns));
  1438. eval(shift(@insns));
  1439. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
  1440. eval(shift(@insns));
  1441. eval(shift(@insns));
  1442. eval(shift(@insns));
  1443. eval(shift(@insns));
  1444. &vpaddd (@Tx[1],@X[0],$Kx);
  1445. eval(shift(@insns));
  1446. eval(shift(@insns));
  1447. eval(shift(@insns));
  1448. eval(shift(@insns));
  1449. &vmovdqu("32*$Xi(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1450. foreach (@insns) { eval; } # remaining instructions
  1451. $Xi++;
  1452. push(@X,shift(@X)); # "rotate" X[]
  1453. }
  1454. sub Xloop_avx2()
  1455. { use integer;
  1456. my $body = shift;
  1457. my @insns = (&$body,&$body,&$body,&$body,&$body); # 32 instructions
  1458. my ($a,$b,$c,$d,$e);
  1459. foreach (@insns) { eval; }
  1460. }
  1461. &align32();
  1462. &Xupdate_avx2_32_79(\&bodyx_00_19);
  1463. &Xupdate_avx2_32_79(\&bodyx_00_19);
  1464. &Xupdate_avx2_32_79(\&bodyx_00_19);
  1465. &Xupdate_avx2_32_79(\&bodyx_00_19);
  1466. &Xupdate_avx2_32_79(\&bodyx_20_39);
  1467. &Xupdate_avx2_32_79(\&bodyx_20_39);
  1468. &Xupdate_avx2_32_79(\&bodyx_20_39);
  1469. &Xupdate_avx2_32_79(\&bodyx_20_39);
  1470. &align32();
  1471. &Xupdate_avx2_32_79(\&bodyx_40_59);
  1472. &Xupdate_avx2_32_79(\&bodyx_40_59);
  1473. &Xupdate_avx2_32_79(\&bodyx_40_59);
  1474. &Xupdate_avx2_32_79(\&bodyx_40_59);
  1475. &Xloop_avx2(\&bodyx_20_39);
  1476. &Xloop_avx2(\&bodyx_20_39);
  1477. &Xloop_avx2(\&bodyx_20_39);
  1478. &Xloop_avx2(\&bodyx_20_39);
  1479. $code.=<<___;
  1480. lea 128($inp),$frame
  1481. lea 128($inp),%rdi # borrow $t0
  1482. cmp $num,$frame
  1483. cmovae $inp,$frame # next or previous block
  1484. # output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c
  1485. add 0($ctx),@ROTX[0] # update context
  1486. add 4($ctx),@ROTX[1]
  1487. add 8($ctx),@ROTX[3]
  1488. mov @ROTX[0],0($ctx)
  1489. add 12($ctx),@ROTX[4]
  1490. mov @ROTX[1],4($ctx)
  1491. mov @ROTX[0],$A # A=d
  1492. add 16($ctx),@ROTX[5]
  1493. mov @ROTX[3],$a5
  1494. mov @ROTX[3],8($ctx)
  1495. mov @ROTX[4],$D # D=b
  1496. #xchg @ROTX[5],$F # F=c, C=f
  1497. mov @ROTX[4],12($ctx)
  1498. mov @ROTX[1],$F # F=e
  1499. mov @ROTX[5],16($ctx)
  1500. #mov $F,16($ctx)
  1501. mov @ROTX[5],$E # E=c
  1502. mov $a5,$C # C=f
  1503. #xchg $F,$E # E=c, F=e
  1504. cmp $num,$inp
  1505. je .Ldone_avx2
  1506. ___
  1507. $Xi=4; # reset variables
  1508. @X=map("%ymm$_",(4..7,0..3));
  1509. $code.=<<___;
  1510. vmovdqu 64($K_XX_XX),@X[2] # pbswap mask
  1511. cmp $num,%rdi # borrowed $t0
  1512. ja .Last_avx2
  1513. vmovdqu -64(%rdi),%xmm0 # low part of @X[-4&7]
  1514. vmovdqu -48(%rdi),%xmm1
  1515. vmovdqu -32(%rdi),%xmm2
  1516. vmovdqu -16(%rdi),%xmm3
  1517. vinserti128 \$1,0($frame),@X[-4&7],@X[-4&7]
  1518. vinserti128 \$1,16($frame),@X[-3&7],@X[-3&7]
  1519. vinserti128 \$1,32($frame),@X[-2&7],@X[-2&7]
  1520. vinserti128 \$1,48($frame),@X[-1&7],@X[-1&7]
  1521. jmp .Last_avx2
  1522. .align 32
  1523. .Last_avx2:
  1524. lea 128+16(%rsp),$frame
  1525. rorx \$2,$F,$B
  1526. andn $D,$F,$t0
  1527. and $C,$F
  1528. xor $t0,$F
  1529. sub \$-128,$inp
  1530. ___
  1531. $rx=$j=0; @ROTX=($A,$F,$B,$C,$D,$E);
  1532. &Xloop_avx2 (\&bodyx_00_19);
  1533. &Xloop_avx2 (\&bodyx_00_19);
  1534. &Xloop_avx2 (\&bodyx_00_19);
  1535. &Xloop_avx2 (\&bodyx_00_19);
  1536. &Xloop_avx2 (\&bodyx_20_39);
  1537. &vmovdqu ($Kx,"-64($K_XX_XX)"); # K_00_19
  1538. &vpshufb (@X[-4&7],@X[-4&7],@X[2]); # byte swap
  1539. &Xloop_avx2 (\&bodyx_20_39);
  1540. &vpshufb (@X[-3&7],@X[-3&7],@X[2]);
  1541. &vpaddd (@Tx[0],@X[-4&7],$Kx); # add K_00_19
  1542. &Xloop_avx2 (\&bodyx_20_39);
  1543. &vmovdqu ("0(%rsp)",@Tx[0]);
  1544. &vpshufb (@X[-2&7],@X[-2&7],@X[2]);
  1545. &vpaddd (@Tx[1],@X[-3&7],$Kx);
  1546. &Xloop_avx2 (\&bodyx_20_39);
  1547. &vmovdqu ("32(%rsp)",@Tx[1]);
  1548. &vpshufb (@X[-1&7],@X[-1&7],@X[2]);
  1549. &vpaddd (@X[2],@X[-2&7],$Kx);
  1550. &Xloop_avx2 (\&bodyx_40_59);
  1551. &align32 ();
  1552. &vmovdqu ("64(%rsp)",@X[2]);
  1553. &vpaddd (@X[3],@X[-1&7],$Kx);
  1554. &Xloop_avx2 (\&bodyx_40_59);
  1555. &vmovdqu ("96(%rsp)",@X[3]);
  1556. &Xloop_avx2 (\&bodyx_40_59);
  1557. &Xupdate_avx2_16_31(\&bodyx_40_59);
  1558. &Xupdate_avx2_16_31(\&bodyx_20_39);
  1559. &Xupdate_avx2_16_31(\&bodyx_20_39);
  1560. &Xupdate_avx2_16_31(\&bodyx_20_39);
  1561. &Xloop_avx2 (\&bodyx_20_39);
  1562. $code.=<<___;
  1563. lea 128(%rsp),$frame
  1564. # output is d-e-[a]-f-b-c => A=d,F=e,C=f,D=b,E=c
  1565. add 0($ctx),@ROTX[0] # update context
  1566. add 4($ctx),@ROTX[1]
  1567. add 8($ctx),@ROTX[3]
  1568. mov @ROTX[0],0($ctx)
  1569. add 12($ctx),@ROTX[4]
  1570. mov @ROTX[1],4($ctx)
  1571. mov @ROTX[0],$A # A=d
  1572. add 16($ctx),@ROTX[5]
  1573. mov @ROTX[3],$a5
  1574. mov @ROTX[3],8($ctx)
  1575. mov @ROTX[4],$D # D=b
  1576. #xchg @ROTX[5],$F # F=c, C=f
  1577. mov @ROTX[4],12($ctx)
  1578. mov @ROTX[1],$F # F=e
  1579. mov @ROTX[5],16($ctx)
  1580. #mov $F,16($ctx)
  1581. mov @ROTX[5],$E # E=c
  1582. mov $a5,$C # C=f
  1583. #xchg $F,$E # E=c, F=e
  1584. cmp $num,$inp
  1585. jbe .Loop_avx2
  1586. .Ldone_avx2:
  1587. vzeroupper
  1588. ___
  1589. $code.=<<___ if ($win64);
  1590. movaps -40-6*16($fp),%xmm6
  1591. movaps -40-5*16($fp),%xmm7
  1592. movaps -40-4*16($fp),%xmm8
  1593. movaps -40-3*16($fp),%xmm9
  1594. movaps -40-2*16($fp),%xmm10
  1595. movaps -40-1*16($fp),%xmm11
  1596. ___
  1597. $code.=<<___;
  1598. mov -40($fp),%r14
  1599. .cfi_restore %r14
  1600. mov -32($fp),%r13
  1601. .cfi_restore %r13
  1602. mov -24($fp),%r12
  1603. .cfi_restore %r12
  1604. mov -16($fp),%rbp
  1605. .cfi_restore %rbp
  1606. mov -8($fp),%rbx
  1607. .cfi_restore %rbx
  1608. lea ($fp),%rsp
  1609. .cfi_def_cfa_register %rsp
  1610. .Lepilogue_avx2:
  1611. ret
  1612. .cfi_endproc
  1613. .size sha1_block_data_order_avx2,.-sha1_block_data_order_avx2
  1614. ___
  1615. }
  1616. }
  1617. $code.=<<___;
  1618. .align 64
  1619. K_XX_XX:
  1620. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1621. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1622. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1623. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1624. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1625. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1626. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1627. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1628. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
  1629. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
  1630. .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
  1631. ___
  1632. }}}
  1633. $code.=<<___;
  1634. .asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  1635. .align 64
  1636. ___
  1637. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1638. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1639. if ($win64) {
  1640. $rec="%rcx";
  1641. $frame="%rdx";
  1642. $context="%r8";
  1643. $disp="%r9";
  1644. $code.=<<___;
  1645. .extern __imp_RtlVirtualUnwind
  1646. .type se_handler,\@abi-omnipotent
  1647. .align 16
  1648. se_handler:
  1649. push %rsi
  1650. push %rdi
  1651. push %rbx
  1652. push %rbp
  1653. push %r12
  1654. push %r13
  1655. push %r14
  1656. push %r15
  1657. pushfq
  1658. sub \$64,%rsp
  1659. mov 120($context),%rax # pull context->Rax
  1660. mov 248($context),%rbx # pull context->Rip
  1661. lea .Lprologue(%rip),%r10
  1662. cmp %r10,%rbx # context->Rip<.Lprologue
  1663. jb .Lcommon_seh_tail
  1664. mov 152($context),%rax # pull context->Rsp
  1665. lea .Lepilogue(%rip),%r10
  1666. cmp %r10,%rbx # context->Rip>=.Lepilogue
  1667. jae .Lcommon_seh_tail
  1668. mov `16*4`(%rax),%rax # pull saved stack pointer
  1669. mov -8(%rax),%rbx
  1670. mov -16(%rax),%rbp
  1671. mov -24(%rax),%r12
  1672. mov -32(%rax),%r13
  1673. mov -40(%rax),%r14
  1674. mov %rbx,144($context) # restore context->Rbx
  1675. mov %rbp,160($context) # restore context->Rbp
  1676. mov %r12,216($context) # restore context->R12
  1677. mov %r13,224($context) # restore context->R13
  1678. mov %r14,232($context) # restore context->R14
  1679. jmp .Lcommon_seh_tail
  1680. .size se_handler,.-se_handler
  1681. ___
  1682. $code.=<<___ if ($shaext);
  1683. .type shaext_handler,\@abi-omnipotent
  1684. .align 16
  1685. shaext_handler:
  1686. push %rsi
  1687. push %rdi
  1688. push %rbx
  1689. push %rbp
  1690. push %r12
  1691. push %r13
  1692. push %r14
  1693. push %r15
  1694. pushfq
  1695. sub \$64,%rsp
  1696. mov 120($context),%rax # pull context->Rax
  1697. mov 248($context),%rbx # pull context->Rip
  1698. lea .Lprologue_shaext(%rip),%r10
  1699. cmp %r10,%rbx # context->Rip<.Lprologue
  1700. jb .Lcommon_seh_tail
  1701. lea .Lepilogue_shaext(%rip),%r10
  1702. cmp %r10,%rbx # context->Rip>=.Lepilogue
  1703. jae .Lcommon_seh_tail
  1704. lea -8-4*16(%rax),%rsi
  1705. lea 512($context),%rdi # &context.Xmm6
  1706. mov \$8,%ecx
  1707. .long 0xa548f3fc # cld; rep movsq
  1708. jmp .Lcommon_seh_tail
  1709. .size shaext_handler,.-shaext_handler
  1710. ___
  1711. $code.=<<___;
  1712. .type ssse3_handler,\@abi-omnipotent
  1713. .align 16
  1714. ssse3_handler:
  1715. push %rsi
  1716. push %rdi
  1717. push %rbx
  1718. push %rbp
  1719. push %r12
  1720. push %r13
  1721. push %r14
  1722. push %r15
  1723. pushfq
  1724. sub \$64,%rsp
  1725. mov 120($context),%rax # pull context->Rax
  1726. mov 248($context),%rbx # pull context->Rip
  1727. mov 8($disp),%rsi # disp->ImageBase
  1728. mov 56($disp),%r11 # disp->HandlerData
  1729. mov 0(%r11),%r10d # HandlerData[0]
  1730. lea (%rsi,%r10),%r10 # prologue label
  1731. cmp %r10,%rbx # context->Rip<prologue label
  1732. jb .Lcommon_seh_tail
  1733. mov 208($context),%rax # pull context->R11
  1734. mov 4(%r11),%r10d # HandlerData[1]
  1735. lea (%rsi,%r10),%r10 # epilogue label
  1736. cmp %r10,%rbx # context->Rip>=epilogue label
  1737. jae .Lcommon_seh_tail
  1738. lea -40-6*16(%rax),%rsi
  1739. lea 512($context),%rdi # &context.Xmm6
  1740. mov \$12,%ecx
  1741. .long 0xa548f3fc # cld; rep movsq
  1742. mov -8(%rax),%rbx
  1743. mov -16(%rax),%rbp
  1744. mov -24(%rax),%r12
  1745. mov -32(%rax),%r13
  1746. mov -40(%rax),%r14
  1747. mov %rbx,144($context) # restore context->Rbx
  1748. mov %rbp,160($context) # restore context->Rbp
  1749. mov %r12,216($context) # restore context->R12
  1750. mov %r13,224($context) # restore context->R13
  1751. mov %r14,232($context) # restore context->R14
  1752. .Lcommon_seh_tail:
  1753. mov 8(%rax),%rdi
  1754. mov 16(%rax),%rsi
  1755. mov %rax,152($context) # restore context->Rsp
  1756. mov %rsi,168($context) # restore context->Rsi
  1757. mov %rdi,176($context) # restore context->Rdi
  1758. mov 40($disp),%rdi # disp->ContextRecord
  1759. mov $context,%rsi # context
  1760. mov \$154,%ecx # sizeof(CONTEXT)
  1761. .long 0xa548f3fc # cld; rep movsq
  1762. mov $disp,%rsi
  1763. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1764. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1765. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1766. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1767. mov 40(%rsi),%r10 # disp->ContextRecord
  1768. lea 56(%rsi),%r11 # &disp->HandlerData
  1769. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1770. mov %r10,32(%rsp) # arg5
  1771. mov %r11,40(%rsp) # arg6
  1772. mov %r12,48(%rsp) # arg7
  1773. mov %rcx,56(%rsp) # arg8, (NULL)
  1774. call *__imp_RtlVirtualUnwind(%rip)
  1775. mov \$1,%eax # ExceptionContinueSearch
  1776. add \$64,%rsp
  1777. popfq
  1778. pop %r15
  1779. pop %r14
  1780. pop %r13
  1781. pop %r12
  1782. pop %rbp
  1783. pop %rbx
  1784. pop %rdi
  1785. pop %rsi
  1786. ret
  1787. .size ssse3_handler,.-ssse3_handler
  1788. .section .pdata
  1789. .align 4
  1790. .rva .LSEH_begin_sha1_block_data_order
  1791. .rva .LSEH_end_sha1_block_data_order
  1792. .rva .LSEH_info_sha1_block_data_order
  1793. ___
  1794. $code.=<<___ if ($shaext);
  1795. .rva .LSEH_begin_sha1_block_data_order_shaext
  1796. .rva .LSEH_end_sha1_block_data_order_shaext
  1797. .rva .LSEH_info_sha1_block_data_order_shaext
  1798. ___
  1799. $code.=<<___;
  1800. .rva .LSEH_begin_sha1_block_data_order_ssse3
  1801. .rva .LSEH_end_sha1_block_data_order_ssse3
  1802. .rva .LSEH_info_sha1_block_data_order_ssse3
  1803. ___
  1804. $code.=<<___ if ($avx);
  1805. .rva .LSEH_begin_sha1_block_data_order_avx
  1806. .rva .LSEH_end_sha1_block_data_order_avx
  1807. .rva .LSEH_info_sha1_block_data_order_avx
  1808. ___
  1809. $code.=<<___ if ($avx>1);
  1810. .rva .LSEH_begin_sha1_block_data_order_avx2
  1811. .rva .LSEH_end_sha1_block_data_order_avx2
  1812. .rva .LSEH_info_sha1_block_data_order_avx2
  1813. ___
  1814. $code.=<<___;
  1815. .section .xdata
  1816. .align 8
  1817. .LSEH_info_sha1_block_data_order:
  1818. .byte 9,0,0,0
  1819. .rva se_handler
  1820. ___
  1821. $code.=<<___ if ($shaext);
  1822. .LSEH_info_sha1_block_data_order_shaext:
  1823. .byte 9,0,0,0
  1824. .rva shaext_handler
  1825. ___
  1826. $code.=<<___;
  1827. .LSEH_info_sha1_block_data_order_ssse3:
  1828. .byte 9,0,0,0
  1829. .rva ssse3_handler
  1830. .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
  1831. ___
  1832. $code.=<<___ if ($avx);
  1833. .LSEH_info_sha1_block_data_order_avx:
  1834. .byte 9,0,0,0
  1835. .rva ssse3_handler
  1836. .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
  1837. ___
  1838. $code.=<<___ if ($avx>1);
  1839. .LSEH_info_sha1_block_data_order_avx2:
  1840. .byte 9,0,0,0
  1841. .rva ssse3_handler
  1842. .rva .Lprologue_avx2,.Lepilogue_avx2 # HandlerData[]
  1843. ___
  1844. }
  1845. ####################################################################
  1846. sub sha1rnds4 {
  1847. if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-7]),\s*%xmm([0-7])/) {
  1848. my @opcode=(0x0f,0x3a,0xcc);
  1849. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1850. my $c=$1;
  1851. push @opcode,$c=~/^0/?oct($c):$c;
  1852. return ".byte\t".join(',',@opcode);
  1853. } else {
  1854. return "sha1rnds4\t".@_[0];
  1855. }
  1856. }
  1857. sub sha1op38 {
  1858. my $instr = shift;
  1859. my %opcodelet = (
  1860. "sha1nexte" => 0xc8,
  1861. "sha1msg1" => 0xc9,
  1862. "sha1msg2" => 0xca );
  1863. if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1864. my @opcode=(0x0f,0x38);
  1865. my $rex=0;
  1866. $rex|=0x04 if ($2>=8);
  1867. $rex|=0x01 if ($1>=8);
  1868. unshift @opcode,0x40|$rex if ($rex);
  1869. push @opcode,$opcodelet{$instr};
  1870. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  1871. return ".byte\t".join(',',@opcode);
  1872. } else {
  1873. return $instr."\t".@_[0];
  1874. }
  1875. }
  1876. foreach (split("\n",$code)) {
  1877. s/\`([^\`]*)\`/eval $1/geo;
  1878. s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
  1879. s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo;
  1880. print $_,"\n";
  1881. }
  1882. close STDOUT or die "error closing STDOUT: $!";