x86_64 assembly pack: make it possible to compile with Perl located on
[openssl.git] / crypto / aes / asm / aesni-sha1-x86_64.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9 #
10 # June 2011
11 #
12 # This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
13 # in http://download.intel.com/design/intarch/papers/323686.pdf, is
14 # that since AESNI-CBC encrypt exhibit *very* low instruction-level
15 # parallelism, interleaving it with another algorithm would allow to
16 # utilize processor resources better and achieve better performance.
17 # SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
18 # AESNI code is weaved into it. Below are performance numbers in
19 # cycles per processed byte, less is better, for standalone AESNI-CBC
20 # encrypt, sum of the latter and standalone SHA1, and "stitched"
21 # subroutine:
22 #
23 #               AES-128-CBC     +SHA1           stitch      gain
24 # Westmere      3.77[+5.6]      9.37            6.65        +41%
25 # Sandy Bridge  5.05[+5.2(6.3)] 10.25(11.35)    6.16(7.08)  +67%(+60%)
26 # Ivy Bridge    5.05[+4.7]      9.75            5.59        +74%
27 # Bulldozer     5.77[+6.1]      11.87           6.47        +83%
28 #
29 #               AES-192-CBC
30 # Westmere      4.51            10.11           6.97        +45%
31 # Sandy Bridge  6.05            11.25(12.35)    6.34(7.27)  +77%(+70%)
32 # Ivy Bridge    6.05            10.75           6.07        +77%
33 # Bulldozer     6.89            12.99           7.02        +85%
34 #
35 #               AES-256-CBC
36 # Westmere      5.25            10.85           7.25        +50%
37 # Sandy Bridge  7.05            12.25(13.35)    7.06(7.70)  +74%(+73%)
38 # Ivy Bridge    7.05            11.75           7.12        +65%
39 # Bulldozer     8.00            14.10           8.24        +71%
40 #
41 # (*)   There are two code paths: SSSE3 and AVX. See sha1-568.pl for
42 #       background information. Above numbers in parentheses are SSSE3
43 #       results collected on AVX-capable CPU, i.e. apply on OSes that
44 #       don't support AVX.
45 #
46 # Needless to mention that it makes no sense to implement "stitched"
47 # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
48 # fully utilize parallelism, so stitching would not give any gain
49 # anyway. Well, there might be some, e.g. because of better cache
50 # locality... For reference, here are performance results for
51 # standalone AESNI-CBC decrypt:
52 #
53 #               AES-128-CBC     AES-192-CBC     AES-256-CBC
54 # Westmere      1.31            1.55            1.80
55 # Sandy Bridge  0.93            1.06            1.22
56 # Ivy Bridge    0.92            1.06            1.21
57 # Bulldozer     0.76            0.90            1.04
58
59 $flavour = shift;
60 $output  = shift;
61 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
62
63 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
64
65 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
66 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
67 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
68 die "can't locate x86_64-xlate.pl";
69
70 $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
71                 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
72            $1>=2.19);
73 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
74            `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
75            $1>=2.09);
76 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
77            `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
78            $1>=10);
79
80 open STDOUT,"| \"$^X\" $xlate $flavour $output";
81
82 # void aesni_cbc_sha1_enc(const void *inp,
83 #                       void *out,
84 #                       size_t length,
85 #                       const AES_KEY *key,
86 #                       unsigned char *iv,
87 #                       SHA_CTX *ctx,
88 #                       const void *in0);
89
90 $code.=<<___;
91 .text
92 .extern OPENSSL_ia32cap_P
93
94 .globl  aesni_cbc_sha1_enc
95 .type   aesni_cbc_sha1_enc,\@abi-omnipotent
96 .align  16
97 aesni_cbc_sha1_enc:
98         # caller should check for SSSE3 and AES-NI bits
99         mov     OPENSSL_ia32cap_P+0(%rip),%r10d
100         mov     OPENSSL_ia32cap_P+4(%rip),%r11d
101 ___
102 $code.=<<___ if ($avx);
103         and     \$`1<<28`,%r11d         # mask AVX bit
104         and     \$`1<<30`,%r10d         # mask "Intel CPU" bit
105         or      %r11d,%r10d
106         cmp     \$`1<<28|1<<30`,%r10d
107         je      aesni_cbc_sha1_enc_avx
108 ___
109 $code.=<<___;
110         jmp     aesni_cbc_sha1_enc_ssse3
111         ret
112 .size   aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
113 ___
114
115 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
116
117 my $Xi=4;
118 my @X=map("%xmm$_",(4..7,0..3));
119 my @Tx=map("%xmm$_",(8..10));
120 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp");    # size optimization
121 my @T=("%esi","%edi");
122 my $j=0; my $jj=0; my $r=0; my $sn=0;
123 my $K_XX_XX="%r11";
124 my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
125 my @rndkey=("%xmm14","%xmm15");
126
127 sub AUTOLOAD()          # thunk [simplified] 32-bit style perlasm
128 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
129   my $arg = pop;
130     $arg = "\$$arg" if ($arg*1 eq $arg);
131     $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
132 }
133
134 my $_rol=sub { &rol(@_) };
135 my $_ror=sub { &ror(@_) };
136
137 $code.=<<___;
138 .type   aesni_cbc_sha1_enc_ssse3,\@function,6
139 .align  16
140 aesni_cbc_sha1_enc_ssse3:
141         mov     `($win64?56:8)`(%rsp),$inp      # load 7th argument
142         #shr    \$6,$len                        # debugging artefact
143         #jz     .Lepilogue_ssse3                # debugging artefact
144         push    %rbx
145         push    %rbp
146         push    %r12
147         push    %r13
148         push    %r14
149         push    %r15
150         lea     `-104-($win64?10*16:0)`(%rsp),%rsp
151         #mov    $in0,$inp                       # debugging artefact
152         #lea    64(%rsp),$ctx                   # debugging artefact
153 ___
154 $code.=<<___ if ($win64);
155         movaps  %xmm6,96+0(%rsp)
156         movaps  %xmm7,96+16(%rsp)
157         movaps  %xmm8,96+32(%rsp)
158         movaps  %xmm9,96+48(%rsp)
159         movaps  %xmm10,96+64(%rsp)
160         movaps  %xmm11,96+80(%rsp)
161         movaps  %xmm12,96+96(%rsp)
162         movaps  %xmm13,96+112(%rsp)
163         movaps  %xmm14,96+128(%rsp)
164         movaps  %xmm15,96+144(%rsp)
165 .Lprologue_ssse3:
166 ___
167 $code.=<<___;
168         mov     $in0,%r12                       # reassign arguments
169         mov     $out,%r13
170         mov     $len,%r14
171         mov     $key,%r15
172         movdqu  ($ivp),$iv                      # load IV
173         mov     $ivp,88(%rsp)                   # save $ivp
174 ___
175 my ($in0,$out,$len,$key)=map("%r$_",(12..15));  # reassign arguments
176 my $rounds="${ivp}d";
177 $code.=<<___;
178         shl     \$6,$len
179         sub     $in0,$out
180         mov     240($key),$rounds
181         add     $inp,$len               # end of input
182
183         lea     K_XX_XX(%rip),$K_XX_XX
184         mov     0($ctx),$A              # load context
185         mov     4($ctx),$B
186         mov     8($ctx),$C
187         mov     12($ctx),$D
188         mov     $B,@T[0]                # magic seed
189         mov     16($ctx),$E
190
191         movdqa  64($K_XX_XX),@X[2]      # pbswap mask
192         movdqa  0($K_XX_XX),@Tx[1]      # K_00_19
193         movdqu  0($inp),@X[-4&7]        # load input to %xmm[0-3]
194         movdqu  16($inp),@X[-3&7]
195         movdqu  32($inp),@X[-2&7]
196         movdqu  48($inp),@X[-1&7]
197         pshufb  @X[2],@X[-4&7]          # byte swap
198         add     \$64,$inp
199         pshufb  @X[2],@X[-3&7]
200         pshufb  @X[2],@X[-2&7]
201         pshufb  @X[2],@X[-1&7]
202         paddd   @Tx[1],@X[-4&7]         # add K_00_19
203         paddd   @Tx[1],@X[-3&7]
204         paddd   @Tx[1],@X[-2&7]
205         movdqa  @X[-4&7],0(%rsp)        # X[]+K xfer to IALU
206         psubd   @Tx[1],@X[-4&7]         # restore X[]
207         movdqa  @X[-3&7],16(%rsp)
208         psubd   @Tx[1],@X[-3&7]
209         movdqa  @X[-2&7],32(%rsp)
210         psubd   @Tx[1],@X[-2&7]
211         movups  ($key),$rndkey0         # $key[0]
212         movups  16($key),$rndkey[0]     # forward reference
213         jmp     .Loop_ssse3
214 ___
215
216 my $aesenc=sub {
217   use integer;
218   my ($n,$k)=($r/10,$r%10);
219     if ($k==0) {
220       $code.=<<___;
221         movups          `16*$n`($in0),$in               # load input
222         xorps           $rndkey0,$in
223 ___
224       $code.=<<___ if ($n);
225         movups          $iv,`16*($n-1)`($out,$in0)      # write output
226 ___
227       $code.=<<___;
228         xorps           $in,$iv
229         aesenc          $rndkey[0],$iv
230         movups          `32+16*$k`($key),$rndkey[1]
231 ___
232     } elsif ($k==9) {
233       $sn++;
234       $code.=<<___;
235         cmp             \$11,$rounds
236         jb              .Laesenclast$sn
237         movups          `32+16*($k+0)`($key),$rndkey[1]
238         aesenc          $rndkey[0],$iv
239         movups          `32+16*($k+1)`($key),$rndkey[0]
240         aesenc          $rndkey[1],$iv
241         je              .Laesenclast$sn
242         movups          `32+16*($k+2)`($key),$rndkey[1]
243         aesenc          $rndkey[0],$iv
244         movups          `32+16*($k+3)`($key),$rndkey[0]
245         aesenc          $rndkey[1],$iv
246 .Laesenclast$sn:
247         aesenclast      $rndkey[0],$iv
248         movups          16($key),$rndkey[1]             # forward reference
249 ___
250     } else {
251       $code.=<<___;
252         aesenc          $rndkey[0],$iv
253         movups          `32+16*$k`($key),$rndkey[1]
254 ___
255     }
256     $r++;       unshift(@rndkey,pop(@rndkey));
257 };
258
259 sub Xupdate_ssse3_16_31()               # recall that $Xi starts wtih 4
260 { use integer;
261   my $body = shift;
262   my @insns = (&$body,&$body,&$body,&$body);    # 40 instructions
263   my ($a,$b,$c,$d,$e);
264
265         &movdqa (@X[0],@X[-3&7]);
266          eval(shift(@insns));
267          eval(shift(@insns));
268         &movdqa (@Tx[0],@X[-1&7]);
269         &palignr(@X[0],@X[-4&7],8);     # compose "X[-14]" in "X[0]"
270          eval(shift(@insns));
271          eval(shift(@insns));
272
273           &paddd        (@Tx[1],@X[-1&7]);
274          eval(shift(@insns));
275          eval(shift(@insns));
276         &psrldq (@Tx[0],4);             # "X[-3]", 3 dwords
277          eval(shift(@insns));
278          eval(shift(@insns));
279         &pxor   (@X[0],@X[-4&7]);       # "X[0]"^="X[-16]"
280          eval(shift(@insns));
281          eval(shift(@insns));
282
283         &pxor   (@Tx[0],@X[-2&7]);      # "X[-3]"^"X[-8]"
284          eval(shift(@insns));
285          eval(shift(@insns));
286          eval(shift(@insns));
287          eval(shift(@insns));
288
289         &pxor   (@X[0],@Tx[0]);         # "X[0]"^="X[-3]"^"X[-8]"
290          eval(shift(@insns));
291          eval(shift(@insns));
292           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
293          eval(shift(@insns));
294          eval(shift(@insns));
295
296         &movdqa (@Tx[2],@X[0]);
297         &movdqa (@Tx[0],@X[0]);
298          eval(shift(@insns));
299          eval(shift(@insns));
300          eval(shift(@insns));
301          eval(shift(@insns));
302
303         &pslldq (@Tx[2],12);            # "X[0]"<<96, extract one dword
304         &paddd  (@X[0],@X[0]);
305          eval(shift(@insns));
306          eval(shift(@insns));
307          eval(shift(@insns));
308          eval(shift(@insns));
309
310         &psrld  (@Tx[0],31);
311          eval(shift(@insns));
312          eval(shift(@insns));
313         &movdqa (@Tx[1],@Tx[2]);
314          eval(shift(@insns));
315          eval(shift(@insns));
316
317         &psrld  (@Tx[2],30);
318         &por    (@X[0],@Tx[0]);         # "X[0]"<<<=1
319          eval(shift(@insns));
320          eval(shift(@insns));
321          eval(shift(@insns));
322          eval(shift(@insns));
323
324         &pslld  (@Tx[1],2);
325         &pxor   (@X[0],@Tx[2]);
326          eval(shift(@insns));
327          eval(shift(@insns));
328           &movdqa       (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)");       # K_XX_XX
329          eval(shift(@insns));
330          eval(shift(@insns));
331
332         &pxor   (@X[0],@Tx[1]);         # "X[0]"^=("X[0]">>96)<<<2
333
334          foreach (@insns) { eval; }     # remaining instructions [if any]
335
336   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
337                 push(@Tx,shift(@Tx));
338 }
339
340 sub Xupdate_ssse3_32_79()
341 { use integer;
342   my $body = shift;
343   my @insns = (&$body,&$body,&$body,&$body);    # 32 to 48 instructions
344   my ($a,$b,$c,$d,$e);
345
346         &movdqa (@Tx[0],@X[-1&7])       if ($Xi==8);
347          eval(shift(@insns));           # body_20_39
348         &pxor   (@X[0],@X[-4&7]);       # "X[0]"="X[-32]"^"X[-16]"
349         &palignr(@Tx[0],@X[-2&7],8);    # compose "X[-6]"
350          eval(shift(@insns));
351          eval(shift(@insns));
352          eval(shift(@insns));           # rol
353
354         &pxor   (@X[0],@X[-7&7]);       # "X[0]"^="X[-28]"
355          eval(shift(@insns));
356          eval(shift(@insns))    if (@insns[0] !~ /&ro[rl]/);
357         if ($Xi%5) {
358           &movdqa       (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
359         } else {                        # ... or load next one
360           &movdqa       (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
361         }
362           &paddd        (@Tx[1],@X[-1&7]);
363          eval(shift(@insns));           # ror
364          eval(shift(@insns));
365
366         &pxor   (@X[0],@Tx[0]);         # "X[0]"^="X[-6]"
367          eval(shift(@insns));           # body_20_39
368          eval(shift(@insns));
369          eval(shift(@insns));
370          eval(shift(@insns));           # rol
371
372         &movdqa (@Tx[0],@X[0]);
373           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
374          eval(shift(@insns));
375          eval(shift(@insns));
376          eval(shift(@insns));           # ror
377          eval(shift(@insns));
378
379         &pslld  (@X[0],2);
380          eval(shift(@insns));           # body_20_39
381          eval(shift(@insns));
382         &psrld  (@Tx[0],30);
383          eval(shift(@insns));
384          eval(shift(@insns));           # rol
385          eval(shift(@insns));
386          eval(shift(@insns));
387          eval(shift(@insns));           # ror
388          eval(shift(@insns));
389
390         &por    (@X[0],@Tx[0]);         # "X[0]"<<<=2
391          eval(shift(@insns));           # body_20_39
392          eval(shift(@insns));
393           &movdqa       (@Tx[1],@X[0])  if ($Xi<19);
394          eval(shift(@insns));
395          eval(shift(@insns));           # rol
396          eval(shift(@insns));
397          eval(shift(@insns));
398          eval(shift(@insns));           # rol
399          eval(shift(@insns));
400
401          foreach (@insns) { eval; }     # remaining instructions
402
403   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
404                 push(@Tx,shift(@Tx));
405 }
406
407 sub Xuplast_ssse3_80()
408 { use integer;
409   my $body = shift;
410   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
411   my ($a,$b,$c,$d,$e);
412
413          eval(shift(@insns));
414           &paddd        (@Tx[1],@X[-1&7]);
415          eval(shift(@insns));
416          eval(shift(@insns));
417          eval(shift(@insns));
418          eval(shift(@insns));
419
420           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
421
422          foreach (@insns) { eval; }             # remaining instructions
423
424         &cmp    ($inp,$len);
425         &je     (".Ldone_ssse3");
426
427         unshift(@Tx,pop(@Tx));
428
429         &movdqa (@X[2],"64($K_XX_XX)");         # pbswap mask
430         &movdqa (@Tx[1],"0($K_XX_XX)");         # K_00_19
431         &movdqu (@X[-4&7],"0($inp)");           # load input
432         &movdqu (@X[-3&7],"16($inp)");
433         &movdqu (@X[-2&7],"32($inp)");
434         &movdqu (@X[-1&7],"48($inp)");
435         &pshufb (@X[-4&7],@X[2]);               # byte swap
436         &add    ($inp,64);
437
438   $Xi=0;
439 }
440
441 sub Xloop_ssse3()
442 { use integer;
443   my $body = shift;
444   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
445   my ($a,$b,$c,$d,$e);
446
447          eval(shift(@insns));
448          eval(shift(@insns));
449         &pshufb (@X[($Xi-3)&7],@X[2]);
450          eval(shift(@insns));
451          eval(shift(@insns));
452         &paddd  (@X[($Xi-4)&7],@Tx[1]);
453          eval(shift(@insns));
454          eval(shift(@insns));
455          eval(shift(@insns));
456          eval(shift(@insns));
457         &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]);  # X[]+K xfer to IALU
458          eval(shift(@insns));
459          eval(shift(@insns));
460         &psubd  (@X[($Xi-4)&7],@Tx[1]);
461
462         foreach (@insns) { eval; }
463   $Xi++;
464 }
465
466 sub Xtail_ssse3()
467 { use integer;
468   my $body = shift;
469   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
470   my ($a,$b,$c,$d,$e);
471
472         foreach (@insns) { eval; }
473 }
474
475 sub body_00_19 () {
476   use integer;
477   my ($k,$n);
478   my @r=(
479         '($a,$b,$c,$d,$e)=@V;'.
480         '&add   ($e,eval(4*($j&15))."(%rsp)");',        # X[]+K xfer
481         '&xor   ($c,$d);',
482         '&mov   (@T[1],$a);',   # $b in next round
483         '&$_rol ($a,5);',
484         '&and   (@T[0],$c);',   # ($b&($c^$d))
485         '&xor   ($c,$d);',      # restore $c
486         '&xor   (@T[0],$d);',
487         '&add   ($e,$a);',
488         '&$_ror ($b,$j?7:2);',  # $b>>>2
489         '&add   ($e,@T[0]);'    .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
490         );
491         $n = scalar(@r);
492         $k = (($jj+1)*12/20)*20*$n/12;  # 12 aesencs per these 20 rounds
493         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
494         $jj++;
495     return @r;
496 }
497
498 sub body_20_39 () {
499   use integer;
500   my ($k,$n);
501   my @r=(
502         '($a,$b,$c,$d,$e)=@V;'.
503         '&add   ($e,eval(4*($j++&15))."(%rsp)");',      # X[]+K xfer
504         '&xor   (@T[0],$d);',   # ($b^$d)
505         '&mov   (@T[1],$a);',   # $b in next round
506         '&$_rol ($a,5);',
507         '&xor   (@T[0],$c);',   # ($b^$d^$c)
508         '&add   ($e,$a);',
509         '&$_ror ($b,7);',       # $b>>>2
510         '&add   ($e,@T[0]);'    .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
511         );
512         $n = scalar(@r);
513         $k = (($jj+1)*8/20)*20*$n/8;    # 8 aesencs per these 20 rounds
514         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
515         $jj++;
516     return @r;
517 }
518
519 sub body_40_59 () {
520   use integer;
521   my ($k,$n);
522   my @r=(
523         '($a,$b,$c,$d,$e)=@V;'.
524         '&mov   (@T[1],$c);',
525         '&xor   ($c,$d);',
526         '&add   ($e,eval(4*($j++&15))."(%rsp)");',      # X[]+K xfer
527         '&and   (@T[1],$d);',
528         '&and   (@T[0],$c);',   # ($b&($c^$d))
529         '&$_ror ($b,7);',       # $b>>>2
530         '&add   ($e,@T[1]);',
531         '&mov   (@T[1],$a);',   # $b in next round
532         '&$_rol ($a,5);',
533         '&add   ($e,@T[0]);',
534         '&xor   ($c,$d);',      # restore $c
535         '&add   ($e,$a);'       .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
536         );
537         $n = scalar(@r);
538         $k=(($jj+1)*12/20)*20*$n/12;    # 12 aesencs per these 20 rounds
539         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
540         $jj++;
541     return @r;
542 }
543 $code.=<<___;
544 .align  16
545 .Loop_ssse3:
546 ___
547         &Xupdate_ssse3_16_31(\&body_00_19);
548         &Xupdate_ssse3_16_31(\&body_00_19);
549         &Xupdate_ssse3_16_31(\&body_00_19);
550         &Xupdate_ssse3_16_31(\&body_00_19);
551         &Xupdate_ssse3_32_79(\&body_00_19);
552         &Xupdate_ssse3_32_79(\&body_20_39);
553         &Xupdate_ssse3_32_79(\&body_20_39);
554         &Xupdate_ssse3_32_79(\&body_20_39);
555         &Xupdate_ssse3_32_79(\&body_20_39);
556         &Xupdate_ssse3_32_79(\&body_20_39);
557         &Xupdate_ssse3_32_79(\&body_40_59);
558         &Xupdate_ssse3_32_79(\&body_40_59);
559         &Xupdate_ssse3_32_79(\&body_40_59);
560         &Xupdate_ssse3_32_79(\&body_40_59);
561         &Xupdate_ssse3_32_79(\&body_40_59);
562         &Xupdate_ssse3_32_79(\&body_20_39);
563         &Xuplast_ssse3_80(\&body_20_39);        # can jump to "done"
564
565                                 $saved_j=$j; @saved_V=@V;
566                                 $saved_r=$r; @saved_rndkey=@rndkey;
567
568         &Xloop_ssse3(\&body_20_39);
569         &Xloop_ssse3(\&body_20_39);
570         &Xloop_ssse3(\&body_20_39);
571
572 $code.=<<___;
573         movups  $iv,48($out,$in0)               # write output
574         lea     64($in0),$in0
575
576         add     0($ctx),$A                      # update context
577         add     4($ctx),@T[0]
578         add     8($ctx),$C
579         add     12($ctx),$D
580         mov     $A,0($ctx)
581         add     16($ctx),$E
582         mov     @T[0],4($ctx)
583         mov     @T[0],$B                        # magic seed
584         mov     $C,8($ctx)
585         mov     $D,12($ctx)
586         mov     $E,16($ctx)
587         jmp     .Loop_ssse3
588
589 .align  16
590 .Ldone_ssse3:
591 ___
592                                 $jj=$j=$saved_j; @V=@saved_V;
593                                 $r=$saved_r;     @rndkey=@saved_rndkey;
594
595         &Xtail_ssse3(\&body_20_39);
596         &Xtail_ssse3(\&body_20_39);
597         &Xtail_ssse3(\&body_20_39);
598
599 $code.=<<___;
600         movups  $iv,48($out,$in0)               # write output
601         mov     88(%rsp),$ivp                   # restore $ivp
602
603         add     0($ctx),$A                      # update context
604         add     4($ctx),@T[0]
605         add     8($ctx),$C
606         mov     $A,0($ctx)
607         add     12($ctx),$D
608         mov     @T[0],4($ctx)
609         add     16($ctx),$E
610         mov     $C,8($ctx)
611         mov     $D,12($ctx)
612         mov     $E,16($ctx)
613         movups  $iv,($ivp)                      # write IV
614 ___
615 $code.=<<___ if ($win64);
616         movaps  96+0(%rsp),%xmm6
617         movaps  96+16(%rsp),%xmm7
618         movaps  96+32(%rsp),%xmm8
619         movaps  96+48(%rsp),%xmm9
620         movaps  96+64(%rsp),%xmm10
621         movaps  96+80(%rsp),%xmm11
622         movaps  96+96(%rsp),%xmm12
623         movaps  96+112(%rsp),%xmm13
624         movaps  96+128(%rsp),%xmm14
625         movaps  96+144(%rsp),%xmm15
626 ___
627 $code.=<<___;
628         lea     `104+($win64?10*16:0)`(%rsp),%rsi
629         mov     0(%rsi),%r15
630         mov     8(%rsi),%r14
631         mov     16(%rsi),%r13
632         mov     24(%rsi),%r12
633         mov     32(%rsi),%rbp
634         mov     40(%rsi),%rbx
635         lea     48(%rsi),%rsp
636 .Lepilogue_ssse3:
637         ret
638 .size   aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
639 ___
640
641 $j=$jj=$r=$sn=0;
642
643 if ($avx) {
644 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
645
646 my $Xi=4;
647 my @X=map("%xmm$_",(4..7,0..3));
648 my @Tx=map("%xmm$_",(8..10));
649 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp");    # size optimization
650 my @T=("%esi","%edi");
651
652 my $_rol=sub { &shld(@_[0],@_) };
653 my $_ror=sub { &shrd(@_[0],@_) };
654
655 $code.=<<___;
656 .type   aesni_cbc_sha1_enc_avx,\@function,6
657 .align  16
658 aesni_cbc_sha1_enc_avx:
659         mov     `($win64?56:8)`(%rsp),$inp      # load 7th argument
660         #shr    \$6,$len                        # debugging artefact
661         #jz     .Lepilogue_avx                  # debugging artefact
662         push    %rbx
663         push    %rbp
664         push    %r12
665         push    %r13
666         push    %r14
667         push    %r15
668         lea     `-104-($win64?10*16:0)`(%rsp),%rsp
669         #mov    $in0,$inp                       # debugging artefact
670         #lea    64(%rsp),$ctx                   # debugging artefact
671 ___
672 $code.=<<___ if ($win64);
673         movaps  %xmm6,96+0(%rsp)
674         movaps  %xmm7,96+16(%rsp)
675         movaps  %xmm8,96+32(%rsp)
676         movaps  %xmm9,96+48(%rsp)
677         movaps  %xmm10,96+64(%rsp)
678         movaps  %xmm11,96+80(%rsp)
679         movaps  %xmm12,96+96(%rsp)
680         movaps  %xmm13,96+112(%rsp)
681         movaps  %xmm14,96+128(%rsp)
682         movaps  %xmm15,96+144(%rsp)
683 .Lprologue_avx:
684 ___
685 $code.=<<___;
686         vzeroall
687         mov     $in0,%r12                       # reassign arguments
688         mov     $out,%r13
689         mov     $len,%r14
690         mov     $key,%r15
691         vmovdqu ($ivp),$iv                      # load IV
692         mov     $ivp,88(%rsp)                   # save $ivp
693 ___
694 my ($in0,$out,$len,$key)=map("%r$_",(12..15));  # reassign arguments
695 my $rounds="${ivp}d";
696 $code.=<<___;
697         shl     \$6,$len
698         sub     $in0,$out
699         mov     240($key),$rounds
700         add     \$112,$key              # size optimization
701         add     $inp,$len               # end of input
702
703         lea     K_XX_XX(%rip),$K_XX_XX
704         mov     0($ctx),$A              # load context
705         mov     4($ctx),$B
706         mov     8($ctx),$C
707         mov     12($ctx),$D
708         mov     $B,@T[0]                # magic seed
709         mov     16($ctx),$E
710
711         vmovdqa 64($K_XX_XX),@X[2]      # pbswap mask
712         vmovdqa 0($K_XX_XX),@Tx[1]      # K_00_19
713         vmovdqu 0($inp),@X[-4&7]        # load input to %xmm[0-3]
714         vmovdqu 16($inp),@X[-3&7]
715         vmovdqu 32($inp),@X[-2&7]
716         vmovdqu 48($inp),@X[-1&7]
717         vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
718         add     \$64,$inp
719         vpshufb @X[2],@X[-3&7],@X[-3&7]
720         vpshufb @X[2],@X[-2&7],@X[-2&7]
721         vpshufb @X[2],@X[-1&7],@X[-1&7]
722         vpaddd  @Tx[1],@X[-4&7],@X[0]   # add K_00_19
723         vpaddd  @Tx[1],@X[-3&7],@X[1]
724         vpaddd  @Tx[1],@X[-2&7],@X[2]
725         vmovdqa @X[0],0(%rsp)           # X[]+K xfer to IALU
726         vmovdqa @X[1],16(%rsp)
727         vmovdqa @X[2],32(%rsp)
728         vmovups -112($key),$rndkey0     # $key[0]
729         vmovups 16-112($key),$rndkey[0] # forward reference
730         jmp     .Loop_avx
731 ___
732
733 my $aesenc=sub {
734   use integer;
735   my ($n,$k)=($r/10,$r%10);
736     if ($k==0) {
737       $code.=<<___;
738         vmovups         `16*$n`($in0),$in               # load input
739         vxorps          $rndkey0,$in,$in
740 ___
741       $code.=<<___ if ($n);
742         vmovups         $iv,`16*($n-1)`($out,$in0)      # write output
743 ___
744       $code.=<<___;
745         vxorps          $in,$iv,$iv
746         vaesenc         $rndkey[0],$iv,$iv
747         vmovups         `32+16*$k-112`($key),$rndkey[1]
748 ___
749     } elsif ($k==9) {
750       $sn++;
751       $code.=<<___;
752         cmp             \$11,$rounds
753         jb              .Lvaesenclast$sn
754         vaesenc         $rndkey[0],$iv,$iv
755         vmovups         `32+16*($k+0)-112`($key),$rndkey[1]
756         vaesenc         $rndkey[1],$iv,$iv
757         vmovups         `32+16*($k+1)-112`($key),$rndkey[0]
758         je              .Lvaesenclast$sn
759         vaesenc         $rndkey[0],$iv,$iv
760         vmovups         `32+16*($k+2)-112`($key),$rndkey[1]
761         vaesenc         $rndkey[1],$iv,$iv
762         vmovups         `32+16*($k+3)-112`($key),$rndkey[0]
763 .Lvaesenclast$sn:
764         vaesenclast     $rndkey[0],$iv,$iv
765         vmovups         16-112($key),$rndkey[1]         # forward reference
766 ___
767     } else {
768       $code.=<<___;
769         vaesenc         $rndkey[0],$iv,$iv
770         vmovups         `32+16*$k-112`($key),$rndkey[1]
771 ___
772     }
773     $r++;       unshift(@rndkey,pop(@rndkey));
774 };
775
776 sub Xupdate_avx_16_31()         # recall that $Xi starts wtih 4
777 { use integer;
778   my $body = shift;
779   my @insns = (&$body,&$body,&$body,&$body);    # 40 instructions
780   my ($a,$b,$c,$d,$e);
781
782          eval(shift(@insns));
783          eval(shift(@insns));
784         &vpalignr(@X[0],@X[-3&7],@X[-4&7],8);   # compose "X[-14]" in "X[0]"
785          eval(shift(@insns));
786          eval(shift(@insns));
787
788           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
789          eval(shift(@insns));
790          eval(shift(@insns));
791         &vpsrldq(@Tx[0],@X[-1&7],4);    # "X[-3]", 3 dwords
792          eval(shift(@insns));
793          eval(shift(@insns));
794         &vpxor  (@X[0],@X[0],@X[-4&7]);         # "X[0]"^="X[-16]"
795          eval(shift(@insns));
796          eval(shift(@insns));
797
798         &vpxor  (@Tx[0],@Tx[0],@X[-2&7]);       # "X[-3]"^"X[-8]"
799          eval(shift(@insns));
800          eval(shift(@insns));
801          eval(shift(@insns));
802          eval(shift(@insns));
803
804         &vpxor  (@X[0],@X[0],@Tx[0]);           # "X[0]"^="X[-3]"^"X[-8]"
805          eval(shift(@insns));
806          eval(shift(@insns));
807           &vmovdqa      (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
808          eval(shift(@insns));
809          eval(shift(@insns));
810
811         &vpsrld (@Tx[0],@X[0],31);
812          eval(shift(@insns));
813          eval(shift(@insns));
814          eval(shift(@insns));
815          eval(shift(@insns));
816
817         &vpslldq(@Tx[2],@X[0],12);              # "X[0]"<<96, extract one dword
818         &vpaddd (@X[0],@X[0],@X[0]);
819          eval(shift(@insns));
820          eval(shift(@insns));
821          eval(shift(@insns));
822          eval(shift(@insns));
823
824         &vpsrld (@Tx[1],@Tx[2],30);
825         &vpor   (@X[0],@X[0],@Tx[0]);           # "X[0]"<<<=1
826          eval(shift(@insns));
827          eval(shift(@insns));
828          eval(shift(@insns));
829          eval(shift(@insns));
830
831         &vpslld (@Tx[2],@Tx[2],2);
832         &vpxor  (@X[0],@X[0],@Tx[1]);
833          eval(shift(@insns));
834          eval(shift(@insns));
835          eval(shift(@insns));
836          eval(shift(@insns));
837
838         &vpxor  (@X[0],@X[0],@Tx[2]);           # "X[0]"^=("X[0]">>96)<<<2
839          eval(shift(@insns));
840          eval(shift(@insns));
841           &vmovdqa      (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)");       # K_XX_XX
842          eval(shift(@insns));
843          eval(shift(@insns));
844
845
846          foreach (@insns) { eval; }     # remaining instructions [if any]
847
848   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
849                 push(@Tx,shift(@Tx));
850 }
851
852 sub Xupdate_avx_32_79()
853 { use integer;
854   my $body = shift;
855   my @insns = (&$body,&$body,&$body,&$body);    # 32 to 48 instructions
856   my ($a,$b,$c,$d,$e);
857
858         &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8);  # compose "X[-6]"
859         &vpxor  (@X[0],@X[0],@X[-4&7]);         # "X[0]"="X[-32]"^"X[-16]"
860          eval(shift(@insns));           # body_20_39
861          eval(shift(@insns));
862          eval(shift(@insns));
863          eval(shift(@insns));           # rol
864
865         &vpxor  (@X[0],@X[0],@X[-7&7]);         # "X[0]"^="X[-28]"
866          eval(shift(@insns));
867          eval(shift(@insns))    if (@insns[0] !~ /&ro[rl]/);
868         if ($Xi%5) {
869           &vmovdqa      (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
870         } else {                        # ... or load next one
871           &vmovdqa      (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
872         }
873           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
874          eval(shift(@insns));           # ror
875          eval(shift(@insns));
876
877         &vpxor  (@X[0],@X[0],@Tx[0]);           # "X[0]"^="X[-6]"
878          eval(shift(@insns));           # body_20_39
879          eval(shift(@insns));
880          eval(shift(@insns));
881          eval(shift(@insns));           # rol
882
883         &vpsrld (@Tx[0],@X[0],30);
884           &vmovdqa      (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
885          eval(shift(@insns));
886          eval(shift(@insns));
887          eval(shift(@insns));           # ror
888          eval(shift(@insns));
889
890         &vpslld (@X[0],@X[0],2);
891          eval(shift(@insns));           # body_20_39
892          eval(shift(@insns));
893          eval(shift(@insns));
894          eval(shift(@insns));           # rol
895          eval(shift(@insns));
896          eval(shift(@insns));
897          eval(shift(@insns));           # ror
898          eval(shift(@insns));
899
900         &vpor   (@X[0],@X[0],@Tx[0]);           # "X[0]"<<<=2
901          eval(shift(@insns));           # body_20_39
902          eval(shift(@insns));
903           &vmovdqa      (@Tx[1],@X[0])  if ($Xi<19);
904          eval(shift(@insns));
905          eval(shift(@insns));           # rol
906          eval(shift(@insns));
907          eval(shift(@insns));
908          eval(shift(@insns));           # rol
909          eval(shift(@insns));
910
911          foreach (@insns) { eval; }     # remaining instructions
912
913   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
914                 push(@Tx,shift(@Tx));
915 }
916
917 sub Xuplast_avx_80()
918 { use integer;
919   my $body = shift;
920   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
921   my ($a,$b,$c,$d,$e);
922
923          eval(shift(@insns));
924           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
925          eval(shift(@insns));
926          eval(shift(@insns));
927          eval(shift(@insns));
928          eval(shift(@insns));
929
930           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
931
932          foreach (@insns) { eval; }             # remaining instructions
933
934         &cmp    ($inp,$len);
935         &je     (".Ldone_avx");
936
937         unshift(@Tx,pop(@Tx));
938
939         &vmovdqa(@X[2],"64($K_XX_XX)");         # pbswap mask
940         &vmovdqa(@Tx[1],"0($K_XX_XX)");         # K_00_19
941         &vmovdqu(@X[-4&7],"0($inp)");           # load input
942         &vmovdqu(@X[-3&7],"16($inp)");
943         &vmovdqu(@X[-2&7],"32($inp)");
944         &vmovdqu(@X[-1&7],"48($inp)");
945         &vpshufb(@X[-4&7],@X[-4&7],@X[2]);      # byte swap
946         &add    ($inp,64);
947
948   $Xi=0;
949 }
950
951 sub Xloop_avx()
952 { use integer;
953   my $body = shift;
954   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
955   my ($a,$b,$c,$d,$e);
956
957          eval(shift(@insns));
958          eval(shift(@insns));
959         &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
960          eval(shift(@insns));
961          eval(shift(@insns));
962         &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
963          eval(shift(@insns));
964          eval(shift(@insns));
965          eval(shift(@insns));
966          eval(shift(@insns));
967         &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]);      # X[]+K xfer to IALU
968          eval(shift(@insns));
969          eval(shift(@insns));
970
971         foreach (@insns) { eval; }
972   $Xi++;
973 }
974
975 sub Xtail_avx()
976 { use integer;
977   my $body = shift;
978   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
979   my ($a,$b,$c,$d,$e);
980
981         foreach (@insns) { eval; }
982 }
983
984 $code.=<<___;
985 .align  16
986 .Loop_avx:
987 ___
988         &Xupdate_avx_16_31(\&body_00_19);
989         &Xupdate_avx_16_31(\&body_00_19);
990         &Xupdate_avx_16_31(\&body_00_19);
991         &Xupdate_avx_16_31(\&body_00_19);
992         &Xupdate_avx_32_79(\&body_00_19);
993         &Xupdate_avx_32_79(\&body_20_39);
994         &Xupdate_avx_32_79(\&body_20_39);
995         &Xupdate_avx_32_79(\&body_20_39);
996         &Xupdate_avx_32_79(\&body_20_39);
997         &Xupdate_avx_32_79(\&body_20_39);
998         &Xupdate_avx_32_79(\&body_40_59);
999         &Xupdate_avx_32_79(\&body_40_59);
1000         &Xupdate_avx_32_79(\&body_40_59);
1001         &Xupdate_avx_32_79(\&body_40_59);
1002         &Xupdate_avx_32_79(\&body_40_59);
1003         &Xupdate_avx_32_79(\&body_20_39);
1004         &Xuplast_avx_80(\&body_20_39);  # can jump to "done"
1005
1006                                 $saved_j=$j; @saved_V=@V;
1007                                 $saved_r=$r; @saved_rndkey=@rndkey;
1008
1009         &Xloop_avx(\&body_20_39);
1010         &Xloop_avx(\&body_20_39);
1011         &Xloop_avx(\&body_20_39);
1012
1013 $code.=<<___;
1014         vmovups $iv,48($out,$in0)               # write output
1015         lea     64($in0),$in0
1016
1017         add     0($ctx),$A                      # update context
1018         add     4($ctx),@T[0]
1019         add     8($ctx),$C
1020         add     12($ctx),$D
1021         mov     $A,0($ctx)
1022         add     16($ctx),$E
1023         mov     @T[0],4($ctx)
1024         mov     @T[0],$B                        # magic seed
1025         mov     $C,8($ctx)
1026         mov     $D,12($ctx)
1027         mov     $E,16($ctx)
1028         jmp     .Loop_avx
1029
1030 .align  16
1031 .Ldone_avx:
1032 ___
1033                                 $jj=$j=$saved_j; @V=@saved_V;
1034                                 $r=$saved_r;     @rndkey=@saved_rndkey;
1035
1036         &Xtail_avx(\&body_20_39);
1037         &Xtail_avx(\&body_20_39);
1038         &Xtail_avx(\&body_20_39);
1039
1040 $code.=<<___;
1041         vmovups $iv,48($out,$in0)               # write output
1042         mov     88(%rsp),$ivp                   # restore $ivp
1043
1044         add     0($ctx),$A                      # update context
1045         add     4($ctx),@T[0]
1046         add     8($ctx),$C
1047         mov     $A,0($ctx)
1048         add     12($ctx),$D
1049         mov     @T[0],4($ctx)
1050         add     16($ctx),$E
1051         mov     $C,8($ctx)
1052         mov     $D,12($ctx)
1053         mov     $E,16($ctx)
1054         vmovups $iv,($ivp)                      # write IV
1055         vzeroall
1056 ___
1057 $code.=<<___ if ($win64);
1058         movaps  96+0(%rsp),%xmm6
1059         movaps  96+16(%rsp),%xmm7
1060         movaps  96+32(%rsp),%xmm8
1061         movaps  96+48(%rsp),%xmm9
1062         movaps  96+64(%rsp),%xmm10
1063         movaps  96+80(%rsp),%xmm11
1064         movaps  96+96(%rsp),%xmm12
1065         movaps  96+112(%rsp),%xmm13
1066         movaps  96+128(%rsp),%xmm14
1067         movaps  96+144(%rsp),%xmm15
1068 ___
1069 $code.=<<___;
1070         lea     `104+($win64?10*16:0)`(%rsp),%rsi
1071         mov     0(%rsi),%r15
1072         mov     8(%rsi),%r14
1073         mov     16(%rsi),%r13
1074         mov     24(%rsi),%r12
1075         mov     32(%rsi),%rbp
1076         mov     40(%rsi),%rbx
1077         lea     48(%rsi),%rsp
1078 .Lepilogue_avx:
1079         ret
1080 .size   aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1081 ___
1082 }
1083 $code.=<<___;
1084 .align  64
1085 K_XX_XX:
1086 .long   0x5a827999,0x5a827999,0x5a827999,0x5a827999     # K_00_19
1087 .long   0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1     # K_20_39
1088 .long   0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc     # K_40_59
1089 .long   0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6     # K_60_79
1090 .long   0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f     # pbswap mask
1091
1092 .asciz  "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1093 .align  64
1094 ___
1095
1096 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1097 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
1098 if ($win64) {
1099 $rec="%rcx";
1100 $frame="%rdx";
1101 $context="%r8";
1102 $disp="%r9";
1103
1104 $code.=<<___;
1105 .extern __imp_RtlVirtualUnwind
1106 .type   ssse3_handler,\@abi-omnipotent
1107 .align  16
1108 ssse3_handler:
1109         push    %rsi
1110         push    %rdi
1111         push    %rbx
1112         push    %rbp
1113         push    %r12
1114         push    %r13
1115         push    %r14
1116         push    %r15
1117         pushfq
1118         sub     \$64,%rsp
1119
1120         mov     120($context),%rax      # pull context->Rax
1121         mov     248($context),%rbx      # pull context->Rip
1122
1123         mov     8($disp),%rsi           # disp->ImageBase
1124         mov     56($disp),%r11          # disp->HandlerData
1125
1126         mov     0(%r11),%r10d           # HandlerData[0]
1127         lea     (%rsi,%r10),%r10        # prologue label
1128         cmp     %r10,%rbx               # context->Rip<prologue label
1129         jb      .Lcommon_seh_tail
1130
1131         mov     152($context),%rax      # pull context->Rsp
1132
1133         mov     4(%r11),%r10d           # HandlerData[1]
1134         lea     (%rsi,%r10),%r10        # epilogue label
1135         cmp     %r10,%rbx               # context->Rip>=epilogue label
1136         jae     .Lcommon_seh_tail
1137
1138         lea     96(%rax),%rsi
1139         lea     512($context),%rdi      # &context.Xmm6
1140         mov     \$20,%ecx
1141         .long   0xa548f3fc              # cld; rep movsq
1142         lea     `104+10*16`(%rax),%rax  # adjust stack pointer
1143
1144         mov     0(%rax),%r15
1145         mov     8(%rax),%r14
1146         mov     16(%rax),%r13
1147         mov     24(%rax),%r12
1148         mov     32(%rax),%rbp
1149         mov     40(%rax),%rbx
1150         lea     48(%rax),%rax
1151         mov     %rbx,144($context)      # restore context->Rbx
1152         mov     %rbp,160($context)      # restore context->Rbp
1153         mov     %r12,216($context)      # restore context->R12
1154         mov     %r13,224($context)      # restore context->R13
1155         mov     %r14,232($context)      # restore context->R14
1156         mov     %r15,240($context)      # restore context->R15
1157
1158 .Lcommon_seh_tail:
1159         mov     8(%rax),%rdi
1160         mov     16(%rax),%rsi
1161         mov     %rax,152($context)      # restore context->Rsp
1162         mov     %rsi,168($context)      # restore context->Rsi
1163         mov     %rdi,176($context)      # restore context->Rdi
1164
1165         mov     40($disp),%rdi          # disp->ContextRecord
1166         mov     $context,%rsi           # context
1167         mov     \$154,%ecx              # sizeof(CONTEXT)
1168         .long   0xa548f3fc              # cld; rep movsq
1169
1170         mov     $disp,%rsi
1171         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
1172         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
1173         mov     0(%rsi),%r8             # arg3, disp->ControlPc
1174         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
1175         mov     40(%rsi),%r10           # disp->ContextRecord
1176         lea     56(%rsi),%r11           # &disp->HandlerData
1177         lea     24(%rsi),%r12           # &disp->EstablisherFrame
1178         mov     %r10,32(%rsp)           # arg5
1179         mov     %r11,40(%rsp)           # arg6
1180         mov     %r12,48(%rsp)           # arg7
1181         mov     %rcx,56(%rsp)           # arg8, (NULL)
1182         call    *__imp_RtlVirtualUnwind(%rip)
1183
1184         mov     \$1,%eax                # ExceptionContinueSearch
1185         add     \$64,%rsp
1186         popfq
1187         pop     %r15
1188         pop     %r14
1189         pop     %r13
1190         pop     %r12
1191         pop     %rbp
1192         pop     %rbx
1193         pop     %rdi
1194         pop     %rsi
1195         ret
1196 .size   ssse3_handler,.-ssse3_handler
1197
1198 .section        .pdata
1199 .align  4
1200         .rva    .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1201         .rva    .LSEH_end_aesni_cbc_sha1_enc_ssse3
1202         .rva    .LSEH_info_aesni_cbc_sha1_enc_ssse3
1203 ___
1204 $code.=<<___ if ($avx);
1205         .rva    .LSEH_begin_aesni_cbc_sha1_enc_avx
1206         .rva    .LSEH_end_aesni_cbc_sha1_enc_avx
1207         .rva    .LSEH_info_aesni_cbc_sha1_enc_avx
1208 ___
1209 $code.=<<___;
1210 .section        .xdata
1211 .align  8
1212 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1213         .byte   9,0,0,0
1214         .rva    ssse3_handler
1215         .rva    .Lprologue_ssse3,.Lepilogue_ssse3       # HandlerData[]
1216 ___
1217 $code.=<<___ if ($avx);
1218 .LSEH_info_aesni_cbc_sha1_enc_avx:
1219         .byte   9,0,0,0
1220         .rva    ssse3_handler
1221         .rva    .Lprologue_avx,.Lepilogue_avx           # HandlerData[]
1222 ___
1223 }
1224
1225 ####################################################################
1226 sub rex {
1227   local *opcode=shift;
1228   my ($dst,$src)=@_;
1229   my $rex=0;
1230
1231     $rex|=0x04                  if($dst>=8);
1232     $rex|=0x01                  if($src>=8);
1233     push @opcode,$rex|0x40      if($rex);
1234 }
1235
1236 sub aesni {
1237   my $line=shift;
1238   my @opcode=(0x66);
1239
1240     if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1241         my %opcodelet = (
1242                 "aesenc" => 0xdc,       "aesenclast" => 0xdd
1243         );
1244         return undef if (!defined($opcodelet{$1}));
1245         rex(\@opcode,$3,$2);
1246         push @opcode,0x0f,0x38,$opcodelet{$1};
1247         push @opcode,0xc0|($2&7)|(($3&7)<<3);   # ModR/M
1248         return ".byte\t".join(',',@opcode);
1249     }
1250     return $line;
1251 }
1252
1253 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1254 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
1255
1256 print $code;
1257 close STDOUT;