ghash-x86_64.pl: "528B" variant delivers further >30% improvement.
[openssl.git] / crypto / modes / asm / ghash-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 # March, June 2010
11 #
12 # The module implements "4-bit" GCM GHASH function and underlying
13 # single multiplication operation in GF(2^128). "4-bit" means that
14 # it uses 256 bytes per-key table [+128 bytes shared table]. GHASH
15 # function features so called "528B" variant utilizing additional
16 # 256+16 bytes of per-key storage [+512 bytes shared table].
17 # Performance results are for this streamed GHASH subroutine and are
18 # expressed in cycles per processed byte, less is better:
19 #
20 #               gcc 3.4.x(*)    assembler
21 #
22 # P4            28.6            14.0            +100%
23 # Opteron       18.5            7.7             +140%
24 # Core2         17.5            8.1(**)         +115%
25 #
26 # (*)   comparison is not completely fair, because C results are
27 #       for vanilla "256B" implementation, not "528B";-)
28 # (**)  it's mystery [to me] why Core2 result is not same as for
29 #       Opteron;
30
31 # May 2010
32 #
33 # Add PCLMULQDQ version performing at 2.07 cycles per processed byte.
34 # See ghash-x86.pl for background information and details about coding
35 # techniques.
36 #
37 # Special thanks to David Woodhouse <dwmw2@infradead.org> for
38 # providing access to a Westmere-based system on behalf of Intel
39 # Open Source Technology Centre.
40
41 $flavour = shift;
42 $output  = shift;
43 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
44
45 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
46
47 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
48 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
49 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
50 die "can't locate x86_64-xlate.pl";
51
52 open STDOUT,"| $^X $xlate $flavour $output";
53
54 # common register layout
55 $nlo="%rax";
56 $nhi="%rbx";
57 $Zlo="%r8";
58 $Zhi="%r9";
59 $tmp="%r10";
60 $rem_4bit = "%r11";
61
62 $Xi="%rdi";
63 $Htbl="%rsi";
64
65 # per-function register layout
66 $cnt="%rcx";
67 $rem="%rdx";
68
69 sub LB() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/     or
70                         $r =~ s/%[er]([sd]i)/%\1l/      or
71                         $r =~ s/%[er](bp)/%\1l/         or
72                         $r =~ s/%(r[0-9]+)[d]?/%\1b/;   $r; }
73
74 sub AUTOLOAD()          # thunk [simplified] 32-bit style perlasm
75 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
76   my $arg = pop;
77     $arg = "\$$arg" if ($arg*1 eq $arg);
78     $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
79 }
80 \f
81 { my $N;
82   sub loop() {
83   my $inp = shift;
84
85         $N++;
86 $code.=<<___;
87         xor     $nlo,$nlo
88         xor     $nhi,$nhi
89         mov     `&LB("$Zlo")`,`&LB("$nlo")`
90         mov     `&LB("$Zlo")`,`&LB("$nhi")`
91         shl     \$4,`&LB("$nlo")`
92         mov     \$14,$cnt
93         mov     8($Htbl,$nlo),$Zlo
94         mov     ($Htbl,$nlo),$Zhi
95         and     \$0xf0,`&LB("$nhi")`
96         mov     $Zlo,$rem
97         jmp     .Loop$N
98
99 .align  16
100 .Loop$N:
101         shr     \$4,$Zlo
102         and     \$0xf,$rem
103         mov     $Zhi,$tmp
104         mov     ($inp,$cnt),`&LB("$nlo")`
105         shr     \$4,$Zhi
106         xor     8($Htbl,$nhi),$Zlo
107         shl     \$60,$tmp
108         xor     ($Htbl,$nhi),$Zhi
109         mov     `&LB("$nlo")`,`&LB("$nhi")`
110         xor     ($rem_4bit,$rem,8),$Zhi
111         mov     $Zlo,$rem
112         shl     \$4,`&LB("$nlo")`
113         xor     $tmp,$Zlo
114         dec     $cnt
115         js      .Lbreak$N
116
117         shr     \$4,$Zlo
118         and     \$0xf,$rem
119         mov     $Zhi,$tmp
120         shr     \$4,$Zhi
121         xor     8($Htbl,$nlo),$Zlo
122         shl     \$60,$tmp
123         xor     ($Htbl,$nlo),$Zhi
124         and     \$0xf0,`&LB("$nhi")`
125         xor     ($rem_4bit,$rem,8),$Zhi
126         mov     $Zlo,$rem
127         xor     $tmp,$Zlo
128         jmp     .Loop$N
129
130 .align  16
131 .Lbreak$N:
132         shr     \$4,$Zlo
133         and     \$0xf,$rem
134         mov     $Zhi,$tmp
135         shr     \$4,$Zhi
136         xor     8($Htbl,$nlo),$Zlo
137         shl     \$60,$tmp
138         xor     ($Htbl,$nlo),$Zhi
139         and     \$0xf0,`&LB("$nhi")`
140         xor     ($rem_4bit,$rem,8),$Zhi
141         mov     $Zlo,$rem
142         xor     $tmp,$Zlo
143
144         shr     \$4,$Zlo
145         and     \$0xf,$rem
146         mov     $Zhi,$tmp
147         shr     \$4,$Zhi
148         xor     8($Htbl,$nhi),$Zlo
149         shl     \$60,$tmp
150         xor     ($Htbl,$nhi),$Zhi
151         xor     $tmp,$Zlo
152         xor     ($rem_4bit,$rem,8),$Zhi
153
154         bswap   $Zlo
155         bswap   $Zhi
156 ___
157 }}
158
159 $code=<<___;
160 .text
161
162 .globl  gcm_gmult_4bit
163 .type   gcm_gmult_4bit,\@function,2
164 .align  16
165 gcm_gmult_4bit:
166         push    %rbx
167         push    %rbp            # %rbp and %r12 are pushed exclusively in
168         push    %r12            # order to reuse Win64 exception handler...
169 .Lgmult_prologue:
170
171         movzb   15($Xi),$Zlo
172         lea     .Lrem_4bit(%rip),$rem_4bit
173 ___
174         &loop   ($Xi);
175 $code.=<<___;
176         mov     $Zlo,8($Xi)
177         mov     $Zhi,($Xi)
178
179         mov     16(%rsp),%rbx
180         lea     24(%rsp),%rsp
181 .Lgmult_epilogue:
182         ret
183 .size   gcm_gmult_4bit,.-gcm_gmult_4bit
184 ___
185 \f
186 # per-function register layout
187 $inp="%rdx";
188 $len="%rcx";
189 $rem_8bit=$rem_4bit;
190
191 $code.=<<___;
192 .globl  gcm_ghash_4bit
193 .type   gcm_ghash_4bit,\@function,4
194 .align  16
195 gcm_ghash_4bit:
196         push    %rbx
197         push    %rbp
198         push    %r12
199         push    %r13
200         push    %r14
201         push    %r15
202         sub     \$280,%rsp
203 .Lghash_prologue:
204         mov     $inp,%r14               # reassign couple of args
205         mov     $len,%r15
206 ___
207 { my $inp="%r14";
208   my $dat="%edx";
209   my $len="%r15";
210   my @nhi=("%ebx","%ecx");
211   my @rem=("%r12","%r13");
212   my $Hshr4="%rbp";
213
214         &sub    ($Htbl,-128);           # size optimization
215         &lea    ($Hshr4,"16+128(%rsp)");
216         { my @lo =($nlo,$nhi);
217           my @hi =($Zlo,$Zhi);
218
219           &xor  ($dat,$dat);
220           for ($i=0,$j=-2;$i<18;$i++,$j++) {
221             &mov        ("$j(%rsp)",&LB($dat))          if ($i>1);
222             &or         ($lo[0],$tmp)                   if ($i>1);
223             &mov        (&LB($dat),&LB($lo[1]))         if ($i>0 && $i<17);
224             &shr        ($lo[1],4)                      if ($i>0 && $i<17);
225             &mov        ($tmp,$hi[1])                   if ($i>0 && $i<17);
226             &shr        ($hi[1],4)                      if ($i>0 && $i<17);
227             &mov        ("8*$j($Hshr4)",$hi[0])         if ($i>1);
228             &mov        ($hi[0],"16*$i+0-128($Htbl)")   if ($i<16);
229             &shl        (&LB($dat),4)                   if ($i>0 && $i<17);
230             &mov        ("8*$j-128($Hshr4)",$lo[0])     if ($i>1);
231             &mov        ($lo[0],"16*$i+8-128($Htbl)")   if ($i<16);
232             &shl        ($tmp,60)                       if ($i>0 && $i<17);
233
234             push        (@lo,shift(@lo));
235             push        (@hi,shift(@hi));
236           }
237         }
238         &add    ($Htbl,-128);
239         &mov    ($Zlo,"8($Xi)");
240         &mov    ($Zhi,"0($Xi)");
241         &add    ($len,$inp);            # pointer to the end of data
242         &lea    ($rem_8bit,".Lrem_8bit(%rip)");
243         &jmp    (".Louter_loop");
244
245 $code.=".align  16\n.Louter_loop:\n";
246         &xor    ($Zhi,"($inp)");
247         &mov    ("%rdx","8($inp)");
248         &lea    ($inp,"16($inp)");
249         &xor    ("%rdx",$Zlo);
250         &mov    ("($Xi)",$Zhi);
251         &mov    ("8($Xi)","%rdx");
252         &shr    ("%rdx",32);
253
254         &xor    ($nlo,$nlo);
255         &rol    ($dat,8);
256         &mov    (&LB($nlo),&LB($dat));
257         &movz   ($nhi[0],&LB($dat));
258         &shl    (&LB($nlo),4);
259         &shr    ($nhi[0],4);
260
261         for ($j=11,$i=0;$i<15;$i++) {
262             &rol        ($dat,8);
263             &xor        ($Zlo,"8($Htbl,$nlo)")                  if ($i>0);
264             &xor        ($Zhi,"($Htbl,$nlo)")                   if ($i>0);
265             &mov        ($Zlo,"8($Htbl,$nlo)")                  if ($i==0);
266             &mov        ($Zhi,"($Htbl,$nlo)")                   if ($i==0);
267
268             &mov        (&LB($nlo),&LB($dat));
269             &xor        ($Zlo,$tmp)                             if ($i>0);
270             &movzw      ($rem[1],"($rem_8bit,$rem[1],2)")       if ($i>0);
271
272             &movz       ($nhi[1],&LB($dat));
273             &shl        (&LB($nlo),4);
274             &movzb      ($rem[0],"(%rsp,$nhi[0])");
275
276             &shr        ($nhi[1],4)                             if ($i<14);
277             &and        ($nhi[1],0xf0)                          if ($i==14);
278             &shl        ($rem[1],48)                            if ($i>0);
279             &xor        ($rem[0],$Zlo);
280
281             &mov        ($tmp,$Zhi);
282             &xor        ($Zhi,$rem[1])                          if ($i>0);
283             &shr        ($Zlo,8);
284
285             &movz       ($rem[0],&LB($rem[0]));
286             &mov        ($dat,"$j($Xi)")                        if (--$j%4==0);
287             &shr        ($Zhi,8);
288
289             &xor        ($Zlo,"-128($Hshr4,$nhi[0],8)");
290             &shl        ($tmp,56);
291             &xor        ($Zhi,"($Hshr4,$nhi[0],8)");
292
293             unshift     (@nhi,pop(@nhi));               # "rotate" registers
294             unshift     (@rem,pop(@rem));
295         }
296         &movzw  ($rem[1],"($rem_8bit,$rem[1],2)");
297         &xor    ($Zlo,"8($Htbl,$nlo)");
298         &xor    ($Zhi,"($Htbl,$nlo)");
299
300         &shl    ($rem[1],48);
301         &xor    ($Zlo,$tmp);
302
303         &xor    ($Zhi,$rem[1]);
304         &movz   ($rem[0],&LB($Zlo));
305         &shr    ($Zlo,4);
306
307         &mov    ($tmp,$Zhi);
308         &shl    (&LB($rem[0]),4);
309         &shr    ($Zhi,4);
310
311         &xor    ($Zlo,"8($Htbl,$nhi[0])");
312         &movzw  ($rem[0],"($rem_8bit,$rem[0],2)");
313         &shl    ($tmp,60);
314
315         &xor    ($Zhi,"($Htbl,$nhi[0])");
316         &xor    ($Zlo,$tmp);
317         &shl    ($rem[0],48);
318
319         &bswap  ($Zlo);
320         &xor    ($Zhi,$rem[0]);
321
322         &bswap  ($Zhi);
323         &cmp    ($inp,$len);
324         &jb     (".Louter_loop");
325 }
326 $code.=<<___;
327         mov     $Zlo,8($Xi)
328         mov     $Zhi,($Xi)
329
330         lea     280(%rsp),%rsi
331         mov     0(%rsi),%r15
332         mov     8(%rsi),%r14
333         mov     16(%rsi),%r13
334         mov     24(%rsi),%r12
335         mov     32(%rsi),%rbp
336         mov     40(%rsi),%rbx
337         lea     48(%rsi),%rsp
338 .Lghash_epilogue:
339         ret
340 .size   gcm_ghash_4bit,.-gcm_ghash_4bit
341 ___
342 \f
343 ######################################################################
344 # PCLMULQDQ version.
345
346 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") :  # Win64 order
347                 ("%rdi","%rsi","%rdx","%rcx");  # Unix order
348
349 ($Xi,$Xhi)=("%xmm0","%xmm1");   $Hkey="%xmm2";
350 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
351
352 sub clmul64x64_T2 {     # minimal register pressure
353 my ($Xhi,$Xi,$Hkey,$modulo)=@_;
354
355 $code.=<<___ if (!defined($modulo));
356         movdqa          $Xi,$Xhi                #
357         pshufd          \$0b01001110,$Xi,$T1
358         pshufd          \$0b01001110,$Hkey,$T2
359         pxor            $Xi,$T1                 #
360         pxor            $Hkey,$T2
361 ___
362 $code.=<<___;
363         pclmulqdq       \$0x00,$Hkey,$Xi        #######
364         pclmulqdq       \$0x11,$Hkey,$Xhi       #######
365         pclmulqdq       \$0x00,$T2,$T1          #######
366         pxor            $Xi,$T1                 #
367         pxor            $Xhi,$T1                #
368
369         movdqa          $T1,$T2                 #
370         psrldq          \$8,$T1
371         pslldq          \$8,$T2                 #
372         pxor            $T1,$Xhi
373         pxor            $T2,$Xi                 #
374 ___
375 }
376
377 sub reduction_alg9 {    # 17/13 times faster than Intel version
378 my ($Xhi,$Xi) = @_;
379
380 $code.=<<___;
381         # 1st phase
382         movdqa          $Xi,$T1                 #
383         psllq           \$1,$Xi
384         pxor            $T1,$Xi                 #
385         psllq           \$5,$Xi                 #
386         pxor            $T1,$Xi                 #
387         psllq           \$57,$Xi                #
388         movdqa          $Xi,$T2                 #
389         pslldq          \$8,$Xi
390         psrldq          \$8,$T2                 #       
391         pxor            $T1,$Xi
392         pxor            $T2,$Xhi                #
393
394         # 2nd phase
395         movdqa          $Xi,$T2
396         psrlq           \$5,$Xi
397         pxor            $T2,$Xi                 #
398         psrlq           \$1,$Xi                 #
399         pxor            $T2,$Xi                 #
400         pxor            $Xhi,$T2
401         psrlq           \$1,$Xi                 #
402         pxor            $T2,$Xi                 #
403 ___
404 }
405 \f
406 { my ($Htbl,$Xip)=@_4args;
407
408 $code.=<<___;
409 .globl  gcm_init_clmul
410 .type   gcm_init_clmul,\@abi-omnipotent
411 .align  16
412 gcm_init_clmul:
413         movdqu          ($Xip),$Hkey
414         pshufd          \$0b01001110,$Hkey,$Hkey        # dword swap
415
416         # <<1 twist
417         pshufd          \$0b11111111,$Hkey,$T2  # broadcast uppermost dword
418         movdqa          $Hkey,$T1
419         psllq           \$1,$Hkey
420         pxor            $T3,$T3                 #
421         psrlq           \$63,$T1
422         pcmpgtd         $T2,$T3                 # broadcast carry bit
423         pslldq          \$8,$T1
424         por             $T1,$Hkey               # H<<=1
425
426         # magic reduction
427         pand            .L0x1c2_polynomial(%rip),$T3
428         pxor            $T3,$Hkey               # if(carry) H^=0x1c2_polynomial
429
430         # calculate H^2
431         movdqa          $Hkey,$Xi
432 ___
433         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);
434         &reduction_alg9 ($Xhi,$Xi);
435 $code.=<<___;
436         movdqu          $Hkey,($Htbl)           # save H
437         movdqu          $Xi,16($Htbl)           # save H^2
438         ret
439 .size   gcm_init_clmul,.-gcm_init_clmul
440 ___
441 }
442
443 { my ($Xip,$Htbl)=@_4args;
444
445 $code.=<<___;
446 .globl  gcm_gmult_clmul
447 .type   gcm_gmult_clmul,\@abi-omnipotent
448 .align  16
449 gcm_gmult_clmul:
450         movdqu          ($Xip),$Xi
451         movdqa          .Lbswap_mask(%rip),$T3
452         movdqu          ($Htbl),$Hkey
453         pshufb          $T3,$Xi
454 ___
455         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);
456         &reduction_alg9 ($Xhi,$Xi);
457 $code.=<<___;
458         pshufb          $T3,$Xi
459         movdqu          $Xi,($Xip)
460         ret
461 .size   gcm_gmult_clmul,.-gcm_gmult_clmul
462 ___
463 }
464 \f
465 { my ($Xip,$Htbl,$inp,$len)=@_4args;
466   my $Xn="%xmm6";
467   my $Xhn="%xmm7";
468   my $Hkey2="%xmm8";
469   my $T1n="%xmm9";
470   my $T2n="%xmm10";
471
472 $code.=<<___;
473 .globl  gcm_ghash_clmul
474 .type   gcm_ghash_clmul,\@abi-omnipotent
475 .align  16
476 gcm_ghash_clmul:
477 ___
478 $code.=<<___ if ($win64);
479 .LSEH_begin_gcm_ghash_clmul:
480         # I can't trust assembler to use specific encoding:-(
481         .byte   0x48,0x83,0xec,0x58             #sub    \$0x58,%rsp
482         .byte   0x0f,0x29,0x34,0x24             #movaps %xmm6,(%rsp)
483         .byte   0x0f,0x29,0x7c,0x24,0x10        #movdqa %xmm7,0x10(%rsp)
484         .byte   0x44,0x0f,0x29,0x44,0x24,0x20   #movaps %xmm8,0x20(%rsp)
485         .byte   0x44,0x0f,0x29,0x4c,0x24,0x30   #movaps %xmm9,0x30(%rsp)
486         .byte   0x44,0x0f,0x29,0x54,0x24,0x40   #movaps %xmm10,0x40(%rsp)
487 ___
488 $code.=<<___;
489         movdqa          .Lbswap_mask(%rip),$T3
490
491         movdqu          ($Xip),$Xi
492         movdqu          ($Htbl),$Hkey
493         pshufb          $T3,$Xi
494
495         sub             \$0x10,$len
496         jz              .Lodd_tail
497
498         movdqu          16($Htbl),$Hkey2
499         #######
500         # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
501         #       [(H*Ii+1) + (H*Xi+1)] mod P =
502         #       [(H*Ii+1) + H^2*(Ii+Xi)] mod P
503         #
504         movdqu          ($inp),$T1              # Ii
505         movdqu          16($inp),$Xn            # Ii+1
506         pshufb          $T3,$T1
507         pshufb          $T3,$Xn
508         pxor            $T1,$Xi                 # Ii+Xi
509 ___
510         &clmul64x64_T2  ($Xhn,$Xn,$Hkey);       # H*Ii+1
511 $code.=<<___;
512         movdqa          $Xi,$Xhi                #
513         pshufd          \$0b01001110,$Xi,$T1
514         pshufd          \$0b01001110,$Hkey2,$T2
515         pxor            $Xi,$T1                 #
516         pxor            $Hkey2,$T2
517
518         lea             32($inp),$inp           # i+=2
519         sub             \$0x20,$len
520         jbe             .Leven_tail
521
522 .Lmod_loop:
523 ___
524         &clmul64x64_T2  ($Xhi,$Xi,$Hkey2,1);    # H^2*(Ii+Xi)
525 $code.=<<___;
526         movdqu          ($inp),$T1              # Ii
527         pxor            $Xn,$Xi                 # (H*Ii+1) + H^2*(Ii+Xi)
528         pxor            $Xhn,$Xhi
529
530         movdqu          16($inp),$Xn            # Ii+1
531         pshufb          $T3,$T1
532         pshufb          $T3,$Xn
533
534         movdqa          $Xn,$Xhn                #
535         pshufd          \$0b01001110,$Xn,$T1n
536         pshufd          \$0b01001110,$Hkey,$T2n
537         pxor            $Xn,$T1n                #
538         pxor            $Hkey,$T2n
539          pxor           $T1,$Xhi                # "Ii+Xi", consume early
540
541           movdqa        $Xi,$T1                 # 1st phase
542           psllq         \$1,$Xi
543           pxor          $T1,$Xi                 #
544           psllq         \$5,$Xi                 #
545           pxor          $T1,$Xi                 #
546         pclmulqdq       \$0x00,$Hkey,$Xn        #######
547           psllq         \$57,$Xi                #
548           movdqa        $Xi,$T2                 #
549           pslldq        \$8,$Xi
550           psrldq        \$8,$T2                 #       
551           pxor          $T1,$Xi
552           pxor          $T2,$Xhi                #
553
554         pclmulqdq       \$0x11,$Hkey,$Xhn       #######
555           movdqa        $Xi,$T2                 # 2nd phase
556           psrlq         \$5,$Xi
557           pxor          $T2,$Xi                 #
558           psrlq         \$1,$Xi                 #
559           pxor          $T2,$Xi                 #
560           pxor          $Xhi,$T2
561           psrlq         \$1,$Xi                 #
562           pxor          $T2,$Xi                 #
563
564         pclmulqdq       \$0x00,$T2n,$T1n        #######
565          movdqa         $Xi,$Xhi                #
566          pshufd         \$0b01001110,$Xi,$T1
567          pshufd         \$0b01001110,$Hkey2,$T2
568          pxor           $Xi,$T1                 #
569          pxor           $Hkey2,$T2
570
571         pxor            $Xn,$T1n                #
572         pxor            $Xhn,$T1n               #
573         movdqa          $T1n,$T2n               #
574         psrldq          \$8,$T1n
575         pslldq          \$8,$T2n                #
576         pxor            $T1n,$Xhn
577         pxor            $T2n,$Xn                #
578
579         lea             32($inp),$inp
580         sub             \$0x20,$len
581         ja              .Lmod_loop
582
583 .Leven_tail:
584 ___
585         &clmul64x64_T2  ($Xhi,$Xi,$Hkey2,1);    # H^2*(Ii+Xi)
586 $code.=<<___;
587         pxor            $Xn,$Xi                 # (H*Ii+1) + H^2*(Ii+Xi)
588         pxor            $Xhn,$Xhi
589 ___
590         &reduction_alg9 ($Xhi,$Xi);
591 $code.=<<___;
592         test            $len,$len
593         jnz             .Ldone
594
595 .Lodd_tail:
596         movdqu          ($inp),$T1              # Ii
597         pshufb          $T3,$T1
598         pxor            $T1,$Xi                 # Ii+Xi
599 ___
600         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);       # H*(Ii+Xi)
601         &reduction_alg9 ($Xhi,$Xi);
602 $code.=<<___;
603 .Ldone:
604         pshufb          $T3,$Xi
605         movdqu          $Xi,($Xip)
606 ___
607 $code.=<<___ if ($win64);
608         movaps  (%rsp),%xmm6
609         movaps  0x10(%rsp),%xmm7
610         movaps  0x20(%rsp),%xmm8
611         movaps  0x30(%rsp),%xmm9
612         movaps  0x40(%rsp),%xmm10
613         add     \$0x58,%rsp
614 ___
615 $code.=<<___;
616         ret
617 .LSEH_end_gcm_ghash_clmul:
618 .size   gcm_ghash_clmul,.-gcm_ghash_clmul
619 ___
620 }
621
622 $code.=<<___;
623 .align  64
624 .Lbswap_mask:
625         .byte   15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
626 .L0x1c2_polynomial:
627         .byte   1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
628 .align  64
629 .type   .Lrem_4bit,\@object
630 .Lrem_4bit:
631         .long   0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
632         .long   0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
633         .long   0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
634         .long   0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
635 .type   .Lrem_8bit,\@object
636 .Lrem_8bit:
637         .value  0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E
638         .value  0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E
639         .value  0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E
640         .value  0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E
641         .value  0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E
642         .value  0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E
643         .value  0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E
644         .value  0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E
645         .value  0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE
646         .value  0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE
647         .value  0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE
648         .value  0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE
649         .value  0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E
650         .value  0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E
651         .value  0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE
652         .value  0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE
653         .value  0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E
654         .value  0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E
655         .value  0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E
656         .value  0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E
657         .value  0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E
658         .value  0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E
659         .value  0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E
660         .value  0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E
661         .value  0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE
662         .value  0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE
663         .value  0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE
664         .value  0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE
665         .value  0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E
666         .value  0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E
667         .value  0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE
668         .value  0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE
669
670 .asciz  "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
671 .align  64
672 ___
673 \f
674 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
675 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
676 if ($win64) {
677 $rec="%rcx";
678 $frame="%rdx";
679 $context="%r8";
680 $disp="%r9";
681
682 $code.=<<___;
683 .extern __imp_RtlVirtualUnwind
684 .type   se_handler,\@abi-omnipotent
685 .align  16
686 se_handler:
687         push    %rsi
688         push    %rdi
689         push    %rbx
690         push    %rbp
691         push    %r12
692         push    %r13
693         push    %r14
694         push    %r15
695         pushfq
696         sub     \$64,%rsp
697
698         mov     120($context),%rax      # pull context->Rax
699         mov     248($context),%rbx      # pull context->Rip
700
701         mov     8($disp),%rsi           # disp->ImageBase
702         mov     56($disp),%r11          # disp->HandlerData
703
704         mov     0(%r11),%r10d           # HandlerData[0]
705         lea     (%rsi,%r10),%r10        # prologue label
706         cmp     %r10,%rbx               # context->Rip<prologue label
707         jb      .Lin_prologue
708
709         mov     152($context),%rax      # pull context->Rsp
710
711         mov     4(%r11),%r10d           # HandlerData[1]
712         lea     (%rsi,%r10),%r10        # epilogue label
713         cmp     %r10,%rbx               # context->Rip>=epilogue label
714         jae     .Lin_prologue
715
716         lea     24(%rax),%rax           # adjust "rsp"
717
718         mov     -8(%rax),%rbx
719         mov     -16(%rax),%rbp
720         mov     -24(%rax),%r12
721         mov     %rbx,144($context)      # restore context->Rbx
722         mov     %rbp,160($context)      # restore context->Rbp
723         mov     %r12,216($context)      # restore context->R12
724
725 .Lin_prologue:
726         mov     8(%rax),%rdi
727         mov     16(%rax),%rsi
728         mov     %rax,152($context)      # restore context->Rsp
729         mov     %rsi,168($context)      # restore context->Rsi
730         mov     %rdi,176($context)      # restore context->Rdi
731
732         mov     40($disp),%rdi          # disp->ContextRecord
733         mov     $context,%rsi           # context
734         mov     \$`1232/8`,%ecx         # sizeof(CONTEXT)
735         .long   0xa548f3fc              # cld; rep movsq
736
737         mov     $disp,%rsi
738         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
739         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
740         mov     0(%rsi),%r8             # arg3, disp->ControlPc
741         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
742         mov     40(%rsi),%r10           # disp->ContextRecord
743         lea     56(%rsi),%r11           # &disp->HandlerData
744         lea     24(%rsi),%r12           # &disp->EstablisherFrame
745         mov     %r10,32(%rsp)           # arg5
746         mov     %r11,40(%rsp)           # arg6
747         mov     %r12,48(%rsp)           # arg7
748         mov     %rcx,56(%rsp)           # arg8, (NULL)
749         call    *__imp_RtlVirtualUnwind(%rip)
750
751         mov     \$1,%eax                # ExceptionContinueSearch
752         add     \$64,%rsp
753         popfq
754         pop     %r15
755         pop     %r14
756         pop     %r13
757         pop     %r12
758         pop     %rbp
759         pop     %rbx
760         pop     %rdi
761         pop     %rsi
762         ret
763 .size   se_handler,.-se_handler
764
765 .section        .pdata
766 .align  4
767         .rva    .LSEH_begin_gcm_gmult_4bit
768         .rva    .LSEH_end_gcm_gmult_4bit
769         .rva    .LSEH_info_gcm_gmult_4bit
770
771         .rva    .LSEH_begin_gcm_ghash_4bit
772         .rva    .LSEH_end_gcm_ghash_4bit
773         .rva    .LSEH_info_gcm_ghash_4bit
774
775         .rva    .LSEH_begin_gcm_ghash_clmul
776         .rva    .LSEH_end_gcm_ghash_clmul
777         .rva    .LSEH_info_gcm_ghash_clmul
778
779 .section        .xdata
780 .align  8
781 .LSEH_info_gcm_gmult_4bit:
782         .byte   9,0,0,0
783         .rva    se_handler
784         .rva    .Lgmult_prologue,.Lgmult_epilogue       # HandlerData
785 .LSEH_info_gcm_ghash_4bit:
786         .byte   9,0,0,0
787         .rva    se_handler
788         .rva    .Lghash_prologue,.Lghash_epilogue       # HandlerData
789 .LSEH_info_gcm_ghash_clmul:
790         .byte   0x01,0x1f,0x0b,0x00
791         .byte   0x1f,0xa8,0x04,0x00     #movaps 0x40(rsp),xmm10
792         .byte   0x19,0x98,0x03,0x00     #movaps 0x30(rsp),xmm9
793         .byte   0x13,0x88,0x02,0x00     #movaps 0x20(rsp),xmm8
794         .byte   0x0d,0x78,0x01,0x00     #movaps 0x10(rsp),xmm7
795         .byte   0x08,0x68,0x00,0x00     #movaps (rsp),xmm6
796         .byte   0x04,0xa2,0x00,0x00     #sub    rsp,0x58
797 ___
798 }
799 \f
800 sub rex {
801  local *opcode=shift;
802  my ($dst,$src)=@_;
803
804    if ($dst>=8 || $src>=8) {
805         $rex=0x40;
806         $rex|=0x04 if($dst>=8);
807         $rex|=0x01 if($src>=8);
808         push @opcode,$rex;
809    }
810 }
811
812 sub pclmulqdq {
813   my $arg=shift;
814   my @opcode=(0x66);
815
816     if ($arg=~/\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
817         rex(\@opcode,$3,$2);
818         push @opcode,0x0f,0x3a,0x44;
819         push @opcode,0xc0|($2&7)|(($3&7)<<3);   # ModR/M
820         my $c=$1;
821         push @opcode,$c=~/^0/?oct($c):$c;
822         return ".byte\t".join(',',@opcode);
823     }
824     return "pclmulqdq\t".$arg;
825 }
826
827 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
828 $code =~ s/\bpclmulqdq\s+(\$.*%xmm[0-9]+).*$/pclmulqdq($1)/gem;
829
830 print $code;
831
832 close STDOUT;