09648a8207c5871ae9502580b60517907f2a6a92
[openssl.git] / crypto / sha / asm / sha256-586.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 # SHA256 block transform for x86. September 2007.
11 #
12 # Performance improvement over compiler generated code varies from
13 # 10% to 40% [see below]. Not very impressive on some ยต-archs, but
14 # it's 5 times smaller and optimizies amount of writes.
15 #
16 # May 2012.
17 #
18 # Optimization including two of Pavel Semjanov's ideas, alternative
19 # Maj and full unroll, resulted in ~20-25% improvement on most CPUs,
20 # ~7% on Pentium, ~40% on Atom. As fully unrolled loop body is almost
21 # 15x larger, 8KB vs. 560B, it's fired only for longer inputs. But not
22 # on P4, where it kills performance, nor Sandy Bridge, where folded
23 # loop is approximately as fast...
24 #
25 # June 2012.
26 #
27 # Add AMD XOP-specific code path, >30% improvement on Bulldozer over
28 # May version, >60% over original. Add AVX+shrd code path, >25%
29 # improvement on Sandy Bridge over May version, 60% over original.
30 #
31 # May 2013.
32 #
33 # Replace AMD XOP code path with SSSE3 to cover more processors.
34 # (Biggest improvement coefficient is on upcoming Atom Silvermont,
35 # not shown.) Add AVX+BMI code path.
36 #
37 # March 2014.
38 #
39 # Add support for Intel SHA Extensions.
40 #
41 # Performance in clock cycles per processed byte (less is better):
42 #
43 #               gcc     icc     x86 asm(*)      SIMD    x86_64 asm(**)  
44 # Pentium       46      57      40/38           -       -
45 # PIII          36      33      27/24           -       -
46 # P4            41      38      28              -       17.3
47 # AMD K8        27      25      19/15.5         -       14.9
48 # Core2         26      23      18/15.6         14.3    13.8
49 # Westmere      27      -       19/15.7         13.4    12.3
50 # Sandy Bridge  25      -       15.9            12.4    11.6
51 # Ivy Bridge    24      -       15.0            11.4    10.3
52 # Haswell       22      -       13.9            9.46    7.80
53 # Bulldozer     36      -       27/22           17.0    13.6
54 # VIA Nano      36      -       25/22           16.8    16.5
55 # Atom          50      -       30/25           21.9    18.9
56 #
57 # (*)   numbers after slash are for unrolled loop, where applicable;
58 # (**)  x86_64 assembly performance is presented for reference
59 #       purposes, results are best-available;
60
61 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
62 push(@INC,"${dir}","${dir}../../perlasm");
63 require "x86asm.pl";
64
65 &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
66
67 $xmm=$avx=0;
68 for (@ARGV) { $xmm=1 if (/-DOPENSSL_IA32_SSE2/); }
69
70 if ($xmm &&     `$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
71                         =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
72         $avx = ($1>=2.19) + ($1>=2.22);
73 }
74
75 if ($xmm && !$avx && $ARGV[0] eq "win32n" &&
76                 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
77         $avx = ($1>=2.03) + ($1>=2.10);
78 }
79
80 if ($xmm && !$avx && $ARGV[0] eq "win32" &&
81                 `ml 2>&1` =~ /Version ([0-9]+)\./) {
82         $avx = ($1>=10) + ($1>=11);
83 }
84
85 $unroll_after = 64*4;   # If pre-evicted from L1P cache first spin of
86                         # fully unrolled loop was measured to run about
87                         # 3-4x slower. If slowdown coefficient is N and
88                         # unrolled loop is m times faster, then you break
89                         # even at (N-1)/(m-1) blocks. Then it needs to be
90                         # adjusted for probability of code being evicted,
91                         # code size/cache size=1/4. Typical m is 1.15...
92
93 $A="eax";
94 $E="edx";
95 $T="ebx";
96 $Aoff=&DWP(4,"esp");
97 $Boff=&DWP(8,"esp");
98 $Coff=&DWP(12,"esp");
99 $Doff=&DWP(16,"esp");
100 $Eoff=&DWP(20,"esp");
101 $Foff=&DWP(24,"esp");
102 $Goff=&DWP(28,"esp");
103 $Hoff=&DWP(32,"esp");
104 $Xoff=&DWP(36,"esp");
105 $K256="ebp";
106
107 sub BODY_16_63() {
108         &mov    ($T,"ecx");                     # "ecx" is preloaded
109          &mov   ("esi",&DWP(4*(9+15+16-14),"esp"));
110         &ror    ("ecx",18-7);
111          &mov   ("edi","esi");
112         &ror    ("esi",19-17);
113          &xor   ("ecx",$T);
114          &shr   ($T,3);
115         &ror    ("ecx",7);
116          &xor   ("esi","edi");
117          &xor   ($T,"ecx");                     # T = sigma0(X[-15])
118         &ror    ("esi",17);
119          &add   ($T,&DWP(4*(9+15+16),"esp"));   # T += X[-16]
120         &shr    ("edi",10);
121          &add   ($T,&DWP(4*(9+15+16-9),"esp")); # T += X[-7]
122         #&xor   ("edi","esi")                   # sigma1(X[-2])
123         # &add  ($T,"edi");                     # T += sigma1(X[-2])
124         # &mov  (&DWP(4*(9+15),"esp"),$T);      # save X[0]
125
126         &BODY_00_15(1);
127 }
128 sub BODY_00_15() {
129     my $in_16_63=shift;
130
131         &mov    ("ecx",$E);
132          &xor   ("edi","esi")                   if ($in_16_63); # sigma1(X[-2])
133          &mov   ("esi",$Foff);
134         &ror    ("ecx",25-11);
135          &add   ($T,"edi")                      if ($in_16_63); # T += sigma1(X[-2])
136          &mov   ("edi",$Goff);
137         &xor    ("ecx",$E);
138          &xor   ("esi","edi");
139          &mov   ($T,&DWP(4*(9+15),"esp"))       if (!$in_16_63);
140          &mov   (&DWP(4*(9+15),"esp"),$T)       if ($in_16_63); # save X[0]
141         &ror    ("ecx",11-6);
142          &and   ("esi",$E);
143          &mov   ($Eoff,$E);             # modulo-scheduled
144         &xor    ($E,"ecx");
145          &add   ($T,$Hoff);             # T += h
146          &xor   ("esi","edi");          # Ch(e,f,g)
147         &ror    ($E,6);                 # Sigma1(e)
148          &mov   ("ecx",$A);
149          &add   ($T,"esi");             # T += Ch(e,f,g)
150
151         &ror    ("ecx",22-13);
152          &add   ($T,$E);                # T += Sigma1(e)
153          &mov   ("edi",$Boff);
154         &xor    ("ecx",$A);
155          &mov   ($Aoff,$A);             # modulo-scheduled
156          &lea   ("esp",&DWP(-4,"esp"));
157         &ror    ("ecx",13-2);
158          &mov   ("esi",&DWP(0,$K256));
159         &xor    ("ecx",$A);
160          &mov   ($E,$Eoff);             # e in next iteration, d in this one
161          &xor   ($A,"edi");             # a ^= b
162         &ror    ("ecx",2);              # Sigma0(a)
163
164          &add   ($T,"esi");             # T+= K[i]
165          &mov   (&DWP(0,"esp"),$A);     # (b^c) in next round
166         &add    ($E,$T);                # d += T
167          &and   ($A,&DWP(4,"esp"));     # a &= (b^c)
168         &add    ($T,"ecx");             # T += Sigma0(a)
169          &xor   ($A,"edi");             # h = Maj(a,b,c) = Ch(a^b,c,b)
170          &mov   ("ecx",&DWP(4*(9+15+16-1),"esp"))       if ($in_16_63); # preload T
171         &add    ($K256,4);
172          &add   ($A,$T);                # h += T
173 }
174
175 &external_label("OPENSSL_ia32cap_P")            if (!$i386);
176
177 &function_begin("sha256_block_data_order");
178         &mov    ("esi",wparam(0));      # ctx
179         &mov    ("edi",wparam(1));      # inp
180         &mov    ("eax",wparam(2));      # num
181         &mov    ("ebx","esp");          # saved sp
182
183         &call   (&label("pic_point"));  # make it PIC!
184 &set_label("pic_point");
185         &blindpop($K256);
186         &lea    ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
187
188         &sub    ("esp",16);
189         &and    ("esp",-64);
190
191         &shl    ("eax",6);
192         &add    ("eax","edi");
193         &mov    (&DWP(0,"esp"),"esi");  # ctx
194         &mov    (&DWP(4,"esp"),"edi");  # inp
195         &mov    (&DWP(8,"esp"),"eax");  # inp+num*128
196         &mov    (&DWP(12,"esp"),"ebx"); # saved sp
197                                                 if (!$i386 && $xmm) {
198         &picmeup("edx","OPENSSL_ia32cap_P",$K256,&label("K256"));
199         &mov    ("ecx",&DWP(0,"edx"));
200         &mov    ("ebx",&DWP(4,"edx"));
201         &test   ("ecx",1<<20);          # check for P4
202         &jnz    (&label("loop"));
203         &mov    ("edx",&DWP(8,"edx"))   if ($xmm);
204         &test   ("ecx",1<<24);          # check for FXSR
205         &jz     ($unroll_after?&label("no_xmm"):&label("loop"));
206         &and    ("ecx",1<<30);          # mask "Intel CPU" bit
207         &and    ("ebx",1<<28|1<<9);     # mask AVX and SSSE3 bits
208         &test   ("edx",1<<29)           if ($xmm);      # check for SHA
209         &jnz    (&label("shaext"))      if ($xmm);
210         &or     ("ecx","ebx");
211         &and    ("ecx",1<<28|1<<30);
212         &cmp    ("ecx",1<<28|1<<30);
213                                         if ($xmm) {
214         &je     (&label("AVX"))         if ($avx);
215         &test   ("ebx",1<<9);           # check for SSSE3
216         &jnz    (&label("SSSE3"));
217                                         } else {
218         &je     (&label("loop_shrd"));
219                                         }
220                                                 if ($unroll_after) {
221 &set_label("no_xmm");
222         &sub    ("eax","edi");
223         &cmp    ("eax",$unroll_after);
224         &jae    (&label("unrolled"));
225                                                 } }
226         &jmp    (&label("loop"));
227
228 sub COMPACT_LOOP() {
229 my $suffix=shift;
230
231 &set_label("loop$suffix",$suffix?32:16);
232     # copy input block to stack reversing byte and dword order
233     for($i=0;$i<4;$i++) {
234         &mov    ("eax",&DWP($i*16+0,"edi"));
235         &mov    ("ebx",&DWP($i*16+4,"edi"));
236         &mov    ("ecx",&DWP($i*16+8,"edi"));
237         &bswap  ("eax");
238         &mov    ("edx",&DWP($i*16+12,"edi"));
239         &bswap  ("ebx");
240         &push   ("eax");
241         &bswap  ("ecx");
242         &push   ("ebx");
243         &bswap  ("edx");
244         &push   ("ecx");
245         &push   ("edx");
246     }
247         &add    ("edi",64);
248         &lea    ("esp",&DWP(-4*9,"esp"));# place for A,B,C,D,E,F,G,H
249         &mov    (&DWP(4*(9+16)+4,"esp"),"edi");
250
251         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
252         &mov    ($A,&DWP(0,"esi"));
253         &mov    ("ebx",&DWP(4,"esi"));
254         &mov    ("ecx",&DWP(8,"esi"));
255         &mov    ("edi",&DWP(12,"esi"));
256         # &mov  ($Aoff,$A);
257         &mov    ($Boff,"ebx");
258         &xor    ("ebx","ecx");
259         &mov    ($Coff,"ecx");
260         &mov    ($Doff,"edi");
261         &mov    (&DWP(0,"esp"),"ebx");  # magic
262         &mov    ($E,&DWP(16,"esi"));    
263         &mov    ("ebx",&DWP(20,"esi"));
264         &mov    ("ecx",&DWP(24,"esi"));
265         &mov    ("edi",&DWP(28,"esi"));
266         # &mov  ($Eoff,$E);
267         &mov    ($Foff,"ebx");
268         &mov    ($Goff,"ecx");
269         &mov    ($Hoff,"edi");
270
271 &set_label("00_15$suffix",16);
272
273         &BODY_00_15();
274
275         &cmp    ("esi",0xc19bf174);
276         &jne    (&label("00_15$suffix"));
277
278         &mov    ("ecx",&DWP(4*(9+15+16-1),"esp"));      # preloaded in BODY_00_15(1)
279         &jmp    (&label("16_63$suffix"));
280
281 &set_label("16_63$suffix",16);
282
283         &BODY_16_63();
284
285         &cmp    ("esi",0xc67178f2);
286         &jne    (&label("16_63$suffix"));
287
288         &mov    ("esi",&DWP(4*(9+16+64)+0,"esp"));#ctx
289         # &mov  ($A,$Aoff);
290         &mov    ("ebx",$Boff);
291         # &mov  ("edi",$Coff);
292         &mov    ("ecx",$Doff);
293         &add    ($A,&DWP(0,"esi"));
294         &add    ("ebx",&DWP(4,"esi"));
295         &add    ("edi",&DWP(8,"esi"));
296         &add    ("ecx",&DWP(12,"esi"));
297         &mov    (&DWP(0,"esi"),$A);
298         &mov    (&DWP(4,"esi"),"ebx");
299         &mov    (&DWP(8,"esi"),"edi");
300         &mov    (&DWP(12,"esi"),"ecx");
301         # &mov  ($E,$Eoff);
302         &mov    ("eax",$Foff);
303         &mov    ("ebx",$Goff);
304         &mov    ("ecx",$Hoff);
305         &mov    ("edi",&DWP(4*(9+16+64)+4,"esp"));#inp
306         &add    ($E,&DWP(16,"esi"));
307         &add    ("eax",&DWP(20,"esi"));
308         &add    ("ebx",&DWP(24,"esi"));
309         &add    ("ecx",&DWP(28,"esi"));
310         &mov    (&DWP(16,"esi"),$E);
311         &mov    (&DWP(20,"esi"),"eax");
312         &mov    (&DWP(24,"esi"),"ebx");
313         &mov    (&DWP(28,"esi"),"ecx");
314
315         &lea    ("esp",&DWP(4*(9+16+64),"esp"));# destroy frame
316         &sub    ($K256,4*64);                   # rewind K
317
318         &cmp    ("edi",&DWP(8,"esp"));          # are we done yet?
319         &jb     (&label("loop$suffix"));
320 }
321         &COMPACT_LOOP();
322         &mov    ("esp",&DWP(12,"esp"));         # restore sp
323 &function_end_A();
324                                                 if (!$i386 && !$xmm) {
325         # ~20% improvement on Sandy Bridge
326         local *ror = sub { &shrd(@_[0],@_) };
327         &COMPACT_LOOP("_shrd");
328         &mov    ("esp",&DWP(12,"esp"));         # restore sp
329 &function_end_A();
330                                                 }
331
332 &set_label("K256",64);  # Yes! I keep it in the code segment!
333 @K256=( 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
334         0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
335         0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
336         0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
337         0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
338         0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
339         0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
340         0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
341         0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
342         0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
343         0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
344         0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
345         0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
346         0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
347         0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
348         0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2     );
349 &data_word(@K256);
350 &data_word(0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f);        # byte swap mask
351 &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
352
353 ($a,$b,$c,$d,$e,$f,$g,$h)=(0..7);       # offsets
354 sub off { &DWP(4*(((shift)-$i)&7),"esp"); }
355
356 if (!$i386 && $unroll_after) {
357 my @AH=($A,$K256);
358
359 &set_label("unrolled",16);
360         &lea    ("esp",&DWP(-96,"esp"));
361         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
362         &mov    ($AH[0],&DWP(0,"esi"));
363         &mov    ($AH[1],&DWP(4,"esi"));
364         &mov    ("ecx",&DWP(8,"esi"));
365         &mov    ("ebx",&DWP(12,"esi"));
366         #&mov   (&DWP(0,"esp"),$AH[0]);
367         &mov    (&DWP(4,"esp"),$AH[1]);
368         &xor    ($AH[1],"ecx");         # magic
369         &mov    (&DWP(8,"esp"),"ecx");
370         &mov    (&DWP(12,"esp"),"ebx");
371         &mov    ($E,&DWP(16,"esi"));    
372         &mov    ("ebx",&DWP(20,"esi"));
373         &mov    ("ecx",&DWP(24,"esi"));
374         &mov    ("esi",&DWP(28,"esi"));
375         #&mov   (&DWP(16,"esp"),$E);
376         &mov    (&DWP(20,"esp"),"ebx");
377         &mov    (&DWP(24,"esp"),"ecx");
378         &mov    (&DWP(28,"esp"),"esi");
379         &jmp    (&label("grand_loop"));
380
381 &set_label("grand_loop",16);
382     # copy input block to stack reversing byte order
383     for($i=0;$i<5;$i++) {
384         &mov    ("ebx",&DWP(12*$i+0,"edi"));
385         &mov    ("ecx",&DWP(12*$i+4,"edi"));
386         &bswap  ("ebx");
387         &mov    ("esi",&DWP(12*$i+8,"edi"));
388         &bswap  ("ecx");
389         &mov    (&DWP(32+12*$i+0,"esp"),"ebx");
390         &bswap  ("esi");
391         &mov    (&DWP(32+12*$i+4,"esp"),"ecx");
392         &mov    (&DWP(32+12*$i+8,"esp"),"esi");
393     }
394         &mov    ("ebx",&DWP($i*12,"edi"));
395         &add    ("edi",64);
396         &bswap  ("ebx");
397         &mov    (&DWP(96+4,"esp"),"edi");
398         &mov    (&DWP(32+12*$i,"esp"),"ebx");
399
400     my ($t1,$t2) = ("ecx","esi");
401
402     for ($i=0;$i<64;$i++) {
403
404       if ($i>=16) {
405         &mov    ($T,$t1);                       # $t1 is preloaded
406         # &mov  ($t2,&DWP(32+4*(($i+14)&15),"esp"));
407         &ror    ($t1,18-7);
408          &mov   ("edi",$t2);
409         &ror    ($t2,19-17);
410          &xor   ($t1,$T);
411          &shr   ($T,3);
412         &ror    ($t1,7);
413          &xor   ($t2,"edi");
414          &xor   ($T,$t1);                       # T = sigma0(X[-15])
415         &ror    ($t2,17);
416          &add   ($T,&DWP(32+4*($i&15),"esp"));  # T += X[-16]
417         &shr    ("edi",10);
418          &add   ($T,&DWP(32+4*(($i+9)&15),"esp"));      # T += X[-7]
419         #&xor   ("edi",$t2)                     # sigma1(X[-2])
420         # &add  ($T,"edi");                     # T += sigma1(X[-2])
421         # &mov  (&DWP(4*(9+15),"esp"),$T);      # save X[0]
422       }
423         &mov    ($t1,$E);
424          &xor   ("edi",$t2)                     if ($i>=16);    # sigma1(X[-2])
425          &mov   ($t2,&off($f));
426         &ror    ($E,25-11);
427          &add   ($T,"edi")                      if ($i>=16);    # T += sigma1(X[-2])
428          &mov   ("edi",&off($g));
429         &xor    ($E,$t1);
430          &mov   ($T,&DWP(32+4*($i&15),"esp"))   if ($i<16);     # X[i]
431          &mov   (&DWP(32+4*($i&15),"esp"),$T)   if ($i>=16 && $i<62);   # save X[0]
432          &xor   ($t2,"edi");
433         &ror    ($E,11-6);
434          &and   ($t2,$t1);
435          &mov   (&off($e),$t1);         # save $E, modulo-scheduled
436         &xor    ($E,$t1);
437          &add   ($T,&off($h));          # T += h
438          &xor   ("edi",$t2);            # Ch(e,f,g)
439         &ror    ($E,6);                 # Sigma1(e)
440          &mov   ($t1,$AH[0]);
441          &add   ($T,"edi");             # T += Ch(e,f,g)
442
443         &ror    ($t1,22-13);
444          &mov   ($t2,$AH[0]);
445          &mov   ("edi",&off($b));
446         &xor    ($t1,$AH[0]);
447          &mov   (&off($a),$AH[0]);      # save $A, modulo-scheduled
448          &xor   ($AH[0],"edi");         # a ^= b, (b^c) in next round
449         &ror    ($t1,13-2);
450          &and   ($AH[1],$AH[0]);        # (b^c) &= (a^b)
451          &lea   ($E,&DWP(@K256[$i],$T,$E));     # T += Sigma1(1)+K[i]
452         &xor    ($t1,$t2);
453          &xor   ($AH[1],"edi");         # h = Maj(a,b,c) = Ch(a^b,c,b)
454          &mov   ($t2,&DWP(32+4*(($i+2)&15),"esp"))      if ($i>=15 && $i<63);
455         &ror    ($t1,2);                # Sigma0(a)
456
457          &add   ($AH[1],$E);            # h += T
458          &add   ($E,&off($d));          # d += T
459         &add    ($AH[1],$t1);           # h += Sigma0(a)
460          &mov   ($t1,&DWP(32+4*(($i+15)&15),"esp"))     if ($i>=15 && $i<63);
461
462         @AH = reverse(@AH);             # rotate(a,h)
463         ($t1,$t2) = ($t2,$t1);          # rotate(t1,t2)
464     }
465         &mov    ("esi",&DWP(96,"esp")); #ctx
466                                         #&mov   ($AH[0],&DWP(0,"esp"));
467         &xor    ($AH[1],"edi");         #&mov   ($AH[1],&DWP(4,"esp"));
468                                         #&mov   ("edi", &DWP(8,"esp"));
469         &mov    ("ecx",&DWP(12,"esp"));
470         &add    ($AH[0],&DWP(0,"esi"));
471         &add    ($AH[1],&DWP(4,"esi"));
472         &add    ("edi",&DWP(8,"esi"));
473         &add    ("ecx",&DWP(12,"esi"));
474         &mov    (&DWP(0,"esi"),$AH[0]);
475         &mov    (&DWP(4,"esi"),$AH[1]);
476         &mov    (&DWP(8,"esi"),"edi");
477         &mov    (&DWP(12,"esi"),"ecx");
478          #&mov  (&DWP(0,"esp"),$AH[0]);
479          &mov   (&DWP(4,"esp"),$AH[1]);
480          &xor   ($AH[1],"edi");         # magic
481          &mov   (&DWP(8,"esp"),"edi");
482          &mov   (&DWP(12,"esp"),"ecx");
483         #&mov   ($E,&DWP(16,"esp"));
484         &mov    ("edi",&DWP(20,"esp"));
485         &mov    ("ebx",&DWP(24,"esp"));
486         &mov    ("ecx",&DWP(28,"esp"));
487         &add    ($E,&DWP(16,"esi"));
488         &add    ("edi",&DWP(20,"esi"));
489         &add    ("ebx",&DWP(24,"esi"));
490         &add    ("ecx",&DWP(28,"esi"));
491         &mov    (&DWP(16,"esi"),$E);
492         &mov    (&DWP(20,"esi"),"edi");
493         &mov    (&DWP(24,"esi"),"ebx");
494         &mov    (&DWP(28,"esi"),"ecx");
495          #&mov  (&DWP(16,"esp"),$E);
496          &mov   (&DWP(20,"esp"),"edi");
497         &mov    ("edi",&DWP(96+4,"esp"));       # inp
498          &mov   (&DWP(24,"esp"),"ebx");
499          &mov   (&DWP(28,"esp"),"ecx");
500
501         &cmp    ("edi",&DWP(96+8,"esp"));       # are we done yet?
502         &jb     (&label("grand_loop"));
503
504         &mov    ("esp",&DWP(96+12,"esp"));      # restore sp
505 &function_end_A();
506 }
507                                                 if (!$i386 && $xmm) {{{
508 {
509 ######################################################################
510 # Intel SHA Extensions implementation of SHA256 update function.
511 #
512 my ($ctx,$inp,$end)=("esi","edi","eax");
513 my ($Wi,$ABEF,$CDGH,$TMP)=map("xmm$_",(0..2,7));
514 my @MSG=map("xmm$_",(3..6));
515
516 sub sha256op38 {
517  my ($opcodelet,$dst,$src)=@_;
518     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
519     {   &data_byte(0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2);       }
520 }
521 sub sha256rnds2 { sha256op38(0xcb,@_); }
522 sub sha256msg1  { sha256op38(0xcc,@_); }
523 sub sha256msg2  { sha256op38(0xcd,@_); }
524
525 &set_label("shaext",32);
526         &sub            ("esp",32);
527
528         &movdqu         ($ABEF,&QWP(0,$ctx));           # DCBA
529         &lea            ($K256,&DWP(0x80,$K256));
530         &movdqu         ($CDGH,&QWP(16,$ctx));          # HGFE
531         &movdqa         ($TMP,&QWP(0x100-0x80,$K256));  # byte swap mask
532
533         &pshufd         ($Wi,$ABEF,0x1b);               # ABCD
534         &pshufd         ($ABEF,$ABEF,0xb1);             # CDAB
535         &pshufd         ($CDGH,$CDGH,0x1b);             # EFGH
536         &palignr        ($ABEF,$CDGH,8);                # ABEF
537         &punpcklqdq     ($CDGH,$Wi);                    # CDGH
538         &jmp            (&label("loop_shaext"));
539
540 &set_label("loop_shaext",16);
541         &movdqu         (@MSG[0],&QWP(0,$inp));
542         &movdqu         (@MSG[1],&QWP(0x10,$inp));
543         &movdqu         (@MSG[2],&QWP(0x20,$inp));
544         &pshufb         (@MSG[0],$TMP);
545         &movdqu         (@MSG[3],&QWP(0x30,$inp));
546         &movdqa         (&QWP(16,"esp"),$CDGH);         # offload
547
548         &movdqa         ($Wi,&QWP(0*16-0x80,$K256));
549         &paddd          ($Wi,@MSG[0]);
550         &pshufb         (@MSG[1],$TMP);
551         &sha256rnds2    ($CDGH,$ABEF);                  # 0-3
552         &pshufd         ($Wi,$Wi,0x0e);
553         &nop            ();
554         &movdqa         (&QWP(0,"esp"),$ABEF);          # offload
555         &sha256rnds2    ($ABEF,$CDGH);
556
557         &movdqa         ($Wi,&QWP(1*16-0x80,$K256));
558         &paddd          ($Wi,@MSG[1]);
559         &pshufb         (@MSG[2],$TMP);
560         &sha256rnds2    ($CDGH,$ABEF);                  # 4-7
561         &pshufd         ($Wi,$Wi,0x0e);
562         &lea            ($inp,&DWP(0x40,$inp));
563         &sha256msg1     (@MSG[0],@MSG[1]);
564         &sha256rnds2    ($ABEF,$CDGH);
565
566         &movdqa         ($Wi,&QWP(2*16-0x80,$K256));
567         &paddd          ($Wi,@MSG[2]);
568         &pshufb         (@MSG[3],$TMP);
569         &sha256rnds2    ($CDGH,$ABEF);                  # 8-11
570         &pshufd         ($Wi,$Wi,0x0e);
571         &movdqa         ($TMP,@MSG[3]);
572         &palignr        ($TMP,@MSG[2],4);
573         &nop            ();
574         &paddd          (@MSG[0],$TMP);
575         &sha256msg1     (@MSG[1],@MSG[2]);
576         &sha256rnds2    ($ABEF,$CDGH);
577
578         &movdqa         ($Wi,&QWP(3*16-0x80,$K256));
579         &paddd          ($Wi,@MSG[3]);
580         &sha256msg2     (@MSG[0],@MSG[3]);
581         &sha256rnds2    ($CDGH,$ABEF);                  # 12-15
582         &pshufd         ($Wi,$Wi,0x0e);
583         &movdqa         ($TMP,@MSG[0]);
584         &palignr        ($TMP,@MSG[3],4);
585         &nop            ();
586         &paddd          (@MSG[1],$TMP);
587         &sha256msg1     (@MSG[2],@MSG[3]);
588         &sha256rnds2    ($ABEF,$CDGH);
589
590 for($i=4;$i<16-3;$i++) {
591         &movdqa         ($Wi,&QWP($i*16-0x80,$K256));
592         &paddd          ($Wi,@MSG[0]);
593         &sha256msg2     (@MSG[1],@MSG[0]);
594         &sha256rnds2    ($CDGH,$ABEF);                  # 16-19...
595         &pshufd         ($Wi,$Wi,0x0e);
596         &movdqa         ($TMP,@MSG[1]);
597         &palignr        ($TMP,@MSG[0],4);
598         &nop            ();
599         &paddd          (@MSG[2],$TMP);
600         &sha256msg1     (@MSG[3],@MSG[0]);
601         &sha256rnds2    ($ABEF,$CDGH);
602
603         push(@MSG,shift(@MSG));
604 }
605         &movdqa         ($Wi,&QWP(13*16-0x80,$K256));
606         &paddd          ($Wi,@MSG[0]);
607         &sha256msg2     (@MSG[1],@MSG[0]);
608         &sha256rnds2    ($CDGH,$ABEF);                  # 52-55
609         &pshufd         ($Wi,$Wi,0x0e);
610         &movdqa         ($TMP,@MSG[1])
611         &palignr        ($TMP,@MSG[0],4);
612         &sha256rnds2    ($ABEF,$CDGH);
613         &paddd          (@MSG[2],$TMP);
614
615         &movdqa         ($Wi,&QWP(14*16-0x80,$K256));
616         &paddd          ($Wi,@MSG[1]);
617         &sha256rnds2    ($CDGH,$ABEF);                  # 56-59
618         &pshufd         ($Wi,$Wi,0x0e);
619         &sha256msg2     (@MSG[2],@MSG[1]);
620         &movdqa         ($TMP,&QWP(0x100-0x80,$K256));  # byte swap mask
621         &sha256rnds2    ($ABEF,$CDGH);
622
623         &movdqa         ($Wi,&QWP(15*16-0x80,$K256));
624         &paddd          ($Wi,@MSG[2]);
625         &nop            ();
626         &sha256rnds2    ($CDGH,$ABEF);                  # 60-63
627         &pshufd         ($Wi,$Wi,0x0e);
628         &cmp            ($end,$inp);
629         &nop            ();
630         &sha256rnds2    ($ABEF,$CDGH);
631
632         &paddd          ($CDGH,&QWP(16,"esp"));
633         &paddd          ($ABEF,&QWP(0,"esp"));
634         &jnz            (&label("loop_shaext"));
635
636         &pshufd         ($CDGH,$CDGH,0xb1);             # DCHG
637         &pshufd         ($TMP,$ABEF,0x1b);              # FEBA
638         &pshufd         ($ABEF,$ABEF,0xb1);             # BAFE
639         &punpckhqdq     ($ABEF,$CDGH);                  # DCBA
640         &palignr        ($CDGH,$TMP,8);                 # HGFE
641
642         &mov            ("esp",&DWP(32+12,"esp"));
643         &movdqu         (&QWP(0,$ctx),$ABEF);
644         &movdqu         (&QWP(16,$ctx),$CDGH);
645 &function_end_A();
646 }
647
648 my @X = map("xmm$_",(0..3));
649 my ($t0,$t1,$t2,$t3) = map("xmm$_",(4..7));
650 my @AH = ($A,$T);
651
652 &set_label("SSSE3",32);
653         &lea    ("esp",&DWP(-96,"esp"));
654         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
655         &mov    ($AH[0],&DWP(0,"esi"));
656         &mov    ($AH[1],&DWP(4,"esi"));
657         &mov    ("ecx",&DWP(8,"esi"));
658         &mov    ("edi",&DWP(12,"esi"));
659         #&mov   (&DWP(0,"esp"),$AH[0]);
660         &mov    (&DWP(4,"esp"),$AH[1]);
661         &xor    ($AH[1],"ecx");                 # magic
662         &mov    (&DWP(8,"esp"),"ecx");
663         &mov    (&DWP(12,"esp"),"edi");
664         &mov    ($E,&DWP(16,"esi"));
665         &mov    ("edi",&DWP(20,"esi"));
666         &mov    ("ecx",&DWP(24,"esi"));
667         &mov    ("esi",&DWP(28,"esi"));
668         #&mov   (&DWP(16,"esp"),$E);
669         &mov    (&DWP(20,"esp"),"edi");
670         &mov    ("edi",&DWP(96+4,"esp"));       # inp
671         &mov    (&DWP(24,"esp"),"ecx");
672         &mov    (&DWP(28,"esp"),"esi");
673         &movdqa ($t3,&QWP(256,$K256));
674         &jmp    (&label("grand_ssse3"));
675
676 &set_label("grand_ssse3",16);
677         # load input, reverse byte order, add K256[0..15], save to stack
678         &movdqu (@X[0],&QWP(0,"edi"));
679         &movdqu (@X[1],&QWP(16,"edi"));
680         &movdqu (@X[2],&QWP(32,"edi"));
681         &movdqu (@X[3],&QWP(48,"edi"));
682         &add    ("edi",64);
683         &pshufb (@X[0],$t3);
684         &mov    (&DWP(96+4,"esp"),"edi");
685         &pshufb (@X[1],$t3);
686         &movdqa ($t0,&QWP(0,$K256));
687         &pshufb (@X[2],$t3);
688         &movdqa ($t1,&QWP(16,$K256));
689         &paddd  ($t0,@X[0]);
690         &pshufb (@X[3],$t3);
691         &movdqa ($t2,&QWP(32,$K256));
692         &paddd  ($t1,@X[1]);
693         &movdqa ($t3,&QWP(48,$K256));
694         &movdqa (&QWP(32+0,"esp"),$t0);
695         &paddd  ($t2,@X[2]);
696         &movdqa (&QWP(32+16,"esp"),$t1);
697         &paddd  ($t3,@X[3]);
698         &movdqa (&QWP(32+32,"esp"),$t2);
699         &movdqa (&QWP(32+48,"esp"),$t3);
700         &jmp    (&label("ssse3_00_47"));
701
702 &set_label("ssse3_00_47",16);
703         &add            ($K256,64);
704
705 sub SSSE3_00_47 () {
706 my $j = shift;
707 my $body = shift;
708 my @X = @_;
709 my @insns = (&$body,&$body,&$body,&$body);      # 120 instructions
710
711           eval(shift(@insns));
712         &movdqa         ($t0,@X[1]);
713           eval(shift(@insns));                  # @
714           eval(shift(@insns));
715         &movdqa         ($t3,@X[3]);
716           eval(shift(@insns));
717           eval(shift(@insns));
718         &palignr        ($t0,@X[0],4);          # X[1..4]
719           eval(shift(@insns));
720           eval(shift(@insns));                  # @
721           eval(shift(@insns));
722          &palignr       ($t3,@X[2],4);          # X[9..12]
723           eval(shift(@insns));
724           eval(shift(@insns));
725           eval(shift(@insns));
726         &movdqa         ($t1,$t0);
727           eval(shift(@insns));                  # @
728           eval(shift(@insns));
729         &movdqa         ($t2,$t0);
730           eval(shift(@insns));
731           eval(shift(@insns));
732         &psrld          ($t0,3);
733           eval(shift(@insns));
734           eval(shift(@insns));                  # @
735          &paddd         (@X[0],$t3);            # X[0..3] += X[9..12]
736           eval(shift(@insns));
737           eval(shift(@insns));
738         &psrld          ($t2,7);
739           eval(shift(@insns));
740           eval(shift(@insns));
741           eval(shift(@insns));                  # @
742           eval(shift(@insns));
743          &pshufd        ($t3,@X[3],0b11111010); # X[14..15]
744           eval(shift(@insns));
745           eval(shift(@insns));
746         &pslld          ($t1,32-18);
747           eval(shift(@insns));
748           eval(shift(@insns));                  # @
749         &pxor           ($t0,$t2);
750           eval(shift(@insns));
751           eval(shift(@insns));
752         &psrld          ($t2,18-7);
753           eval(shift(@insns));
754           eval(shift(@insns));
755           eval(shift(@insns));                  # @
756         &pxor           ($t0,$t1);
757           eval(shift(@insns));
758           eval(shift(@insns));
759         &pslld          ($t1,18-7);
760           eval(shift(@insns));
761           eval(shift(@insns));
762           eval(shift(@insns));                  # @
763         &pxor           ($t0,$t2);
764           eval(shift(@insns));
765           eval(shift(@insns));
766          &movdqa        ($t2,$t3);
767           eval(shift(@insns));
768           eval(shift(@insns));
769           eval(shift(@insns));                  # @
770         &pxor           ($t0,$t1);              # sigma0(X[1..4])
771           eval(shift(@insns));
772           eval(shift(@insns));
773          &psrld         ($t3,10);
774           eval(shift(@insns));
775           eval(shift(@insns));
776           eval(shift(@insns));                  # @
777         &paddd          (@X[0],$t0);            # X[0..3] += sigma0(X[1..4])
778           eval(shift(@insns));
779           eval(shift(@insns));
780          &psrlq         ($t2,17);
781           eval(shift(@insns));
782           eval(shift(@insns));
783           eval(shift(@insns));                  # @
784          &pxor          ($t3,$t2);
785           eval(shift(@insns));
786           eval(shift(@insns));
787          &psrlq         ($t2,19-17);
788           eval(shift(@insns));
789           eval(shift(@insns));
790           eval(shift(@insns));                  # @
791          &pxor          ($t3,$t2);
792           eval(shift(@insns));
793           eval(shift(@insns));
794          &pshufd        ($t3,$t3,0b10000000);
795           eval(shift(@insns));
796           eval(shift(@insns));
797           eval(shift(@insns));                  # @
798           eval(shift(@insns));
799           eval(shift(@insns));
800           eval(shift(@insns));
801           eval(shift(@insns));
802           eval(shift(@insns));                  # @
803           eval(shift(@insns));
804          &psrldq        ($t3,8);
805           eval(shift(@insns));
806           eval(shift(@insns));
807           eval(shift(@insns));
808         &paddd          (@X[0],$t3);            # X[0..1] += sigma1(X[14..15])
809           eval(shift(@insns));                  # @
810           eval(shift(@insns));
811           eval(shift(@insns));
812           eval(shift(@insns));
813           eval(shift(@insns));
814           eval(shift(@insns));                  # @
815           eval(shift(@insns));
816          &pshufd        ($t3,@X[0],0b01010000); # X[16..17]
817           eval(shift(@insns));
818           eval(shift(@insns));
819           eval(shift(@insns));
820          &movdqa        ($t2,$t3);
821           eval(shift(@insns));                  # @
822          &psrld         ($t3,10);
823           eval(shift(@insns));
824          &psrlq         ($t2,17);
825           eval(shift(@insns));
826           eval(shift(@insns));
827           eval(shift(@insns));
828           eval(shift(@insns));                  # @
829          &pxor          ($t3,$t2);
830           eval(shift(@insns));
831           eval(shift(@insns));
832          &psrlq         ($t2,19-17);
833           eval(shift(@insns));
834           eval(shift(@insns));
835           eval(shift(@insns));                  # @
836          &pxor          ($t3,$t2);
837           eval(shift(@insns));
838           eval(shift(@insns));
839           eval(shift(@insns));
840          &pshufd        ($t3,$t3,0b00001000);
841           eval(shift(@insns));
842           eval(shift(@insns));                  # @
843         &movdqa         ($t2,&QWP(16*$j,$K256));
844           eval(shift(@insns));
845           eval(shift(@insns));
846          &pslldq        ($t3,8);
847           eval(shift(@insns));
848           eval(shift(@insns));
849           eval(shift(@insns));                  # @
850           eval(shift(@insns));
851           eval(shift(@insns));
852           eval(shift(@insns));
853           eval(shift(@insns));
854           eval(shift(@insns));                  # @
855         &paddd          (@X[0],$t3);            # X[2..3] += sigma1(X[16..17])
856           eval(shift(@insns));
857           eval(shift(@insns));
858           eval(shift(@insns));
859           eval(shift(@insns));
860         &paddd          ($t2,@X[0]);
861           eval(shift(@insns));                  # @
862
863         foreach (@insns) { eval; }              # remaining instructions
864
865         &movdqa         (&QWP(32+16*$j,"esp"),$t2);
866 }
867
868 sub body_00_15 () {
869         (
870         '&mov   ("ecx",$E);',
871         '&ror   ($E,25-11);',
872          '&mov  ("esi",&off($f));',
873         '&xor   ($E,"ecx");',
874          '&mov  ("edi",&off($g));',
875          '&xor  ("esi","edi");',
876         '&ror   ($E,11-6);',
877          '&and  ("esi","ecx");',
878          '&mov  (&off($e),"ecx");',     # save $E, modulo-scheduled
879         '&xor   ($E,"ecx");',
880          '&xor  ("edi","esi");',        # Ch(e,f,g)
881         '&ror   ($E,6);',               # T = Sigma1(e)
882          '&mov  ("ecx",$AH[0]);',
883          '&add  ($E,"edi");',           # T += Ch(e,f,g)
884          '&mov  ("edi",&off($b));',
885         '&mov   ("esi",$AH[0]);',
886
887         '&ror   ("ecx",22-13);',
888          '&mov  (&off($a),$AH[0]);',    # save $A, modulo-scheduled
889         '&xor   ("ecx",$AH[0]);',
890          '&xor  ($AH[0],"edi");',       # a ^= b, (b^c) in next round
891          '&add  ($E,&off($h));',        # T += h
892         '&ror   ("ecx",13-2);',
893          '&and  ($AH[1],$AH[0]);',      # (b^c) &= (a^b)
894         '&xor   ("ecx","esi");',
895          '&add  ($E,&DWP(32+4*($i&15),"esp"));',        # T += K[i]+X[i]
896          '&xor  ($AH[1],"edi");',       # h = Maj(a,b,c) = Ch(a^b,c,b)
897         '&ror   ("ecx",2);',            # Sigma0(a)
898
899          '&add  ($AH[1],$E);',          # h += T
900          '&add  ($E,&off($d));',        # d += T
901         '&add   ($AH[1],"ecx");'.       # h += Sigma0(a)
902
903         '@AH = reverse(@AH); $i++;'     # rotate(a,h)
904         );
905 }
906
907     for ($i=0,$j=0; $j<4; $j++) {
908         &SSSE3_00_47($j,\&body_00_15,@X);
909         push(@X,shift(@X));             # rotate(@X)
910     }
911         &cmp    (&DWP(16*$j,$K256),0x00010203);
912         &jne    (&label("ssse3_00_47"));
913
914     for ($i=0; $i<16; ) {
915         foreach(body_00_15()) { eval; }
916     }
917
918         &mov    ("esi",&DWP(96,"esp")); #ctx
919                                         #&mov   ($AH[0],&DWP(0,"esp"));
920         &xor    ($AH[1],"edi");         #&mov   ($AH[1],&DWP(4,"esp"));
921                                         #&mov   ("edi", &DWP(8,"esp"));
922         &mov    ("ecx",&DWP(12,"esp"));
923         &add    ($AH[0],&DWP(0,"esi"));
924         &add    ($AH[1],&DWP(4,"esi"));
925         &add    ("edi",&DWP(8,"esi"));
926         &add    ("ecx",&DWP(12,"esi"));
927         &mov    (&DWP(0,"esi"),$AH[0]);
928         &mov    (&DWP(4,"esi"),$AH[1]);
929         &mov    (&DWP(8,"esi"),"edi");
930         &mov    (&DWP(12,"esi"),"ecx");
931          #&mov  (&DWP(0,"esp"),$AH[0]);
932          &mov   (&DWP(4,"esp"),$AH[1]);
933          &xor   ($AH[1],"edi");                 # magic
934          &mov   (&DWP(8,"esp"),"edi");
935          &mov   (&DWP(12,"esp"),"ecx");
936         #&mov   ($E,&DWP(16,"esp"));
937         &mov    ("edi",&DWP(20,"esp"));
938         &mov    ("ecx",&DWP(24,"esp"));
939         &add    ($E,&DWP(16,"esi"));
940         &add    ("edi",&DWP(20,"esi"));
941         &add    ("ecx",&DWP(24,"esi"));
942         &mov    (&DWP(16,"esi"),$E);
943         &mov    (&DWP(20,"esi"),"edi");
944          &mov   (&DWP(20,"esp"),"edi");
945         &mov    ("edi",&DWP(28,"esp"));
946         &mov    (&DWP(24,"esi"),"ecx");
947          #&mov  (&DWP(16,"esp"),$E);
948         &add    ("edi",&DWP(28,"esi"));
949          &mov   (&DWP(24,"esp"),"ecx");
950         &mov    (&DWP(28,"esi"),"edi");
951          &mov   (&DWP(28,"esp"),"edi");
952         &mov    ("edi",&DWP(96+4,"esp"));       # inp
953
954         &movdqa ($t3,&QWP(64,$K256));
955         &sub    ($K256,3*64);                   # rewind K
956         &cmp    ("edi",&DWP(96+8,"esp"));       # are we done yet?
957         &jb     (&label("grand_ssse3"));
958
959         &mov    ("esp",&DWP(96+12,"esp"));      # restore sp
960 &function_end_A();
961                                                 if ($avx) {
962 &set_label("AVX",32);
963                                                 if ($avx>1) {
964         &and    ("edx",1<<8|1<<3);              # check for BMI2+BMI1
965         &cmp    ("edx",1<<8|1<<3);
966         &je     (&label("AVX_BMI"));
967                                                 }
968         &lea    ("esp",&DWP(-96,"esp"));
969         &vzeroall       ();
970         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
971         &mov    ($AH[0],&DWP(0,"esi"));
972         &mov    ($AH[1],&DWP(4,"esi"));
973         &mov    ("ecx",&DWP(8,"esi"));
974         &mov    ("edi",&DWP(12,"esi"));
975         #&mov   (&DWP(0,"esp"),$AH[0]);
976         &mov    (&DWP(4,"esp"),$AH[1]);
977         &xor    ($AH[1],"ecx");                 # magic
978         &mov    (&DWP(8,"esp"),"ecx");
979         &mov    (&DWP(12,"esp"),"edi");
980         &mov    ($E,&DWP(16,"esi"));
981         &mov    ("edi",&DWP(20,"esi"));
982         &mov    ("ecx",&DWP(24,"esi"));
983         &mov    ("esi",&DWP(28,"esi"));
984         #&mov   (&DWP(16,"esp"),$E);
985         &mov    (&DWP(20,"esp"),"edi");
986         &mov    ("edi",&DWP(96+4,"esp"));       # inp
987         &mov    (&DWP(24,"esp"),"ecx");
988         &mov    (&DWP(28,"esp"),"esi");
989         &vmovdqa        ($t3,&QWP(256,$K256));
990         &jmp    (&label("grand_avx"));
991
992 &set_label("grand_avx",32);
993         # load input, reverse byte order, add K256[0..15], save to stack
994         &vmovdqu        (@X[0],&QWP(0,"edi"));
995         &vmovdqu        (@X[1],&QWP(16,"edi"));
996         &vmovdqu        (@X[2],&QWP(32,"edi"));
997         &vmovdqu        (@X[3],&QWP(48,"edi"));
998         &add            ("edi",64);
999         &vpshufb        (@X[0],@X[0],$t3);
1000         &mov            (&DWP(96+4,"esp"),"edi");
1001         &vpshufb        (@X[1],@X[1],$t3);
1002         &vpshufb        (@X[2],@X[2],$t3);
1003         &vpaddd         ($t0,@X[0],&QWP(0,$K256));
1004         &vpshufb        (@X[3],@X[3],$t3);
1005         &vpaddd         ($t1,@X[1],&QWP(16,$K256));
1006         &vpaddd         ($t2,@X[2],&QWP(32,$K256));
1007         &vpaddd         ($t3,@X[3],&QWP(48,$K256));
1008         &vmovdqa        (&QWP(32+0,"esp"),$t0);
1009         &vmovdqa        (&QWP(32+16,"esp"),$t1);
1010         &vmovdqa        (&QWP(32+32,"esp"),$t2);
1011         &vmovdqa        (&QWP(32+48,"esp"),$t3);
1012         &jmp            (&label("avx_00_47"));
1013
1014 &set_label("avx_00_47",16);
1015         &add            ($K256,64);
1016
1017 sub Xupdate_AVX () {
1018         (
1019         '&vpalignr      ($t0,@X[1],@X[0],4);',  # X[1..4]
1020          '&vpalignr     ($t3,@X[3],@X[2],4);',  # X[9..12]
1021         '&vpsrld        ($t2,$t0,7);',
1022          '&vpaddd       (@X[0],@X[0],$t3);',    # X[0..3] += X[9..16]
1023         '&vpsrld        ($t3,$t0,3);',
1024         '&vpslld        ($t1,$t0,14);',
1025         '&vpxor         ($t0,$t3,$t2);',
1026          '&vpshufd      ($t3,@X[3],0b11111010)',# X[14..15]
1027         '&vpsrld        ($t2,$t2,18-7);',
1028         '&vpxor         ($t0,$t0,$t1);',
1029         '&vpslld        ($t1,$t1,25-14);',
1030         '&vpxor         ($t0,$t0,$t2);',
1031          '&vpsrld       ($t2,$t3,10);',
1032         '&vpxor         ($t0,$t0,$t1);',        # sigma0(X[1..4])
1033          '&vpsrlq       ($t1,$t3,17);',
1034         '&vpaddd        (@X[0],@X[0],$t0);',    # X[0..3] += sigma0(X[1..4])
1035          '&vpxor        ($t2,$t2,$t1);',
1036          '&vpsrlq       ($t3,$t3,19);',
1037          '&vpxor        ($t2,$t2,$t3);',        # sigma1(X[14..15]
1038          '&vpshufd      ($t3,$t2,0b10000100);',
1039         '&vpsrldq       ($t3,$t3,8);',
1040         '&vpaddd        (@X[0],@X[0],$t3);',    # X[0..1] += sigma1(X[14..15])
1041          '&vpshufd      ($t3,@X[0],0b01010000)',# X[16..17]
1042          '&vpsrld       ($t2,$t3,10);',
1043          '&vpsrlq       ($t1,$t3,17);',
1044          '&vpxor        ($t2,$t2,$t1);',
1045          '&vpsrlq       ($t3,$t3,19);',
1046          '&vpxor        ($t2,$t2,$t3);',        # sigma1(X[16..17]
1047          '&vpshufd      ($t3,$t2,0b11101000);',
1048         '&vpslldq       ($t3,$t3,8);',
1049         '&vpaddd        (@X[0],@X[0],$t3);'     # X[2..3] += sigma1(X[16..17])
1050         );
1051 }
1052
1053 local *ror = sub { &shrd(@_[0],@_) };
1054 sub AVX_00_47 () {
1055 my $j = shift;
1056 my $body = shift;
1057 my @X = @_;
1058 my @insns = (&$body,&$body,&$body,&$body);      # 120 instructions
1059 my $insn;
1060
1061         foreach (Xupdate_AVX()) {               # 31 instructions
1062             eval;
1063             eval(shift(@insns));
1064             eval(shift(@insns));
1065             eval($insn = shift(@insns));
1066             eval(shift(@insns)) if ($insn =~ /rorx/ && @insns[0] =~ /rorx/);
1067         }
1068         &vpaddd         ($t2,@X[0],&QWP(16*$j,$K256));
1069         foreach (@insns) { eval; }              # remaining instructions
1070         &vmovdqa        (&QWP(32+16*$j,"esp"),$t2);
1071 }
1072
1073     for ($i=0,$j=0; $j<4; $j++) {
1074         &AVX_00_47($j,\&body_00_15,@X);
1075         push(@X,shift(@X));             # rotate(@X)
1076     }
1077         &cmp    (&DWP(16*$j,$K256),0x00010203);
1078         &jne    (&label("avx_00_47"));
1079
1080     for ($i=0; $i<16; ) {
1081         foreach(body_00_15()) { eval; }
1082     }
1083
1084         &mov    ("esi",&DWP(96,"esp")); #ctx
1085                                         #&mov   ($AH[0],&DWP(0,"esp"));
1086         &xor    ($AH[1],"edi");         #&mov   ($AH[1],&DWP(4,"esp"));
1087                                         #&mov   ("edi", &DWP(8,"esp"));
1088         &mov    ("ecx",&DWP(12,"esp"));
1089         &add    ($AH[0],&DWP(0,"esi"));
1090         &add    ($AH[1],&DWP(4,"esi"));
1091         &add    ("edi",&DWP(8,"esi"));
1092         &add    ("ecx",&DWP(12,"esi"));
1093         &mov    (&DWP(0,"esi"),$AH[0]);
1094         &mov    (&DWP(4,"esi"),$AH[1]);
1095         &mov    (&DWP(8,"esi"),"edi");
1096         &mov    (&DWP(12,"esi"),"ecx");
1097          #&mov  (&DWP(0,"esp"),$AH[0]);
1098          &mov   (&DWP(4,"esp"),$AH[1]);
1099          &xor   ($AH[1],"edi");                 # magic
1100          &mov   (&DWP(8,"esp"),"edi");
1101          &mov   (&DWP(12,"esp"),"ecx");
1102         #&mov   ($E,&DWP(16,"esp"));
1103         &mov    ("edi",&DWP(20,"esp"));
1104         &mov    ("ecx",&DWP(24,"esp"));
1105         &add    ($E,&DWP(16,"esi"));
1106         &add    ("edi",&DWP(20,"esi"));
1107         &add    ("ecx",&DWP(24,"esi"));
1108         &mov    (&DWP(16,"esi"),$E);
1109         &mov    (&DWP(20,"esi"),"edi");
1110          &mov   (&DWP(20,"esp"),"edi");
1111         &mov    ("edi",&DWP(28,"esp"));
1112         &mov    (&DWP(24,"esi"),"ecx");
1113          #&mov  (&DWP(16,"esp"),$E);
1114         &add    ("edi",&DWP(28,"esi"));
1115          &mov   (&DWP(24,"esp"),"ecx");
1116         &mov    (&DWP(28,"esi"),"edi");
1117          &mov   (&DWP(28,"esp"),"edi");
1118         &mov    ("edi",&DWP(96+4,"esp"));       # inp
1119
1120         &vmovdqa        ($t3,&QWP(64,$K256));
1121         &sub    ($K256,3*64);                   # rewind K
1122         &cmp    ("edi",&DWP(96+8,"esp"));       # are we done yet?
1123         &jb     (&label("grand_avx"));
1124
1125         &mov    ("esp",&DWP(96+12,"esp"));      # restore sp
1126         &vzeroall       ();
1127 &function_end_A();
1128                                                 if ($avx>1) {
1129 sub bodyx_00_15 () {                    # +10%
1130         (
1131         '&rorx  ("ecx",$E,6)',
1132         '&rorx  ("esi",$E,11)',
1133          '&mov  (&off($e),$E)',         # save $E, modulo-scheduled
1134         '&rorx  ("edi",$E,25)',
1135         '&xor   ("ecx","esi")',
1136          '&andn ("esi",$E,&off($g))',
1137         '&xor   ("ecx","edi")',         # Sigma1(e)
1138          '&and  ($E,&off($f))',
1139          '&mov  (&off($a),$AH[0]);',    # save $A, modulo-scheduled
1140          '&or   ($E,"esi")',            # T = Ch(e,f,g)
1141
1142         '&rorx  ("edi",$AH[0],2)',
1143         '&rorx  ("esi",$AH[0],13)',
1144          '&lea  ($E,&DWP(0,$E,"ecx"))', # T += Sigma1(e)
1145         '&rorx  ("ecx",$AH[0],22)',
1146         '&xor   ("esi","edi")',
1147          '&mov  ("edi",&off($b))',
1148         '&xor   ("ecx","esi")',         # Sigma0(a)
1149
1150          '&xor  ($AH[0],"edi")',        # a ^= b, (b^c) in next round
1151          '&add  ($E,&off($h))',         # T += h
1152          '&and  ($AH[1],$AH[0])',       # (b^c) &= (a^b)
1153          '&add  ($E,&DWP(32+4*($i&15),"esp"))', # T += K[i]+X[i]
1154          '&xor  ($AH[1],"edi")',        # h = Maj(a,b,c) = Ch(a^b,c,b)
1155
1156          '&add  ("ecx",$E)',            # h += T
1157          '&add  ($E,&off($d))',         # d += T
1158         '&lea   ($AH[1],&DWP(0,$AH[1],"ecx"));'.        # h += Sigma0(a)
1159
1160         '@AH = reverse(@AH); $i++;'     # rotate(a,h)
1161         );
1162 }
1163
1164 &set_label("AVX_BMI",32);
1165         &lea    ("esp",&DWP(-96,"esp"));
1166         &vzeroall       ();
1167         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
1168         &mov    ($AH[0],&DWP(0,"esi"));
1169         &mov    ($AH[1],&DWP(4,"esi"));
1170         &mov    ("ecx",&DWP(8,"esi"));
1171         &mov    ("edi",&DWP(12,"esi"));
1172         #&mov   (&DWP(0,"esp"),$AH[0]);
1173         &mov    (&DWP(4,"esp"),$AH[1]);
1174         &xor    ($AH[1],"ecx");                 # magic
1175         &mov    (&DWP(8,"esp"),"ecx");
1176         &mov    (&DWP(12,"esp"),"edi");
1177         &mov    ($E,&DWP(16,"esi"));
1178         &mov    ("edi",&DWP(20,"esi"));
1179         &mov    ("ecx",&DWP(24,"esi"));
1180         &mov    ("esi",&DWP(28,"esi"));
1181         #&mov   (&DWP(16,"esp"),$E);
1182         &mov    (&DWP(20,"esp"),"edi");
1183         &mov    ("edi",&DWP(96+4,"esp"));       # inp
1184         &mov    (&DWP(24,"esp"),"ecx");
1185         &mov    (&DWP(28,"esp"),"esi");
1186         &vmovdqa        ($t3,&QWP(256,$K256));
1187         &jmp    (&label("grand_avx_bmi"));
1188
1189 &set_label("grand_avx_bmi",32);
1190         # load input, reverse byte order, add K256[0..15], save to stack
1191         &vmovdqu        (@X[0],&QWP(0,"edi"));
1192         &vmovdqu        (@X[1],&QWP(16,"edi"));
1193         &vmovdqu        (@X[2],&QWP(32,"edi"));
1194         &vmovdqu        (@X[3],&QWP(48,"edi"));
1195         &add            ("edi",64);
1196         &vpshufb        (@X[0],@X[0],$t3);
1197         &mov            (&DWP(96+4,"esp"),"edi");
1198         &vpshufb        (@X[1],@X[1],$t3);
1199         &vpshufb        (@X[2],@X[2],$t3);
1200         &vpaddd         ($t0,@X[0],&QWP(0,$K256));
1201         &vpshufb        (@X[3],@X[3],$t3);
1202         &vpaddd         ($t1,@X[1],&QWP(16,$K256));
1203         &vpaddd         ($t2,@X[2],&QWP(32,$K256));
1204         &vpaddd         ($t3,@X[3],&QWP(48,$K256));
1205         &vmovdqa        (&QWP(32+0,"esp"),$t0);
1206         &vmovdqa        (&QWP(32+16,"esp"),$t1);
1207         &vmovdqa        (&QWP(32+32,"esp"),$t2);
1208         &vmovdqa        (&QWP(32+48,"esp"),$t3);
1209         &jmp            (&label("avx_bmi_00_47"));
1210
1211 &set_label("avx_bmi_00_47",16);
1212         &add            ($K256,64);
1213
1214     for ($i=0,$j=0; $j<4; $j++) {
1215         &AVX_00_47($j,\&bodyx_00_15,@X);
1216         push(@X,shift(@X));             # rotate(@X)
1217     }
1218         &cmp    (&DWP(16*$j,$K256),0x00010203);
1219         &jne    (&label("avx_bmi_00_47"));
1220
1221     for ($i=0; $i<16; ) {
1222         foreach(bodyx_00_15()) { eval; }
1223     }
1224
1225         &mov    ("esi",&DWP(96,"esp")); #ctx
1226                                         #&mov   ($AH[0],&DWP(0,"esp"));
1227         &xor    ($AH[1],"edi");         #&mov   ($AH[1],&DWP(4,"esp"));
1228                                         #&mov   ("edi", &DWP(8,"esp"));
1229         &mov    ("ecx",&DWP(12,"esp"));
1230         &add    ($AH[0],&DWP(0,"esi"));
1231         &add    ($AH[1],&DWP(4,"esi"));
1232         &add    ("edi",&DWP(8,"esi"));
1233         &add    ("ecx",&DWP(12,"esi"));
1234         &mov    (&DWP(0,"esi"),$AH[0]);
1235         &mov    (&DWP(4,"esi"),$AH[1]);
1236         &mov    (&DWP(8,"esi"),"edi");
1237         &mov    (&DWP(12,"esi"),"ecx");
1238          #&mov  (&DWP(0,"esp"),$AH[0]);
1239          &mov   (&DWP(4,"esp"),$AH[1]);
1240          &xor   ($AH[1],"edi");                 # magic
1241          &mov   (&DWP(8,"esp"),"edi");
1242          &mov   (&DWP(12,"esp"),"ecx");
1243         #&mov   ($E,&DWP(16,"esp"));
1244         &mov    ("edi",&DWP(20,"esp"));
1245         &mov    ("ecx",&DWP(24,"esp"));
1246         &add    ($E,&DWP(16,"esi"));
1247         &add    ("edi",&DWP(20,"esi"));
1248         &add    ("ecx",&DWP(24,"esi"));
1249         &mov    (&DWP(16,"esi"),$E);
1250         &mov    (&DWP(20,"esi"),"edi");
1251          &mov   (&DWP(20,"esp"),"edi");
1252         &mov    ("edi",&DWP(28,"esp"));
1253         &mov    (&DWP(24,"esi"),"ecx");
1254          #&mov  (&DWP(16,"esp"),$E);
1255         &add    ("edi",&DWP(28,"esi"));
1256          &mov   (&DWP(24,"esp"),"ecx");
1257         &mov    (&DWP(28,"esi"),"edi");
1258          &mov   (&DWP(28,"esp"),"edi");
1259         &mov    ("edi",&DWP(96+4,"esp"));       # inp
1260
1261         &vmovdqa        ($t3,&QWP(64,$K256));
1262         &sub    ($K256,3*64);                   # rewind K
1263         &cmp    ("edi",&DWP(96+8,"esp"));       # are we done yet?
1264         &jb     (&label("grand_avx_bmi"));
1265
1266         &mov    ("esp",&DWP(96+12,"esp"));      # restore sp
1267         &vzeroall       ();
1268 &function_end_A();
1269                                                 }
1270                                                 }
1271                                                 }}}
1272 &function_end_B("sha256_block_data_order");
1273
1274 &asm_finish();