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